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

Monitor for Monte Carlo integration. More...

#include <vsmc/core/monitor.hpp>

Public Types

typedef cxx11::function< void(std::size_t, std::size_t, const Particle< T > &, double *)> eval_type
 
typedef T value_type
 

Public Member Functions

 Monitor (std::size_t dim, const eval_type &eval, bool record_only=false)
 Construct a Monitor with an evaluation object. More...
 
void clear ()
 Clear all records of the index and integrations. More...
 
std::size_t dim () const
 The dimension of the Monitor. More...
 
bool empty () const
 Whether the evaluation object is valid. More...
 
void eval (std::size_t iter, const Particle< T > &particle)
 Perform the evaluation for a given iteration and a Particle<T> object. More...
 
std::size_t index (std::size_t iter) const
 Get the iteration index of the sampler of a given Monitor iteration. More...
 
const std::size_t * index_data () const
 Read only access to the raw data of the index vector. More...
 
std::size_t iter_size () const
 The number of iterations has been recorded. More...
 
std::string & name (std::size_t id)
 Read and write access to the names of variables. More...
 
const std::string & name (std::size_t id) const
 Read only access to the names of variables. More...
 
template<typename OutputIter >
void read_index (OutputIter first) const
 Read the index history through an output iterator. More...
 
template<typename OutputIter >
void read_record (std::size_t id, OutputIter first) const
 Read the record history for a given variable through an output iterator. More...
 
template<typename OutputIterIter >
void read_record_matrix (OutputIterIter first) const
 Read the record history of all variables through an array of output iterators. More...
 
template<MatrixOrder Order, typename OutputIter >
void read_record_matrix (OutputIter first) const
 Read the record history of all variables through an output iterator. More...
 
double record (std::size_t id) const
 Get the latest Monte Carlo integration record of a given variable. More...
 
double record (std::size_t id, std::size_t iter) const
 Get the Monte Carlo integration record of a given variable and the Monitor iteration. More...
 
const double * record_data () const
 Read only access to the raw data of records (a row major matrix) More...
 
bool recording () const
 Whether the Monitor is actively recording results. More...
 
void reserve (std::size_t num)
 Reserve space for a specified number of iterations. More...
 
void set_eval (const eval_type &new_eval)
 Set a new evaluation object of type eval_type. More...
 
void turn_off ()
 Turn off the recording. More...
 
void turn_on ()
 Turn on the recording. More...
 

Detailed Description

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

Monitor for Monte Carlo integration.

Definition at line 56 of file monitor.hpp.

Member Typedef Documentation

template<typename T>
typedef cxx11::function< void (std::size_t, std::size_t, const Particle<T> &, double *)> vsmc::Monitor< T >::eval_type

Definition at line 63 of file monitor.hpp.

template<typename T>
typedef T vsmc::Monitor< T >::value_type

Definition at line 60 of file monitor.hpp.

Constructor & Destructor Documentation

template<typename T>
vsmc::Monitor< T >::Monitor ( std::size_t  dim,
const eval_type eval,
bool  record_only = false 
)
inlineexplicit

Construct a Monitor with an evaluation object.

Parameters
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 instead of calculating them itself

The evaluation object has the signature

void eval (std::size_t iter, std::size_t dim, const Particle<T> &particle, double *result)

where the first three arguments are passed in by the Sampler at the end of each iteration. The evaluation occurs after the possible MCMC moves. The output parameter result shall contain the results of the evaluation.

If record_only is true, then the monitor only records the values stored in result. Otherwise, the behavior is explained below

The array result is of length particle.size() * dim, and it represents a row major matrix of dimension particle.size() by dim, say \(R\). Let \(W\) be the vector of the normalized weights. The Monitor will be respoinsible to compute the importance sampling estimate \(r = R^TW\) and record it. For example, say the purpose of the Monitor is to record the importance sampling estimates of \(E[h(X)]\) where \(h(X) = (h_1(X),\dots,h_d(X))\). Then result shall contain the evaluation of \(h(X_i)\) for each \(i\) from 0 to particle.size() - 1 in the order \((h_1(X_0), \dots, h_d(X_0), h_1(X_1), \dots, h_d(X_1), \dots)\).

After each evaluation, the iteration number iter and the imporatance sampling estimates are recorded and can be retrived by index() and record().

Definition at line 98 of file monitor.hpp.

Member Function Documentation

template<typename T>
void vsmc::Monitor< T >::clear ( )
inline

Clear all records of the index and integrations.

Definition at line 288 of file monitor.hpp.

template<typename T>
std::size_t vsmc::Monitor< T >::dim ( ) const
inline

The dimension of the Monitor.

Definition at line 104 of file monitor.hpp.

