BayesOpt
conditionalbayesprocess.cpp
1 
2 /*
3 -------------------------------------------------------------------------
4  This file is part of BayesOpt, an efficient C++ library for
5  Bayesian optimization.
6 
7  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
8 
9  BayesOpt is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Affero General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BayesOpt is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Affero General Public License for more details.
18 
19  You should have received a copy of the GNU Affero General Public License
20  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
21 ------------------------------------------------------------------------
22 */
23 
24 
26 #include "log.hpp"
27 //#include "optimizekernel.hpp"
28 
29 
30 namespace bayesopt
31 {
32  ConditionalBayesProcess::ConditionalBayesProcess(size_t dim, Parameters parameters,
33  const Dataset& data,
34  MeanModel& mean, randEngine& eng):
35  KernelRegressor(dim,parameters,data,mean,eng)
36  {}
37 
38  ConditionalBayesProcess::~ConditionalBayesProcess()
39  {}
40 
41 
42  double ConditionalBayesProcess::evaluateKernelParams()
43  {
44  switch(mScoreType)
45  {
46  case SC_MTL:
47  return negativeTotalLogLikelihood();
48  case SC_ML:
49  return negativeLogLikelihood();
50  case SC_MAP:
51  // It is a minus because the prior is the positive and we want
52  // the negative.
53  return negativeLogLikelihood()-mKernel.kernelLogPrior();
54  case SC_LOOCV:
55  return negativeCrossValidation();
56  default:
57  throw std::invalid_argument("Learning type not supported");
58  }
59  }
60 
61 
62  double ConditionalBayesProcess::negativeCrossValidation()
63  {
64  // This is highly ineffient implementation for comparison purposes.
65  Dataset data(mData);
66 
67  size_t n = data.getNSamples();
68  double sum = 0.0;
69 
70  matrixd tempF(mMean.mFeatM);
71 
72 
73  // We take the first element, use it for validation and then paste
74  // it at the end. Thus, after every iteration, the first element
75  // is different and, at the end, all the elements should have
76  // rotated.
77  for(size_t i = 0; i<n; ++i)
78  {
79  // Take the first element
80  const double y = data.getSampleY(0);
81  const vectord x = data.getSampleX(0);
82 
83  // Remove it for cross validation
84  data.mX.erase(data.mX.begin());
85  utils::erase(data.mY,data.mY.begin());
86  utils::erase_column(mMean.mFeatM,0);
87 
88  // Compute the cross validation
89  computeCholeskyCorrelation();
90  precomputePrediction();
91  ProbabilityDistribution* pd = prediction(x);
92  sum += std::log(pd->pdf(y));
93 
94  //Paste it back at the end
95  data.addSample(x,y);
96  mMean.mFeatM.resize(mMean.mFeatM.size1(),mMean.mFeatM.size2()+1);
97  mMean.mFeatM = tempF;
98  }
99  std::cout << "End" << data.getNSamples();
100  return -sum; //Because we are minimizing.
101  }
102 
103 } // namespace bayesopt
Namespace of the library interface.
Definition: using.dox:1
Kernel based nonparametric process, conditional on kernel hyperparameters.
virtual double pdf(double x)=0
Probability density function.
Dataset model to deal with the vector (real) based datasets.
Definition: dataset.hpp:40
Modules and helper macros for logging.
vectord mY
Data values.
Definition: dataset.hpp:64
vecOfvec mX
Data inputs.
Definition: dataset.hpp:63