BayesOpt
kernelregressor.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 
24 #include <cstdio>
25 #include <cstdlib>
26 #include <stdexcept>
27 #include <boost/lexical_cast.hpp>
28 
29 #include "kernelregressor.hpp"
30 
31 #include "log.hpp"
32 #include "ublas_extra.hpp"
33 
34 
35 namespace bayesopt
36 {
37  KernelRegressor::KernelRegressor(size_t dim, Parameters parameters,
38  const Dataset& data,
39  MeanModel& mean, randEngine& eng):
40  NonParametricProcess(dim,parameters,data,mean,eng),
41  mRegularizer(parameters.noise),
42  mKernel(dim, parameters),
43  mScoreType(parameters.sc_type),
44  mLearnType(parameters.l_type),
45  mLearnAll(parameters.l_all)
46  { }
47 
48  KernelRegressor::~KernelRegressor(){}
49 
50 
51 
53  {
54  const vectord lastX = mData.getLastSampleX();
55  vectord newK = computeCrossCorrelation(lastX);
56  newK(newK.size()-1) += mRegularizer; // We add it to the last element
57  utils::cholesky_add_row(mL,newK);
58  precomputePrediction();
59  } // updateSurrogateModel
60 
61 
63  {
64  size_t nSamples = mData.getNSamples();
65  mL.resize(nSamples,nSamples);
66 
67  // const matrixd K = computeCorrMatrix();
68  matrixd K(nSamples,nSamples);
69  computeCorrMatrix(K);
70  size_t line_error = utils::cholesky_decompose(K,mL);
71  if (line_error)
72  {
73  throw std::runtime_error("Cholesky decomposition error at line " +
74  boost::lexical_cast<std::string>(line_error));
75  }
76  }
77 
79  {
80  const size_t nSamples = mData.getNSamples();
81  matrixd corrMatrix(nSamples,nSamples);
82  mKernel.computeDerivativeCorrMatrix(mData.mX,corrMatrix,dth_index);
83  return corrMatrix;
84  }
85 
86 } //namespace bayesopt
matrixd computeDerivativeCorrMatrix(int dth_index)
Computes the derivative of the correlation matrix with respect to the dth hyperparameter.
void cholesky_add_row(TRIA &L, const VECTOR &v)
decompose the symmetric positive definit matrix A into product L L^T.
void computeCholeskyCorrelation()
Computes the Cholesky decomposition of the Correlation matrix.
void updateSurrogateModel()
Sequential update of the surrogate model by adding a new row to the Kernel matrix, more precisely, to its Cholesky decomposition.
Namespace of the library interface.
Definition: using.dox:1
size_t cholesky_decompose(const MATRIX &A, TRIA &L)
decompose the symmetric positive definit matrix A into product L L^T.
Extra functions for Ublas library.
Modules and helper macros for logging.
Nonparametric process abstract module.