-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmatern_five_halves.hpp
143 lines (121 loc) · 5.51 KB
/
matern_five_halves.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//| Copyright Inria May 2015
//| This project has received funding from the European Research Council (ERC) under
//| the European Union's Horizon 2020 research and innovation programme (grant
//| agreement No 637972) - see http://www.resibots.eu
//|
//| Contributor(s):
//| - Jean-Baptiste Mouret ([email protected])
//| - Antoine Cully ([email protected])
//| - Konstantinos Chatzilygeroudis ([email protected])
//| - Federico Allocati ([email protected])
//| - Vaios Papaspyros ([email protected])
//| - Roberto Rama ([email protected])
//|
//| This software is a computer library whose purpose is to optimize continuous,
//| black-box functions. It mainly implements Gaussian processes and Bayesian
//| optimization.
//| Main repository: https://github.com/resibots/limbo
//| Documentation: http://www.resibots.eu/limbo
//|
//| This software is governed by the CeCILL-C license under French law and
//| abiding by the rules of distribution of free software. You can use,
//| modify and/ or redistribute the software under the terms of the CeCILL-C
//| license as circulated by CEA, CNRS and INRIA at the following URL
//| "http://www.cecill.info".
//|
//| As a counterpart to the access to the source code and rights to copy,
//| modify and redistribute granted by the license, users are provided only
//| with a limited warranty and the software's author, the holder of the
//| economic rights, and the successive licensors have only limited
//| liability.
//|
//| In this respect, the user's attention is drawn to the risks associated
//| with loading, using, modifying and/or developing or reproducing the
//| software by the user in light of its specific status of free software,
//| that may mean that it is complicated to manipulate, and that also
//| therefore means that it is reserved for developers and experienced
//| professionals having in-depth computer knowledge. Users are therefore
//| encouraged to load and test the software's suitability as regards their
//| requirements in conditions enabling the security of their systems and/or
//| data to be ensured and, more generally, to use and operate it in the
//| same conditions as regards security.
//|
//| The fact that you are presently reading this means that you have had
//| knowledge of the CeCILL-C license and that you accept its terms.
//|
#ifndef LIMBO_KERNEL_MATERN_FIVE_HALVES_HPP
#define LIMBO_KERNEL_MATERN_FIVE_HALVES_HPP
#include <limbo/kernel/kernel.hpp>
namespace limbo {
namespace defaults {
struct kernel_maternfivehalves {
/// @ingroup kernel_defaults
BO_PARAM(double, sigma_sq, 1);
/// @ingroup kernel_defaults
BO_PARAM(double, l, 1);
};
} // namespace defaults
namespace kernel {
/**
@ingroup kernel
\rst
Matern kernel
.. math::
d = ||v1 - v2||
\nu = 5/2
C(d) = \sigma^2\frac{2^{1-\nu}}{\Gamma(\nu)}\Bigg(\sqrt{2\nu}\frac{d}{l}\Bigg)^\nu K_\nu\Bigg(\sqrt{2\nu}\frac{d}{l}\Bigg),
Parameters:
- ``double sigma_sq`` (signal variance)
- ``double l`` (characteristic length scale)
Reference: :cite:`matern1960spatial` & :cite:`brochu2010tutorial` p.10 & https://en.wikipedia.org/wiki/Mat%C3%A9rn_covariance_function
\endrst
*/
template <typename Params>
struct MaternFiveHalves : public BaseKernel<Params, MaternFiveHalves<Params>> {
MaternFiveHalves(size_t dim = 1) : _sf2(Params::kernel_maternfivehalves::sigma_sq()), _l(Params::kernel_maternfivehalves::l())
{
_h_params = Eigen::VectorXd(2);
_h_params << std::log(_l), std::log(std::sqrt(_sf2));
}
size_t params_size() const { return 2; }
// Return the hyper parameters in log-space
Eigen::VectorXd params() const { return _h_params; }
// We expect the input parameters to be in log-space
void set_params(const Eigen::VectorXd& p)
{
_h_params = p;
_l = std::exp(p(0));
_sf2 = std::exp(2.0 * p(1));
}
double kernel(const Eigen::VectorXd& v1, const Eigen::VectorXd& v2) const
{
double d = (v1 - v2).norm();
double d_sq = d * d;
double l_sq = _l * _l;
double term1 = std::sqrt(5) * d / _l;
double term2 = 5. * d_sq / (3. * l_sq);
return _sf2 * (1 + term1 + term2) * std::exp(-term1);
}
Eigen::VectorXd gradient(const Eigen::VectorXd& x1, const Eigen::VectorXd& x2) const
{
Eigen::VectorXd grad(this->params_size());
double d = (x1 - x2).norm();
double d_sq = d * d;
double l_sq = _l * _l;
double term1 = std::sqrt(5) * d / _l;
double term2 = 5. * d_sq / (3. * l_sq);
double r = std::exp(-term1);
// derivative of term1 = -term1
// derivative of term2 = -2*term2
// derivative of e^(-term1) = term1*r
grad(0) = _sf2 * (r * term1 * (1 + term1 + term2) + (-term1 - 2. * term2) * r);
grad(1) = 2 * _sf2 * (1 + term1 + term2) * r;
return grad;
}
protected:
double _sf2, _l;
Eigen::VectorXd _h_params;
};
} // namespace kernel
} // namespace limbo
#endif