20 #ifndef SAMPLE_SAMPLER_H_INCLUDED
21 #define SAMPLE_SAMPLER_H_INCLUDED
27 #include <boost/lambda/lambda.hpp>
28 #include <boost/function.hpp>
46 template<
typename Model>
50 typedef boost::function0<void> Callback;
61 const Model& initial_state,
62 double initial_log_target) :
63 m_cur_model(initial_state),
64 m_cur_log_target(initial_log_target)
81 virtual void run(
int num_iterations);
105 return m_cur_log_target;
108 template <
class Recorder>
115 template <
class Recorder>
122 template <
class Callback>
137 template <
class Recorder>
141 return m_record.template get_recorder<Recorder>(
i);
151 if(m_if_accept.empty())
155 m_if_accept = (m_if_accept , cb);
165 if(m_if_reject.empty())
169 m_if_reject = (m_if_reject , cb);
186 if(m_on_accept.empty())
190 m_on_accept = (m_on_accept , cb);
207 if(m_on_reject.empty())
211 m_on_reject = (m_on_reject , cb);
223 double m_cur_log_target;
229 Callback m_if_accept;
230 Callback m_if_reject;
233 Callback m_on_accept;
234 Callback m_on_reject;
239 template<
typename Model>
242 for(
int i = 1;
i <= num_iterations;
i++)
246 m_cur_log_target = log[0].lt;
247 m_record(m_cur_model, log);
249 if(log.back().accept)
251 if(!m_on_accept.empty())
253 if(!m_if_accept.empty())
258 if(!m_on_reject.empty())
260 if(!m_if_reject.empty())
280 template<
typename Model>
303 const Model& initial_state,
304 double initial_log_target) :
305 Parent(initial_state, initial_log_target),
319 template <
class StepType>
322 const Model& initial_state,
323 double initial_log_target) :
324 Parent(initial_state, initial_log_target),
341 const std::vector<Step>& step_list,
342 const std::vector<double>& prob_list,
343 const Model& initial_state,
344 double initial_log_target) :
345 Parent(initial_state, initial_log_target),
351 assert(step_list.size() == prob_list.size());
352 for(
size_t i = 0;
i < step_list.size(); ++
i)
354 add_step(step_list[
i], prob_list[i]);
363 template <
class StepType>
364 void add_step(
const StepType& step,
double prob,
const std::string& name =
"")
368 boost::shared_ptr<StepType> tmp(
new StepType(step));
369 add_step(tmp, prob, name);
378 template <
class StepType>
379 void add_step(StepType* step,
double prob,
const std::string& name =
"")
383 add_step(boost::shared_ptr<StepType>(step,
null_deleter()), prob, name);
391 template <
class StepType>
392 void add_step(
const boost::shared_ptr<StepType>& step,
double prob,
const std::string& name =
"")
394 m_steps.push_back(step);
395 m_step_callbacks.push_back(boost::ref(*step));
396 m_name.push_back(name);
397 m_probabilities.push_back(prob);
403 template <
class StepType>
406 return *boost::any_cast<boost::shared_ptr<StepType> >(m_steps.at(i));
415 assert(m_steps.size() > 0);
416 std::vector<size_t> indices =
kjb::Index_range(0, m_steps.size() - 1).expand();
423 return m_step_callbacks[s];
427 std::vector<boost::any> m_steps;
428 std::vector<Step> m_step_callbacks;
429 std::vector<std::string> m_name;
430 std::vector<double> m_probabilities;
443 template<
typename Model>
464 const Model& initial_state,
465 double initial_log_target) :
466 Parent(initial_state, initial_log_target),
Multi_step_sampler(const std::vector< Step > &step_list, const std::vector< double > &prob_list, const Model &initial_state, double initial_log_target)
Definition: sample_sampler.h:340
void add_record_callback(Callback cb)
Definition: sample_sampler.h:123
Sampler_step< Model >::Type Step
Definition: sample_sampler.h:52
Definition: sample_concept.h:123
const Model & current_state() const
Definition: sample_sampler.h:94
Definition: sample_recorder.h:501
Abstract_sampler< Model >::Step Step
Definition: sample_sampler.h:293
Multi_step_sampler(const StepType &st, const Model &initial_state, double initial_log_target)
Definition: sample_sampler.h:320
void add_step(const StepType &step, double prob, const std::string &name="")
Add a new step with associated probability. This does NOT make sure probabilities add up to 1...
Definition: sample_sampler.h:364
void add_recorder(Recorder r)
Definition: sample_sampler.h:109
Definition: sample_sampler.h:444
Multi_step_sampler(const Model &initial_state, double initial_log_target)
Definition: sample_sampler.h:302
void if_reject(const Callback &cb)
Definition: sample_sampler.h:162
Model & current_state()
Definition: sample_sampler.h:86
void add_step(StepType *step, double prob, const std::string &name="")
Add a new step with associated probability. This does NOT make sure probabilities add up to 1...
Definition: sample_sampler.h:379
const Recorder & get_recorder(size_t i) const
Definition: sample_sampler.h:138
r
Definition: APPgetLargeConnectedEdges.m:127
Definition: sample_base.h:160
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
Vector sample(const MV_gaussian_distribution &dist)
Sample from a multivariate normal distribution.
Definition: prob_sample.cpp:42
virtual const Step & choose_step() const
Chooses a sampler step. Pure virtual method, should be overwritten.
Definition: sample_sampler.h:413
void if_accept(const Callback &cb)
Definition: sample_sampler.h:148
Abstract_sampler< Model > Parent
Definition: sample_sampler.h:289
Definition: sample_sampler.h:47
Abstract_sampler< Model >::Step Step
Definition: sample_sampler.h:449
Single_step_sampler(const Step &step, const Model &initial_state, double initial_log_target)
Constructs an object of this type from a MH step.
Definition: sample_sampler.h:462
void on_accept(const Callback &cb)
Definition: sample_sampler.h:183
void operator()(const void *) const
Definition: sample_sampler.h:286
Model Model_type
Definition: sample_sampler.h:53
Definition: sample_recorder.h:960
void on_reject(const Callback &cb)
Definition: sample_sampler.h:204
double current_log_target() const
Definition: sample_sampler.h:103
Abstract_sampler(const Model &initial_state, double initial_log_target)
Definition: sample_sampler.h:60
void add_step(const boost::shared_ptr< StepType > &step, double prob, const std::string &name="")
Add a new step with associated probability. This does NOT make sure probabilities add up to 1...
Definition: sample_sampler.h:392
Abstract_sampler< Model > Parent
Definition: sample_sampler.h:447
virtual const Step & choose_step() const
Returns the step given in the constructor.
Definition: sample_sampler.h:476
Definition: sample_sampler.h:281
const StepType & get_step(size_t i) const
Definition: sample_sampler.h:404
boost::function2< Step_log< Model >, Model &, double > Type
Definition: sample_base.h:228
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
virtual void run(int num_iterations)
Runs the sampler. This function runs the sampler for the indicated number of iterations. At each iteration it chooses a sampler step using choose_step() and invokes it using its operator(). The best model is saved and returned.
Definition: sample_sampler.h:240
Definition: sample_sampler.h:284
virtual const Step & choose_step() const =0
Chooses a sampler step. Pure virtual method, should be overwritten.
Definition: sample_concept.h:41
void add_recorder(Recorder *r)
Definition: sample_sampler.h:116