MUQ  0.4.3
SLMCMC.cpp
Go to the documentation of this file.
2 
3 namespace muq {
4  namespace SamplingAlgorithms {
5 
6  SLMCMC::SLMCMC (pt::ptree pt, std::shared_ptr<MIComponentFactory> componentFactory, std::shared_ptr<MultiIndex> index)
7  : componentFactory(componentFactory)
8  {
9  auto finestIndex = componentFactory->FinestIndex();
10 
11  assert(index->GetLength() == finestIndex->GetLength());
12  assert(*index <= *(componentFactory->FinestIndex()));
13 
14  pt::ptree ptBlockID;
15  ptBlockID.put("BlockIndex",0);
16 
17  auto problem = componentFactory->SamplingProblem(index);
18  auto proposal = componentFactory->Proposal(index, problem);
19 
20  std::vector<std::shared_ptr<TransitionKernel>> kernels(1);
21  kernels[0] = std::make_shared<MHKernel>(ptBlockID,problem,proposal);
22 
23  Eigen::VectorXd startingPoint = componentFactory->StartingPoint(index);
24 
25  single_chain = std::make_shared<SingleChainMCMC>(pt,kernels);
26  single_chain->SetState(startingPoint);
27  }
28 
29  SLMCMC::SLMCMC (pt::ptree pt, std::shared_ptr<MIComponentFactory> componentFactory)
30  : SLMCMC(pt,componentFactory, componentFactory->FinestIndex()) { }
31 
32  std::shared_ptr<MarkovChain> SLMCMC::GetSamples() const {
33  return single_chain->GetSamples();
34  }
35  std::shared_ptr<MarkovChain> SLMCMC::GetQOIs() const {
36  return single_chain->GetQOIs();
37  }
38 
39  std::shared_ptr<MarkovChain> SLMCMC::Run() {
40  return single_chain->Run();
41  }
42 
43  void SLMCMC::WriteToFile(std::string filename){
44  auto samps = single_chain->GetSamples();
45  auto QOI = single_chain->GetQOIs();
46  if(QOI != nullptr)
47  QOI->WriteToFile(filename,"/qois");
48  samps->WriteToFile(filename,"/samples");
49  }
50 
51  }
52 }
Single-level MCMC for multiindex sampling problems.
Definition: SLMCMC.h:23
virtual std::shared_ptr< MarkovChain > GetSamples() const
Definition: SLMCMC.cpp:32
SLMCMC(pt::ptree pt, std::shared_ptr< MIComponentFactory > componentFactory, std::shared_ptr< MultiIndex > index)
Definition: SLMCMC.cpp:6
std::shared_ptr< SingleChainMCMC > single_chain
Definition: SLMCMC.h:45
std::shared_ptr< MIComponentFactory > componentFactory
Definition: SLMCMC.h:44
virtual std::shared_ptr< MarkovChain > GetQOIs() const
Definition: SLMCMC.cpp:35
void WriteToFile(std::string filename)
Definition: SLMCMC.cpp:43
virtual std::shared_ptr< MarkovChain > Run()
Definition: SLMCMC.cpp:39