MUQ  0.4.3
ParallelProblem.h
Go to the documentation of this file.
2 
3 class MySamplingProblem : public AbstractSamplingProblem {
4 public:
5  MySamplingProblem(std::shared_ptr<muq::Modeling::ModPiece> targetIn)
6  : AbstractSamplingProblem(Eigen::VectorXi::Constant(1,2), Eigen::VectorXi::Constant(1,2)),
7  target(targetIn){}
8 
9  virtual ~MySamplingProblem() = default;
10 
11 
12  virtual double LogDensity(std::shared_ptr<SamplingState> const& state) override {
13  lastState = state;
14  return target->Evaluate(state->state).at(0)(0);
15  };
16 
17  virtual std::shared_ptr<SamplingState> QOI() override {
18  assert (lastState != nullptr);
19  return std::make_shared<SamplingState>(lastState->state, 1.0);
20  }
21 
22 private:
23  std::shared_ptr<SamplingState> lastState = nullptr;
24 
25  std::shared_ptr<muq::Modeling::ModPiece> target;
26 
27 };
28 
29 
30 class MyInterpolation : public MIInterpolation {
31 public:
32  std::shared_ptr<SamplingState> Interpolate (std::shared_ptr<SamplingState> const& coarseProposal, std::shared_ptr<SamplingState> const& fineProposal) {
33  return std::make_shared<SamplingState>(coarseProposal->state);
34  }
35 };
36 
37 class MyMIComponentFactory : public ParallelizableMIComponentFactory {
38 public:
40  : pt(pt)
41  { }
42 
43  void SetComm(std::shared_ptr<parcer::Communicator> const& comm) override {
44 
45  }
46 
47  virtual std::shared_ptr<MCMCProposal> Proposal (std::shared_ptr<MultiIndex> const& index, std::shared_ptr<AbstractSamplingProblem> const& samplingProblem) override {
48  pt::ptree pt;
49  pt.put("BlockIndex",0);
50 
51  Eigen::VectorXd mu(2);
52  mu << 1.0, 2.0;
53  Eigen::MatrixXd cov(2,2);
54  cov << 0.7, 0.6,
55  0.6, 1.0;
56  cov *= 20.0;
57 
58  auto prior = std::make_shared<Gaussian>(mu, cov);
59 
60  return std::make_shared<CrankNicolsonProposal>(pt, samplingProblem, prior);
61  }
62 
63  virtual std::shared_ptr<MultiIndex> FinestIndex() override {
64  auto index = std::make_shared<MultiIndex>(1);
65  index->SetValue(0, 1);
66  return index;
67  }
68 
69  virtual std::shared_ptr<MCMCProposal> CoarseProposal (std::shared_ptr<MultiIndex> const& fineIndex,
70  std::shared_ptr<MultiIndex> const& coarseIndex,
71  std::shared_ptr<AbstractSamplingProblem> const& coarseProblem,
72  std::shared_ptr<SingleChainMCMC> const& coarseChain) override {
73  pt::ptree ptProposal = pt;
74  pt.put("BlockIndex",0);
75  return std::make_shared<SubsamplingMIProposal>(ptProposal, coarseProblem, coarseIndex, coarseChain);
76  }
77 
78  virtual std::shared_ptr<AbstractSamplingProblem> SamplingProblem (std::shared_ptr<MultiIndex> const& index) override {
79  Eigen::VectorXd mu(2);
80  mu << 1.0, 2.0;
81  Eigen::MatrixXd cov(2,2);
82  cov << 0.7, 0.6,
83  0.6, 1.0;
84 
85  if (index->GetValue(0) == 0) {
86  mu *= 0.8;
87  cov *= 2.0;
88  } else if (index->GetValue(0) == 1) {
89  mu *= 0.9;
90  cov *= 1.5;
91  } else if (index->GetValue(0) == 2) {
92  mu *= 0.99;
93  cov *= 1.1;
94  } else if (index->GetValue(0) == 3) {
95  mu *= 1.0;
96  cov *= 1.0;
97  } else {
98  std::cerr << "Sampling problem not defined!" << std::endl;
99  assert (false);
100  }
101 
102  auto coarseTargetDensity = std::make_shared<Gaussian>(mu, cov)->AsDensity();
103  return std::make_shared<MySamplingProblem>(coarseTargetDensity);
104  }
105 
106  virtual std::shared_ptr<MIInterpolation> Interpolation (std::shared_ptr<MultiIndex> const& index) override {
107  return std::make_shared<MyInterpolation>();
108  }
109 
110  virtual Eigen::VectorXd StartingPoint (std::shared_ptr<MultiIndex> const& index) override {
111  Eigen::VectorXd mu(2);
112  mu << 1.0, 2.0;
113  return mu;
114  }
115 
116 private:
117  pt::ptree pt;
118 };
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
void SetComm(std::shared_ptr< parcer::Communicator > const &comm) override
virtual std::shared_ptr< AbstractSamplingProblem > SamplingProblem(std::shared_ptr< MultiIndex > const &index) override
virtual std::shared_ptr< MultiIndex > FinestIndex() override
virtual std::shared_ptr< MCMCProposal > Proposal(std::shared_ptr< MultiIndex > const &index, std::shared_ptr< AbstractSamplingProblem > const &samplingProblem) override
MyMIComponentFactory(pt::ptree pt)
virtual std::shared_ptr< MIInterpolation > Interpolation(std::shared_ptr< MultiIndex > const &index) override