vSMC
vSMC: Scalable Monte Carlo
Public Types | Public Member Functions | Static Public Member Functions | List of all members
vsmc::Sampler< T > Class Template Reference

SMC Sampler. More...

#include <vsmc/core/sampler.hpp>

Public Types

using init_type = std::function< std::size_t(Particle< T > &, void *)>
 
using mcmc_type = std::function< std::size_t(std::size_t, Particle< T > &)>
 
using monitor_map_type = std::map< std::string, Monitor< T >>
 
using move_type = std::function< std::size_t(std::size_t, Particle< T > &)>
 
using resample_type = typename Particle< T >::resample_type
 
using size_type = typename Particle< T >::size_type
 
using value_type = T
 

Public Member Functions

 Sampler (size_type N)
 Construct a Sampler without selection of resampling method. More...
 
 Sampler (size_type N, ResampleScheme scheme)
 Construct a Sampler with a built-in resampling scheme. More...
 
 Sampler (size_type N, const resample_type &res_op)
 Construct a Sampler with a user defined resampling operation. More...
 
 Sampler (size_type N, ResampleScheme scheme, double resample_threshold)
 Construct a Sampler with a built-in resampling scheme and a threshold for resampling. More...
 
 Sampler (size_type N, const resample_type &res_op, double resample_threshold)
 Construct a Sampler with a user defined resampling scheme and a threshold for resampling. More...
 
std::size_t accept_history (std::size_t id, std::size_t iter) const
 Get the accept count of a given move id and the iteration. More...
 
bool clear_monitor (const std::string &name)
 Erase a named monitor. More...
 
Sampler< T > & clear_monitor ()
 Erase all monitors. More...
 
Sampler< T > clone (bool new_rng) const
 Clone the sampler system except the RNG engines. More...
 
Sampler< T > & clone (const Sampler< T > &other, bool retain_rng)
 Clone another sampler system except the RNG engines. More...
 
Sampler< T > & clone (Sampler< T > &&other, bool retain_rng)
 
double ess_history (std::size_t iter) const
 Get ESS of a given iteration, initialization count as iter 0. More...
 
Sampler< T > & init (const init_type &new_init)
 Set the initialization object of type init_type. More...
 
Sampler< T > & init_by_iter (bool initialize_by_iterate)
 Set if initialization should use the move and mcmc queue. More...
 
Sampler< T > & init_by_move (const move_type &new_init)
 Set the initialization object with a type move_type object. More...
 
Sampler< T > & initialize (void *param=nullptr)
 Initialization. More...
 
std::size_t iter_num () const
 Current iteration number (initialization count as zero) More...
 
std::size_t iter_size () const
 Number of iterations (including initialization) More...
 
Sampler< T > & iterate (std::size_t num=1)
 Iteration. More...
 
Sampler< T > & mcmc (const mcmc_type &new_mcmc, bool append)
 Add a new mcmc. More...
 
template<typename InputIter >
Sampler< T > & mcmc (InputIter first, InputIter last, bool append)
 Add a sequence of new mcmcs. More...
 
Sampler< T > & mcmc_queue_clear ()
 Clear the mcmc queue. More...
 
bool mcmc_queue_empty () const
 Check if mcmc queue is empty. More...
 
std::size_t mcmc_queue_size () const
 Check the size of the mcmc queue. More...
 
Sampler< T > & monitor (const std::string &name, const Monitor< T > &mon)
 Add a monitor. More...
 
Sampler< T > & monitor (const std::string &name, std::size_t dim, const typename Monitor< T >::eval_type &eval, bool record_only=false, MonitorStage stage=MonitorMCMC)
 Add a monitor with an evaluation object. More...
 
Monitor< T > & monitor (const std::string &name)
 Read and write access to a named monitor. More...
 
const Monitor< T > & monitor (const std::string &name) const
 Read only access to a named monitor. More...
 
monitor_map_typemonitor ()
 Read and write access to all monitors to the monitor_map_type object. More...
 
const monitor_map_typemonitor () const
 Read only access to all monitors to the the monitor_map_type object. More...
 
Sampler< T > & move (const move_type &new_move, bool append)
 Add a new move. More...
 
template<typename InputIter >
Sampler< T > & move (InputIter first, InputIter last, bool append)
 Add a sequence of new moves. More...
 
