CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Modelling.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2016 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 
36 #include "Modelling.h"
37 
38 using namespace std;
39 
40 using namespace cbl;
41 
42 
43 // ============================================================================================
44 
45 
46 void cbl::modelling::Modelling::m_set_prior (vector<statistics::PriorDistribution> prior_distribution)
47 {
48  // set the priors
49  m_parameter_priors.erase(m_parameter_priors.begin(), m_parameter_priors.end());
50  for (size_t i=0; i<prior_distribution.size(); i++)
51  m_parameter_priors.emplace_back(make_shared<statistics::PriorDistribution>(prior_distribution[i]));
52 }
53 
54 
55 // ============================================================================================
56 
57 
59 
60  if ((m_likelihood!=NULL && m_parameter_priors.size()==m_model->parameters()->nparameters_base()) || m_model->parameters()->nparameters_correlated()>0)
61  m_posterior = make_shared<statistics::Posterior>(statistics::Posterior(m_parameter_priors, *m_likelihood, seed));
62  else
63  ErrorCBL("either the posterior is not defined or a wrong number of prior distributions has been provided!", "m_set_posterior", "Modelling.cpp");
64 
65 m_posterior->set_response_function(m_response_func);
66 }
67 
68 
69 // ============================================================================================
70 
71 
72 shared_ptr<statistics::Likelihood> cbl::modelling::Modelling::likelihood ()
73 {
74  if (m_likelihood!=NULL)
75  return m_likelihood;
76  else
77  ErrorCBL("the likelihood is not defined!", "likelihood", "Modelling.cpp");
78  return NULL;
79 }
80 
81 
82 // ============================================================================================
83 
84 
85 shared_ptr<cbl::statistics::Posterior> cbl::modelling::Modelling::posterior ()
86 {
87  if (m_posterior!=NULL)
88  return m_posterior;
89  else
90  ErrorCBL("the posterior is not defined!", "posterior", "Modelling.cpp");
91  return NULL;
92 }
93 
94 
95 // ============================================================================================
96 
97 
98 shared_ptr<cbl::statistics::ModelParameters> cbl::modelling::Modelling::likelihood_parameters ()
99 {
100  if (m_likelihood!=NULL)
101  return m_likelihood->parameters();
102  else
103  ErrorCBL("the likelihood is not defined!", "likelihood_parameters", "Modelling.cpp");
104  return NULL;
105 }
106 
107 
108 // ============================================================================================
109 
110 
111 shared_ptr<cbl::statistics::ModelParameters> cbl::modelling::Modelling::posterior_parameters ()
112 {
113  if (m_posterior!=NULL)
114  return m_posterior->parameters();
115  else
116  ErrorCBL("the posterior is not defined!", "posterior_parameters", "Modelling.cpp");
117  return NULL;
118 }
119 
120 
121 // ============================================================================================
122 
123 
124 void cbl::modelling::Modelling::set_likelihood (const statistics::LikelihoodType likelihood_type, const std::vector<size_t> x_index, const int w_index, const double prec, const int Nres)
125 {
126  if (m_model==NULL)
127  ErrorCBL("undefined model!", "set_likelihood", "Modelling.cpp");
128 
129  if (m_fit_range) {
130  if (m_data_fit==NULL)
131  ErrorCBL("undefined fit range!", "set_likelihood", "Modelling.cpp");
132  m_likelihood = make_shared<statistics::Likelihood> (statistics::Likelihood(m_data_fit, m_model, likelihood_type, x_index, w_index, NULL, prec, Nres));
133  }
134 
135  else {
136  if (m_data==NULL)
137  ErrorCBL("Error in set_likelihood of Modelling.cpp. Undefined dataset!", "set_likelihood", "Modelling.cpp");
138  m_likelihood = make_shared<statistics::Likelihood> (statistics::Likelihood(m_data, m_model, likelihood_type, x_index, w_index, NULL, prec, Nres));
139  }
140 }
141 
142 
143 // ============================================================================================
144 
145 
147 {
148  if (m_model==NULL)
149  ErrorCBL("undefined model!", "set_likelihood", "Modelling.cpp");
150 
151  if (m_fit_range) {
152  if (m_data_fit==NULL)
153  ErrorCBL("undefined fit range!", "set_likelihood", "Modelling.cpp");
154  m_likelihood = make_shared<statistics::Likelihood> (statistics::Likelihood(m_data_fit, m_model, log_likelihood_function, NULL));
155  }
156 
157  else {
158  if (m_data==NULL)
159  ErrorCBL("Error in set_likelihood of Modelling.cpp. Undefined dataset!", "set_likelihood", "Modelling.cpp");
160  m_likelihood = make_shared<statistics::Likelihood> (statistics::Likelihood(m_data, m_model, log_likelihood_function, NULL));
161  }
162 }
163 
164 
165 // ============================================================================================
166 
167 
168 void cbl::modelling::Modelling::maximize_likelihood (const vector<double> start, const vector<vector<double>> parameter_ranges, const unsigned int max_iter, const double tol, const double epsilon)
169 {
170  m_likelihood->maximize(start, parameter_ranges, max_iter, tol, epsilon);
171 }
172 
173 
174 // ============================================================================================
175 
176 
177 void cbl::modelling::Modelling::maximize_posterior (const std::vector<double> start, const unsigned int max_iter, const double tol, const double epsilon, const int seed)
178 {
179  m_set_posterior(seed);
180  m_posterior->maximize(start, max_iter, tol, epsilon);
181 }
182 
183 
184 // ============================================================================================
185 
186 
187 void cbl::modelling::Modelling::sample_posterior (const int chain_size, const int nwalkers, const int seed, const double aa, const bool parallel)
188 {
189  m_set_posterior(seed);
190  m_posterior->initialize_chains(chain_size, nwalkers);
191  m_posterior->sample_stretch_move(aa, parallel);
192 }
193 
194 // ============================================================================================
195 
196 
197 void cbl::modelling::Modelling::sample_posterior (const int chain_size, const int nwalkers, const double radius, const std::vector<double> start, const unsigned int max_iter, const double tol, const double epsilon, const int seed, const double aa, const bool parallel)
198 {
199  m_set_posterior(seed);
200  m_posterior->initialize_chains(chain_size, nwalkers, radius, start, max_iter, tol, epsilon);
201  m_posterior->sample_stretch_move(aa, parallel);
202 }
203 
204 // ============================================================================================
205 
206 
207 void cbl::modelling::Modelling::sample_posterior (const int chain_size, const int nwalkers, std::vector<double> &value, const double radius, const int seed, const double aa, const bool parallel)
208 {
209  m_set_posterior(seed);
210  m_posterior->initialize_chains(chain_size, nwalkers, value, radius);
211  m_posterior->sample_stretch_move(aa, parallel);
212 }
213 
214 
215 // ============================================================================================
216 
217 
218 void cbl::modelling::Modelling::sample_posterior (const int chain_size, const std::vector<std::vector<double>> chain_value, const int seed, const double aa, const bool parallel)
219 {
220  m_set_posterior(seed);
221  m_posterior->initialize_chains(chain_size, chain_value);
222  m_posterior->sample_stretch_move(aa, parallel);
223 }
224 
225 
226 // ============================================================================================
227 
228 
229 void cbl::modelling::Modelling::sample_posterior (const int chain_size, const int nwalkers, const std::string input_dir, const std::string input_file, const int seed, const double aa, const bool parallel)
230 {
231  m_set_posterior(seed);
232  m_posterior->initialize_chains(chain_size, nwalkers, input_dir, input_file);
233  m_posterior->sample_stretch_move(aa, parallel);
234 }
235 
236 
237 // ============================================================================================
238 
239 
240 void cbl::modelling::Modelling::importance_sampling (const std::string input_dir, const std::string input_file, const int seed, const vector<size_t> column, const int header_lines_to_skip, const bool is_FITS_format, const bool apply_to_likelihood)
241 {
242  m_set_posterior(seed);
243  m_posterior->importance_sampling(input_dir, input_file, column, header_lines_to_skip, is_FITS_format, apply_to_likelihood);
244 }
245 
246 
247 // ============================================================================================
248 
249 
250 void cbl::modelling::Modelling::write_chain (const string output_dir, const string output_file, const int start, const int thin, const bool is_FITS_format, const int prec, const int ww)
251 {
252  m_posterior->write_chain(output_dir, output_file, start, thin, is_FITS_format, prec, ww);
253 }
254 
255 
256 // ============================================================================================
257 
258 
259 void cbl::modelling::Modelling::read_chain (const string input_dir, const string input_file, const int nwalkers, const vector<size_t> columns, const int skip_header, const bool fits)
260 {
261  m_set_posterior(666);
262  m_posterior->read_chain(input_dir, input_file, nwalkers, columns, skip_header, fits);
263 }
264 
265 
266 // ============================================================================================
267 
268 
269 void cbl::modelling::Modelling::show_results (const int start, const int thin, const int nbins, const bool show_mode, const int ns)
270 {
271  if (m_data_fit==NULL)
272  ErrorCBL("undefined fit range!", "show_results", "Modelling.cpp");
273 
274  m_posterior->show_results(start, thin, nbins, show_mode, ns, m_data_fit->ndata());
275 }
276 
277 
278 // ============================================================================================
279 
280 
281 void cbl::modelling::Modelling::write_results (const string dir, const string file, const int start, const int thin, const int nbins, const bool fits, const bool compute_mode, const int ns)
282 {
283  if (m_data_fit==NULL)
284  ErrorCBL("undefined fit range!", "write_results", "Modelling.cpp");
285 
286  m_posterior->write_results(dir, file, start, thin, nbins, fits, compute_mode, ns, m_data_fit->ndata());
287 }
288 
289 
290 // ============================================================================================
291 
292 
293 double cbl::modelling::Modelling::reduced_chi2 (const std::vector<double> parameter)
294 {
295  if (m_posterior==NULL) m_set_posterior(666);
296 
297  return m_posterior->chi2(parameter)/(m_data_fit->ndata()-m_posterior->parameters()->nparameters_free());
298 }
The class Modelling.
void write_results(const std::string output_dir, const std::string root_file, const int start=0, const int thin=1, const int nbins=50, const bool fits=false, const bool compute_mode=false, const int ns=-1)
write the results of the MCMC sampling to file
Definition: Modelling.cpp:281
double reduced_chi2(const std::vector< double > parameter={})
the reduced
Definition: Modelling.cpp:293
void importance_sampling(const std::string input_dir, const std::string input_file, const int seed=666, const std::vector< size_t > column={}, const int header_lines_to_skip=1, const bool is_FITS_format=false, const bool apply_to_likelihood=false)
perform importance sampling
Definition: Modelling.cpp:240
std::shared_ptr< statistics::ModelParameters > likelihood_parameters()
return the likelihood parameters
Definition: Modelling.cpp:98
void maximize_likelihood(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 posterior, finds the best-fit parameters and stores them in the model
Definition: Modelling.cpp:168
void write_chain(const std::string output_dir, const std::string output_file, const int start=0, const int thin=1, const bool is_FITS_format=false, const int prec=5, const int ww=14)
write the chains obtained after the MCMC sampling
Definition: Modelling.cpp:250
void m_set_prior(std::vector< statistics::PriorDistribution > prior_distribution)
set the internal variable m_parameter_priors
Definition: Modelling.cpp:46
void show_results(const int start=0, const int thin=1, const int nbins=50, const bool show_mode=false, const int ns=-1)
show the results of the MCMC sampling on screen
Definition: Modelling.cpp:269
void read_chain(const std::string input_dir, const std::string input_file, const int nwalkers, const std::vector< size_t > columns={}, const int skip_header=1, const bool fits=false)
read the chains
Definition: Modelling.cpp:259
void m_set_posterior(const int seed)
set the interal variable m_posterior
Definition: Modelling.cpp:58
void set_likelihood(const statistics::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 function
Definition: Modelling.cpp:124
std::shared_ptr< statistics::Posterior > posterior()
return the posterior parameters
Definition: Modelling.cpp:85
void sample_posterior(const int chain_size, const int nwalkers, const int seed=666, const double aa=2, const bool parallel=true)
sample the posterior, initializing the chains by drawing from the prior distributions
Definition: Modelling.cpp:187
std::shared_ptr< statistics::Likelihood > likelihood()
return the likelihood parameters
Definition: Modelling.cpp:72
std::shared_ptr< statistics::ModelParameters > posterior_parameters()
return the posterior parameters
Definition: Modelling.cpp:111
void maximize_posterior(const std::vector< double > start, const unsigned int max_iter=10000, const double tol=1.e-6, const double epsilon=1.e-3, const int seed=666)
function that maximizes the posterior, finds the best-fit parameters and stores them in the model
Definition: Modelling.cpp:177
The class Likelihood.
Definition: Likelihood.h:56
The class Posterior.
Definition: Posterior.h:55
LikelihoodType
the type of likelihood function
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
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