Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
o2scl::mcmc_base< func_t, measure_t, data_t, vec_t > Class Template Reference

A generic MCMC simulation class. More...

#include <mcmc.h>

Public Member Functions

virtual void set_seed ()
 Default method for setting the random seed.
 
Basic usage
virtual int mcmc (size_t nparams, vec_t &init, vec_t &low, vec_t &high, func_t &func, measure_t &meas)
 Perform an MCMC simulation. More...
 
Proposal distribution
virtual void set_proposal (o2scl::prob_cond_mdim< vec_t > &p)
 Set the proposal distribution.
 
virtual void unset_proposal ()
 Go back to random-walk Metropolis with a uniform distribution.
 

Public Attributes

Output quantities
size_t n_accept
 The number of Metropolis steps which were accepted.
 
size_t n_reject
 The number of Metropolis steps which were rejected.
 
Settings
bool aff_inv
 If true, use affine-invariant Monte Carlo.
 
double step_fac
 Stepsize factor (default 10.0)
 
size_t n_warm_up
 Number of warm up steps (successful steps not iterations) (default 0) More...
 
int user_seed
 If non-zero, use as the seed for the random number generator (default 0)
 
int verbose
 Output control (default 0)
 
size_t max_bad_steps
 Maximum number of failed steps when generating initial points with affine-invariant sampling (default 1000)
 
size_t n_walk
 Number of walkers for affine-invariant MC or 1 otherwise (default 1)
 
bool err_nonconv
 If true, call the error handler if msolve() or msolve_de() does not converge (default true)
 
bool always_accept
 If true, accept all steps.
 
double ai_initial_step
 Initial step fraction for affine-invariance sampling walkers.
 

Static Public Attributes

static const int mcmc_done =-10
 Integer to indicate completion.
 
static const int mcmc_skip =-20
 Integer to indicate rejection.
 

Protected Member Functions

Interface customization
virtual int mcmc_init ()
 Initializations before the MCMC.
 
virtual void mcmc_cleanup ()
 Cleanup after the MCMC.
 
virtual void best_point (vec_t &best, double w_best, data_t &dat)
 Function to run for the best point.
 

Protected Attributes

rng_gsl rg
 Random number generator.
 
o2scl::prob_cond_mdim< vec_t > * prop_dist
 Proposal distribution.
 
bool pd_mode
 If true, then use the user-specified proposal distribution.
 
bool warm_up
 If true, we are in the warm up phase.
 
std::vector< vec_t > current
 Current points in parameter space.
 
std::vector< data_t > data_arr
 Data array.
 
std::vector< bool > switch_arr
 Data switch array.
 
std::vector< size_t > ret_value_counts
 Return value counters.
 
size_t curr_walker
 Index of the current walker.
 
size_t n_init_points
 Number of initial points specified by the user;.
 

Detailed Description

template<class func_t, class measure_t, class data_t, class vec_t = ubvector>
class o2scl::mcmc_base< func_t, measure_t, data_t, vec_t >

This class generates a Markov chain of a user-specified function. The chain can be generated using the Metropolis-Hastings algorithm with a user-specified proposal distribution or using the affine-invariant sampling method of Goodman and Weare.

By default, the Metropolis-Hastings algorithm is executed with a simple walk, with steps in each dimension of size $ (\mathrm{high} - \mathrm{low})/\mathrm{step\_fac} $ with the denominator specified in step_fac.

The function type is a template type, func_t, which should be of the form

int f(size_t num_of_parameters, const vec_t &parameters,
double &log_pdf, data_t &dat)

which computes log_pdf, the natural logarithm of the function value, for any point in parameter space (any point between low and high ).

If the function being simulated returns mcmc_skip then the point is automatically rejected. After each acceptance or rejection, a user-specified "measurement" function (of type measure_t ) is called, which can be used to store the results. In order to stop the simulation, either this function or the probability distribution being simulated should return the value mcmc_done .

A generic proposal distribution can be specified in set_proposal(). To go back to the default random walk method, one can call the function unset_proposal().

If aff_inv is set to true and the number of walkers, n_walk is set to a number larger than 1, then affine-invariant sampling is used. For affine-invariant sampling, the variable step_fac represents the value of $ a $, the limits of the distribution for $ z $.

In order to store data at each point, the user can store this data in any object of type data_t . If affine-invariant sampling is used, then each chain has it's own data object. The class keeps twice as many copies of these data object as would otherwise be required, in order to avoid copying of data objects in the case that the steps are accepted or rejected.

Note
This class is experimental.

Definition at line 103 of file mcmc.h.

Member Function Documentation

◆ mcmc()

template<class func_t, class measure_t, class data_t, class vec_t = ubvector>
virtual int o2scl::mcmc_base< func_t, measure_t, data_t, vec_t >::mcmc ( size_t  nparams,
vec_t &  init,
vec_t &  low,
vec_t &  high,
func_t &  func,
measure_t &  meas 
)
inlinevirtual

Perform an MCMC simulation over nparams parameters starting at initial point init, limiting the parameters to be between low and high, using func as the objective function and calling the measurement function meas at each MC point.

Definition at line 268 of file mcmc.h.

Member Data Documentation

◆ n_warm_up

template<class func_t, class measure_t, class data_t, class vec_t = ubvector>
size_t o2scl::mcmc_base< func_t, measure_t, data_t, vec_t >::n_warm_up
Note
Not to be confused with warm_up, which is a boolean local variable in some functions not an int.

Definition at line 189 of file mcmc.h.


The documentation for this class was generated from the following file:

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).