Sampler< T > & move_queue_clear ()
 Clear the move queue. More...
 
bool move_queue_empty () const
 Check if move queue is empty. More...
 
std::size_t move_queue_size () const
 Check the size of the move queue. More...
 
Particle< T > & particle ()
 Read and write access to the Particle<T> object. More...
 
const Particle< T > & particle () const
 Read only access to the Particle<T> object. More...
 
Path< T > & path ()
 Read and write access to the Path sampling monitor. More...
 
const Path< T > & path () const
 Read only access to the Path sampling monitor. More...
 
Sampler< T > & path_sampling (const typename Path< T >::eval_type &eval, bool record_only=false)
 Set the Path sampling evaluation object. More...
 
double path_sampling () const
 Path sampling estimate of the logarithm of normalizing constants ratio. More...
 
template<typename CharT , typename Traits >
std::basic_ostream< CharT, Traits > & print (std::basic_ostream< CharT, Traits > &os, char sepchar= '\t') const
 Print the history of the Sampler. More...
 
template<typename OutputIter >
void read_ess_history (OutputIter first) const
 Read ESS history through an output iterator. More...
 
template<typename OutputIter >
void read_resampled_history (OutputIter first) const
 Read resampling indicator history through an output iterator. More...
 
template<typename OutputIter >
void read_size_history (OutputIter first) const
 Read sampler size history through an output iterator. More...
 
Sampler< T > & resample ()
 Force resample. More...
 
Sampler< T > & resample_scheme (const resample_type &res_op)
 Set resampling method by a resample_type object. More...
 
Sampler< T > & resample_scheme (ResampleScheme scheme)
 Set resampling method by a built-in ResampleScheme scheme name. More...
 
double resample_threshold () const
 Get resampling threshold. More...
 
Sampler< T > & resample_threshold (double threshold)
 Set resampling threshold. More...
 
bool resampled_history (std::size_t iter) const
 Get resampling indicator of a given iteration. More...
 
void reserve (std::size_t num)
 Reserve space for a specified number of iterations. More...
 
size_type size () const
 Number of particles. More...
 
double size_history (std::size_t iter) const
 Get sampler size of a given iteration (initialization count as iteration zero) More...
 
template<MatrixOrder Order, typename OutputIter >
void summary_data (OutputIter first) const
 Sampler summary data (floating point data) More...
 
template<MatrixOrder Order, typename OutputIter >
void summary_data_int (OutputIter first) const
 Sampler summary data (integer data) More...
 
std::size_t summary_data_size () const
 The size of Sampler summary data (floating point data) More...
 
std::size_t summary_data_size_int () const
 The size of Sampler summary data (integer data) More...
 
template<typename OutputIter >
void summary_header (OutputIter first) const
 Sampler summary header (floating point data) More...
 
template<typename OutputIter >
void summary_header_int (OutputIter first) const
 Sampler summary header (integer data) More...
 
std::size_t summary_header_size () const
 The size of Sampler summary header (floating point data) More...
 
std::size_t summary_header_size_int () const
 The size of Sampler summary header (integer data, size etc.) More...
 

Static Public Member Functions

static double resample_threshold_always ()
 Special value of resampling threshold that indicate no resampling will always be performed. More...
 
static double resample_threshold_never ()
 Special value of resampling threshold that indicate no resampling will be ever performed. More...
 

Detailed Description

template<typename T>
class vsmc::Sampler< T >

SMC Sampler.

Definition at line 59 of file sampler.hpp.

Member Typedef Documentation

template<typename T>
using vsmc::Sampler< T >::init_type = std::function<std::size_t(Particle<T> &, void *)>

Definition at line 65 of file sampler.hpp.

template<typename T>
using vsmc::Sampler< T >::mcmc_type = std::function<std::size_t(std::size_t, Particle<T> &)>

Definition at line 67 of file sampler.hpp.

template<typename T>
using vsmc::Sampler< T >::monitor_map_type = std::map<std::string, Monitor<T>>

Definition at line 68 of file sampler.hpp.

template<typename T>
using vsmc::Sampler< T >::move_type = std::function<std::size_t(std::size_t, Particle<T> &)>

Definition at line 66 of file sampler.hpp.

