CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Modelling.h
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 
34 #ifndef __MODELLING__
35 #define __MODELLING__
36 
37 
38 #include "Posterior.h"
39 
40 
41 // ===================================================================================================
42 
43 
44 namespace cbl {
45 
53  namespace modelling {
54 
64  class Modelling {
65 
66  protected:
67 
69  std::shared_ptr<data::Data> m_data = NULL;
70 
72  bool m_fit_range = false;
73 
75  std::shared_ptr<data::Data> m_data_fit;
76 
78  std::shared_ptr<statistics::Model> m_model = NULL;
79 
81  std::shared_ptr<statistics::Model> m_response_func = NULL;
82 
84  std::shared_ptr<statistics::Likelihood> m_likelihood = NULL;
85 
87  std::vector<std::shared_ptr<statistics::PriorDistribution>> m_parameter_priors;
88 
90  std::shared_ptr<statistics::Posterior> m_posterior = NULL;
91 
98  void m_set_prior (std::vector<statistics::PriorDistribution> prior_distribution);
99 
104  void m_isSet_response () {if (m_response_func == NULL) ErrorCBL("the response function for the SSC is not set!", "m_isSet_response", "Modelling.h");};
105 
106  public:
107 
112 
116  Modelling () = default;
117 
121  virtual ~Modelling () = default;
122 
124 
130  void m_set_posterior (const int seed);
131 
136 
141  std::shared_ptr<data::Data> data () { return m_data; }
142 
147  std::shared_ptr<data::Data> data_fit ()
148  {
149  if (!m_fit_range)
150  ErrorCBL("no fit range has been set!", "data_fit", "Modelling.h");
151 
152  return m_data_fit;
153  }
154 
159  std::shared_ptr<statistics::Likelihood> likelihood ();
160 
165  std::shared_ptr<statistics::Posterior> posterior ();
166 
171  std::shared_ptr<statistics::ModelParameters> likelihood_parameters ();
172 
177  std::shared_ptr<statistics::ModelParameters> posterior_parameters ();
178 
186  virtual void set_parameter_from_string (const std::string parameter, const double value)
187  { (void)parameter; (void)value; ErrorCBL("", "set_parameter_from_string", "Modelling.h"); }
188 
196  virtual double get_parameter_from_string (const std::string parameter) const
197  { return ErrorCBL("", "get_parameter_from_string ("+parameter+")", "Modelling.h"); }
198 
206  std::shared_ptr<statistics::PriorDistribution> get_prior (const int i) { return m_parameter_priors[i]; }
207 
214  std::shared_ptr<statistics::Model> get_response_function () {m_isSet_response(); return m_response_func;};
215 
217 
218 
223 
230  void reset_fit_range () { m_fit_range = false; }
231 
239  void set_fit_range (const double xmin, const double xmax);
240 
252  void set_fit_range (const double xmin, const double xmax, const double ymin, const double ymax);
253 
259  void set_data (const std::shared_ptr<data::Data> dataset) { m_data = move(dataset); }
260 
291  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);
292 
311  void set_likelihood (const cbl::statistics::Likelihood_function log_likelihood_function);
312 
314 
315 
321 
354  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);
355 
388  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);
389 
408  void sample_posterior (const int chain_size, const int nwalkers, const int seed=666, const double aa=2, const bool parallel=true);
409 
444  void sample_posterior (const int chain_size, const int nwalkers, const double radius, 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, const double aa=2, const bool parallel=true);
445 
469  void sample_posterior (const int chain_size, const int nwalkers, std::vector<double> &value, const double radius, const int seed=666, const double aa=2, const bool parallel=true);
470 
490  void sample_posterior (const int chain_size, const std::vector<std::vector<double>> chain_value, const int seed=666, const double aa=2, const bool parallel=true);
491 
515  void sample_posterior (const int chain_size, const int nwalkers, const std::string input_dir, const std::string input_file, const int seed=666, const double aa=2, const bool parallel=true);
516 
549  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);
550 
552 
553 
558 
578  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);
579 
597  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);
598 
637  void show_results (const int start=0, const int thin=1, const int nbins=50, const bool show_mode=false, const int ns=-1);
638 
696  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);
697 
711  virtual void write_model (const std::string output_dir, const std::string output_file, const std::vector<double> xx, const std::vector<double> parameters);
712 
730  virtual void write_model (const std::string output_dir, const std::string output_file, const std::vector<double> xx, const std::vector<double> yy, const std::vector<double> parameters);
731 
742  virtual void write_model_at_bestfit (const std::string output_dir, const std::string output_file, const std::vector<double> xx);
743 
758  virtual 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);
759 
774  virtual void write_model_from_chains (const std::string output_dir, const std::string output_file, const std::vector<double> xx, const int start=0, const int thin=1);
775 
794  virtual void write_model_from_chains (const std::string output_dir, const std::string output_file, const std::vector<double> xx, const std::vector<double> yy, const int start=0, const int thin=1);
795 
808  double reduced_chi2 (const std::vector<double> parameter={});
809 
811 
812  };
813  }
814 }
815 
816 #endif
The class Posterior.
The class Modelling.
Definition: Modelling.h:64
std::shared_ptr< statistics::Model > m_response_func
response function for the computation of the super-sample covariance
Definition: Modelling.h:81
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
std::shared_ptr< statistics::Model > get_response_function()
return the response function used to compute the super-sample covariance
Definition: Modelling.h:214
bool m_fit_range
check if fit range has been set
Definition: Modelling.h:72
virtual double get_parameter_from_string(const std::string parameter) const
get the value of a parameter providing its name string
Definition: Modelling.h:196
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 reset_fit_range()
reset the fit range
Definition: Modelling.h:230
std::shared_ptr< data::Data > m_data
input data to be modelled
Definition: Modelling.h:69
std::shared_ptr< data::Data > data_fit()
return the dataset
Definition: Modelling.h:147
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
std::vector< std::shared_ptr< statistics::PriorDistribution > > m_parameter_priors
prior
Definition: Modelling.h:87
virtual void set_parameter_from_string(const std::string parameter, const double value)
set the value of a parameter providing its name string
Definition: Modelling.h:186
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
std::shared_ptr< statistics::Posterior > m_posterior
posterior
Definition: Modelling.h:90
std::shared_ptr< statistics::Likelihood > m_likelihood
likelihood
Definition: Modelling.h:84
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
virtual void write_model_from_chains(const std::string output_dir, const std::string output_file, const std::vector< double > xx, const int start=0, const int thin=1)
write the model at xx computing 16th, 50th and 84th percentiles from the chains
Definition: Modelling1D.cpp:80
std::shared_ptr< statistics::PriorDistribution > get_prior(const int i)
get the internal variable m_parameter_priors
Definition: Modelling.h:206
virtual ~Modelling()=default
default destructor
virtual void write_model(const std::string output_dir, const std::string output_file, const std::vector< double > xx, const std::vector< double > parameters)
write the model at xx for given parameters
Definition: Modelling1D.cpp:56
void m_set_posterior(const int seed)
set the interal variable m_posterior
Definition: Modelling.cpp:58
void set_data(const std::shared_ptr< data::Data > dataset)
set the dataset
Definition: Modelling.h:259
Modelling()=default
default constuctor
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< data::Data > data()
return the dataset
Definition: Modelling.h:141
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
void m_isSet_response()
check if the response function used to compute the super-sample covariance is set
Definition: Modelling.h:104
void set_fit_range(const double xmin, const double xmax)
set the fit range
Definition: Modelling1D.cpp:46
std::shared_ptr< statistics::Likelihood > likelihood()
return the likelihood parameters
Definition: Modelling.cpp:72
std::shared_ptr< data::Data > m_data_fit
input data restricted to the range used for the fit
Definition: Modelling.h:75
std::shared_ptr< statistics::Model > m_model
input model
Definition: Modelling.h:78
virtual void write_model_at_bestfit(const std::string output_dir, const std::string output_file, const std::vector< double > xx)
write the model at xx with best-fit parameters obtained from posterior maximization
Definition: Modelling1D.cpp:68
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
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