https://mooseframework.inl.gov
MaternHalfIntCovariance.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 
13 registerMooseObject("StochasticToolsApp", MaternHalfIntCovariance);
14 
17 {
19  params.addClassDescription("Matern half-integer 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  params.addRequiredParam<unsigned int>(
27  "p", "Integer p to use for Matern Half Integer Covariance Kernel");
28  return params;
29 }
30 
32  : CovarianceFunctionBase(parameters),
33  _length_factor(addVectorRealHyperParameter(
34  "length_factor", getParam<std::vector<Real>>("length_factor"), true)),
35  _sigma_f_squared(
36  addRealHyperParameter("signal_variance", getParam<Real>("signal_variance"), true)),
37  _sigma_n_squared(
38  addRealHyperParameter("noise_variance", getParam<Real>("noise_variance"), true)),
39  _p(addRealHyperParameter("p", getParam<unsigned int>("p"), false))
40 {
41 }
42 
43 void
45  const RealEigenMatrix & x,
46  const RealEigenMatrix & xp,
47  const bool is_self_covariance) const
48 {
49  if ((unsigned)x.cols() != _length_factor.size())
50  mooseError("length_factor size does not match dimension of trainer input.");
51 
53  K, x, xp, _length_factor, _sigma_f_squared, _sigma_n_squared, _p, is_self_covariance);
54 }
55 
56 void
58  const RealEigenMatrix & x,
59  const RealEigenMatrix & xp,
60  const std::vector<Real> & length_factor,
61  const Real sigma_f_squared,
62  const Real sigma_n_squared,
63  const unsigned int p,
64  const bool is_self_covariance)
65 {
66  unsigned int num_samples_x = x.rows();
67  unsigned int num_samples_xp = xp.rows();
68  unsigned int num_params_x = x.cols();
69 
70  mooseAssert(num_params_x == xp.cols(),
71  "Number of parameters do not match in covariance kernel calculation");
72 
73  // This factor is used over and over, don't calculate each time
74  Real factor = sqrt(2 * p + 1);
75 
76  for (unsigned int ii = 0; ii < num_samples_x; ++ii)
77  {
78  for (unsigned int jj = 0; jj < num_samples_xp; ++jj)
79  {
80  // Compute distance per parameter, scaled by length factor
81  Real r_scaled = 0;
82  for (unsigned int kk = 0; kk < num_params_x; ++kk)
83  r_scaled += pow((x(ii, kk) - xp(jj, kk)) / length_factor[kk], 2);
84  r_scaled = sqrt(r_scaled);
85  // tgamma(x+1) == x! when x is a natural number, which should always be the case for
86  // MaternHalfInt
87  Real summation = 0;
88  for (unsigned int tt = 0; tt < p + 1; ++tt)
89  summation += (tgamma(p + tt + 1) / (tgamma(tt + 1) * tgamma(p - tt + 1))) *
90  pow(2 * factor * r_scaled, p - tt);
91  K(ii, jj) = sigma_f_squared * std::exp(-factor * r_scaled) *
92  (tgamma(p + 1) / (tgamma(2 * p + 1))) * summation;
93  }
94  if (is_self_covariance)
95  K(ii, ii) += sigma_n_squared;
96  }
97 }
98 
99 bool
101  const RealEigenMatrix & x,
102  const std::string & hyper_param_name,
103  unsigned int ind) const
104 {
105  if (name().length() + 1 > hyper_param_name.length())
106  return false;
107 
108  const std::string name_without_prefix = hyper_param_name.substr(name().length() + 1);
109 
110  if (name_without_prefix == "noise_variance")
111  {
112  maternHalfIntFunction(dKdhp, x, x, _length_factor, 0, 1, _p, true);
113  return true;
114  }
115 
116  if (name_without_prefix == "signal_variance")
117  {
118  maternHalfIntFunction(dKdhp, x, x, _length_factor, 1, 0, _p, false);
119  return true;
120  }
121 
122  if (name_without_prefix == "length_factor")
123  {
125  return true;
126  }
127 
128  return false;
129 }
130 
131 void
133  const RealEigenMatrix & x,
134  const std::vector<Real> & length_factor,
135  const Real sigma_f_squared,
136  const unsigned int p,
137  const int ind)
138 {
139  unsigned int num_samples_x = x.rows();
140  unsigned int num_params_x = x.cols();
141 
142  mooseAssert(ind < x.cols(), "Incorrect length factor index");
143 
144  // This factor is used over and over, don't calculate each time
145  Real factor = sqrt(2 * p + 1);
146 
147  for (unsigned int ii = 0; ii < num_samples_x; ++ii)
148  {
149  for (unsigned int jj = 0; jj < num_samples_x; ++jj)
150  {
151  // Compute distance per parameter, scaled by length factor
152  Real r_scaled = 0;
153  for (unsigned int kk = 0; kk < num_params_x; ++kk)
154  r_scaled += pow((x(ii, kk) - x(jj, kk)) / length_factor[kk], 2);
155  r_scaled = sqrt(r_scaled);
156  if (r_scaled != 0)
157  {
158  // product rule to compute dK/dr_scaled
159  // u'v
160  Real summation = 0;
161  for (unsigned int tt = 0; tt < p + 1; ++tt)
162  summation += (tgamma(p + tt + 1) / (tgamma(tt + 1) * tgamma(p - tt + 1))) *
163  pow(2 * factor * r_scaled, p - tt);
164  K(ii, jj) = -factor * std::exp(-factor * r_scaled) * summation;
165  // uv'
166  // dont need tt=p, (p-tt) factor ->0. Also avoids unsigned integer subtraction wraparound
167  summation = 0;
168  for (unsigned int tt = 0; tt < p; ++tt)
169  summation += (tgamma(p + tt + 1) / (tgamma(tt + 1) * tgamma(p - tt + 1))) * 2 * factor *
170  (p - tt) * pow(2 * factor * r_scaled, p - tt - 1);
171  K(ii, jj) += std::exp(-factor * r_scaled) * summation;
172  // Apply chain rule for dr_scaled/dl_i
173  K(ii, jj) *= -std::pow(x(ii, ind) - x(jj, ind), 2) /
174  (std::pow(length_factor[ind], 3) * r_scaled) * sigma_f_squared *
175  (tgamma(p + 1) / (tgamma(2 * p + 1)));
176  }
177  else // avoid div by 0. 0/0=0 scenario.
178  K(ii, jj) = 0;
179  }
180  }
181 }
const Real & _sigma_n_squared
noise variance (^2)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
static const std::string K
Definition: NS.h:170
Base class for covariance functions that are used in Gaussian Processes.
static InputParameters validParams()
const std::vector< Real > & _length_factor
lengh factor () for the kernel, in vector form for multiple parameters
bool computedKdhyper(RealEigenMatrix &dKdhp, const RealEigenMatrix &x, const std::string &hyper_param_name, unsigned int ind) const override
Redirect dK/dhp for hyperparameter "hp".
virtual const std::string & name() const
void addRequiredParam(const std::string &name, const std::string &doc_string)
unsigned int _p
non-negative p factor for use in Matern half-int. = p+(1/2) in terms of general Matern ...
registerMooseObject("StochasticToolsApp", MaternHalfIntCovariance)
static void computedKdlf(RealEigenMatrix &K, const RealEigenMatrix &x, const std::vector< Real > &length_factor, const Real sigma_f_squared, const unsigned int p, const int ind)
Computes dK/dlf for individual length factors.
const std::vector< double > x
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.
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealEigenMatrix
static void maternHalfIntFunction(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 unsigned int p, const bool is_self_covariance)
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
MaternHalfIntCovariance(const InputParameters &parameters)
const Real & _sigma_f_squared
signal variance (^2)
MooseUnits pow(const MooseUnits &, int)
void ErrorVector unsigned int