template<typename T>
using vsmc::Sampler< T >::resample_type = typename Particle<T>::resample_type

Definition at line 63 of file sampler.hpp.

template<typename T>
using vsmc::Sampler< T >::size_type = typename Particle<T>::size_type

Definition at line 62 of file sampler.hpp.

template<typename T>
using vsmc::Sampler< T >::value_type = T

Definition at line 64 of file sampler.hpp.

Constructor & Destructor Documentation

template<typename T>
vsmc::Sampler< T >::Sampler ( size_type  N)
inlineexplicit

Construct a Sampler without selection of resampling method.

Definition at line 71 of file sampler.hpp.

template<typename T>
vsmc::Sampler< T >::Sampler ( size_type  N,
ResampleScheme  scheme 
)
inline

Construct a Sampler with a built-in resampling scheme.

By default, resampling will be performed at every iteration.

Definition at line 85 of file sampler.hpp.

template<typename T>
vsmc::Sampler< T >::Sampler ( size_type  N,
const resample_type res_op 
)
inline

Construct a Sampler with a user defined resampling operation.

By default, resampling will be performed at every iteration.

Definition at line 99 of file sampler.hpp.

template<typename T>
vsmc::Sampler< T >::Sampler ( size_type  N,
ResampleScheme  scheme,
double  resample_threshold 
)
inline

Construct a Sampler with a built-in resampling scheme and a threshold for resampling.

Definition at line 111 of file sampler.hpp.

template<typename T>
vsmc::Sampler< T >::Sampler ( size_type  N,
const resample_type res_op,
double  resample_threshold 
)
inline

Construct a Sampler with a user defined resampling scheme and a threshold for resampling.

Definition at line 123 of file sampler.hpp.

Member Function Documentation

template<typename T>
std::size_t vsmc::Sampler< T >::accept_history ( std::size_t  id,
std::size_t  iter 
) const
inline

Get the accept count of a given move id and the iteration.

Definition at line 313 of file sampler.hpp.

template<typename T>
bool vsmc::Sampler< T >::clear_monitor ( const std::string &  name)
inline

Erase a named monitor.

Definition at line 566 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::clear_monitor ( )
inline

Erase all monitors.

Definition at line 573 of file sampler.hpp.

template<typename T>
Sampler<T> vsmc::Sampler< T >::clone ( bool  new_rng) const
inline

Clone the sampler system except the RNG engines.

Parameters
new_rngIf true, the new particle system has new-seeded RNG. Otherwise false, it is exactly the same as the current.

Definition at line 138 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::clone ( const Sampler< T > &  other,
bool  retain_rng 
)
inline

Clone another sampler system except the RNG engines.

Parameters
otherThe particle system to be cloned
retain_rngIf true, retain the current system's RNG. Otherwise, it is exactly the same as the new one.

Definition at line 154 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::clone ( Sampler< T > &&  other,
bool  retain_rng 
)
inline

Definition at line 174 of file sampler.hpp.

template<typename T>
double vsmc::Sampler< T >::ess_history ( std::size_t  iter) const
inline

Get ESS of a given iteration, initialization count as iter 0.

Definition at line 290 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::init ( const init_type new_init)
inline

Set the initialization object of type init_type.

Definition at line 325 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::init_by_iter ( bool  initialize_by_iterate)
inline

Set if initialization should use the move and mcmc queue.

If set to false, then the initialization step use the initialization object if it is not empty. Otherwise, it perform the same steps as the iteration step.

Definition at line 340 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::init_by_move ( const move_type new_init)
inline

Set the initialization object with a type move_type object.

When called, the iteration parameter passed to this object will be 0 and the void * parameter will be ignored.

Definition at line 352 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::initialize ( void *  param = nullptr)
inline

Initialization.

Parameters
paramAdditional parameters passed to the initialization object of type init_type

All histories (ESS, resampled, accept, Monitor and Path) are clared before callling the initialization object. Monitors and Path's evaluation objects are untouched.

Definition at line 451 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::iter_num ( ) const
inline

Current iteration number (initialization count as zero)

Definition at line 216 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::iter_size ( ) const
inline

Number of iterations (including initialization)

Definition at line 213 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::iterate ( std::size_t  num = 1)
inline

Iteration.

