https://mooseframework.inl.gov
ExponentialCovariance.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 
10 #include "ExponentialCovariance.h"
11 #include <cmath>
12 
13 registerMooseObject("StochasticToolsApp", ExponentialCovariance);
14 
17 {
19  params.addClassDescription("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  params.addRequiredParam<Real>("gamma", "Gamma to use for Exponential Covariance Kernel");
27  return params;
28 }
29 
31  : CovarianceFunctionBase(parameters),
32  _length_factor(addVectorRealHyperParameter(
33  "length_factor", getParam<std::vector<Real>>("length_factor"), true)),
34  _sigma_f_squared(
35  addRealHyperParameter("signal_variance", getParam<Real>("signal_variance"), true)),
36  _sigma_n_squared(
37  addRealHyperParameter("noise_variance", getParam<Real>("noise_variance"), true)),
38  _gamma(addRealHyperParameter("gamma", getParam<Real>("gamma"), false))
39 {
40 }
41 
42 void
44  const RealEigenMatrix & x,
45  const RealEigenMatrix & xp,
46  const bool is_self_covariance) const
47 {
48  if ((unsigned)x.cols() != _length_factor.size())
49  mooseError("length_factor size does not match dimension of trainer input.");
50 
52  K, x, xp, _length_factor, _sigma_f_squared, _sigma_n_squared, _gamma, is_self_covariance);
53 }
54 
55 void
57  const RealEigenMatrix & x,
58  const RealEigenMatrix & xp,
59  const std::vector<Real> & length_factor,
60  const Real sigma_f_squared,
61  const Real sigma_n_squared,
62  const Real gamma,
63  const bool is_self_covariance)
64 {
65  unsigned int num_samples_x = x.rows();
66  unsigned int num_samples_xp = xp.rows();
67  unsigned int num_params_x = x.cols();
68 
69  mooseAssert(num_params_x == xp.cols(),
70  "Number of parameters do not match in covariance kernel calculation");
71 
72  for (unsigned int ii = 0; ii < num_samples_x; ++ii)
73  {
74  for (unsigned int jj = 0; jj < num_samples_xp; ++jj)
75  {
76  // Compute distance per parameter, scaled by length factor
77  Real r_scaled = 0;
78  for (unsigned int kk = 0; kk < num_params_x; ++kk)
79  r_scaled += pow((x(ii, kk) - xp(jj, kk)) / length_factor[kk], 2);
80  r_scaled = sqrt(r_scaled);
81  K(ii, jj) = sigma_f_squared * std::exp(-pow(r_scaled, gamma));
82  }
83  if (is_self_covariance)
84  K(ii, ii) += sigma_n_squared;
85  }
86 }
87 
88 bool
90  const RealEigenMatrix & x,
91  const std::string & hyper_param_name,
92  unsigned int ind) const
93 {
94  if (name().length() + 1 > hyper_param_name.length())
95  return false;
96 
97  const std::string name_without_prefix = hyper_param_name.substr(name().length() + 1);
98 
99  if (name_without_prefix == "noise_variance")
100  {
101  ExponentialFunction(dKdhp, x, x, _length_factor, 0, 1, _gamma, true);
102  return true;
103  }
104 
105  if (name_without_prefix == "signal_variance")
106  {
107  ExponentialFunction(dKdhp, x, x, _length_factor, 1, 0, _gamma, false);
108  return true;
109  }
110 
111  if (name_without_prefix == "length_factor")
112  {
114  return true;
115  }
116 
117  return false;
118 }
119 
120 void
122  const RealEigenMatrix & x,
123  const std::vector<Real> & length_factor,
124  const Real sigma_f_squared,
125  const Real gamma,
126  const int ind)
127 {
128  unsigned int num_samples_x = x.rows();
129  unsigned int num_params_x = x.cols();
130 
131  mooseAssert(ind < x.cols(), "Incorrect length factor index");
132 
133  for (unsigned int ii = 0; ii < num_samples_x; ++ii)
134  {
135  for (unsigned int jj = 0; jj < num_samples_x; ++jj)
136  {
137  // Compute distance per parameter, scaled by length factor
138  Real r_scaled = 0;
139  for (unsigned int kk = 0; kk < num_params_x; ++kk)
140  r_scaled += pow((x(ii, kk) - x(jj, kk)) / length_factor[kk], 2);
141  r_scaled = sqrt(r_scaled);
142  if (r_scaled != 0)
143  {
144  K(ii, jj) = gamma * std::pow(r_scaled, gamma - 2) * sigma_f_squared *
145  std::exp(-pow(r_scaled, gamma));
146  K(ii, jj) =
147  std::pow(x(ii, ind) - x(jj, ind), 2) / std::pow(length_factor[ind], 3) * K(ii, jj);
148  }
149  else // avoid div by 0. 0/0=0 scenario.
150  K(ii, jj) = 0;
151  }
152  }
153 }
const Real & _gamma
gamma exponential factor for use in kernel
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)
static const std::string K
Definition: NS.h:170
Base class for covariance functions that are used in Gaussian Processes.
static InputParameters validParams()
static InputParameters validParams()
virtual const std::string & name() const
void addRequiredParam(const std::string &name, const std::string &doc_string)
const std::vector< double > x
ExponentialCovariance(const InputParameters &parameters)
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.
static void ExponentialFunction(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 Real gamma, const bool is_self_covariance)
static void computedKdlf(RealEigenMatrix &K, const RealEigenMatrix &x, const std::vector< Real > &length_factor, const Real sigma_f_squared, const Real gamma, const int ind)
Computes dK/dlf for individual length factors.
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic > RealEigenMatrix
ExpressionBuilder::EBTerm pow(const ExpressionBuilder::EBTerm &left, T exponent)
const Real & _sigma_n_squared
noise variance (^2)
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
registerMooseObject("StochasticToolsApp", ExponentialCovariance)
void mooseError(Args &&... args) const
bool computedKdhyper(RealEigenMatrix &dKdhp, const RealEigenMatrix &x, const std::string &hyper_param_name, unsigned int ind) const override
Redirect dK/dhp for hyperparameter "hp".
void addClassDescription(const std::string &doc_string)
const Real & _sigma_f_squared
signal variance (^2)
MooseUnits pow(const MooseUnits &, int)