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

The class Likelihood. More...

#include "Headers/Likelihood.h"

Inheritance diagram for cbl::statistics::Likelihood:

Public Member Functions

std::shared_ptr< ModelParametersparameters () const
 return the likelihood parameters More...
 
double operator() (std::vector< double > &pp) const
 evaluate the likelihood More...
 
double log (std::vector< double > &parameter) 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< Modelget_m_model ()
 get the values of the internal variable m_model More...
 
std::shared_ptr< ModelParametersget_model_parameters ()
 get the values of the internal variable m_model More...
 
std::shared_ptr< data::Dataget_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::Datam_data
 data containers
 
std::shared_ptr< Modelm_model
 model to test
 
std::shared_ptr< void > m_likelihood_inputs
 likelihood inputs
 
std::shared_ptr< ModelParametersm_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
 

Detailed Description

The class Likelihood.

This class is used to handle objects of type Likelihood. It is used for all kind of likelihood maximization

Examples
fit.cpp.

Definition at line 55 of file Likelihood.h.

Constructor & Destructor Documentation

◆ Likelihood() [1/2]

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

Parameters
datapointers to the data container
modelpointers to the model
likelihood_typetype of likelihood
x_indexindex(s) of the extra info std::vector containing the point(s) where to evaluate the model
w_indexstd::vector containing the data point weight
model_parametersparameters of the likelihood
precthe 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.

◆ Likelihood() [2/2]

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

Parameters
datapointers to the data container
modelpointers to the model
log_likelihood_functionuser-defined log-likelihood function (natural logarithm)
model_parametersparameters of the likelihood

Definition at line 157 of file Likelihood.cpp.

Member Function Documentation

◆ get_m_data()

std::shared_ptr<data::Data> cbl::statistics::Likelihood::get_m_data ( )
inline

get the values of the internal variable m_data

Returns
pointer containing the data containers

Definition at line 286 of file Likelihood.h.

◆ get_m_model()

std::shared_ptr<Model> cbl::statistics::Likelihood::get_m_model ( )
inline

get the values of the internal variable m_model

Returns
pointer containing the model to test

Definition at line 272 of file Likelihood.h.

◆ get_model_parameters()

std::shared_ptr<ModelParameters> cbl::statistics::Likelihood::get_model_parameters ( )
inline

get the values of the internal variable m_model

Returns
pointer containing the model parameters

Definition at line 279 of file Likelihood.h.

◆ log()

double cbl::statistics::Likelihood::log ( std::vector< double > &  parameter) const

evaluate the logarithm of the likelihood for the input parameters

Parameters
parameterthe likelihood parameters
Returns
the log-likelihood \( \log(\mathcal{L}) \)

Definition at line 177 of file Likelihood.cpp.

◆ m_set_grid_likelihood_1D() [1/2]

void cbl::statistics::Likelihood::m_set_grid_likelihood_1D ( const int  npoints,
const std::vector< std::vector< double >>  parameter_limits,
const std::string  output_file 
)
protected

set the likelihood grid and write the grid on a file

Parameters
npointsthe number of points to bin the parameter space
parameter_limitsthe limits for the parameter space
output_filethe input file

Definition at line 43 of file Likelihood.cpp.

◆ m_set_grid_likelihood_1D() [2/2]

void cbl::statistics::Likelihood::m_set_grid_likelihood_1D ( const std::string  input_file)
protected

set the likelihood grid from file

Parameters
input_filethe input file

Definition at line 110 of file Likelihood.cpp.

◆ m_set_grid_likelihood_2D() [1/2]

void cbl::statistics::Likelihood::m_set_grid_likelihood_2D ( const int  npoints,
const std::vector< std::vector< double >>  parameter_limits,
const std::string  output_file 
)
protected

set the likelihood grid and write the grid on a file

Parameters
npointsthe number of points to bin the parameter space
parameter_limitsthe limits for the parameter space
output_filethe input file

Definition at line 72 of file Likelihood.cpp.

◆ m_set_grid_likelihood_2D() [2/2]

void cbl::statistics::Likelihood::m_set_grid_likelihood_2D ( const std::string  input_file)
protected

set the likelihood grid from file

Parameters
input_filethe input file

Definition at line 128 of file Likelihood.cpp.

◆ maximize()

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)

Parameters
startstd::vector containing initial values for the likelihood maximization
parameter_limitslimits for the parameters
max_iterthe maximum number of iterations
tolthe tolerance in finding convergence
epsilonthe simplex side

Definition at line 356 of file Likelihood.cpp.

◆ operator()()

double cbl::statistics::Likelihood::operator() ( std::vector< double > &  pp) const

evaluate the likelihood

Parameters
ppthe likelihood parameters
Returns
the likelihood \( \mathcal{L} \)

Definition at line 168 of file Likelihood.cpp.

◆ parameters()

std::shared_ptr<ModelParameters> cbl::statistics::Likelihood::parameters ( ) const
inline

return the likelihood parameters

Returns
pointer containing the likelihood parameters

Definition at line 218 of file Likelihood.h.

◆ set_data()

void cbl::statistics::Likelihood::set_data ( std::shared_ptr< data::Data data)

set the data for the likelihood analysis

Parameters
datapointer to the dataset

Definition at line 186 of file Likelihood.cpp.

◆ set_function() [1/2]

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

Parameters
likelihood_typethe likelihood type, specified with the LikelihoodType object
x_indexindex(s) of the extra info std::vector containing the point(s) where to evaluate the model
w_indexindex of the extra info std::vector containing the data point weight
precthe 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.

◆ set_function() [2/2]

void cbl::statistics::Likelihood::set_function ( const LogLikelihood_function  likelihood_function)

set the likelihood function

Parameters
likelihood_functionthe likelihood function

Definition at line 334 of file Likelihood.cpp.

◆ set_grid()

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

Parameters
npointsthe number of grid points
parameter_limits
filethe file to read/write the likelihood computed on a grid
readif true, read the likelihood grid from a list

Definition at line 225 of file Likelihood.cpp.

◆ set_log_function()

void cbl::statistics::Likelihood::set_log_function ( const LogLikelihood_function  loglikelihood_function)

set the natural logarithm of the likelihood function

Parameters
loglikelihood_functionthe loglikelihood function

Definition at line 345 of file Likelihood.cpp.

◆ set_model()

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

Parameters
modelpointer to the model
model_parametersparameters of the likelihood

Definition at line 195 of file Likelihood.cpp.

◆ write_model()

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

Parameters
output_dirthe output directory
output_filethe output file
parametersthe model parameters
xxvector of points at which the model is computed
yyvector of points at which the model is computed

Definition at line 435 of file Likelihood.cpp.

◆ write_model_at_bestfit()

void cbl::statistics::Likelihood::write_model_at_bestfit ( const std::string  output_dir,
const std::string  output_file,
const std::vector< double >  xx = {},
const std::vector< double >  yy = {} 
)
inline

write the model at xx at bestfit

Parameters
output_dirthe output directory
output_filethe output file
xxvector of points at which the model is computed
yyvector of points at which the model is computed

Definition at line 407 of file Likelihood.h.

◆ write_results()

void cbl::statistics::Likelihood::write_results ( const std::string  dir_output,
const std::string  file 
)

write best-fit results on a file

Parameters
dir_outputoutput directory
fileoutput file

Definition at line 402 of file Likelihood.cpp.

Member Data Documentation

◆ m_x_index

std::vector<size_t> cbl::statistics::Likelihood::m_x_index
protected

the index in extra info for the coordinates in which the model is computed

Definition at line 89 of file Likelihood.h.


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