template<typename T>
bool vsmc::Monitor< T >::empty ( ) const
inline

Whether the evaluation object is valid.

Definition at line 123 of file monitor.hpp.

template<typename T>
void vsmc::Monitor< T >::eval ( std::size_t  iter,
const Particle< T > &  particle 
)
inline

Perform the evaluation for a given iteration and a Particle<T> object.

This function is called by a Sampler at the end of each iteration. It does nothing if recording() returns false. Otherwise it use the user defined evaluation object to compute results. When a Monitor is constructed, recording() always returns true. It can be turned off by turn_off() and turned on later by turn_on().

Definition at line 261 of file monitor.hpp.

template<typename T>
std::size_t vsmc::Monitor< T >::index ( std::size_t  iter) const
inline

Get the iteration index of the sampler of a given Monitor iteration.

For example, if a Monitor is only added to the sampler at the sampler's iteration siter. Then index(0) will be siter and so on. If the Monitor is added before the sampler's initialization and continued to be evaluated during the iterations without calling turnoff(), then iter(iter) shall just be iter.

Definition at line 156 of file monitor.hpp.

template<typename T>
const std::size_t* vsmc::Monitor< T >::index_data ( ) const
inline

Read only access to the raw data of the index vector.

Definition at line 196 of file monitor.hpp.

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

The number of iterations has been recorded.

This is not necessarily the same as Sampler<T>::iter_size. For example, a Monitor can be added only after a certain time point of the sampler's iterations. Also the Monitor can be turned off for a period during the iterations.

Definition at line 113 of file monitor.hpp.

template<typename T>
std::string& vsmc::Monitor< T >::name ( std::size_t  id)
inline

Read and write access to the names of variables.

By default, each variable of a Monitor is unnamed and the returned string is empty. However, the user can selectively set the names of each variable. This effect how Sampler will print the headers of the summary table.

Definition at line 132 of file monitor.hpp.

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

Read only access to the names of variables.

Definition at line 140 of file monitor.hpp.

template<typename T>
template<typename OutputIter >
void vsmc::Monitor< T >::read_index ( OutputIter  first) const
inline

Read the index history through an output iterator.

Definition at line 192 of file monitor.hpp.

template<typename T>
template<typename OutputIter >
void vsmc::Monitor< T >::read_record ( std::size_t  id,
OutputIter  first 
) const
inline

Read the record history for a given variable through an output iterator.

Definition at line 204 of file monitor.hpp.

template<typename T>
template<typename OutputIterIter >
void vsmc::Monitor< T >::read_record_matrix ( OutputIterIter  first) const
inline

Read the record history of all variables through an array of output iterators.

Parameters
firstAn iterator of container of output iterators

Definition at line 217 of file monitor.hpp.

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

Read the record history of all variables through an output iterator.

Parameters
firstThe output iterator

For example, say first is of type double *, then if order == ColMajor, then, first[j * iter_size() + i] == record(i, j). Otherwise, if order == RowMajor, then first[i * dim() + j] == record(i, j). That is, the output is an iter_size() by dim() matrix, with the usual meaning of column or row major order.

Definition at line 234 of file monitor.hpp.

template<typename T>
double vsmc::Monitor< T >::record ( std::size_t  id) const
inline

Get the latest Monte Carlo integration record of a given variable.

For a dim dimension Monitor, id shall be 0 to dim - 1

Definition at line 168 of file monitor.hpp.

template<typename T>
double vsmc::Monitor< T >::record ( std::size_t  id,
std::size_t  iter 
) const
inline

Get the Monte Carlo integration record of a given variable and the Monitor iteration.

For a dim dimension Monitor, id shall be 0 to dim - 1

Definition at line 182 of file monitor.hpp.

template<typename T>
const double* vsmc::Monitor< T >::record_data ( ) const
inline

Read only access to the raw data of records (a row major matrix)

Definition at line 199 of file monitor.hpp.

template<typename T>
bool vsmc::Monitor< T >::recording ( ) const
inline

Whether the Monitor is actively recording results.

Definition at line 295 of file monitor.hpp.

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

Reserve space for a specified number of iterations.

Definition at line 116 of file monitor.hpp.

template<typename T>
void vsmc::Monitor< T >::set_eval ( const eval_type new_eval)
inline

Set a new evaluation object of type eval_type.

Definition at line 250 of file monitor.hpp.

template<typename T>
void vsmc::Monitor< T >::turn_off ( )
inline

Turn off the recording.

Definition at line 301 of file monitor.hpp.

template<typename T>
void vsmc::Monitor< T >::turn_on ( )
inline

Turn on the recording.

Definition at line 298 of file monitor.hpp.