30 #include "kernels/kernel_const.hpp" 31 #include "kernels/kernel_linear.hpp" 32 #include "kernels/kernel_hamming.hpp" 33 #include "kernels/kernel_matern.hpp" 34 #include "kernels/kernel_polynomial.hpp" 35 #include "kernels/kernel_gaussian.hpp" 36 #include "kernels/kernel_rq.hpp" 39 #include "kernels/kernel_sum.hpp" 40 #include "kernels/kernel_prod.hpp" 45 KernelFactory::KernelFactory()
47 registry[
"kConst"] = & create_func<ConstKernel>;
48 registry[
"kLinear"] = & create_func<LinKernel>;
49 registry[
"kLinearARD"] = & create_func<LinKernelARD>;
51 registry[
"kHamming"] = & create_func<HammingKernel>;
53 registry[
"kMaternISO1"] = & create_func<MaternIso1>;
54 registry[
"kMaternISO3"] = & create_func<MaternIso3>;
55 registry[
"kMaternISO5"] = & create_func<MaternIso5>;
56 registry[
"kMaternARD1"] = & create_func<MaternARD1>;
57 registry[
"kMaternARD3"] = & create_func<MaternARD3>;
58 registry[
"kMaternARD5"] = & create_func<MaternARD5>;
60 registry[
"kPoly1"] = & create_func<Polynomial>;
61 registry[
"kPoly2"] = & create_func<Polynomial2>;
62 registry[
"kPoly3"] = & create_func<Polynomial3>;
63 registry[
"kPoly4"] = & create_func<Polynomial4>;
64 registry[
"kPoly5"] = & create_func<Polynomial5>;
65 registry[
"kPoly6"] = & create_func<Polynomial6>;
67 registry[
"kSEARD"] = & create_func<SEArd>;
68 registry[
"kSEISO"] = & create_func<SEIso>;
70 registry[
"kRQISO"] = & create_func<RQIso>;
72 registry[
"kSum"] = & create_func<KernelSum>;
73 registry[
"kProd"] = & create_func<KernelProd>;
90 std::string os, os1, os2;
93 std::map<std::string,KernelFactory::create_func_definition>::iterator it = registry.find(os);
94 if (it == registry.end())
96 throw std::invalid_argument(
"Error while parsing kernel function: " 97 "Kernel not found " + os);
100 kFunc = it->second();
101 if (os1.length() == 0 && os2.length() == 0)
103 kFunc->init(input_dim);
107 kFunc->init(input_dim,
create(os1,input_dim),
create(os2,input_dim));
115 KernelModel::KernelModel(
size_t dim,
Parameters parameters)
116 { setKernel(parameters.
kernel,dim); }
119 const vectord &stheta,
125 mKernel.reset(mKFactory.
create(k_name, dim));
127 if ((thetav.size() == 1) && (stheta.size() == 1) && (mKernel->nHyperParameters() != 1))
130 size_t n = mKernel->nHyperParameters();
132 FILE_LOG(logINFO) <<
"Expected " << n <<
" hyperparameters." 133 <<
" Replicating parameters and prior.";
135 vectord newthetav = svectord(n,thetav(0));
136 vectord newstheta = svectord(n,stheta(0));
138 setKernelPrior(newthetav,newstheta);
139 mKernel->setHyperParameters(newthetav);
143 setKernelPrior(thetav,stheta);
144 mKernel->setHyperParameters(thetav);
151 size_t n = kernel.
hp_mean.size();
153 vectord sth = kernel.
hp_std;
154 setKernel(th, sth, kernel.
name, dim);
158 void KernelModel::computeCorrMatrix(
const vecOfvec& XX, matrixd& corrMatrix,
161 assert(corrMatrix.size1() == XX.size());
162 assert(corrMatrix.size2() == XX.size());
163 const size_t nSamples = XX.size();
165 for (
size_t ii=0; ii< nSamples; ++ii)
167 for (
size_t jj=0; jj < ii; ++jj)
169 corrMatrix(ii,jj) = (*mKernel)(XX[ii], XX[jj]);
170 corrMatrix(jj,ii) = corrMatrix(ii,jj);
172 corrMatrix(ii,ii) = (*mKernel)(XX[ii],XX[ii]) + nugget;
176 void KernelModel::computeDerivativeCorrMatrix(
const vecOfvec& XX,
180 assert(corrMatrix.size1() == XX.size());
181 assert(corrMatrix.size2() == XX.size());
182 const size_t nSamples = XX.size();
184 for (
size_t ii=0; ii< nSamples; ++ii)
186 for (
size_t jj=0; jj < ii; ++jj)
188 corrMatrix(ii,jj) = mKernel->gradient(XX[ii],XX[jj],
190 corrMatrix(jj,ii) = corrMatrix(ii,jj);
192 corrMatrix(ii,ii) = mKernel->gradient(XX[ii],XX[ii],dth_index);
197 double KernelModel::kernelLogPrior()
200 vectord th = mKernel->getHyperParameters();
201 for(
size_t i = 0; i<th.size();++i)
203 if (priorKernel[i].standard_deviation() > 0)
205 prior += std::log(boost::math::pdf(priorKernel[i],th(i)));
KernelParameters kernel
Kernel parameters.
Kernel functions that combine other kernels.
Kernel (covariance) functions.
void setKernel(const vectord &thetav, const vectord &stheta, std::string k_name, size_t dim)
Select kernel (covariance function) for the surrogate process.
vectord hp_mean
Kernel hyperparameters prior (mean, log space)
Namespace of the library interface.
Factory model for kernel functions This factory is based on the libgp library by Manuel Blum https://...
Functions to parse strings.
vectord hp_std
Kernel hyperparameters prior (st dev, log space)
Interface for kernel functors.
void parseExpresion(std::string input, std::string &parent, std::string &child1, std::string &child2)
Parse expresions of the form Parent(Child1, Child2).
Kernel * create(std::string name, size_t input_dim)
Factory model for kernel functions This function is based on the libgp library by Manuel Blum https:/...
Modules and helper macros for logging.
std::string name
Name of the kernel function.
Atomic (simple) kernel functions.