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_prop;
49  pt_prop.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_prop, samplingProblem, prior);
61  }
62 
63  virtual std::shared_ptr<MultiIndex> FinestIndex() override {
64  auto index = std::make_shared<MultiIndex>(2);
65  index->SetValue(0, 2);
66  index->SetValue(1, 2);
67  return index;
68  }
69 
70  virtual std::shared_ptr<MCMCProposal> CoarseProposal (std::shared_ptr<MultiIndex> const& fineIndex,
71  std::shared_ptr<MultiIndex> const& coarseIndex,
72  std::shared_ptr<AbstractSamplingProblem> const& coarseProblem,
73  std::shared_ptr<SingleChainMCMC> const& coarseChain) override {
74  pt::ptree ptProposal = pt;
75  ptProposal.put("BlockIndex",0);
76  return std::make_shared<SubsamplingMIProposal>(ptProposal, coarseProblem, coarseIndex, coarseChain);
77  }
78 
79  virtual std::shared_ptr<AbstractSamplingProblem> SamplingProblem (std::shared_ptr<MultiIndex> const& index) override {
80  Eigen::VectorXd mu(2);
81  mu << 1.0, 2.0;
82  Eigen::MatrixXd cov(2,2);
83  cov << 0.7, 0.6,
84  0.6, 1.0;
85 
86  if (index->GetValue(1) == 0) {
87  mu[1] *= 0.95;
88  cov *= 1.3;
89  } else if (index->GetValue(1) == 1) {
90  mu[1] *= 0.99;
91  cov *= 1.1;
92  } else if (index->GetValue(1) == 2) {
93  mu[1] *= 1.0;
94  cov *= 1.0;
95  } else {
96  std::cerr << "Sampling problem not defined!" << std::endl;
97  assert (false);
98  }
99  if (index->GetValue(0) == 0) {
100  mu[0] *= 0.95;
101  cov *= 1.3;
102  } else if (index->GetValue(0) == 1) {
103  mu[0] *= 0.99;
104  cov *= 1.1;
105  } else if (index->GetValue(0) == 2) {
106  mu[0] *= 1.0;
107  cov *= 1.0;
108  } else {
109  std::cerr << "Sampling problem not defined!" << std::endl;
110  assert (false);
111  }
112 
113  auto coarseTargetDensity = std::make_shared<Gaussian>(mu, cov)->AsDensity();
114  return std::make_shared<MySamplingProblem>(coarseTargetDensity);
115  }
116 
117  virtual std::shared_ptr<MIInterpolation> Interpolation (std::shared_ptr<MultiIndex> const& index) override {
118  return std::make_shared<MyInterpolation>();
119  }
120 
121  virtual Eigen::VectorXd StartingPoint (std::shared_ptr<MultiIndex> const& index) override {
122  Eigen::VectorXd mu(2);
123  mu << 1.0, 2.0;
124  return mu;
125  }
126 
127 private:
128  pt::ptree pt;
129 };
130 
131 
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