![]() |
CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
|
The class Likelihood. More...
#include "Headers/Likelihood.h"
Public Member Functions | |
std::shared_ptr< ModelParameters > | parameters () const |
return the likelihood parameters More... | |
double | operator() (std::vector< double > &pp) const |
evaluate the likelihood More... | |
double | log (std::vector< double > ¶meter) const |
evaluate the logarithm of the likelihood for the input parameters More... | |
void | set_data (std::shared_ptr< data::Data > data) |
set the data for the likelihood analysis More... | |
void | set_model (std::shared_ptr< Model > model=NULL, const std::shared_ptr< ModelParameters > model_parameters=NULL) |
set the model for the likelihood analysis More... | |
void | unset_grid () |
set the likelihood function with internal values of LikelihoodType | |
std::shared_ptr< Model > | get_m_model () |
get the values of the internal variable m_model More... | |
std::shared_ptr< ModelParameters > | get_model_parameters () |
get the values of the internal variable m_model More... | |
std::shared_ptr< data::Data > | get_m_data () |
get the values of the internal variable m_data More... | |
void | set_grid (const int npoints, const std::vector< std::vector< double >> parameter_limits, const std::string file, const bool read=false) |
set the likelihood function as a grid, to speed up computation: this only works for one or two free parameters More... | |
void | set_function (const LikelihoodType likelihood_type, const std::vector< size_t > x_index={0, 2}, const int w_index=-1, const double prec=1.e-10, const int Nres=-1) |
set the likelihood type using the LikelihoodType object More... | |
void | set_function (const LogLikelihood_function likelihood_function) |
set the likelihood function More... | |
void | set_log_function (const LogLikelihood_function loglikelihood_function) |
set the natural logarithm of the likelihood function More... | |
void | write_results (const std::string dir_output, const std::string file) |
write best-fit results on a file More... | |
void | maximize (const std::vector< double > start, const std::vector< std::vector< double >> parameter_limits, const unsigned int max_iter=10000, const double tol=1.e-6, const double epsilon=1.e-3) |
function that maximizes the likelihood, finds the best-fit parameters and stores them in the model More... | |
void | write_model (const std::string output_dir, const std::string output_file, const std::vector< double > parameters, const std::vector< double > xx={}, const std::vector< double > yy={}) |
write the model at xx at bestfit More... | |
void | write_model_at_bestfit (const std::string output_dir, const std::string output_file, const std::vector< double > xx={}, const std::vector< double > yy={}) |
write the model at xx at bestfit More... | |
Constructors/destructors | |
Likelihood () | |
default constructor | |
Likelihood (const std::shared_ptr< data::Data > data, const std::shared_ptr< Model > model, const LikelihoodType likelihood_type, const std::vector< size_t > x_index={0, 2}, const int w_index=-1, const std::shared_ptr< ModelParameters > model_parameters=NULL, const double prec=1.e-10, const int Nres=-1) | |
constructor More... | |
Likelihood (const std::shared_ptr< data::Data > data, const std::shared_ptr< Model > model, const Likelihood_function log_likelihood_function, const std::shared_ptr< ModelParameters > model_parameters=NULL) | |
constructor More... | |
virtual | ~Likelihood ()=default |
default destructor | |
Protected Member Functions | |
void | m_set_grid_likelihood_1D (const int npoints, const std::vector< std::vector< double >> parameter_limits, const std::string output_file) |
set the likelihood grid and write the grid on a file More... | |
void | m_set_grid_likelihood_1D (const std::string input_file) |
set the likelihood grid from file More... | |
void | m_set_grid_likelihood_2D (const int npoints, const std::vector< std::vector< double >> parameter_limits, const std::string output_file) |
set the likelihood grid and write the grid on a file More... | |
void | m_set_grid_likelihood_2D (const std::string input_file) |
set the likelihood grid from file More... | |
Protected Attributes | |
std::shared_ptr< data::Data > | m_data |
data containers | |
std::shared_ptr< Model > | m_model |
model to test | |
std::shared_ptr< void > | m_likelihood_inputs |
likelihood inputs | |
std::shared_ptr< ModelParameters > | m_model_parameters |
likelihood parameters | |
LikelihoodType | m_likelihood_type = LikelihoodType::_NotSet_ |
type of the likelihood | |
LogLikelihood_function | m_log_likelihood_function |
log-likelihood function | |
Likelihood_function | m_likelihood_function |
likelihood function | |
LogLikelihood_function | m_likelihood_function_grid |
likelihood function on a grid | |
LogLikelihood_function | m_log_likelihood_function_grid |
log-likelihood function on a grid | |
std::vector< size_t > | m_x_index |
int | m_w_index |
the index in extra info where the bin weight is stored | |
bool | m_use_grid = false |
use grid | |
The class Likelihood.
This class is used to handle objects of type Likelihood. It is used for all kind of likelihood maximization
Definition at line 55 of file Likelihood.h.
cbl::statistics::Likelihood::Likelihood | ( | const std::shared_ptr< data::Data > | data, |
const std::shared_ptr< Model > | model, | ||
const LikelihoodType | likelihood_type, | ||
const std::vector< size_t > | x_index = {0,2} , |
||
const int | w_index = -1 , |
||
const std::shared_ptr< ModelParameters > | model_parameters = NULL , |
||
const double | prec = 1.e-10 , |
||
const int | Nres = -1 |
||
) |
constructor
data | pointers to the data container |
model | pointers to the model |
likelihood_type | type of likelihood |
x_index | index(s) of the extra info std::vector containing the point(s) where to evaluate the model |
w_index | std::vector containing the data point weight |
model_parameters | parameters of the likelihood |
prec | the precision required in the inversion of the covariance matrix |
Nres | \(N_{res}\), the number of catalogue resamplings used to estimate the covariance matrix; \(N_{res}=-1\) if the covariance matrix has not been estimated with resampling methods |
Definition at line 146 of file Likelihood.cpp.
cbl::statistics::Likelihood::Likelihood | ( | const std::shared_ptr< data::Data > | data, |
const std::shared_ptr< Model > | model, | ||
const Likelihood_function | log_likelihood_function, | ||
const std::shared_ptr< ModelParameters > | model_parameters = NULL |
||
) |
constructor
data | pointers to the data container |
model | pointers to the model |
log_likelihood_function | user-defined log-likelihood function (natural logarithm) |
model_parameters | parameters of the likelihood |
Definition at line 157 of file Likelihood.cpp.
|
inline |
get the values of the internal variable m_data
Definition at line 286 of file Likelihood.h.
|
inline |
get the values of the internal variable m_model
Definition at line 272 of file Likelihood.h.
|
inline |
get the values of the internal variable m_model
Definition at line 279 of file Likelihood.h.
double cbl::statistics::Likelihood::log | ( | std::vector< double > & | parameter | ) | const |
evaluate the logarithm of the likelihood for the input parameters
parameter | the likelihood parameters |
Definition at line 177 of file Likelihood.cpp.
|
protected |
set the likelihood grid and write the grid on a file
npoints | the number of points to bin the parameter space |
parameter_limits | the limits for the parameter space |
output_file | the input file |
Definition at line 43 of file Likelihood.cpp.
|
protected |
set the likelihood grid from file
input_file | the input file |
Definition at line 110 of file Likelihood.cpp.
|
protected |
set the likelihood grid and write the grid on a file
npoints | the number of points to bin the parameter space |
parameter_limits | the limits for the parameter space |
output_file | the input file |
Definition at line 72 of file Likelihood.cpp.
|
protected |
set the likelihood grid from file
input_file | the input file |
Definition at line 128 of file Likelihood.cpp.
void cbl::statistics::Likelihood::maximize | ( | const std::vector< double > | start, |
const std::vector< std::vector< double >> | parameter_limits, | ||
const unsigned int | max_iter = 10000 , |
||
const double | tol = 1.e-6 , |
||
const double | epsilon = 1.e-3 |
||
) |
function that maximizes the likelihood, finds the best-fit parameters and stores them in the model
this function exploits the Nelder-Mead method https://en.wikipedia.org/wiki/Nelder%E2%80%93Mead_method
the algorithm defines a simplex (i.e a k-dimensional polytope which is the convex hull of its k+1 vertices) in the parameter space. At each step, it identifies the simplex vertex at which the function to be minimised (i.e. the negative likelihood in this case) has the greatest value, and moves it, via reflections and scaling, to a new position in which the function has a lower value. This iteration stops when the simplex area becomes lower than the tolerance. For instance, in 2D, the starting vertices of the simplex (a triangle in 2D) are the following: (start[0], start[1]) ; (start[0]+epsilon, start[1]) ; (start[0], start[1]+epsilon)
start | std::vector containing initial values for the likelihood maximization |
parameter_limits | limits for the parameters |
max_iter | the maximum number of iterations |
tol | the tolerance in finding convergence |
epsilon | the simplex side |
Definition at line 356 of file Likelihood.cpp.
double cbl::statistics::Likelihood::operator() | ( | std::vector< double > & | pp | ) | const |
evaluate the likelihood
pp | the likelihood parameters |
Definition at line 168 of file Likelihood.cpp.
|
inline |
return the likelihood parameters
Definition at line 218 of file Likelihood.h.
void cbl::statistics::Likelihood::set_data | ( | std::shared_ptr< data::Data > | data | ) |
set the data for the likelihood analysis
data | pointer to the dataset |
Definition at line 186 of file Likelihood.cpp.
void cbl::statistics::Likelihood::set_function | ( | const LikelihoodType | likelihood_type, |
const std::vector< size_t > | x_index = {0,2} , |
||
const int | w_index = -1 , |
||
const double | prec = 1.e-10 , |
||
const int | Nres = -1 |
||
) |
set the likelihood type using the LikelihoodType object
likelihood_type | the likelihood type, specified with the LikelihoodType object |
x_index | index(s) of the extra info std::vector containing the point(s) where to evaluate the model |
w_index | index of the extra info std::vector containing the data point weight |
prec | the precision required in the inversion of the covariance matrix |
Nres | \(N_{res}\), the number of catalogue resamplings used to estimate the covariance matrix; \(N_{res}=-1\) if the covariance matrix has not been estimated with resampling methods |
Definition at line 262 of file Likelihood.cpp.
void cbl::statistics::Likelihood::set_function | ( | const LogLikelihood_function | likelihood_function | ) |
set the likelihood function
likelihood_function | the likelihood function |
Definition at line 334 of file Likelihood.cpp.
void cbl::statistics::Likelihood::set_grid | ( | const int | npoints, |
const std::vector< std::vector< double >> | parameter_limits, | ||
const std::string | file, | ||
const bool | read = false |
||
) |
set the likelihood function as a grid, to speed up computation: this only works for one or two free parameters
npoints | the number of grid points |
parameter_limits | |
file | the file to read/write the likelihood computed on a grid |
read | if true, read the likelihood grid from a list |
Definition at line 225 of file Likelihood.cpp.
void cbl::statistics::Likelihood::set_log_function | ( | const LogLikelihood_function | loglikelihood_function | ) |
set the natural logarithm of the likelihood function
loglikelihood_function | the loglikelihood function |
Definition at line 345 of file Likelihood.cpp.
void cbl::statistics::Likelihood::set_model | ( | std::shared_ptr< Model > | model = NULL , |
const std::shared_ptr< ModelParameters > | model_parameters = NULL |
||
) |
set the model for the likelihood analysis
model | pointer to the model |
model_parameters | parameters of the likelihood |
Definition at line 195 of file Likelihood.cpp.
void cbl::statistics::Likelihood::write_model | ( | const std::string | output_dir, |
const std::string | output_file, | ||
const std::vector< double > | parameters, | ||
const std::vector< double > | xx = {} , |
||
const std::vector< double > | yy = {} |
||
) |
write the model at xx at bestfit
output_dir | the output directory |
output_file | the output file |
parameters | the model parameters |
xx | vector of points at which the model is computed |
yy | vector of points at which the model is computed |
Definition at line 435 of file Likelihood.cpp.
|
inline |
write the model at xx at bestfit
output_dir | the output directory |
output_file | the output file |
xx | vector of points at which the model is computed |
yy | vector of points at which the model is computed |
Definition at line 407 of file Likelihood.h.
void cbl::statistics::Likelihood::write_results | ( | const std::string | dir_output, |
const std::string | file | ||
) |
write best-fit results on a file
dir_output | output directory |
file | output file |
Definition at line 402 of file Likelihood.cpp.
|
protected |
the index in extra info for the coordinates in which the model is computed
Definition at line 89 of file Likelihood.h.