Moves performed first. Then ESS/N is compared to the threshold and possible resampling is performed. Then mcmcs are performed. Then monitors and Path are computed

Definition at line 472 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::mcmc ( const mcmc_type new_mcmc,
bool  append 
)
inline

Add a new mcmc.

Definition at line 417 of file sampler.hpp.

template<typename T>
template<typename InputIter >
Sampler<T>& vsmc::Sampler< T >::mcmc ( InputIter  first,
InputIter  last,
bool  append 
)
inline

Add a sequence of new mcmcs.

Definition at line 430 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::mcmc_queue_clear ( )
inline

Clear the mcmc queue.

Definition at line 403 of file sampler.hpp.

template<typename T>
bool vsmc::Sampler< T >::mcmc_queue_empty ( ) const
inline

Check if mcmc queue is empty.

Definition at line 411 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::mcmc_queue_size ( ) const
inline

Check the size of the mcmc queue.

Definition at line 414 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::monitor ( const std::string &  name,
const Monitor< T > &  mon 
)
inline

Add a monitor.

Parameters
nameThe name of the monitor
monThe new monitor to be added

Definition at line 510 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::monitor ( const std::string &  name,
std::size_t  dim,
const typename Monitor< T >::eval_type &  eval,
bool  record_only = false,
MonitorStage  stage = MonitorMCMC 
)
inline

Add a monitor with an evaluation object.

Parameters
nameThe name of the Monitor
dimThe dimension of the Monitor, i.e., the number of variables
evalThe evaluation object of type Monitor::eval_type
record_onlyThe Monitor only records results
stageThe stage of the Monitor
See also
Monitor

Definition at line 526 of file sampler.hpp.

template<typename T>
Monitor<T>& vsmc::Sampler< T >::monitor ( const std::string &  name)
inline

Read and write access to a named monitor.

Definition at line 537 of file sampler.hpp.

template<typename T>
const Monitor<T>& vsmc::Sampler< T >::monitor ( const std::string &  name) const
inline

Read only access to a named monitor.

Definition at line 547 of file sampler.hpp.

template<typename T>
monitor_map_type& vsmc::Sampler< T >::monitor ( )
inline

Read and write access to all monitors to the monitor_map_type object.

Definition at line 559 of file sampler.hpp.

template<typename T>
const monitor_map_type& vsmc::Sampler< T >::monitor ( ) const
inline

Read only access to all monitors to the the monitor_map_type object.

Definition at line 563 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::move ( const move_type new_move,
bool  append 
)
inline

Add a new move.

Definition at line 376 of file sampler.hpp.

template<typename T>
template<typename InputIter >
Sampler<T>& vsmc::Sampler< T >::move ( InputIter  first,
InputIter  last,
bool  append 
)
inline

Add a sequence of new moves.

Definition at line 389 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::move_queue_clear ( )
inline

Clear the move queue.

Definition at line 363 of file sampler.hpp.

template<typename T>
bool vsmc::Sampler< T >::move_queue_empty ( ) const
inline

Check if move queue is empty.

Definition at line 370 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::move_queue_size ( ) const
inline

Check the size of the move queue.

Definition at line 373 of file sampler.hpp.

template<typename T>
Particle<T>& vsmc::Sampler< T >::particle ( )
inline

Read and write access to the Particle<T> object.

Definition at line 319 of file sampler.hpp.

template<typename T>
const Particle<T>& vsmc::Sampler< T >::particle ( ) const
inline

Read only access to the Particle<T> object.

Definition at line 322 of file sampler.hpp.

template<typename T>
Path<T>& vsmc::Sampler< T >::path ( )
inline

Read and write access to the Path sampling monitor.

Definition at line 487 of file sampler.hpp.

template<typename T>
const Path<T>& vsmc::Sampler< T >::path ( ) const
inline

Read only access to the Path sampling monitor.

Definition at line 490 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::path_sampling ( const typename Path< T >::eval_type &  eval,
bool  record_only = false 
)
inline

Set the Path sampling evaluation object.

Definition at line 493 of file sampler.hpp.

template<typename T>
double vsmc::Sampler< T >::path_sampling ( ) const
inline

Path sampling estimate of the logarithm of normalizing constants ratio.

Definition at line 504 of file sampler.hpp.

