11 Eigen::VectorXd
const& startPt,
12 std::vector<std::shared_ptr<muq::Modeling::ModPiece>>
const& models,
13 std::shared_ptr<MultiIndexSet>
const& multis) :
GreedyMLMCMC(opts, startPt, CreateProblems(models), ProcessMultis(multis,models.size()))
17 GreedyMLMCMC::GreedyMLMCMC (boost::property_tree::ptree opts,
18 Eigen::VectorXd
const& startPt,
19 std::vector<std::shared_ptr<AbstractSamplingProblem>>
const& problems,
24 GreedyMLMCMC::GreedyMLMCMC (boost::property_tree::ptree opts, std::shared_ptr<MIComponentFactory> componentFactory)
25 : componentFactory(componentFactory),
26 numInitialSamples(opts.
get(
"NumInitialSamples",1000)),
27 e(opts.
get(
"GreedyTargetVariance",0.1)),
28 beta(opts.
get(
"GreedyResamplingFactor",0.5)),
29 levels(componentFactory->FinestIndex()->GetValue(0)),
30 verbosity(opts.
get(
"verbosity",0)),
31 useQOIs(componentFactory->
SamplingProblem(componentFactory->FinestIndex())->numBlocksQOI>0)
34 for (
int level = 0; level <=
levels; level++) {
36 std::cout <<
"Setting up level " << level << std::endl;
38 auto boxHighestIndex = std::make_shared<MultiIndex>(1,level);
44 std::vector<std::shared_ptr<AbstractSamplingProblem>>
GreedyMLMCMC::CreateProblems(std::vector<std::shared_ptr<muq::Modeling::ModPiece>>
const& models)
46 std::vector<std::shared_ptr<AbstractSamplingProblem>> output(models.size());
47 for(
unsigned int i=0; i<models.size(); ++i)
48 output.at(i) = std::make_shared<SamplingProblem>(models.at(i));
55 return std::make_shared<MultiIndexEstimator>(
boxes);
59 return std::make_shared<MultiIndexEstimator>(
boxes,
true);
67 std::cout <<
"Computing " <<
numInitialSamples <<
" initial samples per level" << std::endl;
69 for (
auto box :
boxes) {
75 std::cout <<
"Initial samples done" << std::endl;
80 for (
int i = 0; i <=
levels; i++) {
82 std::shared_ptr<SampleCollection> chain;
84 chain =
boxes.at(i)->FinestChain()->GetQOIs();
86 chain =
boxes.at(i)->FinestChain()->GetSamples();
88 var_mle += chain->Variance().cwiseQuotient(chain->ESS()).maxCoeff();
91 if (var_mle <= std::pow(
e,2)) {
93 std::cout <<
"val_mle " << var_mle <<
" below " << std::pow(
e,2) << std::endl;
100 for (
int i = 0; i <=
levels; i++) {
101 std::shared_ptr<SampleCollection> chain;
103 chain =
boxes.at(i)->FinestChain()->GetQOIs();
105 chain =
boxes.at(i)->FinestChain()->GetSamples();
108 double my_payoff = chain->Variance().maxCoeff() /
boxes.at(i)->FinestChain()->TotalTime();
110 if (my_payoff > payoff_l) {
112 payoff_l = my_payoff;
117 double weight_sum = 0.0;
118 auto finestChain =
boxes[l]->FinestChain();
119 for (
int s = 0; s < finestChain->GetSamples()->size(); s++) {
120 std::shared_ptr<SamplingState> sample = finestChain->GetSamples()->at(s);
121 weight_sum += sample->weight;
123 int n_samples = std::ceil(weight_sum);
124 int n_new_samples = std::ceil(n_samples *
beta);
127 std::cout <<
"var_mle " << var_mle <<
"\t" << n_new_samples <<
" new samples on level " << l << std::endl;
128 for (
int i = 0; i < n_new_samples; i++)
133 for (
int l = 0; l <=
levels; l++)
149 for (
auto box :
boxes) {
150 box->WriteToFile(filename);
155 std::ofstream graphfile;
156 graphfile.open (
"graph");
157 graphfile <<
"digraph {" << std::endl;
158 graphfile <<
"nodesep=1.2;" << std::endl;
159 graphfile <<
"splines=false;" << std::endl;
160 for (
auto box :
boxes) {
161 box->Draw(graphfile, drawSamples);
163 graphfile <<
"}" << std::endl;
168 unsigned int numLevels)
173 return MultiIndexFactory::CreateFullTensor(1, numLevels-1);
Provides a high level interface for the sampling problems on each MIMCMC level.
Greedy Multilevel MCMC method.
static std::shared_ptr< MultiIndexSet > ProcessMultis(std::shared_ptr< MultiIndexSet > const &multis, unsigned int numLevels)
virtual std::shared_ptr< MultiIndexEstimator > GetQOIs() const
void WriteToFile(std::string filename)
GreedyMLMCMC(boost::property_tree::ptree pt, Eigen::VectorXd const &startPt, std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &models, std::shared_ptr< MultiIndexSet > const &multis=nullptr)
std::shared_ptr< MIMCMCBox > GetBox(int index)
std::shared_ptr< MIComponentFactory > componentFactory
virtual std::shared_ptr< MultiIndexEstimator > Run()
std::vector< std::shared_ptr< MIMCMCBox > > GetBoxes()
const int numInitialSamples
void Draw(bool drawSamples=true)
virtual std::shared_ptr< MultiIndexEstimator > GetSamples() const
static std::vector< std::shared_ptr< AbstractSamplingProblem > > CreateProblems(std::vector< std::shared_ptr< muq::Modeling::ModPiece >> const &models)
std::vector< std::shared_ptr< MIMCMCBox > > boxes
Class for sampling problems based purely on a density function.
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values