MUQ  0.4.3
DRKernel.h
Go to the documentation of this file.
1 #ifndef DRKERNEL_H_
2 #define DRKERNEL_H_
3 
7 
8 #include <set>
9 
10 namespace muq {
11  namespace SamplingAlgorithms {
12 
63  class DRKernel : public TransitionKernel {
64  public:
65 
66  DRKernel(boost::property_tree::ptree const& pt,
67  std::shared_ptr<AbstractSamplingProblem> problem);
68 
69  DRKernel(boost::property_tree::ptree const& pt,
70  std::shared_ptr<AbstractSamplingProblem> problem,
71  std::vector<std::shared_ptr<MCMCProposal>> proposalsIn,
72  std::vector<double> scales);
73 
74  DRKernel(boost::property_tree::ptree const& pt,
75  std::shared_ptr<AbstractSamplingProblem> problem,
76  std::vector<std::shared_ptr<MCMCProposal>> proposalsIn);
77 
78  virtual ~DRKernel() = default;
79 
83  virtual inline std::vector<std::shared_ptr<MCMCProposal>> Proposals() {return proposals;};
84 
85  virtual void PostStep(unsigned int const t,
86  std::vector<std::shared_ptr<SamplingState>> const& state) override;
87 
88  virtual std::vector<std::shared_ptr<SamplingState>> Step(unsigned int const t,
89  std::shared_ptr<SamplingState> prevState) override;
90 
96  virtual void PrintStatus(std::string prefix) const override;
97 
105  virtual inline Eigen::VectorXd AcceptanceRates() const{return numProposalAccepts.cast<double>().array()/numProposalCalls.cast<double>().array();};
106 
107 
111  std::shared_ptr<SamplingState> SampleProposal(unsigned int stage,
112  std::shared_ptr<SamplingState> const& state) const;
113 
117  double EvaluateProposal(unsigned int stage,
118  std::shared_ptr<SamplingState> const& x,
119  std::shared_ptr<SamplingState> const& y) const;
120 
121 
123  std::vector<double> GetScales() const{return propScales;};
124 
125 
126  virtual void SetBlockInd(int newBlockInd) override;
127 
128 
129  protected:
130 
133  static std::vector<std::shared_ptr<MCMCProposal>> CreateProposals(boost::property_tree::ptree const& pt,
134  std::shared_ptr<AbstractSamplingProblem> const& problem);
135 
142  static std::vector<double> CreateScales(boost::property_tree::ptree const& pt);
143 
144  // A vector containing one proposal for each stage
145  std::vector<std::shared_ptr<MCMCProposal>> proposals;
146 
147  /* A set containing unique proposals (the proposals vector might have
148  duplicates but with different scales, e.g., for DRAM). This unique set
149  is used to make sure that adaptive proposals that used in multiple
150  stages are updated correctly.
151  */
152  std::set<std::shared_ptr<MCMCProposal>> uniqueProps;
153 
154  // Scales for each stage
155  std::vector<double> propScales;
156  bool isScaled = false;
157 
158  // store how many times each proposal has been called
159  Eigen::VectorXi numProposalCalls;
160 
161  // store how many times each proposal has been accepted
162  Eigen::VectorXi numProposalAccepts;
163 
164 
165 
166  template<typename VecType1, typename VecType2>
167  double Alpha(VecType1& likelies, VecType2& proposed_points) const
168  {
169  // create subcontainers of input variables to use in recursion
170 
171  int stage = likelies.size() - 1;
172 
173  double a1 = 1.0;
174  double a2 = 1.0;
175 
176  for (int k = 1; k < stage; ++k) {
177  auto slice1 = muq::Utilities::GetSlice(likelies, stage, stage - k-1,-1);
178  auto slice2 = muq::Utilities::GetSlice(proposed_points, stage, stage - k-1, -1);
179  a2 *= (1.0 - Alpha(slice1, slice2));
180 
181  slice1 = muq::Utilities::GetSlice(likelies, 0, k+1);
182  slice2 = muq::Utilities::GetSlice(proposed_points, 0, k+1);
183  a1 *= (1.0 - Alpha(slice1, slice2)); // forward
184 
185  // if a2==0, there is no chance of getting accepted, so just give up and try again
186  if (a2 == 0)
187  return 0.0;
188  }
189 
190  double q = 0.0;
191 
192  //now put in qs
193 
194  for (int k = 1; k <= stage; ++k) {
195  q += QFun(muq::Utilities::GetSlice(proposed_points, stage, stage - k-1, -1));
196  q -= QFun(muq::Utilities::GetSlice(proposed_points, 0, k+1)); // forward probabilities
197  }
198 
199  return std::min<double>(1.0, exp(likelies[stage] - likelies[0] + q) * a2 / a1);
200  }
201 
202  template<typename VecType>
203  double QFun(VecType const& proposed_points) const
204  {
205  //use the proposal that generated this distance move
206  int stage = proposed_points.size() - 1;
207  return EvaluateProposal(stage - 1, proposed_points[0], proposed_points[stage]);
208  }
209 
210 
211  };
212  } // namespace SamplingAlgorithms
213 } // namespace muq
214 
215 #endif
An implementation of the delayed rejection kernel.
Definition: DRKernel.h:63
std::vector< double > propScales
Definition: DRKernel.h:155
double QFun(VecType const &proposed_points) const
Definition: DRKernel.h:203
double EvaluateProposal(unsigned int stage, std::shared_ptr< SamplingState > const &x, std::shared_ptr< SamplingState > const &y) const
Definition: DRKernel.cpp:81
double Alpha(VecType1 &likelies, VecType2 &proposed_points) const
Definition: DRKernel.h:167
std::set< std::shared_ptr< MCMCProposal > > uniqueProps
Definition: DRKernel.h:152
DRKernel(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > problem)
static std::vector< double > CreateScales(boost::property_tree::ptree const &pt)
Definition: DRKernel.cpp:171
virtual Eigen::VectorXd AcceptanceRates() const
Definition: DRKernel.h:105
Eigen::VectorXi numProposalCalls
Definition: DRKernel.h:159
Eigen::VectorXi numProposalAccepts
Definition: DRKernel.h:162
virtual std::vector< std::shared_ptr< MCMCProposal > > Proposals()
Definition: DRKernel.h:83
std::vector< std::shared_ptr< MCMCProposal > > proposals
Definition: DRKernel.h:145
virtual void SetBlockInd(int newBlockInd) override
Definition: DRKernel.cpp:30
virtual std::vector< std::shared_ptr< SamplingState > > Step(unsigned int const t, std::shared_ptr< SamplingState > prevState) override
Definition: DRKernel.cpp:99
static std::vector< std::shared_ptr< MCMCProposal > > CreateProposals(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > const &problem)
Definition: DRKernel.cpp:228
virtual void PostStep(unsigned int const t, std::vector< std::shared_ptr< SamplingState >> const &state) override
Allow the kernel to adapt given a new state.
Definition: DRKernel.cpp:63
std::shared_ptr< SamplingState > SampleProposal(unsigned int stage, std::shared_ptr< SamplingState > const &state) const
Definition: DRKernel.cpp:71
std::vector< double > GetScales() const
Definition: DRKernel.h:123
DRKernel(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > problem, std::vector< std::shared_ptr< MCMCProposal >> proposalsIn, std::vector< double > scales)
Defines the transition kernel used by an MCMC algorithm.
std::shared_ptr< AbstractSamplingProblem > problem
The sampling problem that evaluates/samples the target distribution.
VectorSlice< std::vector< ScalarType >, ScalarType > GetSlice(std::vector< ScalarType > &dataIn, int startIndIn, int endIndIn, int skipIn=1)
Definition: VectorSlice.h:109