7 namespace pt = boost::property_tree;
14 MALAProposal::MALAProposal(pt::ptree pt,
15 std::shared_ptr<AbstractSamplingProblem>
const& probIn) :
19 unsigned int problemDim = prob->blockSizes(blockInd);
21 stepSize = pt.get(
"StepSize", 1.0);
25 const Eigen::VectorXd cov = Eigen::VectorXd::Ones(problemDim);
28 zDist = std::make_shared<Gaussian>(Eigen::VectorXd::Zero(problemDim), cov);
31 MALAProposal::MALAProposal(pt::ptree pt,
32 std::shared_ptr<AbstractSamplingProblem>
const& probIn,
33 std::shared_ptr<GaussianBase> zDistIn) :
38 stepSize = pt.get(
"StepSize", 1.0);
42 std::shared_ptr<SamplingState> MALAProposal::Sample(std::shared_ptr<SamplingState>
const& currentState) {
43 assert(currentState->state.size()>
blockInd);
46 std::vector<Eigen::VectorXd> props = currentState->state;
48 Eigen::VectorXd
const& xc = currentState->state.at(
blockInd);
51 Eigen::VectorXd sigmaGrad;
53 std::stringstream blockId;
56 if(!currentState->HasMeta(
"MALA_SigmaGrad" + blockId.str())){
59 if(!currentState->HasMeta(
"GradLogDensity" + blockId.str()))
60 currentState->meta[
"GradLogDensity" + blockId.str()] =
prob->GradLogDensity(currentState,
blockInd);
62 grad =
AnyCast( currentState->meta[
"GradLogDensity" + blockId.str()] );
64 currentState->meta[
"MALA_SigmaGrad" + blockId.str()] =
zDist->ApplyCovariance(grad).col(0).eval();
67 sigmaGrad =
AnyCast(currentState->meta[
"MALA_SigmaGrad" + blockId.str()]);
74 return std::make_shared<SamplingState>(props, 1.0);
78 std::shared_ptr<SamplingState>
const& propState) {
82 Eigen::VectorXd sigmaGrad;
84 std::stringstream blockId;
87 if(!currState->HasMeta(
"GradLogDensity" + blockId.str())){
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();
95 grad =
AnyCast( currState->meta[
"GradLogDensity" + blockId.str()] );
97 if(!currState->HasMeta(
"MALA_SigmaGrad" + blockId.str()))
98 currState->meta[
"MALA_SigmaGrad" + blockId.str()] =
zDist->ApplyCovariance(grad).col(0).eval();
100 sigmaGrad =
AnyCast(currState->meta[
"MALA_SigmaGrad" + blockId.str()]);
104 return zDist->LogDensity(z);
REGISTER_MCMC_PROPOSAL(MALAProposal) MALAProposal
Implementation preconditioned Langevin proposal used in the MALA algorithm.
std::shared_ptr< muq::Modeling::GaussianBase > zDist
The proposal distribution.
virtual double LogDensity(std::shared_ptr< SamplingState > const &currState, std::shared_ptr< SamplingState > const &propState) override
std::shared_ptr< AbstractSamplingProblem > prob
Class for easily casting boost::any's in assignment operations.