SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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

SHOGUN Machine Learning Toolbox - Documentation