https://mooseframework.inl.gov
AffineInvariantDES.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 "AffineInvariantDES.h"
11 #include "Normal.h"
12 #include "Uniform.h"
13 
14 registerMooseObject("StochasticToolsApp", AffineInvariantDES);
15 
16 /*
17  Tuning options for the internal parameters
18  1. Braak2006_static:
19  - the gamma param is set to 2.38 / sqrt(2 * dim)
20  - the b param is set to (scale) * 1e-6
21 */
22 
25 {
27  params.addClassDescription("Perform Affine Invariant Ensemble MCMC with differential sampler.");
29  "previous_state", "Reporter value with the previous state of all the walkers.");
31  "previous_state_var",
32  "Reporter value with the previous state of all the walkers for variance.");
33  MooseEnum tuning_option("Braak2006_static", "Braak2006_static");
34  params.addParam<MooseEnum>(
35  "tuning_option", tuning_option, "The tuning option for internal parameters.");
36  params.addParam<std::vector<Real>>("scales", "Scales for the parameters.");
37  return params;
38 }
39 
41  : PMCMCBase(parameters),
42  _previous_state(getReporterValue<std::vector<std::vector<Real>>>("previous_state")),
43  _previous_state_var(getReporterValue<std::vector<Real>>("previous_state_var")),
44  _tuning_option(getParam<MooseEnum>("tuning_option"))
45 {
47  paramError(
48  "num_parallel_proposals",
49  "At least five parallel proposals should be used for the Differential Evolution Sampler.");
50 
51  if (_num_parallel_proposals < _priors.size())
53  "It is recommended that the parallel proposals be greater than or equal to the "
54  "inferred parameters. This will allow the sampler to not get stuck on a hyper-plane.");
55 
56  if (isParamValid("scales"))
57  {
58  _scales = getParam<std::vector<Real>>("scales");
59  if (_scales.size() != _priors.size())
60  paramError("scales",
61  "The number of scales provided should match the number of tunable params.");
62  }
63  else
64  _scales.assign(_priors.size(), 1.0);
65 }
66 
67 void
69  const Real & state1, const Real & state2, const Real & rnd, const Real & scale, Real & diff)
70 {
71  Real gamma;
72  Real b;
73  tuneParams(gamma, b, scale);
74  diff = gamma * (state1 - state2) + Normal::quantile(rnd, 0.0, b);
75 }
76 
77 void
78 AffineInvariantDES::tuneParams(Real & gamma, Real & b, const Real & scale)
79 {
80  if (_tuning_option == "Braak2006_static")
81  {
82  gamma = 2.38 / std::sqrt(2 * _priors.size());
83  b = 1e-6 * scale;
84  }
85 }
86 
87 void
88 AffineInvariantDES::proposeSamples(const unsigned int seed_value)
89 {
90  unsigned int j = 0;
91  bool indicator;
92  unsigned int index_req1, index_req2;
93  Real diff;
94  while (j < _num_parallel_proposals)
95  {
96  indicator = 0;
97  randomIndexPair(_num_parallel_proposals, j, seed_value, index_req1, index_req2);
98  for (unsigned int i = 0; i < _priors.size(); ++i)
99  {
100  computeDifferential(_previous_state[index_req1][i],
101  _previous_state[index_req2][i],
102  getRand(seed_value),
103  _scales[i],
104  diff);
105  _new_samples[j][i] = (_t_step > decisionStep()) ? (_previous_state[j][i] + diff)
106  : _priors[i]->quantile(getRand(seed_value));
107  if (_lower_bound)
108  indicator =
109  (_new_samples[j][i] < (*_lower_bound)[i] || _new_samples[j][i] > (*_upper_bound)[i])
110  ? 1
111  : indicator;
112  }
113  if (_var_prior)
114  {
116  _previous_state_var[index_req2],
117  getRand(seed_value),
118  1.0,
119  diff);
121  : _var_prior->quantile(getRand(seed_value));
122  if (_new_var_samples[j] < 0.0)
123  indicator = 1;
124  }
125  if (!indicator)
126  ++j;
127  }
128 }
const MooseEnum & _tuning_option
Tuning options for the internal params.
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)
virtual int decisionStep() const override
Return the step after which decision making can begin.
void scale(MeshBase &mesh, const Real xs, const Real ys=0., const Real zs=0.)
const std::vector< Real > * _lower_bound
Lower bounds for making the next proposal.
Definition: PMCMCBase.h:115
const std::vector< std::vector< Real > > & _previous_state
Reporter value with the previous state of all the walkers.
std::vector< const Distribution * > _priors
Storage for prior distribution objects to be utilized.
Definition: PMCMCBase.h:109
static InputParameters validParams()
Real getRand(unsigned int index=0)
void mooseWarning(Args &&... args) const
void addRequiredParam(const std::string &name, const std::string &doc_string)
bool isParamValid(const std::string &name) const
virtual void proposeSamples(const unsigned int seed_value) override
Fill in the _new_samples vector of vectors (happens within sampleSetUp)
std::vector< Real > _new_var_samples
Vector of new proposed variance samples.
Definition: PMCMCBase.h:130
const std::vector< Real > & _previous_state_var
Reporter value with the previous state of all the walkers for variance.
void paramError(const std::string &param, Args... args) const
void randomIndexPair(const unsigned int &upper_bound, const unsigned int &exclude, const unsigned int &seed, unsigned int &req_index1, unsigned int &req_index2)
Sample two random indices without repitition excluding a specified index.
Definition: PMCMCBase.C:152
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
A class for performing Affine Invariant Ensemble MCMC with differential sampler.
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 tuneParams(Real &gamma, Real &b, const Real &scale)
Tune the internal parameters.
AffineInvariantDES(const InputParameters &parameters)
void addClassDescription(const std::string &doc_string)
registerMooseObject("StochasticToolsApp", AffineInvariantDES)
static const std::complex< double > j(0, 1)
Complex number "j" (also known as "i")
virtual Real quantile(const Real &y) const=0
virtual Real quantile(const Real &p) const override
Definition: Normal.C:80
std::vector< Real > _scales
Scales for the parameters.
static InputParameters validParams()
Definition: PMCMCBase.C:18
void computeDifferential(const Real &state1, const Real &state2, const Real &rnd, const Real &scale, Real &diff)
Compute the differential evolution from the current state.
A base class used to perform Parallel Markov Chain Monte Carlo (MCMC) sampling.
Definition: PMCMCBase.h:19