CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Likelihood.h
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2014 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
34 #ifndef __LIKEL__
35 #define __LIKEL__
36 
37 #include "LikelihoodFunction.h"
38 
39 
40 // ============================================================================================
41 
42 
43 namespace cbl {
44 
45  namespace statistics {
46 
55  class Likelihood
56  {
57 
58  protected:
59 
61  std::shared_ptr<data::Data> m_data;
62 
64  std::shared_ptr<Model> m_model;
65 
67  std::shared_ptr<void> m_likelihood_inputs;
68 
70  std::shared_ptr<ModelParameters> m_model_parameters;
71 
74 
77 
80 
83 
86 
89  std::vector<size_t> m_x_index;
90 
92  int m_w_index;
93 
95  bool m_use_grid = false;
96 
111  void m_set_grid_likelihood_1D (const int npoints, const std::vector<std::vector<double>> parameter_limits, const std::string output_file);
112 
121  void m_set_grid_likelihood_1D (const std::string input_file);
122 
137  void m_set_grid_likelihood_2D (const int npoints, const std::vector<std::vector<double>> parameter_limits, const std::string output_file);
138 
147  void m_set_grid_likelihood_2D (const std::string input_file);
148 
149  public:
150 
155 
161 
187  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);
188 
202  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);
203 
209  virtual ~Likelihood () = default;
210 
212 
218  std::shared_ptr<ModelParameters> parameters () const { return m_model_parameters; }
219 
227  double operator () (std::vector<double> &pp) const;
228 
237  double log (std::vector<double> &parameter) const;
238 
246  void set_data (std::shared_ptr<data::Data> data);
247 
257  void set_model (std::shared_ptr<Model> model=NULL, const std::shared_ptr<ModelParameters> model_parameters=NULL);
258 
265  void unset_grid ();
266 
272  std::shared_ptr<Model> get_m_model () { return m_model; }
273 
279  std::shared_ptr<ModelParameters> get_model_parameters () { return m_model_parameters; }
280 
286  std::shared_ptr<data::Data> get_m_data () { return m_data; }
287 
303  void set_grid (const int npoints, const std::vector<std::vector<double>> parameter_limits, const std::string file, const bool read=false);
304 
326  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);
327 
333  void set_function (const LogLikelihood_function likelihood_function);
334 
340  void set_log_function (const LogLikelihood_function loglikelihood_function);
341 
349  void write_results (const std::string dir_output, const std::string file);
350 
382  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);
383 
395  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={});
396 
407  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={})
408  { write_model(output_dir, output_file, m_model_parameters->bestfit_value(), xx, yy); }
409 
410  };
411  }
412 }
413 
414 #endif
Likelihood function.
The class Likelihood.
Definition: Likelihood.h:56
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
Definition: Likelihood.cpp:435
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
Definition: Likelihood.h:407
void write_results(const std::string dir_output, const std::string file)
write best-fit results on a file
Definition: Likelihood.cpp:402
Likelihood_function m_likelihood_function
likelihood function
Definition: Likelihood.h:79
std::shared_ptr< Model > get_m_model()
get the values of the internal variable m_model
Definition: Likelihood.h:272
virtual ~Likelihood()=default
default destructor
void set_model(std::shared_ptr< Model > model=NULL, const std::shared_ptr< ModelParameters > model_parameters=NULL)
set the model for the likelihood analysis
Definition: Likelihood.cpp:195
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
Definition: Likelihood.cpp:356
std::shared_ptr< ModelParameters > parameters() const
return the likelihood parameters
Definition: Likelihood.h:218
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
Definition: Likelihood.cpp:43
std::shared_ptr< void > m_likelihood_inputs
likelihood inputs
Definition: Likelihood.h:67
double log(std::vector< double > &parameter) const
evaluate the logarithm of the likelihood for the input parameters
Definition: Likelihood.cpp:177
std::shared_ptr< ModelParameters > get_model_parameters()
get the values of the internal variable m_model
Definition: Likelihood.h:279
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 p...
Definition: Likelihood.cpp:225
LogLikelihood_function m_log_likelihood_function_grid
log-likelihood function on a grid
Definition: Likelihood.h:85
std::shared_ptr< Model > m_model
model to test
Definition: Likelihood.h:64
double operator()(std::vector< double > &pp) const
evaluate the likelihood
Definition: Likelihood.cpp:168
int m_w_index
the index in extra info where the bin weight is stored
Definition: Likelihood.h:92
void unset_grid()
set the likelihood function with internal values of LikelihoodType
Definition: Likelihood.cpp:216
void set_log_function(const LogLikelihood_function loglikelihood_function)
set the natural logarithm of the likelihood function
Definition: Likelihood.cpp:345
std::shared_ptr< ModelParameters > m_model_parameters
likelihood parameters
Definition: Likelihood.h:70
std::vector< size_t > m_x_index
Definition: Likelihood.h:89
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
Definition: Likelihood.cpp:72
LikelihoodType m_likelihood_type
type of the likelihood
Definition: Likelihood.h:73
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
Definition: Likelihood.cpp:262
LogLikelihood_function m_likelihood_function_grid
likelihood function on a grid
Definition: Likelihood.h:82
std::shared_ptr< data::Data > get_m_data()
get the values of the internal variable m_data
Definition: Likelihood.h:286
std::shared_ptr< data::Data > m_data
data containers
Definition: Likelihood.h:61
void set_data(std::shared_ptr< data::Data > data)
set the data for the likelihood analysis
Definition: Likelihood.cpp:186
LogLikelihood_function m_log_likelihood_function
log-likelihood function
Definition: Likelihood.h:76
Likelihood()
default constructor
Definition: Likelihood.h:160
LikelihoodType
the type of likelihood function
std::function< double(std::vector< double > &, const std::shared_ptr< void >)> LogLikelihood_function
definition of a function for computation of the LogLikelihood
std::function< double(std::vector< double > &, const std::shared_ptr< void >)> Likelihood_function
definition of a function for computation of the Likelihood
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38