Kstars

covariance_functions.cpp
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: 2014-2017 Max Planck Society.
3  All rights reserved.
4 
5  SPDX-License-Identifier: BSD-3-Clause
6 */
7 
8 /**
9  * @file
10  * @date 2014-2017
11  * @copyright Max Planck Society
12  *
13  * @author Edgar D. Klenske <[email protected]>
14  * @author Stephan Wenninger <[email protected]>
15  * @author Raffi Enficiaud <[email protected]>
16  *
17  * @brief The file holds the covariance functions that can be used with the GP class.
18  */
19 
20 #include "covariance_functions.h"
21 #include "math_tools.h"
22 
23 namespace covariance_functions
24 {
25 
26  /* PeriodicSquareExponential */
27  PeriodicSquareExponential::PeriodicSquareExponential() :
28  hyperParameters(Eigen::VectorXd::Zero(4)), extraParameters(Eigen::VectorXd::Ones(1)*std::numeric_limits<double>::max()) { }
29 
30  PeriodicSquareExponential::PeriodicSquareExponential(const Eigen::VectorXd& hyperParameters_) :
31  hyperParameters(hyperParameters_), extraParameters(Eigen::VectorXd::Ones(1)*std::numeric_limits<double>::max()) { }
32 
33  Eigen::MatrixXd PeriodicSquareExponential::evaluate(const Eigen::VectorXd& x, const Eigen::VectorXd& y)
34  {
35 
36  double lsSE0 = exp(hyperParameters(0));
37  double svSE0 = exp(2 * hyperParameters(1));
38  double lsP = exp(hyperParameters(2));
39  double svP = exp(2 * hyperParameters(3));
40 
41  double plP = exp(extraParameters(0));
42 
43  // Work with arrays internally, convert to matrix for return value.
44  // This is because all the operations act elementwise, and Eigen::Arrays
45  // do so, too.
46 
47  // Compute Distances
48  Eigen::ArrayXXd squareDistanceXY = math_tools::squareDistance( x.transpose(), y.transpose());
49 
50  // fast version
51  return svSE0 * ((-0.5 / std::pow(lsSE0, 2)) * squareDistanceXY).exp()
52  + svP * (-2 * (((M_PI / plP) * squareDistanceXY.sqrt()).sin() / lsP).square()).exp();
53 
54  /* // verbose version
55  // Square Exponential Kernel
56  Eigen::ArrayXXd K0 = squareDistanceXY / std::pow(lsSE0, 2);
57  K0 = svSE0 * (-0.5 * K0).exp();
58 
59  // Periodic Kernel
60  Eigen::ArrayXXd K1 = (M_PI * squareDistanceXY.sqrt() / plP);
61  K1 = K1.sin() / lsP;
62  K1 = K1.square();
63  K1 = svP * (-2 * K1).exp();
64 
65  // Combined Kernel
66  return K0 + K1;
67  */
68  }
69 
70  void PeriodicSquareExponential::setParameters(const Eigen::VectorXd& params)
71  {
72  this->hyperParameters = params;
73  }
74 
75  void PeriodicSquareExponential::setExtraParameters(const Eigen::VectorXd& params)
76  {
77  this->extraParameters = params;
78  }
79 
80  const Eigen::VectorXd& PeriodicSquareExponential::getParameters() const
81  {
82  return this->hyperParameters;
83  }
84 
85  const Eigen::VectorXd& PeriodicSquareExponential::getExtraParameters() const
86  {
87  return this->extraParameters;
88  }
89 
90  int PeriodicSquareExponential::getParameterCount() const
91  {
92  return 4;
93  }
94 
95  int PeriodicSquareExponential::getExtraParameterCount() const
96  {
97  return 1;
98  }
99 
100 
101  /* PeriodicSquareExponential2 */
102  PeriodicSquareExponential2::PeriodicSquareExponential2() :
103  hyperParameters(Eigen::VectorXd::Zero(6)), extraParameters(Eigen::VectorXd::Ones(1)*std::numeric_limits<double>::max()) { }
104 
105  PeriodicSquareExponential2::PeriodicSquareExponential2(const Eigen::VectorXd& hyperParameters_) :
106  hyperParameters(hyperParameters_), extraParameters(Eigen::VectorXd::Ones(1)*std::numeric_limits<double>::max()) { }
107 
108  Eigen::MatrixXd PeriodicSquareExponential2::evaluate(const Eigen::VectorXd& x, const Eigen::VectorXd& y)
109  {
110 
111  double lsSE0 = exp(hyperParameters(0));
112  double svSE0 = exp(2 * hyperParameters(1));
113  double lsP = exp(hyperParameters(2));
114  double svP = exp(2 * hyperParameters(3));
115  double lsSE1 = exp(hyperParameters(4));
116  double svSE1 = exp(2 * hyperParameters(5));
117 
118  double plP = exp(extraParameters(0));
119 
120  // Work with arrays internally, convert to matrix for return value.
121  // This is because all the operations act elementwise, and Eigen::Arrays
122  // do so, too.
123 
124  // Compute Distances
125  Eigen::ArrayXXd squareDistanceXY = math_tools::squareDistance( x.transpose(), y.transpose());
126 
127  // fast version
128  return svSE0 * ((-0.5 / std::pow(lsSE0, 2)) * squareDistanceXY).exp()
129  + svP * (-2 * (((M_PI / plP) * squareDistanceXY.sqrt()).sin() / lsP).square()).exp()
130  + svSE1 * ((-0.5 / std::pow(lsSE1, 2)) * squareDistanceXY).exp();
131 
132  /* // verbose version
133  // Square Exponential Kernel
134  Eigen::ArrayXXd K0 = squareDistanceXY / pow(lsSE0, 2);
135  K0 = svSE0 * (-0.5 * K0).exp();
136 
137  // Periodic Kernel
138  Eigen::ArrayXXd K1 = (M_PI * squareDistanceXY.sqrt() / plP);
139  K1 = K1.sin() / lsP;
140  K1 = K1.square();
141  K1 = svP * (-2 * K1).exp();
142 
143  // Square Exponential Kernel
144  Eigen::ArrayXXd K2 = squareDistanceXY / pow(lsSE1, 2);
145  K2 = svSE1 * (-0.5 * K2).exp();
146 
147  // Combined Kernel
148  return K0 + K1 + K2;
149  */
150  }
151 
152  void PeriodicSquareExponential2::setParameters(const Eigen::VectorXd& params)
153  {
154  this->hyperParameters = params;
155  }
156 
157  void PeriodicSquareExponential2::setExtraParameters(const Eigen::VectorXd& params)
158  {
159  this->extraParameters = params;
160  }
161 
162  const Eigen::VectorXd& PeriodicSquareExponential2::getParameters() const
163  {
164  return this->hyperParameters;
165  }
166 
167  const Eigen::VectorXd& PeriodicSquareExponential2::getExtraParameters() const
168  {
169  return this->extraParameters;
170  }
171 
173  {
174  return 6;
175  }
176 
177  int PeriodicSquareExponential2::getExtraParameterCount() const
178  {
179  return 1;
180  }
181 
182 } // namespace covariance_functions
const Eigen::VectorXd & getParameters() const override
Returns the hyper-parameters.
The file holds the covariance functions that can be used with the GP class.
int getParameterCount() const override
Returns the number of hyper-parameters.
Eigen::MatrixXd evaluate(const Eigen::VectorXd &x1, const Eigen::VectorXd &x2) override
void setParameters(const Eigen::VectorXd &params) override
Method to set the hyper-parameters.
Provides mathematical tools needed for the Gaussian process toolbox.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.