CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
cbl::statistics::Sampler Class Reference

The class Sampler. More...

#include "Headers/Sampler.h"

Public Member Functions

double operator() (std::vector< double > &pp)
 evaluate the function More...
 
double get_chain (const int par, const int chain, const int step)
 get the chain value More...
 
std::vector< std::vector< std::vector< double > > > get_chain ()
 return the chains More...
 
double get_function (const int chain, const int step)
 get the function value More...
 
void get_chain_function_acceptance (std::vector< std::vector< double >> &chains, std::vector< double > &function, std::vector< double > &acceptance, const int start=0, const int thin=1)
 get the chain values and the function More...
 
void set_chain (const int npar, const int npar_free, const int chain_size, const int nwalkers)
 function to set the chains More...
 
void set_function (const std::function< double(std::vector< double > &)> function)
 set the function More...
 
void sample_stretch_move (const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2, const std::string outputFile=cbl::par::defaultString)
 sample the input function using the stretch-move algorithm on n-dimensional parameter space More...
 
void sample_stretch_move_parallel (const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2)
 sample the input function using the stretch-move algorithm on n-dimensional parameter space - parallel version More...
 
void write_chain (const std::string dir_output, const std::string file, const int start, const int thin)
 write the chains in an output file More...
 
Constructors/destructors
 Sampler ()=default
 default constructor
 
 Sampler (const int npar, const std::function< double(std::vector< double > &)> function)
 constructor More...
 
 Sampler (const int npar, const int npar_free, const std::function< double(std::vector< double > &)> function)
 constructor More...
 
 ~Sampler ()=default
 default destructor
 

Protected Member Functions

std::shared_ptr< random::DistributionRandomNumbersm_set_gz (const int seed, const double aa=2)
 return the random generator for the stretch-move More...
 
void m_initialize_chains (const std::vector< std::vector< double >> start)
 initialize chains More...
 
void m_initialize_chains_parallel (const std::vector< std::vector< double >> start)
 initialize chains More...
 
void m_sample_stretch_move_parallel_cpp (const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2)
 sample the input function using the stretch-move algorithm on n-dimensional parameter space. Parallel version More...
 
void m_sample_stretch_move_parallel_py (const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2)
 sample the input function using the stretch-move algorithm on n-dimensional parameter space - parallel version; it is used for functions written in python More...
 

Protected Attributes

int m_nwalkers
 number of chains
 
int m_chain_size
 size of the chains
 
int m_npar
 number of parameters
 
int m_npar_free
 number of free parameters
 
std::function< double(std::vector< double > &)> m_function
 the function to be sampled
 
std::vector< double > m_acceptance
 chain acceptance ratio
 
bool m_use_python
 use python-defined function
 
std::vector< std::vector< double > > m_function_chain
 value of the function at sampled points
 
std::vector< std::vector< std::vector< double > > > m_chains
 the chains
 

Detailed Description

The class Sampler.

This class is used to handle objects of type sample. It samples generic functions

Examples
sampler.cpp.

Definition at line 114 of file Sampler.h.

Constructor & Destructor Documentation

◆ Sampler() [1/2]

cbl::statistics::Sampler::Sampler ( const int  npar,
const std::function< double(std::vector< double > &)>  function 
)
inline

constructor

Parameters
nparthe number of parameters
functionthe function to sample

Definition at line 229 of file Sampler.h.

◆ Sampler() [2/2]

cbl::statistics::Sampler::Sampler ( const int  npar,
const int  npar_free,
const std::function< double(std::vector< double > &)>  function 
)
inline

constructor

Parameters
nparthe number of parameters
npar_freethe number of free parameters
functionthe function to sample

Definition at line 237 of file Sampler.h.

Member Function Documentation

◆ get_chain() [1/2]

std::vector<std::vector<std::vector<double> > > cbl::statistics::Sampler::get_chain ( )
inline

return the chains

Returns
vector containing the chains

Definition at line 277 of file Sampler.h.

◆ get_chain() [2/2]

double cbl::statistics::Sampler::get_chain ( const int  par,
const int  chain,
const int  step 
)
inline

get the chain value

Parameters
parthe parameter
chainthe chain number
stepthe step in the chain
Returns
the chain value

Definition at line 270 of file Sampler.h.

◆ get_chain_function_acceptance()

void cbl::statistics::Sampler::get_chain_function_acceptance ( std::vector< std::vector< double >> &  chains,
std::vector< double > &  function,
std::vector< double > &  acceptance,
const int  start = 0,
const int  thin = 1 
)

get the chain values and the function

Parameters
[out]chainsvector containing the chains
[out]functionthe function computed at each step of the chain
[out]acceptancethe acceptance rate
startthe starting position for each chain
thinthe position step

Definition at line 411 of file Sampler.cpp.

◆ get_function()

double cbl::statistics::Sampler::get_function ( const int  chain,
const int  step 
)
inline

get the function value

