BayesOpt
kernelregressor.hpp
Go to the documentation of this file.
1 
3 /*
4 -------------------------------------------------------------------------
5  This file is part of BayesOpt, an efficient C++ library for
6  Bayesian optimization.
7 
8  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
9 
10  BayesOpt is free software: you can redistribute it and/or modify it
11  under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  BayesOpt is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Affero General Public License for more details.
19 
20  You should have received a copy of the GNU Affero General Public License
21  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
22 ------------------------------------------------------------------------
23 */
24 
25 
26 #ifndef __NONPARAMETRICPROCESS_HPP__
27 #define __NONPARAMETRICPROCESS_HPP__
28 
29 #include "ublas_cholesky.hpp"
30 #include "nonparametricprocess.hpp"
31 #include "kernel_functors.hpp"
32 
33 namespace bayesopt
34 {
35 
47  {
48  public:
49  KernelRegressor(size_t dim, Parameters parameters, const Dataset& data,
50  MeanModel& mean, randEngine& eng);
51  virtual ~KernelRegressor();
52 
60  void fitSurrogateModel();
61 
69  void updateSurrogateModel();
70 
71 
72  // Getters and setters
73  double getSignalVariance();
74  size_t nHyperParameters();
75  vectord getHyperParameters();
76  void setHyperParameters(const vectord& theta);
77 
79  //void setLearnType(learning_type l_type);
80 
81  protected:
82 
85  virtual void precomputePrediction() = 0;
86 
88  void computeCorrMatrix(matrixd& corrMatrix);
89 
91  matrixd computeCorrMatrix();
92 
95  matrixd computeDerivativeCorrMatrix(int dth_index);
96  vectord computeCrossCorrelation(const vectord &query);
97  double computeSelfCorrelation(const vectord& query);
98 
101 
102 
103  protected:
104  matrixd mL;
105  score_type mScoreType;
106  learning_type mLearnType;
107  bool mLearnAll;
108  KernelModel mKernel;
109 
110  private:
113  void addNewPointToCholesky(const vectord& correlation,
114  double selfcorrelation);
115 
116 
117  const double mRegularizer;
118  };
119 
122  {
125  };
126 
127  inline size_t KernelRegressor::nHyperParameters()
128  {
129  if (mLearnAll)
130  {
131  return mKernel.nHyperParameters()
132  + mMean.nParameters() + 1;
133  }
134  else
135  {
136  return mKernel.nHyperParameters();
137  }
138  }
139 
140  inline vectord KernelRegressor::getHyperParameters()
141  {
142  using boost::numeric::ublas::subrange;
143  if (mLearnAll)
144  {
145  vectord result(nHyperParameters());
146  size_t nk = mKernel.nHyperParameters();
147  size_t nm = mMean.nParameters();
148 
149  subrange(result,0,nk) = mKernel.getHyperParameters();
150 
151  vectord mean = mMean.getParameters();
152  std::transform(mean.begin(), mean.end(), result.begin()+nk, (double (*)(double)) log);
153 
154  result(nk+nm) = std::log(mSigma);
155  return result;
156  }
157  else
158  {
159  return mKernel.getHyperParameters();
160  }
161  };
162 
163  inline void KernelRegressor::setHyperParameters(const vectord &theta)
164  {
165  using boost::numeric::ublas::subrange;
166  if (mLearnAll)
167  {
168  size_t nk = mKernel.nHyperParameters();
169  size_t nm = mMean.nParameters();
170 
171  mKernel.setHyperParameters(subrange(theta,0,nk));
172 
173  vectord result(nm);
174  std::transform(theta.begin()+nk, theta.begin()+nk+nm,
175  result.begin(), (double (*)(double)) log);
176  mMean.setParameters(result);
177 
178  mSigma = std::exp(theta(nk+nm));
179  }
180  else
181  {
182  mKernel.setHyperParameters(theta);
183  }
184  };
185 
186  // inline void KernelRegressor::setLearnType(learning_type l_type)
187  // { mLearnType = l_type; };
188 
189  inline void KernelRegressor::computeCorrMatrix(matrixd& corrMatrix)
190  { mKernel.computeCorrMatrix(mData.mX,corrMatrix,mRegularizer); }
191 
193  {
194  const size_t nSamples = mData.getNSamples();
195  matrixd corrMatrix(nSamples,nSamples);
196  mKernel.computeCorrMatrix(mData.mX,corrMatrix,mRegularizer);
197  return corrMatrix;
198  }
199 
200  inline vectord KernelRegressor::computeCrossCorrelation(const vectord &query)
201  { return mKernel.computeCrossCorrelation(mData.mX,query); }
202 
203  inline double KernelRegressor::computeSelfCorrelation(const vectord& query)
204  { return mKernel.computeSelfCorrelation(query); }
205 
206  inline void KernelRegressor::addNewPointToCholesky(const vectord& correlation,
207  double selfcorrelation)
208  {
209  vectord newK(correlation);
210  utils::append(newK, selfcorrelation);
212  }
213 
216 } //namespace bayesopt
217 
218 #endif
matrixd computeDerivativeCorrMatrix(int dth_index)
Computes the derivative of the correlation matrix with respect to the dth hyperparameter.
matrixd computeCorrMatrix()
Computes the Correlation (Kernel or Gram) matrix.
Kernel (covariance) functions.
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
Abstract class to implement Bayesian regressors.
void fitSurrogateModel()
Computes the initial surrogate model and updates the kernel parameters estimation.
matrixd mL
Cholesky decomposition of the Correlation matrix.
Dataset model to deal with the vector (real) based datasets.
Definition: dataset.hpp:40
virtual void precomputePrediction()=0
Sets the kind of learning methodology for kernel hyperparameters.
const double mRegularizer
Std of the obs. model (also used as nugget)
Abstract module for a Bayesian regressor.
Cholesky decomposition.
Abstract class to implement non-parametric processes.
void addNewPointToCholesky(const vectord &correlation, double selfcorrelation)
Adds a new point to the Cholesky decomposition of the Correlation matrix.
vecOfvec mX
Data inputs.
Definition: dataset.hpp:63