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