MUQ  0.4.3
MALAProposal.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 MALAProposal::MALAProposal(pt::ptree pt,
15  std::shared_ptr<AbstractSamplingProblem> const& probIn) :
16  MCMCProposal(pt,probIn)
17 {
18 
19  unsigned int problemDim = prob->blockSizes(blockInd);
20 
21  stepSize = pt.get("StepSize", 1.0);
22  assert(stepSize>0);
23 
24  // compute the (diagonal) covariance for the proposal
25  const Eigen::VectorXd cov = Eigen::VectorXd::Ones(problemDim);
26 
27  // created a Gaussian with scaled identity covariance
28  zDist = std::make_shared<Gaussian>(Eigen::VectorXd::Zero(problemDim), cov);
29 }
30 
31 MALAProposal::MALAProposal(pt::ptree pt,
32  std::shared_ptr<AbstractSamplingProblem> const& probIn,
33  std::shared_ptr<GaussianBase> zDistIn) :
34 
35  MCMCProposal(pt,probIn),
36  zDist(zDistIn)
37 {
38  stepSize = pt.get("StepSize", 1.0);
39  assert(stepSize>0);
40 }
41 
42 std::shared_ptr<SamplingState> MALAProposal::Sample(std::shared_ptr<SamplingState> const& currentState) {
43  assert(currentState->state.size()>blockInd);
44 
45  // the mean of the proposal is the current point
46  std::vector<Eigen::VectorXd> props = currentState->state;
47  assert(props.size()>blockInd);
48  Eigen::VectorXd const& xc = currentState->state.at(blockInd);
49 
50  // Get the gradient from the previous step
51  Eigen::VectorXd sigmaGrad; // vector holding the covariance of the proposal times the gradient
52 
53  std::stringstream blockId;
54  blockId << "_" << blockInd;
55 
56  if(!currentState->HasMeta("MALA_SigmaGrad" + blockId.str())){
57  Eigen::VectorXd grad; // vector holding the gradient of the log density
58 
59  if(!currentState->HasMeta("GradLogDensity" + blockId.str()))
60  currentState->meta["GradLogDensity" + blockId.str()] = prob->GradLogDensity(currentState, blockInd);
61 
62  grad = AnyCast( currentState->meta["GradLogDensity" + blockId.str()] );
63 
64  currentState->meta["MALA_SigmaGrad" + blockId.str()] = zDist->ApplyCovariance(grad).col(0).eval();
65  }
66 
67  sigmaGrad = AnyCast(currentState->meta["MALA_SigmaGrad" + blockId.str()]);
68 
69  // Draw a sample
70  Eigen::VectorXd prop = stepSize*sigmaGrad + std::sqrt(2.0*stepSize)*zDist->Sample();
71  props.at(blockInd) = xc + prop;
72 
73  // store the new state in the output
74  return std::make_shared<SamplingState>(props, 1.0);
75 }
76 
77 double MALAProposal::LogDensity(std::shared_ptr<SamplingState> const& currState,
78  std::shared_ptr<SamplingState> const& propState) {
79 
80  // Get the gradient from the previous step
81  Eigen::VectorXd grad; // vector holding the gradient of the log density
82  Eigen::VectorXd sigmaGrad; // vector holding the covariance of the proposal times the gradient
83 
84  std::stringstream blockId;
85  blockId << "_" << blockInd;
86 
87  if(!currState->HasMeta("GradLogDensity" + blockId.str())){
88  try{
89  currState->meta["GradLogDensity" + blockId.str()] = prob->GradLogDensity(currState, blockInd);
90  }catch(std::runtime_error &e){
91  currState->meta["GradLogDensity" + blockId.str()] = Eigen::VectorXd::Zero(currState->state.at(blockInd).size()).eval();
92  }
93  }
94 
95  grad = AnyCast( currState->meta["GradLogDensity" + blockId.str()] );
96 
97  if(!currState->HasMeta("MALA_SigmaGrad" + blockId.str()))
98  currState->meta["MALA_SigmaGrad" + blockId.str()] = zDist->ApplyCovariance(grad).col(0).eval();
99 
100  sigmaGrad = AnyCast(currState->meta["MALA_SigmaGrad" + blockId.str()]);
101 
102  Eigen::VectorXd z = (propState->state.at(blockInd) - currState->state.at(blockInd) - stepSize*sigmaGrad) / std::sqrt(2.0*stepSize);
103 
104  return zDist->LogDensity(z);
105 }
REGISTER_MCMC_PROPOSAL(MALAProposal) MALAProposal
Implementation preconditioned Langevin proposal used in the MALA algorithm.
Definition: MALAProposal.h:34
std::shared_ptr< muq::Modeling::GaussianBase > zDist
The proposal distribution.
Definition: MALAProposal.h:51
virtual double LogDensity(std::shared_ptr< SamplingState > const &currState, std::shared_ptr< SamplingState > const &propState) override
std::shared_ptr< AbstractSamplingProblem > prob
Definition: MCMCProposal.h:81
Class for easily casting boost::any's in assignment operations.
Definition: AnyHelpers.h:34