Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
o2scl::mcmc_table< func_t, fill_t, data_t, vec_t > Class Template Reference

A generic MCMC simulation class writing data to a o2scl::table_units object. More...

#include <mcmc.h>

Inheritance diagram for o2scl::mcmc_table< func_t, fill_t, data_t, vec_t >:
o2scl::mcmc_base< func_t, std::function< int(const vec_t &, double, size_t, bool, data_t &)>, data_t, vec_t > o2scl::mcmc_mpi< func_t, fill_t, data_t, vec_t > o2scl::mcmc_cli< func_t, fill_t, data_t, vec_t >

Public Member Functions

void reblock (size_t n_blocks)
 Reaverage the data into blocks of a fixed size in order to avoid autocorrelations. More...
 
Basic usage
virtual void set_names_units (std::vector< std::string > names, std::vector< std::string > units)
 Set the table names and units.
 
virtual int mcmc (size_t nparams, vec_t &init, vec_t &low, vec_t &high, func_t &func, fill_t &fill)
 Perform an MCMC simulation. More...
 
std::shared_ptr< o2scl::table_units<> > get_table ()
 Get the output table.
 
void set_table (std::shared_ptr< o2scl::table_units<> > &t)
 Set the output table.
 
virtual int add_line (const vec_t &pars, double log_weight, size_t walker_ix, bool new_meas, data_t &dat, fill_t &fill)
 A measurement function which adds the point to the table.
 
- Public Member Functions inherited from o2scl::mcmc_base< func_t, std::function< int(const vec_t &, double, size_t, bool, data_t &)>, data_t, vec_t >
virtual void set_seed ()
 Default method for setting the random seed.
 
virtual int mcmc (size_t nparams, vec_t &init, vec_t &low, vec_t &high, func_t &func, std::function< int(const vec_t &, double, size_t, bool, data_t &)> &meas)
 Perform an MCMC simulation. More...
 
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.
 

Protected Types

typedef std::function< int(const vec_t &, double, size_t, bool, data_t &)> internal_measure_t
 Measurement functor type for the parent.
 
typedef mcmc_base< func_t, internal_measure_t, data_t, vec_t > parent_t
 Type of parent class.
 

Protected Member Functions

virtual int mcmc_init ()
 MCMC initialization function. More...
 
virtual int fill_line (const vec_t &pars, double log_weight, std::vector< double > &line, data_t &dat, fill_t &fill)
 Fill line with data for insertion into the table.
 
- Protected Member Functions inherited from o2scl::mcmc_base< func_t, std::function< int(const vec_t &, double, size_t, bool, data_t &)>, data_t, vec_t >
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

std::vector< std::string > col_names
 Column names.
 
std::vector< std::string > col_units
 Column units.
 
std::shared_ptr< o2scl::table_units<> > tab
 Main data table for Markov chain.
 
std::vector< int > walker_rows
 Record the last row in the table which corresponds to each walker.
 
- Protected Attributes inherited from o2scl::mcmc_base< func_t, std::function< int(const vec_t &, double, size_t, bool, data_t &)>, data_t, vec_t >
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;.
 

Additional Inherited Members

- Public Attributes inherited from o2scl::mcmc_base< func_t, std::function< int(const vec_t &, double, size_t, bool, data_t &)>, data_t, vec_t >
size_t n_accept
 The number of Metropolis steps which were accepted.
 
size_t n_reject
 The number of Metropolis steps which were rejected.
 
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 inherited from o2scl::mcmc_base< func_t, std::function< int(const vec_t &, double, size_t, bool, data_t &)>, data_t, vec_t >
static const int mcmc_done
 Integer to indicate completion.
 
static const int mcmc_skip
 Integer to indicate rejection.
 

Detailed Description

template<class func_t, class fill_t, class data_t, class vec_t = ubvector>
class o2scl::mcmc_table< func_t, fill_t, data_t, vec_t >

This class performs a MCMC simulation and stores the results in a o2scl::table_units object. The user must specify the column names and units in set_names_units() before mcmc() is called.

The function add_line is the measurement function of type measure_t in the parent. The overloaded function mcmc() in this class works a bit differently in that it takes a function object (type fill_t) of the form

int fill_func(const vec_t &pars, double log_weight,
std::vector<double> &line, data_t &dat);

which should store any auxillary values stored in the data object to line, in order to be added to the table.

The output table will contain the parameters, the logarithm of the function (called "log_wgt") and a multiplying factor called "mult". This "fill" function is called only when a step is accepted and the multiplier for that row is set to 1. If a future step is rejected, then the multiplier is increased by one, rather than adding the same row to the table again.

This class forms the basis of the MCMC used in the Bayesian analysis of neutron star mass and radius in http://github.com/awsteiner/bamr .

Note
This class is experimental.

Definition at line 805 of file mcmc.h.

Member Function Documentation

◆ mcmc()

template<class func_t, class fill_t, class data_t, class vec_t = ubvector>
virtual int o2scl::mcmc_table< func_t, fill_t, data_t, vec_t >::mcmc ( size_t  nparams,
vec_t &  init,
vec_t &  low,
vec_t &  high,
func_t &  func,
fill_t &  fill 
)
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.

Reimplemented in o2scl::mcmc_mpi< func_t, fill_t, data_t, vec_t >.

Definition at line 914 of file mcmc.h.

◆ mcmc_init()

template<class func_t, class fill_t, class data_t, class vec_t = ubvector>
virtual int o2scl::mcmc_table< func_t, fill_t, data_t, vec_t >::mcmc_init ( )
inlineprotectedvirtual

This function sets the column names and units.

Reimplemented from o2scl::mcmc_base< func_t, std::function< int(const vec_t &, double, size_t, bool, data_t &)>, data_t, vec_t >.

Reimplemented in o2scl::mcmc_mpi< func_t, fill_t, data_t, vec_t >.

Definition at line 831 of file mcmc.h.

◆ reblock()

template<class func_t, class fill_t, class data_t, class vec_t = ubvector>
void o2scl::mcmc_table< func_t, fill_t, data_t, vec_t >::reblock ( size_t  n_blocks)
inline
Note
The number of blocks n_blocks must be larger than the current table size. This function expects to find a column named "mult" which contains the multiplicity of each column, as is the case after a call to mcmc_base::mcmc().

This function is useful to remove autocorrelations to the table so long as the autocorrelation length is shorter than the block size. This function does not compute the autocorrelation length to check that this is the case.

Definition at line 1048 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).