SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VarianceKernelNormalizer.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _VARIANCEKERNELNORMALIZER_H___
12 #define _VARIANCEKERNELNORMALIZER_H___
13 
14 #include <shogun/lib/config.h>
15 
17 
18 namespace shogun
19 {
30 {
31  public:
36  {
37  SG_ADD(&meandiff, "meandiff", "Scaling constant.", MS_AVAILABLE);
38  SG_ADD(&sqrt_meandiff, "sqrt_meandiff",
39  "Square root of scaling constant.", MS_AVAILABLE);
40  }
41 
44  {
45  }
46 
49  virtual bool init(CKernel* k)
50  {
51  ASSERT(k)
52  int32_t n=k->get_num_vec_lhs();
53  ASSERT(n>0)
54 
55  CFeatures* old_lhs=k->lhs;
56  CFeatures* old_rhs=k->rhs;
57  k->lhs=old_lhs;
58  k->rhs=old_lhs;
59 
60  float64_t diag_mean=0;
61  float64_t overall_mean=0;
62  for (int32_t i=0; i<n; i++)
63  {
64  diag_mean+=k->compute(i, i);
65 
66  for (int32_t j=0; j<n; j++)
67  overall_mean+=k->compute(i, j);
68  }
69  diag_mean/=n;
70  overall_mean/=((float64_t) n)*n;
71 
72  k->lhs=old_lhs;
73  k->rhs=old_rhs;
74 
75  meandiff=1.0/(diag_mean-overall_mean);
77 
78  return true;
79  }
80 
87  float64_t value, int32_t idx_lhs, int32_t idx_rhs)
88  {
89  return value*meandiff;
90  }
91 
96  virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
97  {
98  return value*sqrt_meandiff;
99  }
100 
105  virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
106  {
107  return value*sqrt_meandiff;
108  }
109 
111  virtual const char* get_name() const { return "VarianceKernelNormalizer"; }
112 
113  protected:
118 };
119 }
120 #endif
virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
virtual float64_t compute(int32_t x, int32_t y)=0
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
virtual const char * get_name() const
virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
The class Kernel Normalizer defines a function to post-process kernel values.
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:159
#define SG_ADD(...)
Definition: SGObject.h:84
static float32_t sqrt(float32_t x)
Definition: Math.h:459
VarianceKernelNormalizer divides by the ``variance''.

SHOGUN Machine Learning Toolbox - Documentation