SHOGUN  v3.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  * Written (W) 2013 Roman Votyakov
8  * Copyright (C) 2012 Jacob Walker
9  * Copyright (C) 2013 Roman Votyakov
10  */
11 
13 
14 #ifdef HAVE_EIGEN3
15 
19 
20 using namespace shogun;
21 using namespace Eigen;
22 
24 {
25  init();
26 }
27 
29 {
30  REQUIRE(sigma>0.0, "Standard deviation must be greater than zero\n")
31  init();
32  m_sigma=sigma;
33 }
34 
35 void CGaussianLikelihood::init()
36 {
37  m_sigma=1.0;
38  SG_ADD(&m_sigma, "sigma", "Observation noise", MS_AVAILABLE, GRADIENT_AVAILABLE);
39 }
40 
42 {
43 }
44 
46  CLikelihoodModel* lik)
47 {
48  ASSERT(lik!=NULL);
49 
50  if (lik->get_model_type()!=LT_GAUSSIAN)
51  SG_SERROR("Provided likelihood is not of type CGaussianLikelihood!\n")
52 
53  SG_REF(lik);
54  return (CGaussianLikelihood*)lik;
55 }
56 
58  SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels* lab) const
59 {
60  return SGVector<float64_t>(mu);
61 }
62 
64  SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels* lab) const
65 {
66  SGVector<float64_t> result(s2);
67  Map<VectorXd> eigen_result(result.vector, result.vlen);
68 
69  eigen_result=eigen_result.array()+CMath::sq(m_sigma);
70 
71  return result;
72 }
73 
75  SGVector<float64_t> func) const
76 {
77  // check the parameters
78  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
80  "Labels must be type of CRegressionLabels\n")
81  REQUIRE(lab->get_num_labels()==func.vlen, "Number of labels must match "
82  "length of the function vector\n")
83 
84  Map<VectorXd> eigen_f(func.vector, func.vlen);
85 
86  SGVector<float64_t> result(func.vlen);
87  Map<VectorXd> eigen_result(result.vector, result.vlen);
88 
89  SGVector<float64_t> y=((CRegressionLabels*)lab)->get_labels();
90  Map<VectorXd> eigen_y(y.vector, y.vlen);
91 
92  // compute log probability: lp=-(y-f).^2./sigma^2/2-log(2*pi*sigma^2)/2
93  eigen_result=eigen_y-eigen_f;
94  eigen_result=-eigen_result.cwiseProduct(eigen_result)/(2*CMath::sq(m_sigma))-
95  VectorXd::Ones(result.vlen)*log(2*CMath::PI*CMath::sq(m_sigma))/2.0;
96 
97  return result;
98 }
99 
101  const CLabels* lab, SGVector<float64_t> func, index_t i) const
102 {
103  // check the parameters
104  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
106  "Labels must be type of CRegressionLabels\n")
107  REQUIRE(lab->get_num_labels()==func.vlen, "Number of labels must match "
108  "length of the function vector\n")
109  REQUIRE(i>=1 && i<=3, "Index for derivative should be 1, 2 or 3\n")
110 
111  Map<VectorXd> eigen_f(func.vector, func.vlen);
112 
113  SGVector<float64_t> result(func.vlen);
114  Map<VectorXd> eigen_result(result.vector, result.vlen);
115 
116  SGVector<float64_t> y=((CRegressionLabels*)lab)->get_labels();
117  Map<VectorXd> eigen_y(y.vector, y.vlen);
118 
119  // set result=y-f
120  eigen_result=eigen_y-eigen_f;
121 
122  // compute derivatives of log probability wrt f
123  if (i == 1)
124  eigen_result/=CMath::sq(m_sigma);
125  else if (i == 2)
126  eigen_result=-VectorXd::Ones(result.vlen)/CMath::sq(m_sigma);
127  else if (i == 3)
128  eigen_result=VectorXd::Zero(result.vlen);
129 
130  return result;
131 }
132 
134  SGVector<float64_t> func, const TParameter* param) const
135 {
136  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
138  "Labels must be type of CRegressionLabels\n")
139  REQUIRE(lab->get_num_labels()==func.vlen, "Number of labels must match "
140  "length of the function vector\n")
141 
142  Map<VectorXd> eigen_f(func.vector, func.vlen);
143 
144  SGVector<float64_t> result(func.vlen);
145  Map<VectorXd> eigen_result(result.vector, result.vlen);
146 
147  if (strcmp(param->m_name, "sigma"))
148  return SGVector<float64_t>();
149 
150  SGVector<float64_t> y=((CRegressionLabels*)lab)->get_labels();
151  Map<VectorXd> eigen_y(y.vector, y.vlen);
152 
153  // compute derivative of log probability wrt sigma:
154  // lp_dsigma=(y-f).^2/sigma^2-1
155  eigen_result=eigen_y-eigen_f;
156  eigen_result=eigen_result.cwiseProduct(eigen_result)/CMath::sq(m_sigma);
157  eigen_result-=VectorXd::Ones(result.vlen);
158 
159  return result;
160 }
161 
163  SGVector<float64_t> func, const TParameter* param) const
164 {
165  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
167  "Labels must be type of CRegressionLabels\n")
168  REQUIRE(lab->get_num_labels()==func.vlen, "Number of labels must match "
169  "length of the function vector\n")
170 
171  if (strcmp(param->m_name, "sigma"))
172  return SGVector<float64_t>();
173 
174  Map<VectorXd> eigen_f(func.vector, func.vlen);
175 
176  SGVector<float64_t> result(func.vlen);
177  Map<VectorXd> eigen_result(result.vector, result.vlen);
178 
179  SGVector<float64_t> y=((CRegressionLabels*)lab)->get_labels();
180  Map<VectorXd> eigen_y(y.vector, y.vlen);
181 
182  // compute derivative of the first derivative of log probability wrt sigma:
183  // dlp_dsigma=2*(f-y)/sigma^2
184  eigen_result=2*(eigen_f-eigen_y)/CMath::sq(m_sigma);
185 
186  return result;
187 }
188 
190  SGVector<float64_t> func, const TParameter* param) const
191 {
192  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
194  "Labels must be type of CRegressionLabels\n")
195  REQUIRE(lab->get_num_labels()==func.vlen, "Number of labels must match "
196  "length of the function vector\n")
197 
198  if (strcmp(param->m_name, "sigma"))
199  return SGVector<float64_t>();
200 
201  Map<VectorXd> eigen_f(func.vector, func.vlen);
202 
203  SGVector<float64_t> result(func.vlen);
204  Map<VectorXd> eigen_result(result.vector, result.vlen);
205 
206  // compute derivative of the second derivative of log probability wrt sigma:
207  // d2lp_dsigma=1/sigma^2
208  eigen_result=2*VectorXd::Ones(result.vlen)/CMath::sq(m_sigma);
209 
210  return result;
211 }
212 
214  SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels *lab) const
215 {
217 
218  if (lab)
219  {
220  REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
221  "Length of the vector of means (%d), length of the vector of "
222  "variances (%d) and number of labels (%d) should be the same\n",
223  mu.vlen, s2.vlen, lab->get_num_labels())
225  "Labels must be type of CRegressionLabels\n")
226 
227  y=((CRegressionLabels*)lab)->get_labels();
228  }
229  else
230  {
231  REQUIRE(mu.vlen==s2.vlen, "Length of the vector of means (%d) and "
232  "length of the vector of variances (%d) should be the same\n",
233  mu.vlen, s2.vlen)
234 
236  y.set_const(1.0);
237  }
238 
239  // create eigen representation of y, mu and s2
240  Map<VectorXd> eigen_mu(mu.vector, mu.vlen);
241  Map<VectorXd> eigen_s2(s2.vector, s2.vlen);
242  Map<VectorXd> eigen_y(y.vector, y.vlen);
243 
244  SGVector<float64_t> result(mu.vlen);
245  Map<VectorXd> eigen_result(result.vector, result.vlen);
246 
247  // compule lZ=-(y-mu).^2./(sn2+s2)/2-log(2*pi*(sn2+s2))/2
248  eigen_s2=eigen_s2.array()+CMath::sq(m_sigma);
249  eigen_result=-(eigen_y-eigen_mu).array().square()/(2.0*eigen_s2.array())-
250  (2.0*CMath::PI*eigen_s2.array()).log()/2.0;
251 
252  return result;
253 }
254 
256  SGVector<float64_t> s2, const CLabels *lab, index_t i) const
257 {
258  // check the parameters
259  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
260  REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
261  "Length of the vector of means (%d), length of the vector of "
262  "variances (%d) and number of labels (%d) should be the same\n",
263  mu.vlen, s2.vlen, lab->get_num_labels())
264  REQUIRE(i>=0 && i<=mu.vlen, "Index (%d) out of bounds!\n", i)
266  "Labels must be type of CRegressionLabels\n")
267 
268  SGVector<float64_t> y=((CRegressionLabels*)lab)->get_labels();
269 
270  // compute 1st moment
271  float64_t Ex=mu[i]+s2[i]*(y[i]-mu[i])/(CMath::sq(m_sigma)+s2[i]);
272 
273  return Ex;
274 }
275 
277  SGVector<float64_t> s2, const CLabels *lab, index_t i) const
278 {
279  // check the parameters
280  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
281  REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
282  "Length of the vector of means (%d), length of the vector of "
283  "variances (%d) and number of labels (%d) should be the same\n",
284  mu.vlen, s2.vlen, lab->get_num_labels())
285  REQUIRE(i>=0 && i<=mu.vlen, "Index (%d) out of bounds!\n", i)
287  "Labels must be type of CRegressionLabels\n")
288 
289  // compute 2nd moment
290  float64_t Var=s2[i]-CMath::sq(s2[i])/(CMath::sq(m_sigma)+s2[i]);
291 
292  return Var;
293 }
294 
295 #endif /* HAVE_EIGEN3 */

SHOGUN Machine Learning Toolbox - Documentation