37 #include <boost/property_tree/ptree.hpp>
40 namespace pt = boost::property_tree;
49 MySamplingProblem(std::shared_ptr<muq::Modeling::ModPiece> targetIn)
53 virtual ~MySamplingProblem() =
default;
56 virtual double LogDensity(std::shared_ptr<SamplingState>
const& state)
override {
58 return target->Evaluate(state->state).at(0)(0);
61 virtual std::shared_ptr<SamplingState>
QOI()
override {
62 assert (lastState !=
nullptr);
63 return std::make_shared<SamplingState>(lastState->state, 1.0);
67 std::shared_ptr<SamplingState> lastState =
nullptr;
69 std::shared_ptr<muq::Modeling::ModPiece> target;
76 std::shared_ptr<SamplingState>
Interpolate (std::shared_ptr<SamplingState>
const& coarseProposal,
77 std::shared_ptr<SamplingState>
const& fineProposal) {
78 return std::make_shared<SamplingState>(coarseProposal->state);
86 : startingPoint(start), pt(pt)
89 virtual std::shared_ptr<MCMCProposal>
Proposal (std::shared_ptr<MultiIndex>
const& index, std::shared_ptr<AbstractSamplingProblem>
const& samplingProblem)
override {
91 pt.put(
"BlockIndex",0);
93 Eigen::VectorXd mu(2);
95 Eigen::MatrixXd cov(2,2);
100 auto prior = std::make_shared<Gaussian>(mu, cov);
102 return std::make_shared<CrankNicolsonProposal>(pt, samplingProblem, prior);
106 auto index = std::make_shared<MultiIndex>(1);
107 index->SetValue(0, 3);
111 virtual std::shared_ptr<MCMCProposal>
CoarseProposal (std::shared_ptr<MultiIndex>
const& fineIndex,
112 std::shared_ptr<MultiIndex>
const& coarseIndex,
113 std::shared_ptr<AbstractSamplingProblem>
const& coarseProblem,
114 std::shared_ptr<SingleChainMCMC>
const& coarseChain)
override {
115 pt::ptree ptProposal = pt;
116 ptProposal.put(
"BlockIndex",0);
117 return std::make_shared<SubsamplingMIProposal>(ptProposal, coarseProblem, coarseIndex, coarseChain);
120 virtual std::shared_ptr<AbstractSamplingProblem>
SamplingProblem (std::shared_ptr<MultiIndex>
const& index)
override {
121 Eigen::VectorXd mu(2);
123 Eigen::MatrixXd cov(2,2);
127 if (index->GetValue(0) == 0) {
130 }
else if (index->GetValue(0) == 1) {
133 }
else if (index->GetValue(0) == 2) {
136 }
else if (index->GetValue(0) == 3) {
140 std::cerr <<
"Sampling problem not defined!" << std::endl;
144 auto coarseTargetDensity = std::make_shared<Gaussian>(mu, cov)->AsDensity();
145 return std::make_shared<MySamplingProblem>(coarseTargetDensity);
148 virtual std::shared_ptr<MIInterpolation>
Interpolation (std::shared_ptr<MultiIndex>
const& index)
override {
149 return std::make_shared<MyInterpolation>();
152 virtual Eigen::VectorXd
StartingPoint (std::shared_ptr<MultiIndex>
const& index)
override {
153 return startingPoint;
168 pt.put(
"NumSamples", 1e2);
169 pt.put(
"NumInitialSamples", 1e3);
170 pt.put(
"GreedyTargetVariance", 0.05);
171 pt.put(
"verbosity", 1);
172 pt.put(
"MLMCMC.Subsampling_0", 8);
173 pt.put(
"MLMCMC.Subsampling_1", 4);
174 pt.put(
"MLMCMC.Subsampling_2", 2);
175 pt.put(
"MLMCMC.Subsampling_3", 0);
178 unsigned int numChains = 5;
179 std::vector<std::shared_ptr<MultiIndexEstimator>> estimators(numChains);
181 for(
int chainInd=0; chainInd<numChains; ++chainInd){
182 Eigen::VectorXd x0 = RandomGenerator::GetNormal(2);
183 auto componentFactory = std::make_shared<MyMIComponentFactory>(x0, pt);
185 std::cout <<
"\n=============================\n";
186 std::cout <<
"Running MLMCMC Chain " << chainInd <<
": \n";
187 std::cout <<
"-----------------------------\n";
190 estimators.at(chainInd) = greedymlmcmc.
Run();
192 std::cout <<
"mean QOI: " << estimators.at(chainInd)->Mean().transpose() << std::endl;
194 std::stringstream filename;
195 filename <<
"MultilevelGaussianSampling_Chain" << chainInd <<
".h5";
199 std::cout <<
"\n=============================\n";
200 std::cout <<
"Multilevel Summary: \n";
201 std::cout <<
"-----------------------------\n";
202 std::cout <<
" Rhat: " << Diagnostics::Rhat(estimators).transpose() << std::endl;
203 std::cout <<
" Mean (chain 0): " << estimators.at(0)->Mean().transpose() << std::endl;
204 std::cout <<
" MCSE (chain 0): " << estimators.at(0)->StandardError().transpose() << std::endl;
205 std::cout <<
" ESS (chain 0): " << estimators.at(0)->ESS().transpose() << std::endl;
206 std::cout <<
" Variance (chain 0): " << estimators.at(0)->Variance().transpose() << std::endl;
207 std::cout << std::endl;
std::shared_ptr< SamplingState > Interpolate(std::shared_ptr< SamplingState > const &coarseProposal, std::shared_ptr< SamplingState > const &fineProposal)
virtual Eigen::VectorXd StartingPoint(std::shared_ptr< MultiIndex > const &index) override
virtual std::shared_ptr< MCMCProposal > CoarseProposal(std::shared_ptr< MultiIndex > const &fineIndex, std::shared_ptr< MultiIndex > const &coarseIndex, std::shared_ptr< AbstractSamplingProblem > const &coarseProblem, std::shared_ptr< SingleChainMCMC > const &coarseChain) override
virtual std::shared_ptr< AbstractSamplingProblem > SamplingProblem(std::shared_ptr< MultiIndex > const &index) override
virtual std::shared_ptr< MultiIndex > FinestIndex() override
Eigen::VectorXd startingPoint
virtual std::shared_ptr< MCMCProposal > Proposal(std::shared_ptr< MultiIndex > const &index, std::shared_ptr< AbstractSamplingProblem > const &samplingProblem) override
MyMIComponentFactory(Eigen::VectorXd const &start, pt::ptree pt)
virtual std::shared_ptr< MIInterpolation > Interpolation(std::shared_ptr< MultiIndex > const &index) override
Abstract base class for MCMC and Importance Sampling problems.
Greedy Multilevel MCMC method.
void WriteToFile(std::string filename)
virtual std::shared_ptr< MultiIndexEstimator > Run()
Interface defining models on a multiindex structure.
Interpolation interface combining coarse and fine samples.