SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExactInferenceMethod.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  * Code adapted from Gaussian Process Machine Learning Toolbox
12  * http://www.gaussianprocess.org/gpml/code/matlab/doc/
13  */
14 
16 
17 #ifdef HAVE_EIGEN3
18 
23 
24 using namespace shogun;
25 using namespace Eigen;
26 
28 {
29 }
30 
32  CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) :
33  CInferenceMethod(kern, feat, m, lab, mod)
34 {
35 }
36 
38 {
39 }
40 
42 {
43  SG_DEBUG("entering\n");
44 
46  update_chol();
47  update_alpha();
48  update_deriv();
49  update_mean();
50  update_cov();
52 
53  SG_DEBUG("leaving\n");
54 }
55 
57 {
59 
61  "Exact inference method can only use Gaussian likelihood function\n")
63  "Labels must be type of CRegressionLabels\n")
64 }
65 
67 {
69  update();
70 
71  // get the sigma variable from the Gaussian likelihood model
73  float64_t sigma=lik->get_sigma();
74  SG_UNREF(lik);
75 
76  // compute diagonal vector: sW=1/sigma
78  result.fill_vector(result.vector, m_features->get_num_vectors(), 1.0/sigma);
79 
80  return result;
81 }
82 
84 {
86  update();
87 
88  // get the sigma variable from the Gaussian likelihood model
90  float64_t sigma=lik->get_sigma();
91  SG_UNREF(lik);
92 
93  // create eigen representation of alpha and L
94  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
95  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
96 
97  // get labels and mean vectors and create eigen representation
98  SGVector<float64_t> y=((CRegressionLabels*) m_labels)->get_labels();
99  Map<VectorXd> eigen_y(y.vector, y.vlen);
101  Map<VectorXd> eigen_m(m.vector, m.vlen);
102 
103  // compute negative log of the marginal likelihood:
104  // nlZ=(y-m)'*alpha/2+sum(log(diag(L)))+n*log(2*pi*sigma^2)/2
105  float64_t result=(eigen_y-eigen_m).dot(eigen_alpha)/2.0+
106  eigen_L.diagonal().array().log().sum()+m_L.num_rows*
107  CMath::log(2*CMath::PI*CMath::sq(sigma))/2.0;
108 
109  return result;
110 }
111 
113 {
115  update();
116 
118 }
119 
121 {
123  update();
124 
125  return SGMatrix<float64_t>(m_L);
126 }
127 
129 {
131  update();
132 
133  return SGVector<float64_t>(m_mu);
134 }
135 
137 {
139  update();
140 
141  return SGMatrix<float64_t>(m_Sigma);
142 }
143 
145 {
146  // get the sigma variable from the Gaussian likelihood model
148  float64_t sigma=lik->get_sigma();
149  SG_UNREF(lik);
150 
151  /* check whether to allocate cholesky memory */
154 
155  /* creates views on kernel and cholesky matrix and perform cholesky */
156  Map<MatrixXd> K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
157  Map<MatrixXd> L(m_L.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
158  LLT<MatrixXd> llt(K*(CMath::sq(m_scale)/CMath::sq(sigma))+
159  MatrixXd::Identity(m_ktrtr.num_rows, m_ktrtr.num_cols));
160  L=llt.matrixU();
161 }
162 
164 {
165  // get the sigma variable from the Gaussian likelihood model
167  float64_t sigma=lik->get_sigma();
168  SG_UNREF(lik);
169 
170  // get labels and mean vector and create eigen representation
171  SGVector<float64_t> y=((CRegressionLabels*) m_labels)->get_labels();
172  Map<VectorXd> eigen_y(y.vector, y.vlen);
174  Map<VectorXd> eigen_m(m.vector, m.vlen);
175 
177 
178  /* creates views on cholesky matrix and alpha and solve system
179  * (L * L^T) * a = y for a */
180  Map<VectorXd> a(m_alpha.vector, m_alpha.vlen);
181  Map<MatrixXd> L(m_L.matrix, m_L.num_rows, m_L.num_cols);
182 
183  a=L.triangularView<Upper>().adjoint().solve(eigen_y-eigen_m);
184  a=L.triangularView<Upper>().solve(a);
185 
186  a/=CMath::sq(sigma);
187 }
188 
190 {
191  // create eigen representataion of kernel matrix and alpha
192  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
193  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
194 
195  // get mean and create eigen representation of it
197  Map<VectorXd> eigen_m(m.vector, m.vlen);
198 
199  m_mu=SGVector<float64_t>(m.vlen);
200  Map<VectorXd> eigen_mu(m_mu.vector, m_mu.vlen);
201 
202  // compute mean: mu=K'*alpha+m
203  eigen_mu=eigen_K*CMath::sq(m_scale)*eigen_alpha+eigen_m;
204 }
205 
207 {
208  // create eigen representataion of upper triangular factor L^T and kernel
209  // matrix
210  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
211  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
212 
214  Map<MatrixXd> eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows,
215  m_Sigma.num_cols);
216 
217  // compute V = L^(-1) * K, using upper triangular factor L^T
218  MatrixXd eigen_V=eigen_L.triangularView<Upper>().adjoint().solve(
219  eigen_K*CMath::sq(m_scale));
220 
221  // compute covariance matrix of the posterior: Sigma = K - V^T * V
222  eigen_Sigma=eigen_K*CMath::sq(m_scale)-eigen_V.adjoint()*eigen_V;
223 }
224 
226 {
227  // get the sigma variable from the Gaussian likelihood model
229  float64_t sigma=lik->get_sigma();
230  SG_UNREF(lik);
231 
232  // create eigen representation of derivative matrix and cholesky
233  Map<MatrixXd> eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols);
234  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
235 
237  Map<MatrixXd> eigen_Q(m_Q.matrix, m_Q.num_rows, m_Q.num_cols);
238 
239  // solve L * L' * Q = I
240  eigen_Q=eigen_L.triangularView<Upper>().adjoint().solve(
241  MatrixXd::Identity(m_L.num_rows, m_L.num_cols));
242  eigen_Q=eigen_L.triangularView<Upper>().solve(eigen_Q);
243 
244  // divide Q by sigma^2
245  eigen_Q/=CMath::sq(sigma);
246 
247  // create eigen representation of alpha and compute Q=Q-alpha*alpha'
248  eigen_Q-=eigen_alpha*eigen_alpha.transpose();
249 }
250 
252  const TParameter* param)
253 {
254  REQUIRE(!strcmp(param->m_name, "scale"), "Can't compute derivative of "
255  "the nagative log marginal likelihood wrt %s.%s parameter\n",
256  get_name(), param->m_name)
257 
258  Map<MatrixXd> eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols);
259  Map<MatrixXd> eigen_Q(m_Q.matrix, m_Q.num_rows, m_Q.num_cols);
260 
261  SGVector<float64_t> result(1);
262 
263  // compute derivative wrt kernel scale: dnlZ=sum(Q.*K*scale*2)/2
264  result[0]=(eigen_Q.cwiseProduct(eigen_K)*m_scale*2.0).sum()/2.0;
265 
266  return result;
267 }
268 
270  const TParameter* param)
271 {
272  REQUIRE(!strcmp(param->m_name, "sigma"), "Can't compute derivative of "
273  "the nagative log marginal likelihood wrt %s.%s parameter\n",
274  m_model->get_name(), param->m_name)
275 
276  // get the sigma variable from the Gaussian likelihood model
278  float64_t sigma=lik->get_sigma();
279  SG_UNREF(lik);
280 
281  // create eigen representation of the matrix Q
282  Map<MatrixXd> eigen_Q(m_Q.matrix, m_Q.num_rows, m_Q.num_cols);
283 
284  SGVector<float64_t> result(1);
285 
286  // compute derivative wrt likelihood model parameter sigma:
287  // dnlZ=sigma^2*trace(Q)
288  result[0]=CMath::sq(sigma)*eigen_Q.trace();
289 
290  return result;
291 }
292 
294  const TParameter* param)
295 {
296  // create eigen representation of the matrix Q
297  Map<MatrixXd> eigen_Q(m_Q.matrix, m_Q.num_rows, m_Q.num_cols);
298 
299  SGVector<float64_t> result;
300 
301  if (param->m_datatype.m_ctype==CT_VECTOR ||
302  param->m_datatype.m_ctype==CT_SGVECTOR)
303  {
305  "Length of the parameter %s should not be NULL\n", param->m_name)
306  result=SGVector<float64_t>(*(param->m_datatype.m_length_y));
307  }
308  else
309  {
310  result=SGVector<float64_t>(1);
311  }
312 
313  for (index_t i=0; i<result.vlen; i++)
314  {
316 
317  if (result.vlen==1)
318  dK=m_kernel->get_parameter_gradient(param);
319  else
320  dK=m_kernel->get_parameter_gradient(param, i);
321 
322  Map<MatrixXd> eigen_dK(dK.matrix, dK.num_rows, dK.num_cols);
323 
324  // compute derivative wrt kernel parameter: dnlZ=sum(Q.*dK*scale)/2.0
325  result[i]=(eigen_Q.cwiseProduct(eigen_dK)*CMath::sq(m_scale)).sum()/2.0;
326  }
327 
328  return result;
329 }
330 
332  const TParameter* param)
333 {
334  // create eigen representation of alpha vector
335  Map<VectorXd> eigen_alpha(m_alpha.vector, m_alpha.vlen);
336 
337  SGVector<float64_t> result;
338 
339  if (param->m_datatype.m_ctype==CT_VECTOR ||
340  param->m_datatype.m_ctype==CT_SGVECTOR)
341  {
343  "Length of the parameter %s should not be NULL\n", param->m_name)
344 
345  result=SGVector<float64_t>(*(param->m_datatype.m_length_y));
346  }
347  else
348  {
349  result=SGVector<float64_t>(1);
350  }
351 
352  for (index_t i=0; i<result.vlen; i++)
353  {
355 
356  if (result.vlen==1)
358  else
360 
361  Map<VectorXd> eigen_dmu(dmu.vector, dmu.vlen);
362 
363  // compute derivative wrt mean parameter: dnlZ=-dmu'*alpha
364  result[i]=-eigen_dmu.dot(eigen_alpha);
365  }
366 
367  return result;
368 }
369 
370 #endif /* HAVE_EIGEN3 */
virtual const char * get_name() const =0
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
Definition: SGVector.h:344
virtual ELabelType get_label_type() const =0
virtual void update_parameter_hash()
Definition: SGObject.cpp:187
Class that models Gaussian likelihood.
Real Labels are real-valued labels.
SGVector< float64_t > m_alpha
The Inference Method base class.
virtual SGMatrix< float64_t > get_posterior_covariance()
int32_t index_t
Definition: common.h:60
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
real valued labels (e.g. for regression, classifier outputs)
Definition: LabelTypes.h:18
virtual const char * get_name() const
virtual SGVector< float64_t > get_alpha()
static T sum(T *vec, int32_t len)
return sum(vec)
Definition: SGVector.h:506
#define SG_UNREF(x)
Definition: SGRefObject.h:35
static T sq(T x)
x^2
Definition: Math.h:302
virtual ELikelihoodModelType get_model_type() const
parameter struct
Definition: Parameter.h:26
virtual int32_t get_num_vectors() const =0
#define REQUIRE(x,...)
Definition: SGIO.h:208
virtual SGVector< float64_t > get_mean_vector(const CFeatures *features) const =0
An abstract class of the mean function.
Definition: MeanFunction.h:26
TSGDataType m_datatype
Definition: Parameter.h:159
SGMatrix< float64_t > m_L
virtual SGMatrix< float64_t > get_cholesky()
virtual SGVector< float64_t > get_derivative_wrt_mean(const TParameter *param)
double float64_t
Definition: common.h:48
virtual SGVector< float64_t > get_derivative_wrt_inference_method(const TParameter *param)
virtual SGVector< float64_t > get_diagonal_vector()
index_t num_rows
Definition: SGMatrix.h:301
static void fill_vector(T *vec, int32_t len, T value)
Definition: SGVector.cpp:271
index_t num_cols
Definition: SGMatrix.h:303
static CGaussianLikelihood * obtain_from_generic(CLikelihoodModel *lik)
index_t * m_length_y
Definition: DataType.h:77
virtual SGVector< float64_t > get_parameter_derivative(const CFeatures *features, const TParameter *param, index_t index=-1)
Definition: MeanFunction.h:50
EContainerType m_ctype
Definition: DataType.h:70
#define SG_DEBUG(...)
Definition: SGIO.h:109
virtual float64_t get_negative_log_marginal_likelihood()
The class Features is the base class of all feature objects.
Definition: Features.h:62
virtual SGVector< float64_t > get_derivative_wrt_kernel(const TParameter *param)
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
Definition: Kernel.h:574
static float64_t log(float64_t v)
Definition: Math.h:483
virtual void check_members() const
virtual SGVector< float64_t > get_derivative_wrt_likelihood_model(const TParameter *param)
The Kernel base class.
Definition: Kernel.h:150
virtual bool parameter_hash_changed()
Definition: SGObject.cpp:200
The Likelihood model base class.
SGMatrix< float64_t > m_ktrtr
CLikelihoodModel * m_model
index_t vlen
Definition: SGVector.h:706
virtual SGVector< float64_t > get_posterior_mean()
static const float64_t PI
Definition: Math.h:1400

SHOGUN Machine Learning Toolbox - Documentation