38 #include <boost/property_tree/ptree.hpp>
41 namespace pt = boost::property_tree;
53 unsigned int numLevels = 4;
54 std::vector<std::shared_ptr<ModPiece>> logDensities(numLevels);
56 Eigen::VectorXd tgtMu(2);
57 Eigen::MatrixXd tgtCov(2,2);
63 Eigen::VectorXd levelMu = 0.8*tgtMu;
64 Eigen::MatrixXd levelCov = 2.0*tgtCov;
65 logDensities.at(0) = std::make_shared<Gaussian>(levelMu, levelCov)->AsDensity();
69 levelCov = 1.5*tgtCov;
70 logDensities.at(1) = std::make_shared<Gaussian>(levelMu, levelCov)->AsDensity();
74 levelCov = 1.1*tgtCov;
75 logDensities.at(2) = std::make_shared<Gaussian>(levelMu, levelCov)->AsDensity();
78 logDensities.at(3) = std::make_shared<Gaussian>(tgtMu, tgtCov)->AsDensity();
90 options.put(
"NumInitialSamples", 1000);
91 options.put(
"GreedyTargetVariance", 0.05);
92 options.put(
"verbosity", 1);
93 options.put(
"MLMCMC.Subsampling_0", 8);
94 options.put(
"MLMCMC.Subsampling_1", 4);
95 options.put(
"MLMCMC.Subsampling_2", 2);
96 options.put(
"MLMCMC.Subsampling_3", 0);
98 options.put(
"Proposal.Method",
"MHProposal");
99 options.put(
"Proposal.ProposalVariance", 16.0);
102 unsigned int numChains = 5;
103 std::vector<std::shared_ptr<MultiIndexEstimator>> estimators(numChains);
105 for(
int chainInd=0; chainInd<numChains; ++chainInd){
106 Eigen::VectorXd x0 = RandomGenerator::GetNormal(2);
108 std::cout <<
"\n=============================\n";
109 std::cout <<
"Running MLMCMC Chain " << chainInd <<
": \n";
110 std::cout <<
"-----------------------------\n";
113 estimators.at(chainInd) = sampler.
Run();
115 std::cout <<
"Chain " << chainInd <<
" Mean: " << estimators.at(chainInd)->Mean().transpose() << std::endl;
116 std::cout <<
"Chain " << chainInd <<
" Variance: " << estimators.at(chainInd)->Variance().transpose() << std::endl;
118 std::stringstream filename;
119 filename <<
"MultilevelGaussianSampling_Chain" << chainInd <<
".h5";
125 unsigned int numCalls = logDensities.back()->GetNumCalls();
127 std::cout <<
"\n=============================\n";
128 std::cout <<
"Multilevel Summary: \n";
129 std::cout <<
"-----------------------------\n";
130 std::cout <<
" Rhat: " << Diagnostics::Rhat(estimators).transpose() << std::endl;
131 std::cout <<
" Mean (chain 0): " << estimators.at(0)->Mean().transpose() << std::endl;
132 std::cout <<
" MCSE (chain 0): " << estimators.at(0)->StandardError().transpose() << std::endl;
133 std::cout <<
" ESS (chain 0): " << estimators.at(0)->ESS().transpose() << std::endl;
134 std::cout <<
" Variance (chain 0): " << estimators.at(0)->Variance().transpose() << std::endl;
135 std::cout <<
" Finest evals: " << logDensities.back()->GetNumCalls() << std::endl;
136 std::cout << std::endl;
140 std::cout <<
"\n=============================\n";
141 std::cout <<
"Running Single Level Chain" <<
": \n";
142 std::cout <<
"-----------------------------\n";
145 auto problem = std::make_shared<SamplingProblem>(logDensities.back());
149 pt.put(
"NumSamples", numCalls);
151 pt.put(
"PrintLevel",3);
152 pt.put(
"KernelList",
"Kernel1");
153 pt.put(
"Kernel1.Method",
"MHKernel");
154 pt.put(
"Kernel1.Proposal",
"MyProposal");
155 pt.put(
"Kernel1.MyProposal.Method",
"MHProposal");
156 pt.put(
"Kernel1.MyProposal.ProposalVariance", 4.0);
158 Eigen::VectorXd startPt(2);
160 auto mcmc = std::make_shared<SingleChainMCMC>(pt, problem);
162 auto samps = mcmc->Run(startPt);
164 std::cout <<
"\n=============================\n";
165 std::cout <<
"Single Level Summary: \n";
166 std::cout <<
"-----------------------------\n";
167 std::cout <<
" Mean: " << samps->Mean().transpose() << std::endl;
168 std::cout <<
" MCSE: " << samps->StandardError().transpose() << std::endl;
169 std::cout <<
" ESS: " << samps->ESS().transpose() << std::endl;
170 std::cout <<
" Variance: " << samps->Variance().transpose() << std::endl;
171 std::cout <<
" Finest evals: " << logDensities.back()->GetNumCalls()- numCalls << std::endl;
172 std::cout << std::endl;
std::vector< std::shared_ptr< ModPiece > > ConstructDensities()
Greedy Multilevel MCMC method.
void WriteToFile(std::string filename)
virtual std::shared_ptr< MultiIndexEstimator > Run()