SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianLikelihood.cpp
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  * Copyright (C) 2012 Jacob Walker
8  */
9 
11 #ifdef HAVE_EIGEN3
14 
15 #include <shogun/base/Parameter.h>
16 
17 using namespace shogun;
18 using namespace Eigen;
19 
21 {
22  init();
23 }
24 
25 void CGaussianLikelihood::init()
26 {
27  m_sigma = 0.01;
28  SG_ADD(&m_sigma, "sigma", "Observation Noise.", MS_AVAILABLE);
29 }
30 
32 {
33 }
34 
35 
37  SGVector<float64_t>& means)
38 {
39  return SGVector<float64_t>(means);
40 }
41 
43  SGVector<float64_t>& vars)
44 {
45  SGVector<float64_t> result(vars);
46 
47  for (index_t i = 0; i < result.vlen; i++)
48  result[i] += (m_sigma*m_sigma);
49 
50  return result;
51 }
52 
54  SGVector<float64_t> m_function)
55 {
56  Map<VectorXd> function(m_function.vector, m_function.vlen);
57 
58  VectorXd result(function.rows());
59 
60  for (index_t i = 0; i < function.rows(); i++)
61  result[i] = labels->get_labels()[i] - function[i];
62 
63  result = result.cwiseProduct(result);
64 
65  result /= -2*m_sigma*m_sigma;
66 
67  for (index_t i = 0; i < function.rows(); i++)
68  result[i] -= log(2*CMath::PI*m_sigma*m_sigma)/2.0;
69 
70  return result.sum();
71 }
72 
74  CRegressionLabels* labels, SGVector<float64_t> m_function, index_t j)
75 {
76  Map<VectorXd> function(m_function.vector, m_function.vlen);
77  VectorXd result(function.rows());
78 
79  for (index_t i = 0; i < function.rows(); i++)
80  result[i] = labels->get_labels()[i] - function[i];
81 
82  if (j == 1)
83  result = result/(m_sigma*m_sigma);
84 
85  else if (j == 2)
86  result = -VectorXd::Ones(result.rows())/(m_sigma*m_sigma);
87 
88  else if (j == 3)
89  result = VectorXd::Zero(result.rows());
90 
91  else
92  SG_ERROR("Invalid Index for Likelihood Derivative\n");
93 
94  SGVector<float64_t> sgresult(result.rows());
95 
96  for (index_t i = 0; i < result.rows(); i++)
97  sgresult[i] = result[i];
98 
99  return sgresult;
100 }
101 
103  TParameter* param, CSGObject* obj, SGVector<float64_t> m_function)
104 {
105  Map<VectorXd> function(m_function.vector, m_function.vlen);
106 
107  VectorXd result(function.rows());
108 
109  SGVector<float64_t> sgresult(result.rows());
110 
111  if (strcmp(param->m_name, "sigma") || obj != this)
112  {
113  sgresult[0] = CMath::INFTY;
114  return sgresult;
115  }
116 
117  for (index_t i = 0; i < function.rows(); i++)
118  result[i] = labels->get_labels()[i] - function[i];
119 
120  result = result.cwiseProduct(result);
121 
122  result /= m_sigma*m_sigma;
123 
124  for (index_t i = 0; i < function.rows(); i++)
125  result[i] -= 1;
126 
127  for (index_t i = 0; i < result.rows(); i++)
128  sgresult[i] = result[i];
129 
130  return sgresult;
131 }
132 
134  TParameter* param, CSGObject* obj, SGVector<float64_t> m_function)
135 {
136  Map<VectorXd> function(m_function.vector, m_function.vlen);
137  VectorXd result(function.rows());
138 
139  SGVector<float64_t> sgresult(result.rows());
140 
141  if (strcmp(param->m_name, "sigma") || obj != this)
142  {
143  sgresult[0] = CMath::INFTY;
144  return sgresult;
145  }
146 
147  result = 2*VectorXd::Ones(function.rows())/(m_sigma*m_sigma);
148 
149  for (index_t i = 0; i < result.rows(); i++)
150  sgresult[i] = result[i];
151 
152  return sgresult;
153 }
154 
155 #endif //HAVE_EIGEN3
156 
157 

SHOGUN Machine Learning Toolbox - Documentation