https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
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{
48 "num_parallel_proposals",
49 "At least five parallel proposals should be used for the Differential Evolution Sampler.");
50
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
67void
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
77void
78AffineInvariantDES::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
87void
89{
90 unsigned int j = 0;
91 bool indicator;
92 std::pair<unsigned int, unsigned int> index_req;
93 Real diff;
94 while (j < _num_parallel_proposals)
95 {
96 indicator = 0;
98 for (unsigned int i = 0; i < _priors.size(); ++i)
99 {
100 computeDifferential(_previous_state[index_req.first][i],
101 _previous_state[index_req.second][i],
102 random(),
103 _scales[i],
104 diff);
105 _new_samples[j][i] = (_t_step + 1 > decisionStep()) ? (_previous_state[j][i] + diff)
106 : _priors[i]->quantile(random());
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_req.second],
117 random(),
118 1.0,
119 diff);
120 _new_var_samples[j] = (_t_step + 1 > decisionStep()) ? (_previous_state_var[j] + diff)
123 indicator = 1;
124 }
125 if (!indicator)
126 ++j;
127 }
128}
registerMooseObject("StochasticToolsApp", AffineInvariantDES)
Real scale
A class for performing Affine Invariant Ensemble MCMC with differential sampler.
void tuneParams(Real &gamma, Real &b, const Real &scale)
Tune the internal parameters.
const std::vector< Real > & _previous_state_var
Reporter value with the previous state of all the walkers for variance.
AffineInvariantDES(const InputParameters &parameters)
std::vector< Real > _scales
Scales for the parameters.
virtual void proposeSamples() override
Fill in the _new_samples vector of vectors (happens within sampleSetUp)
static InputParameters validParams()
const std::vector< std::vector< Real > > & _previous_state
Reporter value with the previous state of all the walkers.
void computeDifferential(const Real &state1, const Real &state2, const Real &rnd, const Real &scale, Real &diff)
Compute the differential evolution from the current state.
const MooseEnum & _tuning_option
Tuning options for the internal params.
virtual int decisionStep() const override
Return the step after which decision making can begin.
virtual Real quantile(const Real &y) const=0
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void paramError(const std::string &param, Args... args) const
void mooseWarning(Args &&... args) const
bool isParamValid(const std::string &name) const
virtual Real quantile(const Real &p) const override
Definition Normal.C:80
A base class used to perform Parallel Markov Chain Monte Carlo (MCMC) sampling.
Definition PMCMCBase.h:20
const std::vector< Real > * _lower_bound
Lower bounds for making the next proposal.
Definition PMCMCBase.h:121
std::vector< const Distribution * > _priors
Storage for prior distribution objects to be utilized.
Definition PMCMCBase.h:115
static InputParameters validParams()
Definition PMCMCBase.C:18
Real random()
Sample a random number between 0 and 1.
Definition PMCMCBase.C:142
const Distribution * _var_prior
Storage for prior distribution object of the variance to be utilized.
Definition PMCMCBase.h:118
std::pair< unsigned int, unsigned int > randomIndexPair(const unsigned int &upper_bound, const unsigned int &exclude)
Sample two random indices without repitition excluding a specified index.
Definition PMCMCBase.C:157
std::vector< std::vector< Real > > _new_samples
Vectors of new proposed samples.
Definition PMCMCBase.h:133
std::vector< Real > _new_var_samples
Vector of new proposed variance samples.
Definition PMCMCBase.h:136
const unsigned int _num_parallel_proposals
Number of parallel proposals to be made and subApps to be executed.
Definition PMCMCBase.h:112
const Real & _variance_bound
Upper bound for variance for making the next proposal.
Definition PMCMCBase.h:127