31 #ifndef __TASMANIAN_DREAM_SAMPLE_HPP
32 #define __TASMANIAN_DREAM_SAMPLE_HPP
69 using DreamDomain = std::function<bool(std::vector<double>
const &x)>;
76 return [=](
const std::vector<double> &x)->
bool{
77 auto il = lower.begin(), iu = upper.begin();
78 for(
auto v : x)
if ((v < *il++) || (v > *iu++))
return false;
110 template<
int weight_percent>
142 using DreamPDF = std::function<void(
const std::vector<double> &candidates, std::vector<double> &values)>;
157 using DreamModel = std::function<void(
const std::vector<double> &candidates, std::vector<double> &outputs)>;
259 template<TypeSamplingForm form = regform>
263 return [=](
const std::vector<double> &candidates, std::vector<double> &values)->
void{
264 std::vector<double> model_outs;
265 model(candidates, model_outs);
266 likelihood(form, model_outs, values);
268 std::vector<double> prior_vals(values.size());
269 prior(form, candidates, prior_vals);
271 auto iv = values.begin();
273 for(
auto p : prior_vals) *iv++ *= p;
275 for(
auto p : prior_vals) *iv++ += p;
290 template<TypeSamplingForm form = regform>
293 return [=](
const std::vector<double> &candidates, std::vector<double> &values)->
void{
294 likelihood_model(candidates, values);
296 std::vector<double> prior_vals(values.size());
297 prior(form, candidates, prior_vals);
299 auto iv = values.begin();
301 for(
auto p : prior_vals) *iv++ *= p;
303 for(
auto p : prior_vals) *iv++ += p;
413 template<TypeSamplingForm form = regform>
418 std::function<
void(std::vector<double> &x)> independent_update =
no_update,
419 std::function<
double(
void)> differential_update =
const_one,
423 double unitlength = (double) num_chains;
425 if (num_chains == 0)
return;
427 if (!state.
isStateReady())
throw std::runtime_error(
"ERROR: DREAM sampling requires that the setState() has been called first on the TasmanianDREAM.");
435 int total_iterations = std::max(num_burnup, 0) + std::max(num_collect, 0);
436 for(
int t = 0; t < total_iterations; t++){
437 std::vector<double> candidates, values;
438 candidates.reserve(num_chains * num_dimensions);
439 values.reserve(num_chains);
441 std::vector<bool> valid(num_chains,
true);
443 for(
size_t i=0; i<num_chains; i++){
444 std::vector<double> propose(num_dimensions);
446 size_t jindex = (size_t) (get_random01() * unitlength);
447 size_t kindex = (size_t) (get_random01() * unitlength);
448 if (jindex >= num_chains) jindex = num_chains - 1;
449 if (kindex >= num_chains) jindex = num_chains - 1;
451 state.
getIJKdelta(i, jindex, kindex, differential_update(), propose);
452 independent_update(propose);
454 if (inside(propose)){
455 candidates.insert(candidates.end(), propose.begin(), propose.end());
456 values.resize(values.size() + 1);
462 if (!candidates.empty())
463 probability_distribution(candidates, values);
465 std::vector<double> new_state(num_chains * num_dimensions), new_values(num_chains);
467 auto icand = candidates.begin();
468 auto ival = values.begin();
472 for(
size_t i=0; i<num_chains; i++){
473 bool keep_new = valid[i];
479 keep_new = (*ival / state.
getPDFvalue(i) >= get_random01());
481 keep_new = (*ival - state.
getPDFvalue(i) >= log(get_random01()));
488 std::copy_n(icand, num_dimensions, new_state.begin() + i * num_dimensions);
489 new_values[i] = *ival;
492 state.
getChainState((
int) i, &*(new_state.begin() + i * num_dimensions));
497 std::advance(icand, num_dimensions);
520 template<TypeSamplingForm form = regform>
526 std::function<
double(
void)> differential_update =
const_one,
529 SampleDREAM<form>(num_burnup, num_collect, probability_distribution, inside, state,
530 [&](std::vector<double> &x)->
void{
applyUniformUpdate(x, magnitude, get_random01); }, differential_update, get_random01);
532 SampleDREAM<form>(num_burnup, num_collect, probability_distribution, inside, state,
533 [&](std::vector<double> &x)->
void{
applyGaussianUpdate(x, magnitude, get_random01); }, differential_update, get_random01);
535 SampleDREAM<form>(num_burnup, num_collect, probability_distribution, inside, state,
no_update, differential_update, get_random01);
Contains the current state and the history of the DREAM chains.
Definition: tsgDreamState.hpp:70
double getPDFvalue(size_t i) const
Return the value of the probability_distribution of the i-th chain.
Definition: tsgDreamState.hpp:155
void saveStateHistory(size_t num_accepted)
Appends the current state to the history.
int getNumDimensions() const
Return the number of dimensions.
Definition: tsgDreamState.hpp:91
void setPDFvalues(const std::vector< double > &new_values)
Set the current set of pdf values to the new_values.
bool isStateReady() const
Return true if the state has already been initialized with setState().
Definition: tsgDreamState.hpp:98
void getChainState(size_t i, double *x) const
Return the state of the i-th chain, store an array x of size at least num_dimensions.
Definition: tsgDreamState.hpp:147
void expandHistory(int num_snapshots)
Allocate (expand) internal storage for the history snapshots, avoids reallocating data when saving a ...
void getIJKdelta(size_t i, size_t j, size_t k, double w, std::vector< double > &x) const
Used by the DREAM sampler, , where s indicates the current i, j, and k-th state vectors.
int getNumChains() const
Return the number of chains.
Definition: tsgDreamState.hpp:93
bool isPDFReady() const
Return true if the pdf values have already been initialized with setPDF().
Definition: tsgDreamState.hpp:100
void setState(const std::vector< double > &new_state)
Set the current DREAM state to the new_state.
void applyGaussianUpdate(std::vector< double > &x, double magnitude, std::function< double(void)> get_random01=tsgCoreUniform01)
Add a correction to every entry in x, sue Gaussian distribution with zero mean and standard deviation...
Definition: tsgDreamCoreRandom.hpp:77
double tsgCoreUniform01()
Default random sampler, using rand() divided by RAND_MAX.
Definition: tsgDreamCoreRandom.hpp:61
void applyUniformUpdate(std::vector< double > &x, double magnitude, std::function< double(void)> get_random01=tsgCoreUniform01)
Add a correction to every entry in x, use uniform samples over (-magnitude, magnitude).
Definition: tsgDreamCoreRandom.hpp:67
std::function< void(const std::vector< double > &candidates, std::vector< double > &values)> DreamMergedLikelyModel
Generic signature for a combination of a likelihood and a model.
Definition: tsgDreamSample.hpp:200
double const_percent()
Template that returns a constant based on the percentage, i.e., weight_percent / 100....
Definition: tsgDreamSample.hpp:111
void no_update(std::vector< double > &)
Dummy function that does not make any changes to the vector as default for the independent_update() i...
Definition: tsgDreamSample.hpp:89
std::function< void(const std::vector< double > &candidates, std::vector< double > &outputs)> DreamModel
Generic model signature used by Tasmanian.
Definition: tsgDreamSample.hpp:157
void SampleDREAM(int num_burnup, int num_collect, DreamPDF probability_distribution, DreamDomain inside, TasmanianDREAM &state, std::function< void(std::vector< double > &x)> independent_update=no_update, std::function< double(void)> differential_update=const_one, std::function< double(void)> get_random01=tsgCoreUniform01)
Core template for the sampling algorithm.
Definition: tsgDreamSample.hpp:414
DreamDomain hypercube(std::vector< double > const &lower, std::vector< double > const &upper)
Make a lambda that matches the inside signature in SampleDREAM(), test if the vector x is in the hype...
Definition: tsgDreamSample.hpp:75
DreamPDF posterior(DreamModel model, DreamLikelihood likelihood, DreamPrior prior)
Combines the three components of a Bayesian posterior into a single distribution.
Definition: tsgDreamSample.hpp:260
void uniform_prior(TypeSamplingForm, const std::vector< double > &, std::vector< double > &values)
Uniform prior distribution for both regular and log form.
Definition: tsgDreamSample.hpp:120
double const_one()
Dummy function that returns 1.0, used as default for the differential_update() in SampleDREAM().
Definition: tsgDreamSample.hpp:97
std::function< void(const std::vector< double > &candidates, std::vector< double > &values)> DreamPDF
Generic probability distribution used by Tasmanian.
Definition: tsgDreamSample.hpp:142
std::function< void(TypeSamplingForm form, const std::vector< double > &model_outputs, std::vector< double > &likely)> DreamLikelihood
Generic likelihood signature used by Tasmanian.
Definition: tsgDreamSample.hpp:175
std::function< void(TypeSamplingForm form, const std::vector< double > &candidates, std::vector< double > &values)> DreamPrior
Generic signature for the prior distributions used by Tasmanian.
Definition: tsgDreamSample.hpp:187
std::function< bool(std::vector< double > const &x)> DreamDomain
Generic test function whether a sample belongs in the domain.
Definition: tsgDreamSample.hpp:69
Encapsulates the Tasmanian DREAM module.
Definition: TasmanianDREAM.hpp:80
Gives the unscaled formulas for several probability distributions.
Core random sampling methods.
The container class holding the DREAM history.