9 namespace pt = boost::property_tree;
14 MIKernel::MIKernel(boost::property_tree::ptree
const& pt,
15 std::shared_ptr<AbstractSamplingProblem> problem,
16 std::shared_ptr<AbstractSamplingProblem> coarse_problem,
17 std::shared_ptr<MCMCProposal> proposal,
18 std::shared_ptr<MCMCProposal> coarse_proposal,
19 std::shared_ptr<MIInterpolation> proposalInterpolation,
20 std::shared_ptr<SingleChainMCMC> coarse_chain)
22 coarse_problem(coarse_problem),
24 coarse_proposal(coarse_proposal),
25 proposalInterpolation(proposalInterpolation),
26 coarse_chain(coarse_chain) {}
31 void MIKernel::PostStep(
unsigned int const t, std::vector<std::shared_ptr<SamplingState>>
const& state){
35 std::vector<std::shared_ptr<SamplingState>>
MIKernel::Step(
unsigned int const t, std::shared_ptr<SamplingState> prevState){
39 std::shared_ptr<SamplingState> coarsePrevState;
40 if(prevState->HasMeta(
"coarseSample")) {
41 coarsePrevState =
AnyCast(prevState->meta[
"coarseSample"]);
47 std::shared_ptr<SamplingState> prop =
proposal->Sample(prevState);
48 std::shared_ptr<SamplingState> coarseProp =
coarse_proposal->Sample(coarsePrevState);
52 if(prevState->HasMeta(
"iteration")) {
53 fineProp->meta[
"iteration"] = prevState->meta[
"iteration"];
55 if(coarsePrevState->HasMeta(
"iteration")) {
56 coarseProp->meta[
"iteration"] = coarsePrevState->meta[
"iteration"];
58 coarseProp->meta[
"iteration"] = (
unsigned int)0;
59 coarsePrevState->meta[
"iteration"] = (
unsigned int)0;
61 coarsePrevState->meta[
"IsProposal"] =
false;
62 fineProp->meta[
"IsProposal"] =
true;
63 coarseProp->meta[
"IsProposal"] =
true;
68 double propTargetCoarse;
69 double currentTargetCoarse;
71 if(prevState->HasMeta(
"LogTarget") && (prevState->HasMeta(
"QOI") ||
problem->numBlocksQOI == 0)){
72 currentTarget =
AnyCast(prevState->meta[
"LogTarget"]);
74 currentTarget =
problem->LogDensity(prevState);
75 prevState->meta[
"LogTarget"] = currentTarget;
76 if (
problem->numBlocksQOI > 0) {
77 prevState->meta[
"QOI"] =
problem->QOI();
81 if(coarsePrevState->HasMeta(
"LogTarget") && (coarsePrevState->HasMeta(
"QOI") ||
coarse_problem->numBlocksQOI == 0)){
82 currentTargetCoarse =
AnyCast(coarsePrevState->meta[
"LogTarget"]);
85 coarsePrevState->meta[
"LogTarget"] = currentTargetCoarse;
91 propTarget =
problem->LogDensity(fineProp);
92 assert(coarseProp->HasMeta(
"LogTarget"));
93 propTargetCoarse =
AnyCast(coarseProp->meta[
"LogTarget"]);
95 fineProp->meta[
"LogTarget"] = propTarget;
96 fineProp->meta[
"coarseSample"] = coarseProp;
99 const double forwardPropDens =
proposal->LogDensity(prevState, fineProp);
100 const double backPropDens =
proposal->LogDensity(fineProp, prevState);
101 const double alpha = std::exp(propTarget + currentTargetCoarse + backPropDens
102 -(currentTarget + propTargetCoarse + forwardPropDens));
106 if(RandomGenerator::GetUniform()<alpha) {
108 if (
problem->numBlocksQOI > 0) {
109 fineProp->meta[
"QOI"] =
problem->QOI();
111 return std::vector<std::shared_ptr<SamplingState>>(1,fineProp);
114 auto prevStateCopy = std::make_shared<SamplingState>(prevState->state);
115 prevStateCopy->meta = prevState->meta;
116 prevStateCopy->meta[
"coarseSample"] = coarseProp;
117 return std::vector<std::shared_ptr<SamplingState>>(1,prevStateCopy);
123 std::stringstream msg;
124 msg << std::setprecision(2);
125 msg << prefix <<
"MIKernel acceptance Rate = " << 100.0*double(
numAccepts)/double(
numCalls) <<
"%";
127 std::cout << msg.str() << std::endl;
std::shared_ptr< AbstractSamplingProblem > coarse_problem
std::shared_ptr< SingleChainMCMC > coarse_chain
std::shared_ptr< MCMCProposal > proposal
std::shared_ptr< MIInterpolation > proposalInterpolation
virtual void PostStep(unsigned int const t, std::vector< std::shared_ptr< SamplingState >> const &state) override
Allow the kernel to adapt given a new state.
std::shared_ptr< MCMCProposal > coarse_proposal
virtual std::vector< std::shared_ptr< SamplingState > > Step(unsigned int const t, std::shared_ptr< SamplingState > prevState) override
Defines the transition kernel used by an MCMC algorithm.
std::shared_ptr< AbstractSamplingProblem > problem
The sampling problem that evaluates/samples the target distribution.
virtual void PrintStatus() const
Class for easily casting boost::any's in assignment operations.