MUQ  0.4.3
MixtureProposal.cpp
Go to the documentation of this file.
2 
6 
7 namespace pt = boost::property_tree;
8 using namespace muq::Modeling;
9 using namespace muq::SamplingAlgorithms;
10 using namespace muq::Utilities;
11 
13 
14 
15 MixtureProposal::MixtureProposal(boost::property_tree::ptree pt,
16  std::shared_ptr<AbstractSamplingProblem> const& prob) : MixtureProposal(pt,
17  prob,
18  GetProposals(pt,prob),
19  GetWeights(pt))
20 {
21 }
22 
23 MixtureProposal::MixtureProposal(boost::property_tree::ptree pt,
24  std::shared_ptr<AbstractSamplingProblem> const& prob,
25  std::vector<std::shared_ptr<MCMCProposal>> const& proposalsIn,
26  std::vector<double> const& weightsIn) : MCMCProposal(pt,prob),
27  proposals(proposalsIn),
28  weights(weightsIn)
29 {
30  if(weights.size()==0)
31  weights.resize(proposals.size(), 1.0);
32 
33  assert(proposals.size()==weights.size());
34 
35  // Make sure the weights are positive and normalize them
36  double wtSum = 0.0;
37  for(int i=0; i<weights.size(); ++i){
38  assert(weights.at(i)>0.0);
39  wtSum += weights.at(i);
40  }
41  for(int i=0; i<weights.size();++i)
42  weights.at(i) /= wtSum;
43 }
44 
45 
46 std::vector<std::shared_ptr<MCMCProposal>> MixtureProposal::GetProposals(boost::property_tree::ptree const& pt,
47  std::shared_ptr<AbstractSamplingProblem> const& prob)
48 {
49  std::vector<std::string> propStrings = StringUtilities::Split(pt.get<std::string>("Components"));
50  assert(propStrings.size()>0);
51 
52  std::vector<std::shared_ptr<MCMCProposal>> props(propStrings.size());
53 
54  for(int i=0; i<props.size(); ++i){
55  boost::property_tree::ptree subTree = pt.get_child(propStrings.at(i));
56  subTree.put("BlockIndex", pt.get("BlockIndex",0));
57  props.at(i) = MCMCProposal::Construct(subTree, prob);
58  assert(props.at(i));
59  }
60 
61  return props;
62 }
63 
64 std::vector<double> MixtureProposal::GetWeights(boost::property_tree::ptree const& pt)
65 {
66  std::vector<std::string> wtStrings = StringUtilities::Split(pt.get("Weights",""));
67 
68  if(wtStrings.size()==0){
69  return std::vector<double>();
70 
71  }else{
72 
73  std::vector<double> weights(wtStrings.size());
74  double wtSum = 0.0;
75 
76  for(int i=0; i<weights.size(); ++i)
77  weights.at(i) = std::stof(wtStrings.at(i));
78 
79  return weights;
80  }
81 }
82 
83 
84 std::shared_ptr<SamplingState> MixtureProposal::Sample(std::shared_ptr<SamplingState> const& currentState)
85 {
86 
87  // Pick a component at random
88  int randInd = RandomGenerator::GetDiscrete(weights);
89 
90  // Return a sample of the random index
91  return proposals.at(randInd)->Sample(currentState);
92 }
93 
94 double MixtureProposal::LogDensity(std::shared_ptr<SamplingState> const& currState,
95  std::shared_ptr<SamplingState> const& propState)
96 {
97  double density = 0.0;
98  for(int i=0; i<proposals.size(); ++i)
99  density += weights.at(i) * std::exp(proposals.at(i)->LogDensity(currState, propState));
100 
101  return std::log(density);
102 }
REGISTER_MCMC_PROPOSAL(MixtureProposal) MixtureProposal
std::shared_ptr< AbstractSamplingProblem > prob
Definition: MCMCProposal.h:81
static std::shared_ptr< MCMCProposal > Construct(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > const &probIn)
Static constructor for the transition kernel.
This class implements a weighted mixture of other proposals.
virtual double LogDensity(std::shared_ptr< SamplingState > const &currState, std::shared_ptr< SamplingState > const &propState) override
static std::vector< std::shared_ptr< MCMCProposal > > GetProposals(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > const &prob)
static std::vector< double > GetWeights(boost::property_tree::ptree const &pt)
std::vector< std::shared_ptr< MCMCProposal > > proposals
The proposal distribution.
virtual std::shared_ptr< SamplingState > Sample(std::shared_ptr< SamplingState > const &currentState) override
std::vector< std::string > Split(std::string str, char delim=',')
Split a string into parts based on a particular character delimiter. Also Strips whitespace from part...