https://mooseframework.inl.gov
Gaussian.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 "Gaussian.h"
11 #include "Normal.h"
12 #include "DelimitedFileReader.h"
13 
14 registerMooseObject("StochasticToolsApp", Gaussian);
15 
18 {
20  params.addClassDescription(
21  "Gaussian likelihood function evaluating the model goodness against experiments.");
22  params.addParam<bool>("log_likelihood", true, "Compute log-likelihood or likelihood.");
24  "noise", "Experimental noise plus model deviations against experiments.");
25  params.addParam<FileName>("file_name", "Name of the CSV file with experimental values.");
26  params.addParam<std::string>(
27  "file_column_name", "Name of column in CSV file to use, by default first column is used.");
28  params.addParam<std::vector<Real>>(
29  "exp_values", "User-specified experimental values when CSV file is not provided.");
30  return params;
31 }
32 
34  : LikelihoodFunctionBase(parameters),
35  ReporterInterface(this),
36  _log_likelihood(getParam<bool>("log_likelihood")),
37  _noise(getReporterValue<Real>("noise"))
38 {
39  if (isParamValid("exp_values") && isParamValid("file_name"))
40  paramError("exp_values", "exp_values and file_name both cannot be set at the same time.");
41  else if (isParamValid("file_name"))
42  {
43  MooseUtils::DelimitedFileReader reader(getParam<FileName>("file_name"));
44  reader.read();
45  if (isParamValid("file_column_name"))
46  _exp_values = reader.getData(getParam<std::string>("file_column_name"));
47  else
48  _exp_values = reader.getData(0);
49  }
50  else if (isParamValid("exp_values"))
51  _exp_values = getParam<std::vector<Real>>("exp_values");
52  else
53  mooseError("Either 'exp_values' or 'file_name' parameters must be specified to represent "
54  "experimental data.");
55 }
56 
57 Real
58 Gaussian::function(const std::vector<Real> & exp,
59  const std::vector<Real> & model,
60  const Real & noise,
61  const bool & log_likelihood)
62 {
63  Real result = 0.0;
64  Real val1;
65  for (unsigned i = 0; i < exp.size(); ++i)
66  {
67  val1 = Normal::pdf(exp[i], model[i], noise);
68  val1 = std::log(val1);
69  result += val1;
70  }
71  if (!log_likelihood)
72  result = std::exp(result);
73  return result;
74 }
75 
76 Real
77 Gaussian::function(const std::vector<Real> & x) const
78 {
79  return function(_exp_values, x, _noise, _log_likelihood);
80 }
All Likelihoods should inherit from this class.
virtual Real function(const std::vector< Real > &x) const override
Return the probability density or mass function at vector x.
Definition: Gaussian.C:77
Gaussian(const InputParameters &parameters)
Definition: Gaussian.C:33
auto exp(const T &)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const Real & _noise
Noise value.
Definition: Gaussian.h:44
A class used to generate a Gaussian likelihood of observing model predictions.
Definition: Gaussian.h:18
void addRequiredParam(const std::string &name, const std::string &doc_string)
virtual Real pdf(const Real &x) const override
Definition: Normal.C:68
bool isParamValid(const std::string &name) const
const std::vector< double > x
static InputParameters validParams()
Definition: Gaussian.C:17
const bool _log_likelihood
return log-likelihood or likelihood
Definition: Gaussian.h:41
std::vector< Real > _exp_values
Experimental data values.
Definition: Gaussian.h:47
void paramError(const std::string &param, Args... args) const
const std::vector< std::vector< T > > & getData() const
const PertinentGeochemicalSystem model(database, {"H2O", "H+", "HCO3-", "O2(aq)", "Ca++", ">(s)FeOH", "radius_neg1", "radius_neg1.5"}, {"Calcite"}, {}, {"Calcite_asdf"}, {"CH4(aq)"}, {">(s)FeOCa+"}, "O2(aq)", "e-")
registerMooseObject("StochasticToolsApp", Gaussian)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static InputParameters validParams()
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)