https://mooseframework.inl.gov
AffineInvariantStretchSampler.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 
13 
16 {
18  params.addClassDescription("Perform Affine Invariant Ensemble MCMC with stretch sampler.");
20  "previous_state", "Reporter value with the previous state of all the walkers.");
22  "previous_state_var",
23  "Reporter value with the previous state of all the walkers for variance.");
24  params.addParam<Real>("step_size", 2.0, "Step size for each of the walkers.");
25  return params;
26 }
27 
29  : PMCMCBase(parameters),
30  _step_size(getParam<Real>("step_size")),
31  _previous_state(getReporterValue<std::vector<std::vector<Real>>>("previous_state")),
32  _previous_state_var(getReporterValue<std::vector<Real>>("previous_state_var"))
33 {
35  paramError("num_parallel_proposals",
36  "At least three parallel proposals should be used for the Stretch Sampler.");
37 
38  if (_num_parallel_proposals < _priors.size())
40  "It is recommended that the parallel proposals be greater than or equal to the "
41  "inferred parameters. This will allow the sampler to not get stuck on a hyper-plane.");
42 
43  // Assign the correct size to the step size vector
45 }
46 
47 void
48 AffineInvariantStretchSampler::proposeSamples(const unsigned int seed_value)
49 {
50  unsigned int j = 0;
51  bool indicator;
52  unsigned int index_req = 0;
53  while (j < _num_parallel_proposals)
54  {
55  indicator = 0;
56  _affine_step[j] = Utility::pow<2>((_step_size - 1.0) * getRand(seed_value) + 1.0) / _step_size;
57  randomIndex(_num_parallel_proposals, j, seed_value, index_req);
58  for (unsigned int i = 0; i < _priors.size(); ++i)
59  {
60  _new_samples[j][i] =
61  (_t_step > decisionStep())
62  ? (_previous_state[index_req][i] +
63  _affine_step[j] * (_previous_state[j][i] - _previous_state[index_req][i]))
64  : _priors[i]->quantile(getRand(seed_value));
65  if (_lower_bound)
66  indicator =
67  (_new_samples[j][i] < (*_lower_bound)[i] || _new_samples[j][i] > (*_upper_bound)[i])
68  ? 1
69  : indicator;
70  }
71  if (_var_prior)
72  {
74  (_t_step > decisionStep())
75  ? (_previous_state_var[index_req] +
77  : _var_prior->quantile(getRand(seed_value));
78  if (_new_var_samples[j] < 0.0)
79  indicator = 1;
80  }
81  if (!indicator)
82  ++j;
83  }
84 }
85 
86 const std::vector<Real> &
88 {
89  return _affine_step;
90 }
const unsigned int _num_parallel_proposals
Number of parallel proposals to be made and subApps to be executed.
Definition: PMCMCBase.h:106
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
const std::vector< Real > * _lower_bound
Lower bounds for making the next proposal.
Definition: PMCMCBase.h:115
virtual int decisionStep() const override
Return the step after which decision making can begin.
std::vector< Real > _affine_step
Vector of affine step sizes.
std::vector< const Distribution * > _priors
Storage for prior distribution objects to be utilized.
Definition: PMCMCBase.h:109
AffineInvariantStretchSampler(const InputParameters &parameters)
const std::vector< Real > & getAffineStepSize() const
Return the vector of step size for decision making.
Real getRand(unsigned int index=0)
void mooseWarning(Args &&... args) const
const std::vector< std::vector< Real > > & _previous_state
Reporter value with the previous state of all the walkers.
void addRequiredParam(const std::string &name, const std::string &doc_string)
registerMooseObject("StochasticToolsApp", AffineInvariantStretchSampler)
A class for performing Affine Invariant Ensemble MCMC with stretch sampler.
std::vector< Real > _new_var_samples
Vector of new proposed variance samples.
Definition: PMCMCBase.h:130
virtual void proposeSamples(const unsigned int seed_value) override
Fill in the _new_samples vector of vectors (happens within sampleSetUp)
const Real _step_size
The step size for the stretch sampler.
void paramError(const std::string &param, Args... args) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const std::vector< Real > & _previous_state_var
Reporter value with the previous state of all the walkers for variance.
std::vector< std::vector< Real > > _new_samples
Vectors of new proposed samples.
Definition: PMCMCBase.h:127
const Distribution * _var_prior
Storage for prior distribution object of the variance to be utilized.
Definition: PMCMCBase.h:112
void addClassDescription(const std::string &doc_string)
void randomIndex(const unsigned int &upper_bound, const unsigned int &exclude, const unsigned int &seed, unsigned int &req_index)
Sample a random index excluding a specified index.
Definition: PMCMCBase.C:141
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
virtual Real quantile(const Real &y) const=0
static InputParameters validParams()
Definition: PMCMCBase.C:18
A base class used to perform Parallel Markov Chain Monte Carlo (MCMC) sampling.
Definition: PMCMCBase.h:19