CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Model.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 
33 #ifndef __MODEL__
34 #define __MODEL__
35 
36 #include "ModelParameters.h"
37 
38 
39 // ======================================================================================
40 
41 
42 namespace cbl {
43 
51  namespace statistics {
52 
58  typedef std::function<std::vector<double>(std::vector<double>, std::shared_ptr<void>, std::vector<double> &)> model_function_1D;
59 
65  typedef std::function<std::vector<std::vector<double>>(std::vector<double>, std::vector<double>, std::shared_ptr<void>, std::vector<double> &)> model_function_2D;
66 
72  typedef std::function<std::vector<std::vector<double>>(std::vector<std::vector<double>>, std::shared_ptr<void>, std::vector<double> &)> model_function_generic;
73 
81  class Model {
82 
83  protected:
84 
86  std::shared_ptr<void> m_inputs;
87 
89  std::shared_ptr<ModelParameters> m_parameters;
90 
93 
96 
97  public:
98 
103 
107  Model () = default;
108 
120  Model (const int nparameters, std::vector<ParameterType> parameterTypes, std::vector<std::string> parameterNames, const std::shared_ptr<void> inputs)
121  { set_inputs(inputs); set_parameters(nparameters, parameterTypes, parameterNames); }
122 
128  virtual ~Model () = default;
129 
131 
137  Dim dimension() const { return m_dimension; }
138 
146  void set_function (const model_function_1D function)
147  { (void)function; ErrorCBL("", "set_function", "Model.h"); }
148 
159  void set_function (const std::vector<double>(*function)(const std::vector<double> xx, std::vector<double> &val))
160  { (void)function; ErrorCBL("", "set_function", "Model.h"); }
161 
167  void set_function (const model_function_2D function)
168  { (void)function; ErrorCBL("", "set_function", "Model.h"); }
169 
175  void set_inputs (std::shared_ptr<void> inputs) { m_inputs = inputs; }
176 
182  void set_parameters (const std::shared_ptr<ModelParameters> parameters) {m_parameters = parameters;}
183 
193  void set_parameters (const size_t nparameters, std::vector<ParameterType> parameterTypes, std::vector<std::string> parameterNames);
194 
200  std::shared_ptr<void> inputs () { return m_inputs; }
201 
208  std::shared_ptr<ModelParameters> parameters () { return m_parameters; }
209 
220  std::vector<std::vector<double>> operator () (const std::vector<std::vector<double>> xx, std::vector<double> &parameters) const;
221 
232  virtual double operator () (const double xx, std::vector<double> &parameters) const
233  { (void)xx; (void)parameters; ErrorCBL("", "operator ()", "Model.h"); return 0; }
234 
245  virtual std::vector<double> operator () (const std::vector<double> xx, std::vector<double> &parameters) const
246  { (void)xx; (void)parameters; ErrorCBL("", "operator ()", "Model.h"); std::vector<double> vv; return vv; }
247 
259  virtual double operator () (const double xx, const double yy, std::vector<double> &parameters) const
260  { (void)xx; (void)yy; (void)parameters; ErrorCBL("", "operator ()", "Model.h"); return 0; }
261 
275  virtual std::vector<std::vector<double>> operator () (const std::vector<double> xx, const std::vector<double> yy, std::vector<double> &parameters) const
276  { (void)xx; (void)yy; (void)parameters; ErrorCBL("", "operator ()", "Model.h"); std::vector<std::vector<double>> vv; return vv; }
277 
290  void stats_from_chains (const std::vector<std::vector<double>> xx, std::vector<std::vector<double>> &median_model, std::vector<std::vector<double>> &low_model, std::vector<std::vector<double>> &up_model, const int start=0, const int thin=1);
291 
303  virtual void stats_from_chains (const std::vector<double> xx, std::vector<double> &median_model, std::vector<double> &low_model, std::vector<double> &up_model, const int start=0, const int thin=1)
304  { (void)xx; (void)median_model; (void)low_model; (void)up_model; (void)start; (void)thin; ErrorCBL("", "stats_from_chains", "Model.h"); }
305 
306 
321  virtual void stats_from_chains (const std::vector<double> xx, const std::vector<double> yy, std::vector<std::vector<double>> &median_model, std::vector<std::vector<double>> &low_model, std::vector<std::vector<double>> &up_model, const int start=0, const int thin=1)
322  { (void)xx; (void)yy; (void)median_model; (void)low_model; (void)up_model; (void)start; (void)thin; ErrorCBL("", "stats_from_chains", "Model.h"); }
323 
335  virtual void write (const std::string output_dir, const std::string output_file, const std::vector<double> xx, const std::vector<double> parameters)
336  { (void)output_dir; (void)output_file; (void)xx; (void)parameters; cbl::ErrorCBL("", "write", "Model.h"); }
337 
352  virtual void write (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)
353  { (void)output_dir; (void)output_file; (void)xx; (void)yy; (void)parameters; cbl::ErrorCBL("", "write", "Model.h"); }
354 
364  virtual void write_at_bestfit (const std::string output_dir, const std::string output_file, const std::vector<double> xx)
365  { (void)output_dir; (void)output_file; (void)xx; cbl::ErrorCBL("", "write_at_bestfit", "Model.h"); }
366 
379  virtual void write_at_bestfit (const std::string output_dir, const std::string output_file, const std::vector<double> xx, const std::vector<double> yy)
380  { (void)output_dir; (void)output_file; (void)xx; (void)yy; cbl::ErrorCBL("", "write_at_bestfit", "Model.h"); }
381 
393  virtual void write_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)
394  { (void)output_dir; (void)output_file; (void)xx; (void)start; (void)thin; cbl::ErrorCBL("", "write_from_chains", "Model.h"); }
395 
410  virtual void write_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)
411  { (void)output_dir; (void)output_file; (void)xx; (void)yy; (void)start; (void)thin; cbl::ErrorCBL("", "write_from_chains", "Model.h"); }
412 
413  };
414  }
415 }
416 
417 #endif
The class ModelParameters.
The class Model.
Definition: Model.h:81
virtual void write_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)
write the model at xx, yy computing 16th, 50th and 84th percentiles from the chains.
Definition: Model.h:410
std::shared_ptr< void > m_inputs
inputs of the model
Definition: Model.h:86
virtual void write(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: Model.h:335
std::shared_ptr< ModelParameters > parameters()
return the model parameters
Definition: Model.h:208
void set_parameters(const std::shared_ptr< ModelParameters > parameters)
set the model parameters
Definition: Model.h:182
model_function_generic m_function
the model function
Definition: Model.h:92
virtual void write_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: Model.h:393
std::shared_ptr< ModelParameters > m_parameters
parameters of the model
Definition: Model.h:89
Dim m_dimension
the model dimension
Definition: Model.h:95
virtual void write_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, yy with best-fit parameters obtained from likelihood maximization
Definition: Model.h:379
std::vector< std::vector< double > > operator()(const std::vector< std::vector< double >> xx, std::vector< double > &parameters) const
evalueate the model function at xx, for Model1D
Definition: Model.cpp:43
Model()=default
default constructor
void set_function(const model_function_2D function)
set the model function, for Model2D
Definition: Model.h:167
virtual void write(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)
write the model at xx, yy for given parameters
Definition: Model.h:352
void set_inputs(std::shared_ptr< void > inputs)
set the model inputs
Definition: Model.h:175
virtual void write_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 likelihood maximization
Definition: Model.h:364
virtual void stats_from_chains(const std::vector< double > xx, std::vector< double > &median_model, std::vector< double > &low_model, std::vector< double > &up_model, const int start=0, const int thin=1)
compute the median and percentiles of the model from MCMC chains
Definition: Model.h:303
virtual ~Model()=default
default destructor
void set_function(const std::vector< double >(*function)(const std::vector< double > xx, std::vector< double > &val))
set the model function, for Model1D
Definition: Model.h:159
std::shared_ptr< void > inputs()
return the model inputs
Definition: Model.h:200
Dim dimension() const
return the model dimension
Definition: Model.h:137
virtual void stats_from_chains(const std::vector< double > xx, const std::vector< double > yy, std::vector< std::vector< double >> &median_model, std::vector< std::vector< double >> &low_model, std::vector< std::vector< double >> &up_model, const int start=0, const int thin=1)
compute the median and percentiles of the model from MCMC chains
Definition: Model.h:321
void set_function(const model_function_1D function)
set the model function, for Model1D
Definition: Model.h:146
Model(const int nparameters, std::vector< ParameterType > parameterTypes, std::vector< std::string > parameterNames, const std::shared_ptr< void > inputs)
constructor
Definition: Model.h:120
void stats_from_chains(const std::vector< std::vector< double >> xx, std::vector< std::vector< double >> &median_model, std::vector< std::vector< double >> &low_model, std::vector< std::vector< double >> &up_model, const int start=0, const int thin=1)
compute the median and percentiles of the model from MCMC chains
Definition: Model.cpp:62
std::function< std::vector< std::vector< double > >std::vector< double >, std::vector< double >, std::shared_ptr< void >, std::vector< double > &)> model_function_2D
2D function: the inputs are the values at which the function is computed, a pointer to a set of data ...
Definition: Model.h:65
std::function< std::vector< std::vector< double > >std::vector< std::vector< double > >, std::shared_ptr< void >, std::vector< double > &)> model_function_generic
generic function: the inputs are the values at which the function is computed, a pointer to a set of ...
Definition: Model.h:72
std::function< std::vector< double >std::vector< double >, std::shared_ptr< void >, std::vector< double > &)> model_function_1D
1D function: the inputs are a vector of values at which the function is computed, a pointer to a set ...
Definition: Model.h:58
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
Dim
the dimension, used e.g. for pair and triplet vectors
Definition: Kernel.h:483