A generic MCMC simulation class writing data to a o2scl::table_units object. More...
#include <mcmc_para.h>
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
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.
There is some output which occurs in addition to the output from o2scl::mcmc_para_base depending on the value of o2scl::mcmc_para_base::verbose . If there is a misalignment between the number of columns in the table and the number of data points in any line, then some debugging information is sent to cout
. When verbose is 2 or larger,
Verbose output may need improvement
Use reorder_table() and possibly reblock() to create a full post-processing function.
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
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.
There is some output which occurs in addition to the output from o2scl::mcmc_para_base depending on the value of o2scl::mcmc_para_base::verbose . If there is a misalignment between the number of columns in the table and the number of data points in any line, then some debugging information is sent to cout
. When verbose is 2 or larger,
Verbose output may need improvement
Use reorder_table() and possibly reblock() to create a full post-processing function.
Definition at line 1808 of file mcmc_para.h.
Public Member Functions | |
Basic usage | |
virtual void | set_names_units (std::vector< std::string > names, std::vector< std::string > units) |
Set the table names and units. | |
virtual void | initial_points_file_last (std::string fname, size_t n_param_loc, size_t offset=5) |
Read initial points from the last points recorded in file named fname . More... | |
virtual void | initial_points_file_dist (std::string fname, size_t n_param_loc, size_t offset=5) |
Read initial points from file named fname , distributing across the chain if necessary. More... | |
virtual void | initial_points_file_best (std::string fname, size_t n_param_loc, double thresh=1.0e-6, size_t offset=5) |
Read initial points from the best points recorded in file named fname . More... | |
virtual int | mcmc (size_t n_params_local, vec_t &low, vec_t &high, std::vector< func_t > &func, std::vector< 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. | |
void | get_chain_sizes (std::vector< size_t > &chain_sizes) |
Determine the chain sizes. More... | |
virtual void | read_prev_results (o2scl_hdf::hdf_file &hf, size_t n_param_loc, std::string name="") |
Read previous results (number of threads and walkers must be set first) More... | |
virtual void | critical_extra (size_t i_thread) |
Additional code to execute inside the OpenMP critical section. | |
virtual int | add_line (const vec_t &pars, double log_weight, size_t walker_ix, int func_ret, bool mcmc_accept, data_t &dat, size_t i_thread, fill_t &fill) |
A measurement function which adds the point to the table. | |
virtual void | mcmc_cleanup () |
Perform cleanup after an MCMC simulation. | |
virtual void | ac_coeffs (size_t icol, std::vector< double > &ac_coeff_avg, int loc_verbose=0) |
Compute autocorrelation coefficient for column with index icol averaging over all walkers and all threads. | |
virtual void | reorder_table () |
Reorder the table by thread and walker index. | |
void | reblock (size_t n_blocks) |
Reaverage the data into blocks of a fixed size in order to avoid autocorrelations. More... | |
virtual void | set_names_units (std::vector< std::string > names, std::vector< std::string > units) |
Set the table names and units. | |
virtual void | initial_points_file_last (std::string fname, size_t n_param_loc, size_t offset=5) |
Read initial points from the last points recorded in file named fname . More... | |
virtual void | initial_points_file_dist (std::string fname, size_t n_param_loc, size_t offset=5) |
Read initial points from file named fname , distributing across the chain if necessary. More... | |
virtual void | initial_points_file_best (std::string fname, size_t n_param_loc, double thresh=1.0e-6, size_t offset=5) |
Read initial points from the best points recorded in file named fname . More... | |
virtual int | mcmc (size_t n_params_local, vec_t &low, vec_t &high, std::vector< func_t > &func, std::vector< 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. | |
void | get_chain_sizes (std::vector< size_t > &chain_sizes) |
Determine the chain sizes. More... | |
virtual void | read_prev_results (o2scl_hdf::hdf_file &hf, size_t n_param_loc, std::string name="") |
Read previous results (number of threads and walkers must be set first) More... | |
virtual void | critical_extra (size_t i_thread) |
Additional code to execute inside the OpenMP critical section. | |
virtual int | add_line (const vec_t &pars, double log_weight, size_t walker_ix, int func_ret, bool mcmc_accept, data_t &dat, size_t i_thread, fill_t &fill) |
A measurement function which adds the point to the table. | |
virtual void | mcmc_cleanup () |
Perform cleanup after an MCMC simulation. | |
virtual void | ac_coeffs (size_t icol, std::vector< double > &ac_coeff_avg, int loc_verbose=0) |
Compute autocorrelation coefficient for column with index icol averaging over all walkers and all threads. | |
virtual void | reorder_table () |
Reorder the table by thread and walker index. | |
void | reblock (size_t n_blocks) |
Reaverage the data into blocks of a fixed size in order to avoid autocorrelations. More... | |
![]() | |
virtual int | mcmc (size_t n_params, ubvector &low, ubvector &high, std::vector< func_t > &func, std::vector< std::function< int(const ubvector &, double, size_t, int, bool, data_t &)> > &meas) |
Perform a MCMC simulation. More... | |
virtual int | mcmc (size_t n_params, ubvector &low, ubvector &high, func_t &func, std::function< int(const ubvector &, double, size_t, int, bool, data_t &)> &meas) |
Perform a MCMC simulation with a thread-safe function or with only one OpenMP thread. | |
virtual int | mcmc (size_t n_params, ubvector &low, ubvector &high, std::vector< func_t > &func, std::vector< std::function< int(const ubvector &, double, size_t, int, bool, data_t &)> > &meas) |
Perform a MCMC simulation. More... | |
virtual int | mcmc (size_t n_params, ubvector &low, ubvector &high, func_t &func, std::function< int(const ubvector &, double, size_t, int, bool, data_t &)> &meas) |
Perform a MCMC simulation with a thread-safe function or with only one OpenMP thread. | |
void | set_proposal (prob_vec_t &pv) |
Set the proposal distribution. More... | |
void | set_proposal (prob_vec_t &pv) |
Set the proposal distribution. More... | |
void | set_proposal_ptrs (prob_ptr_vec_t &pv) |
Set pointers to proposal distributions. More... | |
void | set_proposal_ptrs (prob_vec_t &pv) |
Set pointers to proposal distributions. More... | |
virtual void | unset_proposal () |
Go back to random-walk Metropolis with a uniform distribution. | |
virtual void | unset_proposal () |
Go back to random-walk Metropolis with a uniform distribution. | |
mcmc_para_base () | |
mcmc_para_base () | |
Protected Types | |
typedef std::function< int(const vec_t &, double, size_t, int, bool, data_t &)> | internal_measure_t |
Measurement functor type for the parent. | |
typedef mcmc_para_base< func_t, internal_measure_t, data_t, vec_t > | parent_t |
Type of parent class. | |
typedef std::function< int(const vec_t &, double, size_t, int, bool, data_t &)> | internal_measure_t |
Measurement functor type for the parent. | |
typedef mcmc_para_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, size_t i_walker, fill_t &fill) |
Fill line with data for insertion into the table. | |
virtual void | file_header (o2scl_hdf::hdf_file &hf) |
Initial write to HDF5 file. | |
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, size_t i_walker, fill_t &fill) |
Fill line with data for insertion into the table. | |
virtual void | file_header (o2scl_hdf::hdf_file &hf) |
Initial write to HDF5 file. | |
![]() | |
virtual void | best_point (ubvector &best, double w_best, data_t &dat) |
Function to run for the best point. More... | |
virtual void | best_point (ubvector &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. | |
size_t | n_params |
Number of parameters. | |
std::shared_ptr< o2scl::table_units<> > | table |
Main data table for Markov chain. | |
bool | first_write |
If true, the HDF5 I/O initial info has been written to the file (set by mcmc() ) | |
std::vector< int > | walker_accept_rows |
For each walker, record the last row in the table which corresponds to an accept. | |
std::vector< int > | walker_reject_rows |
For each walker, record the last row in the table which corresponds to an reject. | |
vec_t | low_copy |
A copy of the lower limits for HDF5 output. | |
vec_t | high_copy |
A copy of the upper limits for HDF5 output. | |
size_t | last_write_iters |
Total number of MCMC acceptances over all threads at last file write() (default 0) | |
double | last_write_time |
Time at last file write() (default 0.0) | |
bool | prev_read |
If true, previous results have been read. More... | |
![]() | |
int | mpi_rank |
The MPI processor rank. | |
int | mpi_size |
The MPI number of processors. | |
std::ofstream | scr_out |
The screen output file. | |
std::vector< rng_gsl > | rg |
Random number generators. | |
std::vector< o2scl::prob_cond_mdim< ubvector > * > | prop_dist |
Pointer to proposal distribution for each thread. | |
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< ubvector > | current |
Current points in parameter space for each walker and each OpenMP thread. More... | |
std::vector< data_t > | data_arr |
Data array. More... | |
std::vector< bool > | switch_arr |
Data switch array for each walker and each OpenMP thread. More... | |
std::vector< std::vector< size_t > > | ret_value_counts |
Return value counters, one vector independent chain. | |
std::vector< size_t > | curr_walker |
Index of the current walker. More... | |
size_t | n_chains_per_rank |
Number of fully independent chains in each MPI rank. More... | |
Settings | |
bool | table_sequence |
If true, ensure sure walkers and OpenMP threads are written to the table with equal spacing between rows (default true) | |
size_t | file_update_iters |
Iterations between file updates (default 0 for no file updates) | |
double | file_update_time |
Time between file updates (default 0.0 for no file updates) | |
size_t | table_prealloc |
Number of rows to allocate for the table before the MCMC run. | |
int | table_io_chunk |
The number of tables to combine before I/O (default 1) | |
bool | store_rejects |
If true, store MCMC rejections in the table. | |
virtual void | write_files (bool sync_write=false) |
Write MCMC tables to files. | |
mcmc_para_table () | |
virtual void | write_files (bool sync_write=false) |
Write MCMC tables to files. | |
mcmc_para_table () | |
Additional Inherited Members | |
![]() | |
std::vector< size_t > | n_accept |
The number of Metropolis steps which were accepted in each independent chain (summed over all walkers) More... | |
std::vector< size_t > | n_reject |
The number of Metropolis steps which were rejected in each independent chain (summed over all walkers) More... | |
bool | meas_for_initial |
If true, call the measurement function for the initial point. | |
double | mpi_start_time |
The MPI starting time (defaults to 0.0) More... | |
size_t | max_iters |
If non-zero, the maximum number of MCMC iterations (default 0) More... | |
double | max_time |
Time in seconds (default is 0) More... | |
std::string | prefix |
Prefix for output filenames (default "mcmc") | |
bool | aff_inv |
If true, use affine-invariant Monte Carlo. | |
double | step_fac |
Stepsize factor (default 10.0) | |
std::vector< double > | step_vec |
Optionally specify step sizes for each parameter. | |
bool | couple_threads |
If true, couple the walkers across threads (default false) | |
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) More... | |
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 (per openMP thread) for affine-invariant MC or 1 otherwise (default 1) More... | |
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 (default 0.1) | |
size_t | n_threads |
Number of OpenMP threads. | |
std::vector< ubvector > | initial_points |
Initial points in parameter space. More... | |
size_t | n_walk_per_thread |
Number of walkers per thread (default 0 to set equal to n_walk) | |
![]() | |
static const int | mcmc_done |
Integer to indicate completion. | |
static const int | mcmc_skip |
Integer to indicate rejection. | |
|
inline |
Definition at line 2521 of file mcmc_para.h.
|
inline |
Definition at line 2563 of file mcmc_para_new.h.
|
inlinevirtual |
The values of o2scl::mcmc_para_base::n_walk and o2scl::mcmc_para_base::n_threads, must be set to their correct values before calling this function. This function requires that a table is present in fname
which stores parameters in a block of columns and contains a separate column named log_wgt
.
Definition at line 2362 of file mcmc_para.h.
|
inlinevirtual |
The values of o2scl::mcmc_para_base::n_walk and o2scl::mcmc_para_base::n_threads, must be set to their correct values before calling this function. This function requires that a table is present in fname
which stores parameters in a block of columns and contains a separate column named log_wgt
.
Definition at line 2404 of file mcmc_para_new.h.
|
inlinevirtual |
The values of o2scl::mcmc_para_base::n_walk and o2scl::mcmc_para_base::n_threads, must be set to their correct values before calling this function. This function requires that a table is present in fname
which stores parameters in a block of columns.
Definition at line 2289 of file mcmc_para.h.
|
inlinevirtual |
The values of o2scl::mcmc_para_base::n_walk and o2scl::mcmc_para_base::n_threads, must be set to their correct values before calling this function. This function requires that a table is present in fname
which stores parameters in a block of columns.
Definition at line 2331 of file mcmc_para_new.h.
|
inlinevirtual |
The values of o2scl::mcmc_para_base::n_walk and o2scl::mcmc_para_base::n_threads, must be set to their correct values before calling this function. This function requires that a table is present in fname
which stores parameters in a block of columns and has columns named mult
, thread
, walker
, and log_wgt
.
Definition at line 2188 of file mcmc_para.h.
|
inlinevirtual |
The values of o2scl::mcmc_para_base::n_walk and o2scl::mcmc_para_base::n_threads, must be set to their correct values before calling this function. This function requires that a table is present in fname
which stores parameters in a block of columns and has columns named mult
, thread
, walker
, and log_wgt
.
Definition at line 2230 of file mcmc_para_new.h.
|
inlinevirtual |
Perform an MCMC simulation over n_params
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 2468 of file mcmc_para.h.
|
inlinevirtual |
Perform an MCMC simulation over n_params
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 2510 of file mcmc_para_new.h.
|
inlineprotectedvirtual |
This function sets the column names and units.
Reimplemented from o2scl::mcmc_para_base< func_t, std::function< int(const ubvector &, double, size_t, int, bool, data_t &)>, data_t, ubvector >.
Definition at line 1842 of file mcmc_para.h.
|
inlineprotectedvirtual |
This function sets the column names and units.
Reimplemented from o2scl::mcmc_para_base< func_t, std::function< int(const ubvector &, double, size_t, int, bool, data_t &)>, data_t, ubvector >.
Definition at line 1882 of file mcmc_para_new.h.
|
inlinevirtual |
Definition at line 2550 of file mcmc_para.h.
|
inlinevirtual |
Definition at line 2592 of file mcmc_para_new.h.
|
inline |
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_para_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 2983 of file mcmc_para.h.
|
inline |
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_para_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 3025 of file mcmc_para_new.h.
|
protected |
This is set to true
by read_prev_results() and set back to false
after mcmc_init() is called.
Definition at line 2004 of file mcmc_para.h.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).