MUQ  0.4.3
MHProposal.cpp
Go to the documentation of this file.
2 
4 
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 MHProposal::MHProposal(pt::ptree const& pt,
15  std::shared_ptr<AbstractSamplingProblem> prob) :
16  MCMCProposal(pt,prob) {
17 
18  unsigned int problemDim = prob->blockSizes(blockInd);
19 
20  // compute the (diagonal) covariance for the proposal
21  const Eigen::VectorXd cov = pt.get("ProposalVariance", 1.0)*
22  Eigen::VectorXd::Ones(problemDim);
23 
24  // created a Gaussian with scaled identity covariance
25  proposal = std::make_shared<Gaussian>(Eigen::VectorXd::Zero(problemDim), cov);
26 }
27 
28 MHProposal::MHProposal(pt::ptree const& pt,
29  std::shared_ptr<AbstractSamplingProblem> prob,
30  std::shared_ptr<GaussianBase> proposalIn) :
31  MCMCProposal(pt,prob), proposal(proposalIn) {}
32 
33 std::shared_ptr<SamplingState> MHProposal::Sample(std::shared_ptr<SamplingState> const& currentState) {
34  assert(currentState->state.size()>blockInd);
35 
36  // the mean of the proposal is the current point
37  std::vector<Eigen::VectorXd> props = currentState->state;
38  assert(props.size()>blockInd);
39  Eigen::VectorXd const& xc = currentState->state.at(blockInd);
40 
41  Eigen::VectorXd prop = proposal->Sample();
42  props.at(blockInd) = xc + prop;
43 
44  // store the new state in the output
45  return std::make_shared<SamplingState>(props, 1.0);
46 }
47 
48 double MHProposal::LogDensity(std::shared_ptr<SamplingState> const& currState,
49  std::shared_ptr<SamplingState> const& propState) {
50 
51  Eigen::VectorXd diff = propState->state.at(blockInd)-currState->state.at(blockInd);
52  return proposal->LogDensity(diff);//, std::pair<boost::any, Gaussian::Mode>(conditioned->state.at(blockInd), Gaussian::Mode::Mean));
53 }
REGISTER_MCMC_PROPOSAL(MHProposal) MHProposal
Definition: MHProposal.cpp:12
Implementation of the classic Random Walk Metropolis proposal.
Definition: MHProposal.h:20
virtual double LogDensity(std::shared_ptr< SamplingState > const &currState, std::shared_ptr< SamplingState > const &propState) override
Definition: MHProposal.cpp:48
std::shared_ptr< muq::Modeling::GaussianBase > proposal
The proposal distribution.
Definition: MHProposal.h:35