https://mooseframework.inl.gov
SquaredExponentialCovariance.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
11 #include <cmath>
12 
14 
17 {
19  params.addClassDescription("Squared Exponential covariance function.");
20  params.addRequiredParam<std::vector<Real>>("length_factor",
21  "Length factors to use for Covariance Kernel");
22  params.addRequiredParam<Real>("signal_variance",
23  "Signal Variance ($\\sigma_f^2$) to use for kernel calculation.");
24  params.addParam<Real>(
25  "noise_variance", 0.0, "Noise Variance ($\\sigma_n^2$) to use for kernel calculation.");
26  return params;
27 }
28 
30  : CovarianceFunctionBase(parameters),
31  _length_factor(addVectorRealHyperParameter(
32  "length_factor", getParam<std::vector<Real>>("length_factor"), true)),
33  _sigma_f_squared(
34  addRealHyperParameter("signal_variance", getParam<Real>("signal_variance"), true)),
35  _sigma_n_squared(
36  addRealHyperParameter("noise_variance", getParam<Real>("noise_variance"), true))
37 {
38 }
39 
40 void
42  const RealEigenMatrix & x,
43  const RealEigenMatrix & xp,
44  const bool is_self_covariance) const
45 {
46  if ((unsigned)x.cols() != _length_factor.size())
47  mooseError("length_factor size does not match dimension of trainer input.");
48 
50  K, x, xp, _length_factor, _sigma_f_squared, _sigma_n_squared, is_self_covariance);
51 }
52 
53 void
55  const RealEigenMatrix & x,
56  const RealEigenMatrix & xp,
57  const std::vector<Real> & length_factor,
58  const Real sigma_f_squared,
59  const Real sigma_n_squared,
60  const bool is_self_covariance)
61 {
62  unsigned int num_samples_x = x.rows();
63  unsigned int num_samples_xp = xp.rows();
64  unsigned int num_params_x = x.cols();
65 
66  mooseAssert(num_params_x == xp.cols(),
67  "Number of parameters do not match in covariance kernel calculation");
68 
69  for (unsigned int ii = 0; ii < num_samples_x; ++ii)
70  {
71  for (unsigned int jj = 0; jj < num_samples_xp; ++jj)
72  {
73  // Compute distance per parameter, scaled by length factor
74  Real r_squared_scaled = 0;
75  for (unsigned int kk = 0; kk < num_params_x; ++kk)
76  r_squared_scaled += std::pow((x(ii, kk) - xp(jj, kk)) / length_factor[kk], 2);
77  K(ii, jj) = sigma_f_squared * std::exp(-r_squared_scaled / 2.0);
78  }
79  if (is_self_covariance)
80  K(ii, ii) += sigma_n_squared;
81  }
82 }
83 
84 bool
86  const RealEigenMatrix & x,
87  const std::string & hyper_param_name,
88  unsigned int ind) const
89 {
90  if (name().length() + 1 > hyper_param_name.length())
91  return false;
92 
93  const std::string name_without_prefix = hyper_param_name.substr(name().length() + 1);
94 
95  if (name_without_prefix == "noise_variance")
96  {
97  SquaredExponentialFunction(dKdhp, x, x, _length_factor, 0, 1, true);
98  return true;
99  }
100 
101  if (name_without_prefix == "signal_variance")
102  {
103  SquaredExponentialFunction(dKdhp, x, x, _length_factor, 1, 0, false);
104  return true;
105  }
106 
107  if (name_without_prefix == "length_factor")
108  {
110  return true;
111  }
112 
113  return false;
114 }
115 
116 void
118  const RealEigenMatrix & x,
119  const std::vector<Real> & length_factor,
120  const Real sigma_f_squared,
121  const int ind)
122 {
123  unsigned int num_samples_x = x.rows();
124  unsigned int num_params_x = x.cols();
125 
126  mooseAssert(ind < x.cols(), "Incorrect length factor index");
127 
128  for (unsigned int ii = 0; ii < num_samples_x; ++ii)
129  {
130  for (unsigned int jj = 0; jj < num_samples_x; ++jj)
131  {
132  // Compute distance per parameter, scaled by length factor
133  Real r_squared_scaled = 0;
134  for (unsigned int kk = 0; kk < num_params_x; ++kk)
135  r_squared_scaled += std::pow((x(ii, kk) - x(jj, kk)) / length_factor[kk], 2);
136  K(ii, jj) = sigma_f_squared * std::exp(-r_squared_scaled / 2.0);
137  K(ii, jj) =
138  std::pow(x(ii, ind) - x(jj, ind), 2) / std::pow(length_factor[ind], 3) * K(ii, jj);
139  }
140  }
141 }
const std::vector< Real > & _length_factor
lengh factor () for the kernel, in vector form for multiple parameters
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
SquaredExponentialCovariance(const InputParameters &parameters)
static const std::string K
Definition: NS.h:170
static void computedKdlf(RealEigenMatrix &K, const RealEigenMatrix &x, const std::vector< Real > &length_factor, const Real sigma_f_squared, const int ind)
Computes dK/dlf for individual length factors.
Base class for covariance functions that are used in Gaussian Processes.
static InputParameters validParams()
void computeCovarianceMatrix(RealEigenMatrix &K, const RealEigenMatrix &x, const RealEigenMatrix &xp, const bool is_self_covariance) const override
Generates the Covariance Matrix given two points in the parameter space.
virtual const std::string & name() const
registerMooseObject("StochasticToolsApp", SquaredExponentialCovariance)
void addRequiredParam(const std::string &name, const std::string &doc_string)
const Real & _sigma_f_squared
signal variance (^2)
const std::vector< double > x
const Real & _sigma_n_squared
noise variance (^2)
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealEigenMatrix
bool computedKdhyper(RealEigenMatrix &dKdhp, const RealEigenMatrix &x, const std::string &hyper_param_name, unsigned int ind) const override
Redirect dK/dhp for hyperparameter "hp".
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
static void SquaredExponentialFunction(RealEigenMatrix &K, const RealEigenMatrix &x, const RealEigenMatrix &xp, const std::vector< Real > &length_factor, const Real sigma_f_squared, const Real sigma_n_squared, const bool is_self_covariance)
MooseUnits pow(const MooseUnits &, int)