BayesOpt
gaussian_process.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.hpp"
25 
26 namespace bayesopt
27 {
28 
29  namespace ublas = boost::numeric::ublas;
30 
31  GaussianProcess::GaussianProcess(size_t dim, Parameters params,
32  const Dataset& data, MeanModel& mean,
33  randEngine& eng):
34  ConditionalBayesProcess(dim, params, data, mean, eng)
35  {
36  mSigma = params.sigma_s;
37  d_ = new GaussianDistribution(eng);
38  } // Constructor
39 
40 
41  GaussianProcess::~GaussianProcess()
42  {
43  delete d_;
44  } // Default destructor
45 
46 
48  {
49  // In this case it is equivalent.
50  return negativeLogLikelihood();
51  }
52 
53 
55  {
56  const matrixd K = computeCorrMatrix();
57  const size_t n = K.size1();
58  matrixd L(n,n);
60 
61  vectord alpha(mData.mY-mMean.muTimesFeat());
62  inplace_solve(L,alpha,ublas::lower_tag());
63  double loglik = ublas::inner_prod(alpha,alpha)/(2*mSigma);
64  loglik += utils::log_trace(L);
65  return loglik;
66  }
67 
69  {
70  const double kq = computeSelfCorrelation(query);
71  const vectord kn = computeCrossCorrelation(query);
72 
73 
74  vectord vd(kn);
75  inplace_solve(mL,vd,ublas::lower_tag());
76  double basisPred = mMean.muTimesFeat(query);
77  double yPred = basisPred + ublas::inner_prod(vd,mAlphaV);
78  double sPred = sqrt(mSigma*(kq - ublas::inner_prod(vd,vd)));
79 
80  d_->setMeanAndStd(yPred,sPred);
81  return d_;
82  }
83 
84 
86  {
87  const size_t n = mData.getNSamples();
88 
89  mAlphaV.resize(n,false);
90  mAlphaV = mData.mY-mMean.muTimesFeat();
91  inplace_solve(mL,mAlphaV,ublas::lower_tag());
92  }
93 
94 } //namespace bayesopt
Namespace of the library interface.
Definition: using.dox:1
ProbabilityDistribution * prediction(const vectord &query)
Function that returns the prediction of the GP for a query point in the hypercube [0...
double negativeTotalLogLikelihood()
Computes the negative log likelihood of the data for all the parameters.
double negativeLogLikelihood()
Computes the negative log likelihood of the data.
size_t cholesky_decompose(const MATRIX &A, TRIA &L)
decompose the symmetric positive definit matrix A into product L L^T.
Standard zero mean gaussian process with noisy observations.
void precomputePrediction()
Precompute some values of the prediction that do not depends on the query.