MUQ  0.4.3
GreedyMLMCMC.cpp
Go to the documentation of this file.
3 
5 
6 using namespace muq::SamplingAlgorithms;
7 using namespace muq::Utilities;
8 using namespace muq::Modeling;
9 
10 GreedyMLMCMC::GreedyMLMCMC (boost::property_tree::ptree opts,
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()))
14 {
15 }
16 
17 GreedyMLMCMC::GreedyMLMCMC (boost::property_tree::ptree opts,
18  Eigen::VectorXd const& startPt,
19  std::vector<std::shared_ptr<AbstractSamplingProblem>> const& problems,
20  std::shared_ptr<MultiIndexSet> const& multis) : GreedyMLMCMC(opts, std::make_shared<DefaultComponentFactory>(opts,startPt,ProcessMultis(multis,problems.size()),problems))
21 {
22 }
23 
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)
32 {
33 
34  for (int level = 0; level <= levels; level++) {
35  if (verbosity > 0)
36  std::cout << "Setting up level " << level << std::endl;
37 
38  auto boxHighestIndex = std::make_shared<MultiIndex>(1,level);
39  auto box = std::make_shared<MIMCMCBox>(componentFactory, boxHighestIndex);
40  boxes.push_back(box);
41  }
42 }
43 
44 std::vector<std::shared_ptr<AbstractSamplingProblem>> GreedyMLMCMC::CreateProblems(std::vector<std::shared_ptr<muq::Modeling::ModPiece>> const& models)
45 {
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));
49 
50  return output;
51 }
52 
53 
54 std::shared_ptr<MultiIndexEstimator> GreedyMLMCMC::GetSamples() const {
55  return std::make_shared<MultiIndexEstimator>(boxes);
56 }
57 
58 std::shared_ptr<MultiIndexEstimator> GreedyMLMCMC::GetQOIs() const {
59  return std::make_shared<MultiIndexEstimator>(boxes, true);
60 }
61 
62 std::shared_ptr<MultiIndexEstimator> GreedyMLMCMC::Run() {
63 
64  const int levels = componentFactory->FinestIndex()->GetValue(0);
65 
66  if (verbosity > 0)
67  std::cout << "Computing " << numInitialSamples << " initial samples per level" << std::endl;
68 
69  for (auto box : boxes) {
70  for (int samp = 0; samp < numInitialSamples; samp++) {
71  box->Sample();
72  }
73  }
74  if (verbosity > 0)
75  std::cout << "Initial samples done" << std::endl;
76 
77  while(true) {
78 
79  double var_mle = 0.0;
80  for (int i = 0; i <= levels; i++) {
81 
82  std::shared_ptr<SampleCollection> chain;
83  if(useQOIs){
84  chain = boxes.at(i)->FinestChain()->GetQOIs();
85  }else{
86  chain = boxes.at(i)->FinestChain()->GetSamples();
87  }
88  var_mle += chain->Variance().cwiseQuotient(chain->ESS()).maxCoeff();
89  }
90 
91  if (var_mle <= std::pow(e,2)) {
92  if (verbosity > 0)
93  std::cout << "val_mle " << var_mle << " below " << std::pow(e,2) << std::endl;
94  break;
95  }
96 
97  // Find level with largest payoff ratio
98  int l = -1;
99  double payoff_l = -1;
100  for (int i = 0; i <= levels; i++) {
101  std::shared_ptr<SampleCollection> chain;
102  if(useQOIs){
103  chain = boxes.at(i)->FinestChain()->GetQOIs();
104  }else{
105  chain = boxes.at(i)->FinestChain()->GetSamples();
106  }
107 
108  double my_payoff = chain->Variance().maxCoeff() / boxes.at(i)->FinestChain()->TotalTime();
109 
110  if (my_payoff > payoff_l) {
111  l = i;
112  payoff_l = my_payoff;
113  }
114  }
115 
116  // Beta percent new samples on largest payoff level
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;
122  }
123  int n_samples = std::ceil(weight_sum);
124  int n_new_samples = std::ceil(n_samples * beta);
125 
126  if (verbosity > 0)
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++)
129  boxes[l]->Sample();
130  }
131 
132  if (verbosity > 0) {
133  for (int l = 0; l <= levels; l++)
134  boxes[l]->FinestChain()->PrintStatus("lvl " + std::to_string(l) + " ");
135  }
136 
137  return GetSamples();
138 }
139 
140 std::shared_ptr<MIMCMCBox> GreedyMLMCMC::GetBox(int index) {
141  return boxes[index];
142 }
143 
144 std::vector<std::shared_ptr<MIMCMCBox>> GreedyMLMCMC::GetBoxes() {
145  return boxes;
146 }
147 
148 void GreedyMLMCMC::WriteToFile(std::string filename) {
149  for (auto box : boxes) {
150  box->WriteToFile(filename);
151  }
152 }
153 
154 void GreedyMLMCMC::Draw(bool drawSamples) {
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);
162  }
163  graphfile << "}" << std::endl;
164  graphfile.close();
165 }
166 
167 std::shared_ptr<MultiIndexSet> GreedyMLMCMC::ProcessMultis(std::shared_ptr<MultiIndexSet> const& multis,
168  unsigned int numLevels)
169 {
170  if(multis){
171  return multis;
172  }else{
173  return MultiIndexFactory::CreateFullTensor(1, numLevels-1);
174  }
175 }
Provides a high level interface for the sampling problems on each MIMCMC level.
Greedy Multilevel MCMC method.
Definition: GreedyMLMCMC.h:24
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
Definition: GreedyMLMCMC.h:58
virtual std::shared_ptr< MultiIndexEstimator > Run()
std::vector< std::shared_ptr< MIMCMCBox > > GetBoxes()
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
Definition: GreedyMLMCMC.h:64
Class for sampling problems based purely on a density function.
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.h:3956
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.h:25172