BayesOpt
kernel_functors.hpp
Go to the documentation of this file.
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 #ifndef _KERNEL_FUNCTORS_HPP_
25 #define _KERNEL_FUNCTORS_HPP_
26 
27 #include <map>
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/math/distributions/normal.hpp>
30 #include "bayesopt/parameters.hpp"
31 #include "specialtypes.hpp"
32 
33 namespace bayesopt
34 {
35 
41 
43  class Kernel
44  {
45  public:
46  virtual ~Kernel(){};
47  virtual void init(size_t input_dim) {};
48  virtual void init(size_t input_dim, Kernel* left, Kernel* right) {};
49 
50  virtual void setHyperParameters(const vectord &theta) = 0;
51  virtual vectord getHyperParameters() = 0;
52  virtual size_t nHyperParameters() = 0;
53 
54  virtual double operator()( const vectord &x1, const vectord &x2 ) = 0;
55  virtual double gradient( const vectord &x1, const vectord &x2,
56  size_t component ) = 0;
57 
58  protected:
59  size_t n_inputs;
60  };
61 
62 
63 
64  template <typename KernelType> Kernel * create_func()
65  {
66  return new KernelType();
67  }
68 
77  {
78  public:
79  KernelFactory ();
80  virtual ~KernelFactory () {};
81 
82  Kernel* create(std::string name, size_t input_dim);
83 
84  private:
85  typedef Kernel* (*create_func_definition)();
86  std::map<std::string , KernelFactory::create_func_definition> registry;
87  };
88 
89 
91  {
92  public:
93  KernelModel(size_t dim, Parameters parameters);
94  virtual ~KernelModel() {};
95 
96  Kernel* getKernel();
97 
98  void setHyperParameters(const vectord &theta);
99  vectord getHyperParameters();
100  size_t nHyperParameters();
101 
102 
109  void setKernel (const vectord &thetav, const vectord &stheta,
110  std::string k_name, size_t dim);
111 
113  void setKernel (KernelParameters kernel, size_t dim);
114 
115  void computeCorrMatrix(const vecOfvec& XX, matrixd& corrMatrix, double nugget);
116  void computeDerivativeCorrMatrix(const vecOfvec& XX, matrixd& corrMatrix,
117  int dth_index);
118  vectord computeCrossCorrelation(const vecOfvec& XX, const vectord &query);
119  void computeCrossCorrelation(const vecOfvec& XX, const vectord &query,
120  vectord& knx);
121  double computeSelfCorrelation(const vectord& query);
122  double kernelLogPrior();
123 
124  private:
126  void setKernelPrior (const vectord &theta, const vectord &s_theta);
127 
128  boost::scoped_ptr<Kernel> mKernel;
129  std::vector<boost::math::normal> priorKernel;
130  };
131 
132  inline Kernel* KernelModel::getKernel()
133  { return mKernel.get(); }
134 
135  inline void KernelModel::setHyperParameters(const vectord &theta)
136  { mKernel->setHyperParameters(theta); };
137 
138  inline vectord KernelModel::getHyperParameters()
139  {return mKernel->getHyperParameters();};
140 
141  inline size_t KernelModel::nHyperParameters()
142  {return mKernel->nHyperParameters();};
143 
144  inline vectord KernelModel::computeCrossCorrelation(const vecOfvec& XX,
145  const vectord &query)
146  {
147  vectord knx(XX.size());
148  computeCrossCorrelation(XX,query,knx);
149  return knx;
150  }
151 
152  inline void KernelModel::computeCrossCorrelation(const vecOfvec& XX,
153  const vectord &query,
154  vectord& knx)
155  {
156  std::vector<vectord>::const_iterator x_it = XX.begin();
157  vectord::iterator k_it = knx.begin();
158  while(x_it != XX.end())
159  { *k_it++ = (*mKernel)(*x_it++, query); }
160  }
161 
162 
163  inline double KernelModel::computeSelfCorrelation(const vectord& query)
164  { return (*mKernel)(query,query); }
165 
166  inline void KernelModel::setKernelPrior (const vectord &theta,
167  const vectord &s_theta)
168  {
169  for (size_t i = 0; i<theta.size(); ++i)
170  {
171  boost::math::normal n(theta(i),s_theta(i));
172  priorKernel.push_back(n);
173  }
174  };
175 
176 
178 
179 } //namespace bayesopt
180 
181 
182 #endif
Boost vector and matrix types.
Namespace of the library interface.
Definition: using.dox:1
Factory model for kernel functions This factory is based on the libgp library by Manuel Blum https://...
void setKernelPrior(const vectord &theta, const vectord &s_theta)
Set prior (Gaussian) for kernel hyperparameters.
boost::scoped_ptr< Kernel > mKernel
Pointer to kernel function.
Interface for kernel functors.
std::vector< boost::math::normal > priorKernel
Prior of kernel parameters.
Parameter definitions.