Parameters
chainthe chain number
stepthe step in the chain
Returns
the function value

Definition at line 288 of file Sampler.h.

◆ m_initialize_chains()

void cbl::statistics::Sampler::m_initialize_chains ( const std::vector< std::vector< double >>  start)
protected

initialize chains

Parameters
startvector containing the starting position for the parameters

Definition at line 62 of file Sampler.cpp.

◆ m_initialize_chains_parallel()

void cbl::statistics::Sampler::m_initialize_chains_parallel ( const std::vector< std::vector< double >>  start)
protected

initialize chains

Parameters
startvector containing the starting position for the parameters

Definition at line 76 of file Sampler.cpp.

◆ m_sample_stretch_move_parallel_cpp()

void cbl::statistics::Sampler::m_sample_stretch_move_parallel_cpp ( const int  chain_size,
const int  nwalkers,
const std::vector< std::vector< double >>  start,
const int  seed = 4241,
const double  aa = 2 
)
protected

sample the input function using the stretch-move algorithm on n-dimensional parameter space. Parallel version

Parameters
chain_sizenumber of step in each chain
nwalkersnumber of parallel walkers
startvector containing the starting position for the parameters
seedthe seed for random number generator
aathe stretch-move distribution parameter
Warning
pointers cannot be used inside the input function

Definition at line 225 of file Sampler.cpp.

◆ m_sample_stretch_move_parallel_py()

void cbl::statistics::Sampler::m_sample_stretch_move_parallel_py ( const int  chain_size,
const int  nwalkers,
const std::vector< std::vector< double >>  start,
const int  seed = 4241,
const double  aa = 2 
)
protected

sample the input function using the stretch-move algorithm on n-dimensional parameter space - parallel version; it is used for functions written in python

Parameters
chain_sizenumber of step in each chain
nwalkersnumber of parallel walkers
startvector containing the starting position for the parameters
seedthe seed for random number generator
aathe stretch-move distribution parameter
Warning
pointers cannot be used inside the input function

Definition at line 312 of file Sampler.cpp.

◆ m_set_gz()

shared_ptr< random::DistributionRandomNumbers > cbl::statistics::Sampler::m_set_gz ( const int  seed,
const double  aa = 2 
)
protected

return the random generator for the stretch-move

Parameters
seedthe random generator seed
aathe stretch-move distribution parameter
Returns
pointer to the random number generator

Definition at line 43 of file Sampler.cpp.

◆ operator()()

double cbl::statistics::Sampler::operator() ( std::vector< double > &  pp)
inline

evaluate the function

Parameters
ppthe function parameters
Returns
the function value and derived parameters

Definition at line 254 of file Sampler.h.

◆ sample_stretch_move()

void cbl::statistics::Sampler::sample_stretch_move ( const int  chain_size,
const int  nwalkers,
const std::vector< std::vector< double >>  start,
const int  seed = 4241,
const double  aa = 2,
const std::string  outputFile = cbl::par::defaultString 
)

sample the input function using the stretch-move algorithm on n-dimensional parameter space

Parameters
chain_sizenumber of step in each chain
nwalkersnumber of parallel walkers
startvector containing the starting position for the parameters
seedthe seed for random number generator
aathe stretch-move distribution parameter
outputFileoutput file where the chains are written while running

Definition at line 133 of file Sampler.cpp.

◆ sample_stretch_move_parallel()

void cbl::statistics::Sampler::sample_stretch_move_parallel ( const int  chain_size,
const int  nwalkers,
const std::vector< std::vector< double >>  start,
const int  seed = 4241,
const double  aa = 2 
)

sample the input function using the stretch-move algorithm on n-dimensional parameter space - parallel version

Parameters
chain_sizenumber of step in each chain
nwalkersnumber of parallel walkers
startvector containing the starting position for the parameters
seedthe seed for random number generator
aathe stretch-move distribution parameter
Warning
pointers cannot be used inside the input function

Definition at line 396 of file Sampler.cpp.

◆ set_chain()

void cbl::statistics::Sampler::set_chain ( const int  npar,
const int  npar_free,
const int  chain_size,
const int  nwalkers 
)

function to set the chains

Parameters
nparthe number of parameters
npar_freethe number of free parameters
chain_sizenumber of step in each chain
nwalkersnumber of parallel walkers

Definition at line 90 of file Sampler.cpp.

◆ set_function()

void cbl::statistics::Sampler::set_function ( const std::function< double(std::vector< double > &)>  function)

set the function

Parameters
functionthe function to be sampled

Definition at line 112 of file Sampler.cpp.

◆ write_chain()

void cbl::statistics::Sampler::write_chain ( const std::string  dir_output,
const std::string  file,
const int  start,
const int  thin 
)

write the chains in an output file

Parameters
dir_outputthe output directory
filethe output file
startthe starting position for each chain
thinthe position step

Definition at line 431 of file Sampler.cpp.


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