https://mooseframework.inl.gov
IndependentMHDecision.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 "IndependentMHDecision.h"
11 
12 registerMooseObject("StochasticToolsApp", IndependentMHDecision);
13 
16 {
18  params.addClassDescription("Perform decision making for independent Metropolis-Hastings MCMC.");
19  params.addParam<ReporterValueName>(
20  "seed_input", "seed_input", "The seed vector input for proposing new samples.");
21  return params;
22 }
23 
25  : PMCMCDecision(parameters),
26  _igmh(dynamic_cast<const IndependentGaussianMH *>(&_sampler)),
27  _seed_input(declareValue<std::vector<Real>>("seed_input"))
28 {
29  // Check whether the selected sampler is a M-H sampler or not
30  if (!_igmh)
31  paramError("sampler", "The selected sampler is not of type IndependentGaussianMH.");
32 
34  _tpm_modified.assign(_props + 1, 1.0 / (_props + 1));
35 }
36 
37 void
38 IndependentMHDecision::computeEvidence(std::vector<Real> & evidence,
39  const DenseMatrix<Real> & input_matrix)
40 {
41  std::vector<Real> out(_num_confg_values);
42  for (unsigned int i = 0; i < evidence.size(); ++i)
43  {
44  evidence[i] = 0.0;
45  for (unsigned int j = 0; j < _priors.size(); ++j)
46  evidence[i] += (std::log(_priors[j]->pdf(input_matrix(i, j))) -
47  std::log(_priors[j]->pdf(_seed_input[j])));
48  for (unsigned int j = 0; j < _num_confg_values; ++j)
49  out[j] = _outputs_required[j * _props + i];
50  for (unsigned int j = 0; j < _likelihoods.size(); ++j)
51  evidence[i] += (_likelihoods[j]->function(out) - _likelihoods[j]->function(_seed_outputs));
52  }
53 }
54 
55 void
57  const std::vector<Real> & evidence)
58 {
59  for (unsigned int i = 0; i < tv.size(); ++i)
60  tv[i] = (1.0 / tv.size()) * std::exp(std::min(evidence[i], 0.0));
61  _tpm_modified = tv;
62  _tpm_modified.push_back((1.0 - std::accumulate(tv.begin(), tv.end(), 0.0)));
64 }
65 
66 void
67 IndependentMHDecision::nextSamples(std::vector<Real> & req_inputs,
68  DenseMatrix<Real> & input_matrix,
69  const std::vector<Real> & /*tv*/,
70  const unsigned int & parallel_index)
71 {
72  const bool value = (_tpm_modified[0] == 1.0 / (_props + 1));
73  if (!value)
74  {
75  unsigned int index =
77  if (index < _props)
78  {
79  for (unsigned int k = 0; k < _sampler.getNumberOfCols() - _num_confg_params; ++k)
80  req_inputs[k] = input_matrix(index, k);
81  for (unsigned int k = 0; k < _num_confg_values; ++k)
82  _outputs_required[k * _props + parallel_index] = _outputs_sto[k * _props + index];
83  }
84  else
85  {
86  req_inputs = _seed_input;
87  for (unsigned int k = 0; k < _num_confg_values; ++k)
88  _outputs_required[k * _props + parallel_index] = _seed_outputs[k];
89  }
90  }
91  else
92  {
93  for (unsigned int k = 0; k < _sampler.getNumberOfCols() - _num_confg_params; ++k)
94  req_inputs[k] = input_matrix(parallel_index, k);
95  _variance[parallel_index] = _new_var_samples[parallel_index];
96  }
97 }
98 
99 void
101 {
102  _seed_input = _inputs[_props - 1];
103  for (unsigned int k = 0; k < _num_confg_values; ++k)
104  _seed_outputs[k] = _outputs_required[(k + 1) * _props - 1];
105 }
dof_id_type _num_confg_values
Storage for the number of experimental configuration values.
PMCMCDecision will help making sample accept/reject decisions in MCMC schemes (for e...
Definition: PMCMCDecision.h:22
virtual void computeEvidence(std::vector< Real > &evidence, const DenseMatrix< Real > &input_matrix) override
Compute the evidence (aka, betterness of the proposed sample vs the previous)
const IndependentGaussianMH *const _igmh
IndependentGaussianMH sampler.
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
unsigned int weightedResample(const std::vector< Real > &weights, Real rnd)
return a resampled vector from a population given a weight vector.
virtual void nextSeeds() override
Compute the next set of seeds to facilitate proposals.
std::vector< Real > _tpm_modified
Modified transition vector considering the seed input.
registerMooseObject("StochasticToolsApp", IndependentMHDecision)
dof_id_type _num_confg_params
Storage for the number of experimental configuration parameters.
virtual void nextSamples(std::vector< Real > &req_inputs, DenseMatrix< Real > &input_matrix, const std::vector< Real > &tv, const unsigned int &parallel_index) override
Resample inputs given the transition vector (after transition vector computed)
std::vector< Real > & _variance
Model variance term.
Definition: PMCMCDecision.h:78
std::vector< Real > & _seed_input
Seed vector input for proposing new samples.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void paramError(const std::string &param, Args... args) const
const std::vector< const Distribution * > _priors
Storage for the priors.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IndependentMHDecision(const InputParameters &parameters)
dof_id_type _props
Storage for the number of parallel proposals.
Definition: PMCMCDecision.h:93
OStreamProxy out
std::vector< Real > _outputs_sto
Store the gathered outputs.
static InputParameters validParams()
Definition: PMCMCDecision.C:17
void addClassDescription(const std::string &doc_string)
virtual void computeTransitionVector(std::vector< Real > &tv, const std::vector< Real > &evidence) override
Compute the transition probability vector (after the computation of evidence)
std::vector< const LikelihoodFunctionBase * > _likelihoods
Storage for the likelihood objects to be utilized.
Definition: PMCMCDecision.h:84
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
Sampler & _sampler
The MCMC sampler.
Definition: PMCMCDecision.h:87
A class for performing independent Metropolis-Hastings MCMC decision making.
const std::vector< Real > & _new_var_samples
Storage for new proposed variance samples.
Definition: PMCMCDecision.h:99
const std::vector< Real > & _rnd_vec
Storage for the random numbers for decision making.
Definition: PMCMCDecision.h:96
std::vector< Real > & _outputs_required
Transfer the right outputs to the file.
Definition: PMCMCDecision.h:69
A class for performing M-H MCMC sampling with independent Gaussian propoposals.
static const std::string k
Definition: NS.h:130
std::vector< std::vector< Real > > & _inputs
Model input data that is uncertain.
Definition: PMCMCDecision.h:72
dof_id_type getNumberOfCols() const
static InputParameters validParams()
std::vector< Real > _seed_outputs
Outputs corresponding to the seed input vector.