15 #include <boost/property_tree/ptree.hpp>
17 namespace pt = boost::property_tree;
24 Eigen::VectorXd mux(1), varx(1);
28 auto px = std::make_shared<Gaussian>(mux,varx)->AsDensity();
30 Eigen::VectorXd muy(2), vary(2);
34 auto py = std::make_shared<Gaussian>(muy,vary)->AsDensity();
36 auto graph = std::make_shared<WorkGraph>();
37 graph->AddNode(px,
"p(x)");
38 graph->AddNode(
py,
"p(y)");
39 graph->AddNode(std::make_shared<DensityProduct>(2),
"p(x,y)");
40 graph->AddEdge(
"p(x)",0,
"p(x,y)",0);
41 graph->AddEdge(
"p(y)",0,
"p(x,y)",1);
43 auto pxy = graph->CreateModPiece(
"p(x,y)");
46 auto problem = std::make_shared<SamplingProblem>(pxy);
49 boost::property_tree::ptree opts;
52 std::vector<std::shared_ptr<TransitionKernel>> kernels(2);
55 opts.put(
"ProposalVariance", 3.0);
56 opts.put(
"BlockIndex", 0);
57 auto propx = std::make_shared<MHProposal>(opts, problem);
58 kernels.at(0) = std::make_shared<MHKernel>(opts, problem, propx);
61 opts.put(
"ProposalVariance", 5.0);
62 opts.put(
"BlockIndex", 1);
63 auto propy = std::make_shared<MHProposal>(opts, problem);
64 kernels.at(1) = std::make_shared<MHKernel>(opts, problem, propy);
67 opts.put(
"NumSamples", 2000);
68 opts.put(
"BurnIn", 100);
69 opts.put(
"PrintLevel", 3);
73 unsigned int numChains = 4;
74 std::vector<std::shared_ptr<SampleCollection>> chains(numChains);
75 std::vector<Eigen::VectorXd> startPt(2);
77 for(
int i=0; i<numChains;++i){
78 startPt.at(0) = RandomGenerator::GetNormal(1);
79 startPt.at(1) = 2.0*RandomGenerator::GetNormal(2);
81 auto sampler = std::make_shared<SingleChainMCMC>(opts, kernels);
82 chains.at(i) = sampler->Run(startPt);
86 Eigen::VectorXd rhat = Diagnostics::Rhat(chains);
87 std::cout <<
"Rhat = " << rhat.transpose() << std::endl;
90 Eigen::VectorXd ess = chains.at(0)->ESS();
91 for(
int i=1; i<numChains; ++i)
92 ess += chains.at(i)->ESS();
94 std::cout <<
"ESS: " << ess.transpose() << std::endl;