KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sample_proposer.h
Go to the documentation of this file.
1 /* $Id: sample_proposer.h 17393 2014-08-23 20:19:14Z predoehl $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek, Ernesto Brau
18  * =========================================================================== */
19 
20 #ifndef SAMPLE_PROPOSER_H_INCLUDED
21 #define SAMPLE_PROPOSER_H_INCLUDED
22 
24 #include "sample_cpp/sample_base.h"
29 #include "prob_cpp/prob_sample.h"
30 #include "prob_cpp/prob_pdf.h"
31 #include "l_cpp/l_exception.h"
32 
33 
34 /* =================================================================================== *
35  * USEFUL, GENERIC METROPOLIS-HASTINGS PROPOSERS *
36  * =================================================================================== */
37 
55 template<typename ConditionalDistribution, typename Model>
57 {
58 public:
60 
61  Conditional_distribution_proposer(const ConditionalDistribution& cond_dist) :
62  cd(cond_dist)
63  {}
64 
70  Mh_proposal_result operator()(const Model& m, Model& mp) const
71  {
72  mp = conditional_sample(cd, m);
73  double fwd = conditional_log_pdf(cd, mp, m);
74  double rev = conditional_log_pdf(cd, m, mp);
75 
76  return Mh_proposal_result(fwd, rev);
77  }
78 
79 private:
80  ConditionalDistribution cd;
81 };
82 
83 
95 template<typename Model>
97 {
98 private:
99  BOOST_CONCEPT_ASSERT((BaseModel<Model>));
100 
101  typedef typename Mh_model_proposer<Model>::Type Proposer;
102 
103 public:
104  typedef std::vector<Proposer> Proposer_vector;
105 
106 private:
107  Proposer_vector m_proposers;
109 
110 public:
116  m_proposers(proposers),
117  m_dist(0, proposers.size() - 1, 1)
118  {}
119 
125  m_proposers(proposers),
126  m_dist(dist)
127  {
128  if(m_proposers.size() != m_dist.size())
129  {
130  KJB_THROW_2(kjb::Illegal_argument, "Multi_proposer_proposer:"
131  "distribution and proposer vector must have same size.");
132  }
133  }
134 
140  Mh_proposal_result operator()(const Model &m, Model& m_p)
141  {
142  //int num_prop = m_proposers.size();
143  double fwd;
144  double rev;
145  bool keep_going = true;
146 
147  while(keep_going)
148  {
149  int j = kjb::sample(m_dist);
150 
151  Mh_proposal_result res = m_proposers[j](m, m_p);
152  if(res.no_change)
153  {
154  fwd = res.fwd_prob + kjb::log_pdf(m_dist, j);
155  rev = res.rev_prob + kjb::log_pdf(m_dist, j);
156  keep_going = false;
157  }
158  }
159 
160  return Mh_proposal_result(fwd, rev);
161  }
162 };
163 
186 template<typename Model>
188 {
189 private:
190  BOOST_CONCEPT_ASSERT((BaseModel<Model>));
191 
192 public:
197 
198  typedef std::vector<Proposer> Proposer_vector;
199 
200 private:
201  Proposer_vector m_proposers;
203  Get_param get_p;
204  Set_param set_p;
205  Dimension m_dim;
206  mutable int last_modified_dim;
207 
208 public:
214  (
215  const Proposer_vector& proposers,
216  const Get_param& get_param = get_vector_model_parameter<Model>,
217  const Set_param& set_param = set_vector_model_parameter<Model>,
218  const Dimension& model_dimension = get_vector_model_dimension<Model>
219  ) :
220  m_proposers(proposers),
221  m_dist(0, proposers.size() - 1, 1),
222  get_p(get_param),
223  set_p(set_param),
224  m_dim(model_dimension),
225  last_modified_dim(-1)
226  {}
227 
233  (
234  const Proposer_vector& proposers,
236  const Get_param& get_param = get_vector_model_parameter<Model>,
237  const Set_param& set_param = set_vector_model_parameter<Model>,
238  const Dimension& model_dimension = get_vector_model_dimension<Model>
239  ) :
240  m_proposers(proposers),
241  m_dist(dist),
242  get_p(get_param),
243  set_p(set_param),
244  m_dim(model_dimension),
245  last_modified_dim(-1)
246  {
247  if(m_proposers.size() != 1 && m_proposers.size() != m_dist.size())
248  {
250  "Single_dimension_proposer: distribution and proposer vector "
251  "must have same size.");
252  }
253  }
254 
260  Mh_proposal_result operator()(const Model &m, Model& m_p) const
261  {
262  int D = m_dim(m);
263  int num_prop = m_proposers.size();
264 
265  if(num_prop != 1)
266  {
267  if(D != num_prop)
268  {
270  "Single_dimension_proposer: model dimension must be "
271  "equal to number of proposers.");
272  }
273  }
274  else
275  {
276  if(D != m_dist.size())
277  {
279  "Single_dimension_proposer: model dimension and "
280  "distribution size must be equal.");
281  }
282  }
283 
284  m_p = m;
285  int j = kjb::sample(m_dist);
286  int k = (D == num_prop) ? j : 0;
287 
288  //Mh_proposal_result res = m_proposers[k](m[j], m_p[j]);
289  double m_j = get_p(m, j);
290  double m_p_j;
291  Mh_proposal_result res = m_proposers[k](m_j, m_p_j);
292  set_p(m_p, j, m_p_j);
293 
294  double fwd = res.fwd_prob + kjb::log_pdf(m_dist, j);
295  double rev = res.rev_prob + kjb::log_pdf(m_dist, j);
296 
297  last_modified_dim = j;
298 
299  return Mh_proposal_result(fwd, rev);
300  }
301 
304  {
305  IFT(last_modified_dim != -1, kjb::KJB_error,
306  "Single dimension proposer: cannot get latest modified dimension; "
307  "proposer has not been called yet.");
308 
309  return last_modified_dim;
310  }
311 };
312 
321 {
322 private:
323  double sigma;
324 
325 public:
327  Gaussian_random_walk_proposer(double variance) : sigma(variance) {}
328 
330  Mh_proposal_result operator()(const double& m, double& mp) const
331  {
332  kjb::Normal_distribution N(m, sqrt(sigma));
333  mp = kjb::sample(N);
334  double p = kjb::pdf(N, mp);
335 
336  return Mh_proposal_result(p, p);
337  }
338 };
339 
340 #endif /*SAMPLE_PROPOSER_H_INCLUDED */
341 
Distribution_traits< Target >::type conditional_sample(const Conditional_distribution< Target, Given, Func > &cd, const typename Distribution_traits< Given >::type &y)
Produces a sample from the given conditioanl distro, given a value for the given variable.
Definition: prob_conditional_distribution.h:135
Conditional_distribution_proposer(const ConditionalDistribution &cond_dist)
Definition: sample_proposer.h:61
double fwd_prob
log forward proposal probability
Definition: sample_base.h:379
Definition of various standard probability distributions.
Mh_proposal_result operator()(const Model &m, Model &m_p) const
Proposes new model.
Definition: sample_proposer.h:260
Indicates the result of an MH proposal. It is simply a pair of probabilities, forward and backward...
Definition: sample_base.h:334
Multi_proposer_proposer(const Proposer_vector &proposers, const kjb::Categorical_distribution< int > &dist)
Constructs a Multi_proposer_proposer with the given proposers and their distribution.
Definition: sample_proposer.h:124
for k
Definition: APPgetLargeConnectedEdges.m:61
Definition: sample_proposer.h:320
Get_model_parameter< Model >::Type Get_param
Definition: sample_proposer.h:194
double pdf(const MV_gaussian_distribution &P, const Vector &x)
Computes the joint PDF of a multivariate Gaussian at x.
Definition: prob_pdf.cpp:32
bool no_change
proposal didn't modify the model
Definition: sample_base.h:386
double rev_prob
log reverse proposal probability
Definition: sample_base.h:381
Vector sample(const MV_gaussian_distribution &dist)
Sample from a multivariate normal distribution.
Definition: prob_sample.cpp:42
double conditional_log_pdf(const Conditional_distribution< Target, Given, Func > &cd, const typename Distribution_traits< Target >::type &x, const typename Distribution_traits< Given >::type &y)
Gets the conditional log-pdf log p(x | y) of the given ConditionalDistribution.
Definition: prob_conditional_distribution.h:120
size_t size() const
Size of this distribution.
Definition: prob_distribution.h:316
Single_dimension_proposer(const Proposer_vector &proposers, const Get_param &get_param=get_vector_model_parameter< Model >, const Set_param &set_param=set_vector_model_parameter< Model >, const Dimension &model_dimension=get_vector_model_dimension< Model >)
Constructs a Single_dimension_proposer with a uniform distribution.
Definition: sample_proposer.h:214
int get_last_modified_dimension() const
Returns the most recently changed dimension of the model.
Definition: sample_proposer.h:303
std::vector< Proposer > Proposer_vector
Definition: sample_proposer.h:198
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
#define IFT(a, ex, msg)
Definition: l_exception.h:101
double dist(const pt a, const pt b)
compute approx. Great Circle distance between two UTM points
Definition: layer.cpp:45
Definition: sample_proposer.h:56
PDFs and CDFs for the different distributions defined in "prob_distribution.h".
double log_pdf(const MV_gaussian_distribution &P, const Vector &x)
Computes the log PDF a multivariate normal distribution at x.
Definition: prob_pdf.cpp:64
boost::function3< void, Model &, size_t, double > Type
Definition: sample_base.h:282
boost::function2< Mh_proposal_result, const Model &, Model & > Type
Definition: sample_base.h:406
Definition of various conditional distibutions.
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Set_model_parameter< Model >::Type Set_param
Definition: sample_proposer.h:195
Exception often thrown when wrapped C functions return error codes.
Definition: l_exception.h:262
Mh_proposal_result operator()(const Model &m, Model &mp) const
Proposes new model.
Definition: sample_proposer.h:70
boost::math::normal Normal_distribution
Definition: prob_distribution.h:68
Sampling functionality for the different distributions defined in "prob_distributions.h".
Definition: sample_proposer.h:187
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
std::vector< Proposer > Proposer_vector
Definition: sample_proposer.h:104
Gaussian_random_walk_proposer(double variance)
Construct a GRWP.
Definition: sample_proposer.h:327
for m
Definition: APPgetLargeConnectedEdges.m:64
D
Definition: APPgetLargeConnectedEdges.m:106
Support for error handling exception classes in libKJB.
Mh_proposal_result operator()(const double &m, double &mp) const
Propose a new value.
Definition: sample_proposer.h:330
boost::function2< double, const Model &, size_t > Type
Definition: sample_base.h:270
Definition: sample_concept.h:41
Definition: sample_proposer.h:96
boost::function1< size_t, const Model & > Type
Definition: sample_base.h:305
Multi_proposer_proposer(const Proposer_vector &proposers)
Constructs a Multi_proposer_proposer with a uniform distribution.
Definition: sample_proposer.h:115
Mh_proposal_result operator()(const Model &m, Model &m_p)
Proposes new model.
Definition: sample_proposer.h:140
Mh_model_proposer< double >::Type Proposer
Definition: sample_proposer.h:193
Model_dimension< Model >::Type Dimension
Definition: sample_proposer.h:196