97 #include <boost/property_tree/ptree.hpp>
99 namespace pt = boost::property_tree;
127 auto varPiece = std::make_shared<IdentityOperator>(1);
146 Eigen::VectorXd mu(2);
151 std::cout <<
"Gaussian piece has " << gaussDens->inputSizes.size()
152 <<
" inputs with sizes " << gaussDens->inputSizes.transpose() << std::endl;
157 const double alpha = 2.5;
158 const double beta = 1.0;
160 auto varDens = std::make_shared<InverseGamma>(alpha,beta)->AsDensity();
165 auto prodDens = std::make_shared<DensityProduct>(2);
174 auto replOp = std::make_shared<ReplicateOperator>(1,2);
176 auto graph = std::make_shared<WorkGraph>();
178 graph->AddNode(gaussDens,
"Gaussian Density");
179 graph->AddNode(varPiece,
"Variance");
180 graph->AddNode(varDens,
"Variance Density");
181 graph->AddNode(prodDens,
"Joint Density");
182 graph->AddNode(replOp,
"Replicated Variance");
184 graph->AddEdge(
"Variance", 0,
"Replicated Variance", 0);
185 graph->AddEdge(
"Replicated Variance", 0,
"Gaussian Density", 1);
186 graph->AddEdge(
"Variance", 0,
"Variance Density", 0);
188 graph->AddEdge(
"Gaussian Density", 0,
"Joint Density", 0);
189 graph->AddEdge(
"Variance Density", 0,
"Joint Density", 1);
199 graph->Visualize(
"DensityGraph.png");
212 auto jointDens = graph->CreateModPiece(
"Joint Density");
214 auto problem = std::make_shared<SamplingProblem>(jointDens);
234 pt.put(
"NumSamples", 1e5);
235 pt.put(
"BurnIn", 1e4);
236 pt.put(
"PrintLevel",3);
237 pt.put(
"KernelList",
"Kernel1,Kernel2");
239 pt.put(
"Kernel1.Method",
"MHKernel");
240 pt.put(
"Kernel1.Proposal",
"MyProposal");
241 pt.put(
"Kernel1.MyProposal.Method",
"MHProposal");
242 pt.put(
"Kernel1.MyProposal.ProposalVariance", 1.0);
244 pt.put(
"Kernel2.Method",
"MHKernel");
245 pt.put(
"Kernel2.Proposal",
"GammaProposal");
247 pt.put(
"Kernel2.GammaProposal.Method",
"InverseGammaProposal");
248 pt.put(
"Kernel2.GammaProposal.InverseGammaNode",
"Variance Density");
249 pt.put(
"Kernel2.GammaProposal.GaussianNode",
"Gaussian Density");
256 auto mcmc = MCMCFactory::CreateSingleChain(pt, problem);
265 std::vector<Eigen::VectorXd> startPt(2);
267 startPt.at(1) = Eigen::VectorXd::Ones(1);
269 std::shared_ptr<SampleCollection> samps = mcmc->Run(startPt);
275 Eigen::VectorXd sampMean = samps->Mean();
276 std::cout <<
"Sample Mean = \n" << sampMean.transpose() << std::endl;
278 Eigen::VectorXd sampVar = samps->Variance();
279 std::cout <<
"\nSample Variance = \n" << sampVar.transpose() << std::endl;
281 Eigen::MatrixXd sampCov = samps->Covariance();
282 std::cout <<
"\nSample Covariance = \n" << sampCov << std::endl;
284 Eigen::VectorXd sampMom3 = samps->CentralMoment(3);
285 std::cout <<
"\nSample Third Moment = \n" << sampMom3 << std::endl << std::endl;
298 std::vector<std::shared_ptr<SampleCollection>> chains(numChains);
299 chains.at(0) = samps;
301 for(
int i=1; i<numChains; ++i){
302 startPt.at(0) = mu + RandomGenerator::GetNormal(mu.size());
303 startPt.at(1) = RandomGenerator::GetNormal(1).array().exp();
305 mcmc = MCMCFactory::CreateSingleChain(pt, problem);
306 chains.at(i) = mcmc->Run(startPt);
309 Eigen::VectorXd rhat = Diagnostics::Rhat(chains);
310 std::cout <<
"Rhat = " << rhat.transpose() << std::endl;