BayesOpt
gaussian_process_ml.cpp
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 
23 #include "ublas_trace.hpp"
24 #include "gaussian_process_ml.hpp"
25 
26 namespace bayesopt
27 {
28 
29  namespace ublas = boost::numeric::ublas;
30 
31  GaussianProcessML::GaussianProcessML(size_t dim, Parameters params,
32  const Dataset& data,
33  MeanModel& mean, randEngine& eng):
34  HierarchicalGaussianProcess(dim, params, data, mean, eng)
35  {
36  d_ = new GaussianDistribution(eng);
37  } // Constructor
38 
39 
40 
41  GaussianProcessML::~GaussianProcessML()
42  {
43  delete d_;
44  } // Default destructor
45 
46 
47 
48 
50  {
51  /* In this case, they are equivalent */
53  }
54 
55 
57  {
58  double kq = computeSelfCorrelation(query);
59  vectord kn = computeCrossCorrelation(query);
60  vectord phi = mMean.getFeatures(query);
61 
62  vectord v(kn);
63  inplace_solve(mL,v,ublas::lower_tag());
64 
65  vectord rq = phi - prod(v,mKF);
66 
67  vectord rho(rq);
68  inplace_solve(mL2,rho,ublas::lower_tag());
69 
70  double yPred = inner_prod(phi,mWML) + inner_prod(v,mAlphaF);
71  double sPred = sqrt( mSigma * (kq - inner_prod(v,v)
72  + inner_prod(rho,rho)));
73 
74  d_->setMeanAndStd(yPred,sPred);
75  return d_;
76  }
77 
79  {
80  size_t n = mData.getNSamples();
81  size_t p = mMean.nFeatures();
82 
83  mKF = trans(mMean.mFeatM);
84  inplace_solve(mL,mKF,ublas::lower_tag());
85 
86  matrixd FKF = prod(trans(mKF),mKF);
87  mL2 = FKF;
89 
90  vectord Ky(mData.mY);
91  inplace_solve(mL,Ky,ublas::lower_tag());
92 
93  mWML = prod(Ky,mKF);
94  utils::cholesky_solve(mL2,mWML,ublas::lower());
95 
96  mAlphaF = mData.mY - prod(mWML,mMean.mFeatM);
97  inplace_solve(mL,mAlphaF,ublas::lower_tag());
98  mSigma = inner_prod(mAlphaF,mAlphaF)/(n-p);
99  }
100 
101 } //namespace bayesopt
Gaussian process with ML parameters.
ProbabilityDistribution * prediction(const vectord &query)
Function that returns the prediction of the GP for a query point in the hypercube [0...
Namespace of the library interface.
Definition: using.dox:1
double negativeTotalLogLikelihood()
Computes the negative log likelihood of the data for all the parameters.
matrixd mL
Cholesky decomposition of the Correlation matrix.
size_t cholesky_decompose(const MATRIX &A, TRIA &L)
decompose the symmetric positive definit matrix A into product L L^T.
double negativeLogLikelihood()
Computes the negative log likelihood and its gradient of the data.
void cholesky_solve(const TRIA &L, MATRIX &x, ublas::lower)
solve system L L^T x = b inplace
matrixd mFeatM
Value of the mean features at the input points.
vectord mY
Data values.
Definition: dataset.hpp:64
void precomputePrediction()
Precompute some values of the prediction that do not depends on the query.