MUQ  0.4.3
MIDummyKernel.cpp
Go to the documentation of this file.
3 
5 
6 #include <iomanip>
7 
8 namespace pt = boost::property_tree;
9 using namespace muq::Utilities;
10 using namespace muq::Modeling;
11 using namespace muq::SamplingAlgorithms;
12 
13 MIDummyKernel::MIDummyKernel(boost::property_tree::ptree const& pt,
14  std::shared_ptr<AbstractSamplingProblem> problem,
15  std::shared_ptr<MCMCProposal> proposal,
16  std::shared_ptr<MCMCProposal> coarse_proposal,
17  std::shared_ptr<MIInterpolation> proposalInterpolation,
18  std::shared_ptr<SingleChainMCMC> coarse_chain)
19  : TransitionKernel(pt, problem),
20  proposal(proposal),
21  coarse_proposal(coarse_proposal),
22  proposalInterpolation(proposalInterpolation),
23  coarse_chain(coarse_chain)
24  {}
25 
26 
28 
29 void MIDummyKernel::PostStep(unsigned int const t, std::vector<std::shared_ptr<SamplingState>> const& state){
30  proposal->Adapt(t,state);
31 }
32 
33 std::vector<std::shared_ptr<SamplingState>> MIDummyKernel::Step(unsigned int const t, std::shared_ptr<SamplingState> prevState){
34 
35  assert(proposal);
36 
37  numCalls++;
38 
39  // If no coarse sample is specified, assume it's the first one
40  std::shared_ptr<SamplingState> coarsePrevState;
41  if(prevState->HasMeta("coarseSample")) {
42  coarsePrevState = AnyCast(prevState->meta["coarseSample"]);
43  } else {
44  coarsePrevState = coarse_chain->GetSamples()->at(0);
45  }
46 
47  // New fine proposal
48  std::shared_ptr<SamplingState> fineProp = proposal->Sample(prevState);
49  std::shared_ptr<SamplingState> coarseProp = coarse_proposal->Sample(coarsePrevState);
50  std::shared_ptr<SamplingState> prop = proposalInterpolation->Interpolate (coarseProp, fineProp);
51 
52  // Let's retrieve density and QOI as usual, but just not do anything with it
53  problem->LogDensity(prop);
54  if (problem->numBlocksQOI > 0) {
55  prop->meta["QOI"] = problem->QOI();
56  }
57  return std::vector<std::shared_ptr<SamplingState>>(1, prop);
58 }
59 
60 void MIDummyKernel::PrintStatus(const std::string prefix) const
61 {
62  std::stringstream msg;
63  msg << prefix << "MI dummy kernel was called " << numCalls << " times";
64 
65  std::cout << msg.str() << std::endl;
66 }
virtual std::vector< std::shared_ptr< SamplingState > > Step(unsigned int const t, std::shared_ptr< SamplingState > prevState) override
std::shared_ptr< MCMCProposal > proposal
Definition: MIDummyKernel.h:37
std::shared_ptr< MCMCProposal > coarse_proposal
Definition: MIDummyKernel.h:38
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< MIInterpolation > proposalInterpolation
Definition: MIDummyKernel.h:39
std::shared_ptr< SingleChainMCMC > coarse_chain
Definition: MIDummyKernel.h:40
Defines the transition kernel used by an MCMC algorithm.
std::shared_ptr< AbstractSamplingProblem > problem
The sampling problem that evaluates/samples the target distribution.
Class for easily casting boost::any's in assignment operations.
Definition: AnyHelpers.h:34