MUQ  0.4.3
ImportanceSampling.cpp
Go to the documentation of this file.
2 
3 #include "MUQ/config.h"
4 
6 
7 namespace pt = boost::property_tree;
8 using namespace muq::Modeling;
9 using namespace muq::SamplingAlgorithms;
10 
11 ImportanceSampling::ImportanceSampling(std::shared_ptr<muq::Modeling::Distribution> const& target, boost::property_tree::ptree const& pt) : SamplingAlgorithm(std::make_shared<SampleCollection>()), numSamps(pt.get<unsigned int>("NumSamples")), bias(target) {}
12 
13 ImportanceSampling::ImportanceSampling(std::shared_ptr<ModPiece> const& target, std::shared_ptr<Distribution> const& bias, pt::ptree const& pt) : SamplingAlgorithm(std::make_shared<SampleCollection>()),
14  numSamps(pt.get<unsigned int>("NumSamples")), target(target), bias(bias) {}
15 
16 ImportanceSampling::ImportanceSampling(std::shared_ptr<ModPiece> const& target, std::shared_ptr<Distribution> const& bias, std::vector<Eigen::VectorXd> hyperparameters, pt::ptree const& pt) : SamplingAlgorithm(std::make_shared<SampleCollection>()), numSamps(pt.get<unsigned int>("NumSamples")), target(target), bias(bias), hyperparameters(hyperparameters) {}
17 
18 ImportanceSampling::ImportanceSampling(std::shared_ptr<Distribution> const& target, std::vector<Eigen::VectorXd> hyperparameters, pt::ptree const& pt) : SamplingAlgorithm(std::make_shared<SampleCollection>()), numSamps(pt.get<unsigned int>("NumSamples")), bias(target), hyperparameters(hyperparameters) {}
19 
20 std::shared_ptr<SampleCollection> ImportanceSampling::RunImpl(std::vector<Eigen::VectorXd> const& x0) {
21  // store a copy of the biasing distribution hyper parameters
22  std::vector<Eigen::VectorXd> biasingPara = hyperparameters;
23 
24  // insert an empty vector for the state
25  biasingPara.insert(biasingPara.begin(), Eigen::VectorXd());
26 
27  // loop through the samples to generate them
28  for( unsigned int i=0; i<numSamps; ++i ) {
29  // sample from the biasing distribution
30  biasingPara[0] = bias->Sample(hyperparameters);
31 
32  // compute the weight
33  double logweight = 0.0, logbias = 0.0, logtarget = 0.0;
34  if( target ) {
35  logbias = bias->LogDensity(biasingPara);
36  logtarget = target->Evaluate(biasingPara[0])[0](0);
37  logweight = logtarget - logbias;
38  }
39 
40  auto state = std::make_shared<SamplingState>(biasingPara[0], std::exp(logweight));
41  if( target ) {
42  state->meta["log target"] = logtarget;
43  state->meta["log bias"] = logbias;
44  }
45 
46  // store the sample
47  samples->Add(state);
48  }
49 
50  return samples;
51 }
const unsigned int numSamps
The number of samples.
std::shared_ptr< muq::Modeling::Distribution > bias
The biasing distribution.
ImportanceSampling(std::shared_ptr< muq::Modeling::Distribution > const &target, boost::property_tree::ptree const &pt)
virtual std::shared_ptr< SampleCollection > RunImpl(std::vector< Eigen::VectorXd > const &x0) override
const std::vector< Eigen::VectorXd > hyperparameters
Hyperparameters for the biasing distribution.
std::shared_ptr< muq::Modeling::ModPiece > target
A class to hold and analyze a collection of SamplingState objects.
std::shared_ptr< SampleCollection > samples
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.h:3956