20 #ifndef SAMPLE_DEFAULT_H_INCLUDED
21 #define SAMPLE_DEFAULT_H_INCLUDED
26 #include <boost/bind.hpp>
75 evals(static_cast<int>(size), evaluation)
98 template<
typename Model>
127 l_target(log_target),
128 move_parameter(move_param),
129 get_neighborhood(get_neighbors),
130 get_dimension(get_dim)
144 gradient_curvature_dispatch_(q, boost::none, result);
151 gradient_curvature_dispatch_(q, result, boost::none);
157 gradient_curvature_dispatch_(q, gradient, curvature);
166 void gradient_curvature_dispatch_(
168 boost::optional<kjb::Vector&>
gradient,
169 boost::optional<kjb::Vector&>
curvature)
const;
180 template<
typename Model>
184 if(!gradient && !curvature)
return;
187 size_t dim = get_dimension(q2);
191 (*gradient).resize(dim);
193 (*curvature).resize(dim);
198 t_center = l_target(q2);
201 for(
size_t i = 0;
i < dim;
i++)
203 double eta = etas[
i];
212 (*gradient)[
i] = 0.0;
214 (*curvature)[
i] = 0.0;
219 move_parameter(q2,
i, eta);
220 double t_right = l_target(q2);
223 move_parameter(q2,
i, -2 * eta);
224 double t_left = l_target(q2);
228 (*gradient)[
i] = (t_right - t_left) / (2 * eta);
229 assert((*gradient)[
i] == (*gradient)[
i]);
232 (*curvature)[
i] = ( t_left - 2*t_center + t_right) / (eta * eta);
235 move_parameter(q2,
i, etas[
i]);
245 template<
typename Model>
263 const Target& target,
264 const Get_upper_bounds& get_upper,
265 const Get_lower_bounds& get_lower
268 m_get_upper_bounds(get_upper),
269 m_get_lower_bounds(get_lower)
285 return m_get_upper_bounds(m);
293 return m_get_lower_bounds(m);
298 Get_upper_bounds m_get_upper_bounds;
299 Get_lower_bounds m_get_lower_bounds;
319 template <
class Model>
326 Model_eval m_likelihood;
328 bool m_short_circuit;
329 double m_annealing_compensation;
331 Posterior(
const Model_eval& prior,
const Model_eval& likelihood) :
333 m_likelihood(likelihood),
334 m_short_circuit(false),
335 m_annealing_compensation(1.0)
340 m_short_circuit = sc;
345 double p = m_prior(m);
346 if(m_short_circuit && p < -DBL_MAX)
353 p *= m_annealing_compensation;
355 return p + m_likelihood(m);
370 m_annealing_compensation = temperature;
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