template<typename T>
template<typename CharT , typename Traits >
std::basic_ostream<CharT, Traits>& vsmc::Sampler< T >::print ( std::basic_ostream< CharT, Traits > &  os,
char  sepchar = '\t' 
) const
inline

Print the history of the Sampler.

Parameters
osThe ostream to which the contents are printed
sepcharThe seperator of fields

Definition at line 686 of file sampler.hpp.

template<typename T>
template<typename OutputIter >
void vsmc::Sampler< T >::read_ess_history ( OutputIter  first) const
inline

Read ESS history through an output iterator.

Definition at line 294 of file sampler.hpp.

template<typename T>
template<typename OutputIter >
void vsmc::Sampler< T >::read_resampled_history ( OutputIter  first) const
inline

Read resampling indicator history through an output iterator.

Definition at line 307 of file sampler.hpp.

template<typename T>
template<typename OutputIter >
void vsmc::Sampler< T >::read_size_history ( OutputIter  first) const
inline

Read sampler size history through an output iterator.

Definition at line 284 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::resample ( )
inline

Force resample.

Definition at line 219 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::resample_scheme ( const resample_type res_op)
inline

Set resampling method by a resample_type object.

Definition at line 227 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::resample_scheme ( ResampleScheme  scheme)
inline

Set resampling method by a built-in ResampleScheme scheme name.

Definition at line 236 of file sampler.hpp.

template<typename T>
double vsmc::Sampler< T >::resample_threshold ( ) const
inline

Get resampling threshold.

Definition at line 255 of file sampler.hpp.

template<typename T>
Sampler<T>& vsmc::Sampler< T >::resample_threshold ( double  threshold)
inline

Set resampling threshold.

Definition at line 258 of file sampler.hpp.

template<typename T>
static double vsmc::Sampler< T >::resample_threshold_always ( )
inlinestatic

Special value of resampling threshold that indicate no resampling will always be performed.

Definition at line 273 of file sampler.hpp.

template<typename T>
static double vsmc::Sampler< T >::resample_threshold_never ( )
inlinestatic

Special value of resampling threshold that indicate no resampling will be ever performed.

Definition at line 266 of file sampler.hpp.

template<typename T>
bool vsmc::Sampler< T >::resampled_history ( std::size_t  iter) const
inline

Get resampling indicator of a given iteration.

Definition at line 300 of file sampler.hpp.

template<typename T>
void vsmc::Sampler< T >::reserve ( std::size_t  num)
inline

Reserve space for a specified number of iterations.

Definition at line 198 of file sampler.hpp.

template<typename T>
size_type vsmc::Sampler< T >::size ( ) const
inline

Number of particles.

Definition at line 195 of file sampler.hpp.

template<typename T>
double vsmc::Sampler< T >::size_history ( std::size_t  iter) const
inline

Get sampler size of a given iteration (initialization count as iteration zero)

Definition at line 280 of file sampler.hpp.

template<typename T>
template<MatrixOrder Order, typename OutputIter >
void vsmc::Sampler< T >::summary_data ( OutputIter  first) const
inline

Sampler summary data (floating point data)

Definition at line 670 of file sampler.hpp.

template<typename T>
template<MatrixOrder Order, typename OutputIter >
void vsmc::Sampler< T >::summary_data_int ( OutputIter  first) const
inline

Sampler summary data (integer data)

Definition at line 657 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::summary_data_size ( ) const
inline

The size of Sampler summary data (floating point data)

Definition at line 650 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::summary_data_size_int ( ) const
inline

The size of Sampler summary data (integer data)

Definition at line 644 of file sampler.hpp.

template<typename T>
template<typename OutputIter >
void vsmc::Sampler< T >::summary_header ( OutputIter  first) const
inline

Sampler summary header (floating point data)

Definition at line 620 of file sampler.hpp.

template<typename T>
template<typename OutputIter >
void vsmc::Sampler< T >::summary_header_int ( OutputIter  first) const
inline

Sampler summary header (integer data)

Definition at line 607 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::summary_header_size ( ) const
inline

The size of Sampler summary header (floating point data)

Definition at line 590 of file sampler.hpp.

template<typename T>
std::size_t vsmc::Sampler< T >::summary_header_size_int ( ) const
inline

The size of Sampler summary header (integer data, size etc.)

Definition at line 581 of file sampler.hpp.