KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sample_default.h
Go to the documentation of this file.
1 /* $Id: sample_default.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_DEFAULT_H_INCLUDED
21 #define SAMPLE_DEFAULT_H_INCLUDED
22 
24 #include "sample_cpp/sample_base.h"
25 #include "l_cpp/l_exception.h"
26 #include <boost/bind.hpp>
27 
28 
29 /* =================================================================================== *
30  * DEFAULT GENERAL FUNCTIONS
31  * =================================================================================== */
32 
36 template<class Model>
38 {
41 
43  set_p(set), get_p(get)
44  {}
45 
46  void operator()(Model& m, size_t i, double dx) const
47  {
48  double x = get_p(m, i);
49  set_p(m, i, x + dx);
50  }
51 
54 };
55 
56 
57 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
58 
59 /* =================================================================================== *
60  * USEFUL, GENERIC PARAMETER EVALUATORS -- MOST LIKELY USED FOR PARAMETERS
61  * =================================================================================== */
62 
67 template<class Model>
69 {
71  evals(evaluations)
72  {}
73 
74  Constant_parameter_evaluator(double evaluation, size_t size) :
75  evals(static_cast<int>(size), evaluation)
76  {}
77 
78  const kjb::Vector& operator()(const Model& /*m*/) const
79  {
80  return evals;
81  }
82 
84 };
85 
86 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
87 
88 /* =================================================================================== *
89  * DEFAULTS RELATED TO HYBRID MC AND STOCHASTIC DYNAMICS
90  * =================================================================================== */
91 
98 template<typename Model>
100 {
101 public:
106 
121  (
122  const Target_distribution& log_target,
123  const Move_parameter& move_param,
124  const Get_neighborhood& get_neighbors,
125  const Get_dimension& get_dim
126  ) :
127  l_target(log_target),
128  move_parameter(move_param),
129  get_neighborhood(get_neighbors),
130  get_dimension(get_dim)
131  {}
132 
136  kjb::Vector operator()(const Model& q) const
137  {
138  return gradient(q);
139  }
140 
141  kjb::Vector curvature(const Model& q) const
142  {
143  kjb::Vector result;
144  gradient_curvature_dispatch_(q, boost::none, result);
145  return result;
146  }
147 
148  kjb::Vector gradient (const Model& q) const
149  {
150  kjb::Vector result;
151  gradient_curvature_dispatch_(q, result, boost::none);
152  return result;
153  }
154 
156  {
157  gradient_curvature_dispatch_(q, gradient, curvature);
158  }
159 
160 private:
166  void gradient_curvature_dispatch_(
167  const Model& q,
168  boost::optional<kjb::Vector&> gradient,
169  boost::optional<kjb::Vector&> curvature) const;
170 
171 private:
172  Target_distribution l_target;
173  Move_parameter move_parameter;
174  Get_neighborhood get_neighborhood;
175  Get_dimension get_dimension;
176 };
177 
178 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
179 
180 template<typename Model>
181 void Numerical_gradient<Model>::gradient_curvature_dispatch_(const Model& q, boost::optional<kjb::Vector&> gradient, boost::optional<kjb::Vector&> curvature) const
182 {
183  // if neither curvature or gradient is requested, do nothing
184  if(!gradient && !curvature) return;
185 
186  Model q2 = q;
187  size_t dim = get_dimension(q2);
188  kjb::Vector etas = get_neighborhood(q2);
189 
190  if(gradient)
191  (*gradient).resize(dim);
192  if(curvature)
193  (*curvature).resize(dim);
194 
195  double t_center = 0;
196 
197  if(curvature)
198  t_center = l_target(q2);
199 
200 
201  for(size_t i = 0; i < dim; i++)
202  {
203  double eta = etas[i];
204  // 0.0 is a sentinal value meaning "this
205  // dimension will be ignored".
206  //
207  //
208  // So ignore it.
209  if(eta == 0)
210  {
211  if(gradient)
212  (*gradient)[i] = 0.0;
213  if(curvature)
214  (*curvature)[i] = 0.0;
215  continue;
216  }
217 
218  // compute value of target at q + eta
219  move_parameter(q2, i, eta);
220  double t_right = l_target(q2);
221 
222  // compute value of target at q - eta
223  move_parameter(q2, i, -2 * eta);
224  double t_left = l_target(q2);
225 
226  if(gradient)
227  {
228  (*gradient)[i] = (t_right - t_left) / (2 * eta);
229  assert((*gradient)[i] == (*gradient)[i]); // nan test
230  }
231  if(curvature)
232  (*curvature)[i] = ( t_left - 2*t_center + t_right) / (eta * eta);
233 
234  // move it back to its original position
235  move_parameter(q2, i, etas[i]);
236  }
237 }
238 
239 
240 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
241 
245 template<typename Model>
247 {
248 private:
249  typedef typename Model_evaluator<Model>::Type Target;
250  typedef typename Model_parameter_evaluator<Model>::Type Get_upper_bounds;
251  typedef typename Model_parameter_evaluator<Model>::Type Get_lower_bounds;
252 
253 public:
262  (
263  const Target& target,
264  const Get_upper_bounds& get_upper,
265  const Get_lower_bounds& get_lower
266  ) :
267  m_target(target),
268  m_get_upper_bounds(get_upper),
269  m_get_lower_bounds(get_lower)
270  {}
271 
275  double operator()(const Model& m) const
276  {
277  return m_target(m);
278  }
279 
283  kjb::Vector get_upper_bounds(const Model& m) const
284  {
285  return m_get_upper_bounds(m);
286  }
287 
291  kjb::Vector get_lower_bounds(const Model& m) const
292  {
293  return m_get_lower_bounds(m);
294  }
295 
296 private:
297  Target m_target;
298  Get_upper_bounds m_get_upper_bounds;
299  Get_lower_bounds m_get_lower_bounds;
300 };
301 
302 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
303 
304 /* =================================================================================== *
305  * OTHER DEFAULTS
306  * =================================================================================== */
307 
319 template <class Model>
321 {
322 private:
323  typedef typename Model_evaluator<Model>::Type Model_eval;
324 
325  Model_eval m_prior;
326  Model_eval m_likelihood;
327 
328  bool m_short_circuit;
329  double m_annealing_compensation;
330 public:
331  Posterior(const Model_eval& prior, const Model_eval& likelihood) :
332  m_prior(prior),
333  m_likelihood(likelihood),
334  m_short_circuit(false),
335  m_annealing_compensation(1.0)
336  {}
337 
338  void set_short_circuiting(bool sc)
339  {
340  m_short_circuit = sc;
341  }
342 
343  double operator()(const Model& m) const
344  {
345  double p = m_prior(m);
346  if(m_short_circuit && p < -DBL_MAX)
347  {
348  return p;
349  }
350 
351  // multiply by annealing temperature to cancel-out the prior being divided
352  // by temperature inside the sampling step
353  p *= m_annealing_compensation;
354 
355  return p + m_likelihood(m);
356  }
357 
368  void set_annealing_compensation(double temperature)
369  {
370  m_annealing_compensation = temperature;
371  }
372 };
373 
374 #endif /*SAMPLE_DEFAULT_H_INCLUDED */
375 
kjb::Vector get_lower_bounds(const Model &m) const
Return the vector of lower bounds for a given model.
Definition: sample_default.h:291
Constant_parameter_evaluator(const kjb::Vector &evaluations)
Definition: sample_default.h:70
Set_model_parameter< Model >::Type Set_param
Definition: sample_default.h:39
Posterior(const Model_eval &prior, const Model_eval &likelihood)
Definition: sample_default.h:331
void move_param(Model &x, size_t i, double dv, const Adapter &aptr)
Helper function that moves a parameter by an amount.
Definition: diff_util.h:102
Adapts a target distribution to be one with bounds.
Definition: sample_default.h:246
Constrained_target(const Target &target, const Get_upper_bounds &get_upper, const Get_lower_bounds &get_lower)
Construct this target distribution.
Definition: sample_default.h:262
Numerical_gradient(const Target_distribution &log_target, const Move_parameter &move_param, const Get_neighborhood &get_neighbors, const Get_dimension &get_dim)
Construct a gradient approximation functor.
Definition: sample_default.h:121
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
kjb::Vector curvature(const Model &q) const
Definition: sample_default.h:141
Get_param get_p
Definition: sample_default.h:53
kjb::Vector get_upper_bounds(const Model &m) const
Return the vector of upper bounds for a given model.
Definition: sample_default.h:283
kjb::Vector evals
Definition: sample_default.h:83
Move_model_parameter< Model >::Type Move_parameter
Definition: sample_default.h:103
Model_parameter_evaluator< Model >::Type Get_neighborhood
Definition: sample_default.h:104
void set_annealing_compensation(double temperature)
Definition: sample_default.h:368
Default move function; uses '+'.
Definition: sample_default.h:37
void gradient_and_curvature(const Model &q, kjb::Vector &gradient, kjb::Vector &curvature) const
Definition: sample_default.h:155
x
Definition: APPgetLargeConnectedEdges.m:100
Model_evaluator< Model >::Type Target_distribution
Definition: sample_default.h:102
double operator()(const Model &m) const
Definition: sample_default.h:343
boost::function3< void, Model &, size_t, double > Type
Definition: sample_base.h:282
boost::function1< double, const Model & > Type
Definition: sample_base.h:214
void operator()(Model &m, size_t i, double dx) const
Definition: sample_default.h:46
kjb::Vector operator()(const Model &q) const
Evaluates gradient at q.
Definition: sample_default.h:136
Returns the same result no matter what model is received.
Definition: sample_default.h:68
boost::function1< kjb::Vector, const Model & > Type
Definition: sample_base.h:317
boost::function3< void, Model &, size_t, double > Type
Definition: sample_base.h:294
Constant_parameter_evaluator(double evaluation, size_t size)
Definition: sample_default.h:74
Move_model_parameter_as_plus(const Set_param &set, const Get_param &get)
Definition: sample_default.h:42
double operator()(const Model &m) const
Apply the target distribution to a model.
Definition: sample_default.h:275
Model_dimension< Model >::Type Get_dimension
Definition: sample_default.h:105
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
for m
Definition: APPgetLargeConnectedEdges.m:64
Generic posterior class.
Definition: sample_default.h:320
Get_model_parameter< Model >::Type Get_param
Definition: sample_default.h:40
Support for error handling exception classes in libKJB.
boost::function2< double, const Model &, size_t > Type
Definition: sample_base.h:270
const kjb::Vector & operator()(const Model &) const
Definition: sample_default.h:78
boost::function1< size_t, const Model & > Type
Definition: sample_base.h:305
kjb::Vector gradient(const Model &q) const
Definition: sample_default.h:148
Approximates the gradient and/or curvature of a target distribution, evaluated at a certain location...
Definition: sample_default.h:99
void set_short_circuiting(bool sc)
Definition: sample_default.h:338
Set_param set_p
Definition: sample_default.h:52