MUQ  0.4.3
MultiindexGaussianSampling.cpp
Go to the documentation of this file.
4 
7 
13 
16 
18 
19 #include <boost/property_tree/ptree.hpp>
20 
21 namespace pt = boost::property_tree;
22 using namespace muq::Modeling;
23 using namespace muq::SamplingAlgorithms;
24 using namespace muq::Utilities;
25 
26 
27 int main(){
28 
29  Eigen::MatrixXd tgtCov(2,2);
30  tgtCov << 1.0, 0.3,
31  0.3, 1.5;
32 
33  Eigen::VectorXd tgtMu(2);
34  tgtMu << 0.8, 2.4;
35 
36  // Create a multiindex set specifying all the different model levels
37  auto multis = MultiIndexFactory::CreateFullTensor(2,1);
38 
39  // Make up a "hierarchy" of models from Gaussian distributions with difference variances
40  std::vector<std::shared_ptr<ModPiece>> models(multis->Size());
41  models.at(0) = std::make_shared<Gaussian>(tgtMu, tgtCov*2.0)->AsDensity();
42  models.at(1) = std::make_shared<Gaussian>(tgtMu, tgtCov*1.3)->AsDensity();
43  models.at(2) = std::make_shared<Gaussian>(tgtMu, tgtCov*1.5)->AsDensity();
44  models.at(3) = std::make_shared<Gaussian>(tgtMu, tgtCov)->AsDensity();
45 
46 
47  pt::ptree pt;
48 
49  pt.put("NumSamples", 1e4); // number of samples for single level MCMC
50  pt.put("NumSamples_0_0", 1e5);
51  pt.put("NumSamples_0_1", 1e5);
52  pt.put("NumSamples_0_2", 1e4);
53  pt.put("NumSamples_1_0", 1e5);
54  pt.put("NumSamples_1_1", 1e4);
55  pt.put("NumSamples_1_2", 1e3);
56  pt.put("NumSamples_2_0", 1e4);
57  pt.put("NumSamples_2_1", 1e3);
58  pt.put("NumSamples_2_2", 1e3);
59  pt.put("MLMCMC.Subsampling_0_0", 5);
60  pt.put("MLMCMC.Subsampling_0_1", 5);
61  pt.put("MLMCMC.Subsampling_0_2", 5);
62  pt.put("MLMCMC.Subsampling_1_0", 5);
63  pt.put("MLMCMC.Subsampling_1_1", 5);
64  pt.put("MLMCMC.Subsampling_1_2", 5);
65  pt.put("MLMCMC.Subsampling_2_0", 5);
66  pt.put("MLMCMC.Subsampling_2_1", 5);
67  pt.put("MLMCMC.Subsampling_2_2", 5);
68 
69  pt.put("Proposal.Method", "MHProposal");
70  pt.put("Proposal.ProposalVariance", 16.0);
71 
72  unsigned int numChains = 5;
73  std::vector<std::shared_ptr<MultiIndexEstimator>> estimators(numChains);
74 
75  for(int chainInd=0; chainInd<numChains; ++chainInd){
76  Eigen::VectorXd x0 = RandomGenerator::GetNormal(2);
77 
78  std::cout << "\n=============================\n";
79  std::cout << "Running MIMCMC Chain " << chainInd << ": \n";
80  std::cout << "-----------------------------\n";
81 
82  MIMCMC mimcmc (pt, x0, models, multis);
83  estimators.at(chainInd) = mimcmc.Run();
84 
85  std::cout << "mean QOI: " << estimators.at(chainInd)->Mean().transpose() << std::endl;
86 
87  std::stringstream filename;
88  filename << "MultilevelGaussianSampling_Chain" << chainInd << ".h5";
89  mimcmc.WriteToFile(filename.str());
90  }
91 
92  std::cout << "\n=============================\n";
93  std::cout << "Multilevel Summary: \n";
94  std::cout << "-----------------------------\n";
95  std::cout << " Rhat: " << Diagnostics::Rhat(estimators).transpose() << std::endl;
96  std::cout << " Mean (chain 0): " << estimators.at(0)->Mean().transpose() << std::endl;
97  std::cout << " MCSE (chain 0): " << estimators.at(0)->StandardError().transpose() << std::endl;
98  std::cout << " ESS (chain 0): " << estimators.at(0)->ESS().transpose() << std::endl;
99  std::cout << " Variance (chain 0): " << estimators.at(0)->Variance().transpose() << std::endl;
100  std::cout << std::endl;
101 
102  return 0;
103 }
Multiindex MCMC method.
Definition: MIMCMC.h:21
virtual std::shared_ptr< MultiIndexEstimator > Run()
Definition: MIMCMC.cpp:52
void WriteToFile(std::string filename)
Write HDF5 output for the entire MIMCMC method.
Definition: MIMCMC.cpp:72