SHOGUN  3.2.1
 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.0*CMath::sq(m_sigma))-
95  VectorXd::Ones(result.vlen)*log(2.0*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 log_sigma:
154  // dlp_dlogsigma
155  // lp_dsigma=(y-f).^2/sigma^2-1
156  eigen_result=eigen_y-eigen_f;
157  eigen_result=eigen_result.cwiseProduct(eigen_result)/CMath::sq(m_sigma);
158  eigen_result-=VectorXd::Ones(result.vlen);
159 
160  return result;
161 }
162 
164  SGVector<float64_t> func, const TParameter* param) const
165 {
166  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
168  "Labels must be type of CRegressionLabels\n")
169  REQUIRE(lab->get_num_labels()==func.vlen, "Number of labels must match "
170  "length of the function vector\n")
171 
172  if (strcmp(param->m_name, "sigma"))
173  return SGVector<float64_t>();
174 
175  Map<VectorXd> eigen_f(func.vector, func.vlen);
176 
177  SGVector<float64_t> result(func.vlen);
178  Map<VectorXd> eigen_result(result.vector, result.vlen);
179 
180  SGVector<float64_t> y=((CRegressionLabels*)lab)->get_labels();
181  Map<VectorXd> eigen_y(y.vector, y.vlen);
182 
183  // compute derivative of (the first log_sigma derivative of log probability) wrt f:
184  // d2lp_dlogsigma_df == d2lp_df_dlogsigma
185  // dlp_dsigma=2*(f-y)/sigma^2
186  eigen_result=2.0*(eigen_f-eigen_y)/CMath::sq(m_sigma);
187 
188  return result;
189 }
190 
192  SGVector<float64_t> func, const TParameter* param) const
193 {
194  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
196  "Labels must be type of CRegressionLabels\n")
197  REQUIRE(lab->get_num_labels()==func.vlen, "Number of labels must match "
198  "length of the function vector\n")
199 
200  if (strcmp(param->m_name, "sigma"))
201  return SGVector<float64_t>();
202 
203  Map<VectorXd> eigen_f(func.vector, func.vlen);
204 
205  SGVector<float64_t> result(func.vlen);
206  Map<VectorXd> eigen_result(result.vector, result.vlen);
207 
208  // compute derivative of (the derivative of the first log_sigma derivative of log probability) wrt f:
209  // d3lp_dlogsigma_df_df == d3lp_df_df_dlogsigma
210  // d2lp_dsigma=2/sigma^2
211  eigen_result=2.0*VectorXd::Ones(result.vlen)/CMath::sq(m_sigma);
212 
213  return result;
214 }
215 
217  SGVector<float64_t> mu, SGVector<float64_t> s2, const CLabels *lab) const
218 {
220 
221  if (lab)
222  {
223  REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
224  "Length of the vector of means (%d), length of the vector of "
225  "variances (%d) and number of labels (%d) should be the same\n",
226  mu.vlen, s2.vlen, lab->get_num_labels())
228  "Labels must be type of CRegressionLabels\n")
229 
230  y=((CRegressionLabels*)lab)->get_labels();
231  }
232  else
233  {
234  REQUIRE(mu.vlen==s2.vlen, "Length of the vector of means (%d) and "
235  "length of the vector of variances (%d) should be the same\n",
236  mu.vlen, s2.vlen)
237 
239  y.set_const(1.0);
240  }
241 
242  // create eigen representation of y, mu and s2
243  Map<VectorXd> eigen_mu(mu.vector, mu.vlen);
244  Map<VectorXd> eigen_s2(s2.vector, s2.vlen);
245  Map<VectorXd> eigen_y(y.vector, y.vlen);
246 
247  SGVector<float64_t> result(mu.vlen);
248  Map<VectorXd> eigen_result(result.vector, result.vlen);
249 
250  // compule lZ=-(y-mu).^2./(sn2+s2)/2-log(2*pi*(sn2+s2))/2
251  eigen_s2=eigen_s2.array()+CMath::sq(m_sigma);
252  eigen_result=-(eigen_y-eigen_mu).array().square()/(2.0*eigen_s2.array())-
253  (2.0*CMath::PI*eigen_s2.array()).log()/2.0;
254 
255  return result;
256 }
257 
259  SGVector<float64_t> s2, const CLabels *lab, index_t i) const
260 {
261  // check the parameters
262  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
263  REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
264  "Length of the vector of means (%d), length of the vector of "
265  "variances (%d) and number of labels (%d) should be the same\n",
266  mu.vlen, s2.vlen, lab->get_num_labels())
267  REQUIRE(i>=0 && i<=mu.vlen, "Index (%d) out of bounds!\n", i)
269  "Labels must be type of CRegressionLabels\n")
270 
271  SGVector<float64_t> y=((CRegressionLabels*)lab)->get_labels();
272 
273  // compute 1st moment
274  float64_t Ex=mu[i]+s2[i]*(y[i]-mu[i])/(CMath::sq(m_sigma)+s2[i]);
275 
276  return Ex;
277 }
278 
280  SGVector<float64_t> s2, const CLabels *lab, index_t i) const
281 {
282  // check the parameters
283  REQUIRE(lab, "Labels are required (lab should not be NULL)\n")
284  REQUIRE((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()),
285  "Length of the vector of means (%d), length of the vector of "
286  "variances (%d) and number of labels (%d) should be the same\n",
287  mu.vlen, s2.vlen, lab->get_num_labels())
288  REQUIRE(i>=0 && i<=mu.vlen, "Index (%d) out of bounds!\n", i)
290  "Labels must be type of CRegressionLabels\n")
291 
292  // compute 2nd moment
293  float64_t Var=s2[i]-CMath::sq(s2[i])/(CMath::sq(m_sigma)+s2[i]);
294 
295  return Var;
296 }
297 
298 #endif /* HAVE_EIGEN3 */

SHOGUN Machine Learning Toolbox - Documentation