CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Chi2.cpp
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 
33 #include "Chi2.h"
34 
35 using namespace std;
36 
37 using namespace cbl;
38 
39 
40 // ============================================================================================
41 
42 
43 double cbl::statistics::Chi2::operator() (vector<double> &pp) const
44 {
45  if (m_likelihood_type==statistics::LikelihoodType::_NotSet_)
46  ErrorCBL("a dataset should be provided!", "operator ()", "Chi2.cpp");
47 
48  return -2*m_log_likelihood_function(pp, m_likelihood_inputs);
49 }
50 
51 
52 // ============================================================================================
53 
54 
55 cbl::statistics::Chi2::Chi2 (const std::shared_ptr<data::Data> data, const std::shared_ptr<Model> model, const bool use_covariance, const std::vector<size_t> x_index, const int w_index)
56 {
57  set_data(data);
58  set_model(model);
59  m_x_index = x_index;
60  m_w_index = w_index;
61 
62  if (use_covariance)
63  set_function(LikelihoodType::_Gaussian_Covariance_, m_x_index, m_w_index);
64  else
65  set_function(LikelihoodType::_Gaussian_Error_, m_x_index, m_w_index);
66 
67  m_likelihood_inputs = make_shared<STR_likelihood_inputs>(STR_likelihood_inputs(m_data, m_model, m_x_index, m_w_index));
68 }
69 
70 
71 // ============================================================================================
72 
73 
74 void cbl::statistics::Chi2::minimize (const vector<double> start, vector<vector<double>> parameter_limits, const unsigned int max_iter, const double tol, const double epsilon)
75 {
76  if (m_likelihood_type==statistics::LikelihoodType::_NotSet_)
77  ErrorCBL("a dataset should be provided!", "minimize", "Chi2.cpp");
78 
79  unsigned int npar = m_model->parameters()->nparameters_free();
80 
81  if (npar==0)
82  ErrorCBL("there is no parameter free to vary!", "minimize", "Chi2.cpp");
83  if (start.size() != npar && parameter_limits.size()!=0)
84  ErrorCBL("wrong size for the vector of starting parameters!", "minimize", "Chi2.cpp");
85  if (parameter_limits.size() != npar && parameter_limits.size()!=0)
86  ErrorCBL("wrong size for the vector of parameter limits!", "minimize", "Chi2.cpp");
87 
88 
89  function<double(vector<double> &)> func = [this](vector<double> & pp) {
90  return this->operator()(pp);
91  };
92 
93  coutCBL << "Minimizing..." << endl;
94  vector<double> result = cbl::wrapper::gsl::GSL_minimize_nD(func, start, parameter_limits, max_iter, tol, epsilon);
95  coutCBL << "Done!" << endl << endl;
96 
97  m_model->parameters()->set_bestfit_values(result);
98 
99  m_model->parameters()->write_bestfit_info();
100  coutCBL << "Chi2 = " << this->operator()(result) << endl << endl;
101 }
The class Chi2.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
Chi2()
default constructor
Definition: Chi2.h:68
double operator()(std::vector< double > &pp) const
evaluate the
Definition: Chi2.cpp:43
void minimize(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 minimizes the , finds the best-fit parameters and stores them in model parameters
Definition: Chi2.cpp:74
std::vector< double > GSL_minimize_nD(FunctionDoubleVector func, const std::vector< double > start, const std::vector< std::vector< double >> ranges, const unsigned int max_iter=1000, const double tol=1.e-6, const double epsilon=0.1)
minimize the provided function using GSL procedure
Definition: GSLwrapper.cpp:497
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
the struct STR_likelihood_inputs