MUQ  0.4.3
Problem.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 MIComponentFactory {
38 public:
39  MyMIComponentFactory (Eigen::VectorXd const& start, pt::ptree pt)
40  : startingPoint(start), pt(pt)
41  { }
42 
43  virtual std::shared_ptr<MCMCProposal> Proposal (std::shared_ptr<MultiIndex> const& index, std::shared_ptr<AbstractSamplingProblem> const& samplingProblem) override {
44  pt::ptree pt;
45  pt.put("BlockIndex",0);
46 
47  Eigen::VectorXd mu(2);
48  mu << 1.0, 2.0;
49  Eigen::MatrixXd cov(2,2);
50  cov << 0.7, 0.6,
51  0.6, 1.0;
52  cov *= 20.0;
53 
54  auto prior = std::make_shared<Gaussian>(mu, cov);
55 
56  return std::make_shared<CrankNicolsonProposal>(pt, samplingProblem, prior);
57  }
58 
59  virtual std::shared_ptr<MultiIndex> FinestIndex() override {
60  auto index = std::make_shared<MultiIndex>(1);
61  index->SetValue(0, 3);
62  return index;
63  }
64 
65  virtual std::shared_ptr<MCMCProposal> CoarseProposal (std::shared_ptr<MultiIndex> const& fineIndex,
66  std::shared_ptr<MultiIndex> const& coarseIndex,
67  std::shared_ptr<AbstractSamplingProblem> const& coarseProblem,
68  std::shared_ptr<SingleChainMCMC> const& coarseChain) override {
69  pt::ptree ptProposal = pt;
70  ptProposal.put("BlockIndex",0);
71  return std::make_shared<SubsamplingMIProposal>(ptProposal, coarseProblem, coarseIndex, coarseChain);
72  }
73 
74  virtual std::shared_ptr<AbstractSamplingProblem> SamplingProblem (std::shared_ptr<MultiIndex> const& index) override {
75  Eigen::VectorXd mu(2);
76  mu << 1.0, 2.0;
77  Eigen::MatrixXd cov(2,2);
78  cov << 0.7, 0.6,
79  0.6, 1.0;
80 
81  if (index->GetValue(0) == 0) {
82  mu *= 0.8;
83  cov *= 2.0;
84  } else if (index->GetValue(0) == 1) {
85  mu *= 0.9;
86  cov *= 1.5;
87  } else if (index->GetValue(0) == 2) {
88  mu *= 0.99;
89  cov *= 1.1;
90  } else if (index->GetValue(0) == 3) {
91  mu *= 1.0;
92  cov *= 1.0;
93  } else {
94  std::cerr << "Sampling problem not defined!" << std::endl;
95  assert (false);
96  }
97 
98  auto coarseTargetDensity = std::make_shared<Gaussian>(mu, cov)->AsDensity();
99  return std::make_shared<MySamplingProblem>(coarseTargetDensity);
100  }
101 
102  virtual std::shared_ptr<MIInterpolation> Interpolation (std::shared_ptr<MultiIndex> const& index) override {
103  return std::make_shared<MyInterpolation>();
104  }
105 
106  virtual Eigen::VectorXd StartingPoint (std::shared_ptr<MultiIndex> const& index) override {
107  return startingPoint;
108  }
109 
110 private:
111  Eigen::VectorXd startingPoint;
112  pt::ptree pt;
113 };
114 
115 
std::shared_ptr< SamplingState > Interpolate(std::shared_ptr< SamplingState > const &coarseProposal, std::shared_ptr< SamplingState > const &fineProposal)
Definition: Problem.h:32
virtual Eigen::VectorXd StartingPoint(std::shared_ptr< MultiIndex > const &index) override
Definition: Problem.h:106
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
Definition: Problem.h:65
virtual std::shared_ptr< AbstractSamplingProblem > SamplingProblem(std::shared_ptr< MultiIndex > const &index) override
Definition: Problem.h:74
virtual std::shared_ptr< MultiIndex > FinestIndex() override
Definition: Problem.h:59
virtual std::shared_ptr< MCMCProposal > Proposal(std::shared_ptr< MultiIndex > const &index, std::shared_ptr< AbstractSamplingProblem > const &samplingProblem) override
Definition: Problem.h:43
MyMIComponentFactory(Eigen::VectorXd const &start, pt::ptree pt)
Definition: Problem.h:39
virtual std::shared_ptr< MIInterpolation > Interpolation(std::shared_ptr< MultiIndex > const &index) override
Definition: Problem.h:102