CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Model2D.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 
34 #include "Model2D.h"
35 
36 using namespace std;
37 
38 using namespace cbl;
39 
40 
41 // ======================================================================================
42 
43 
45 {
46  m_function = [this, function](vector<vector<double>> xx, shared_ptr<void> inputs, vector<double> &parameters)
47  {
48  return function(xx[0], xx[1], inputs, parameters);
49  };
50 }
51 
52 
53 // ======================================================================================
54 
55 
56 void cbl::statistics::Model2D::stats_from_chains (const vector<double> xx, const vector<double> yy, vector<vector<double>> &median_model, vector<vector<double>> &low_model, vector<vector<double>> &up_model, const int start, const int thin)
57 {
58  Model::stats_from_chains({xx, yy}, median_model, low_model, up_model, start, thin);
59 }
60 
61 
62 // ======================================================================================
63 
64 
65 void cbl::statistics::Model2D::write (const string output_dir, const string output_file, const vector<double> xx, const vector<double> yy, const vector<double> parameters)
66 {
67  vector<double> pp = parameters;
68  vector<vector<double>> model = this->operator()(xx, yy, pp);
69 
70  const string mkdir = "mkdir -p "+output_dir;
71  if (system(mkdir.c_str())) {}
72 
73  const string file = output_dir+output_file;
74 
75  ofstream fout(file.c_str()); checkIO(fout, file);
76 
77  fout << "### [1] x # [2] y # [3] model(x,y) ###" << endl;
78 
79  for (size_t i=0; i<xx.size(); i++)
80  for (size_t j=0; j<yy.size(); j++)
81  fout << setprecision(5) << setw(10) << right << xx[i] << " "
82  << setprecision(5) << setw(10) << right << yy[j] << " "
83  << setprecision(5) << setw(10) << right << model[i][j] << endl;
84 
85  fout.clear(); fout.close(); coutCBL << "I wrote the file: " << file << endl << endl;
86 }
87 
88 
89 // ======================================================================================
90 
91 
92 void cbl::statistics::Model2D::write_at_bestfit (const string output_dir, const string output_file, const vector<double> xx, const vector<double> yy)
93 {
94  vector<double> bf = m_parameters->bestfit_value();
95  write(output_dir, output_file, xx, yy, bf);
96 }
97 
98 
99 // ======================================================================================
100 
101 
102 void cbl::statistics::Model2D::write_from_chains (const string output_dir, const string output_file, const vector<double> xx, const vector<double> yy, const int start, const int thin)
103 {
104  vector<vector<double>> median_model, low_model, up_model;
105  stats_from_chains(xx, yy, median_model, low_model, up_model, start, thin);
106 
107  const string mkdir = "mkdir -p "+output_dir;
108  if (system(mkdir.c_str())) {}
109 
110  const string file = output_dir+output_file;
111 
112  ofstream fout(file.c_str()); checkIO(fout, file);
113 
114  fout << "### [1] x # [2] y # [3] median model(x,y) # [4] 16% percentile model(x,y) # [5] 84% percentile ###" << endl;
115 
116  for (size_t i=0; i<xx.size(); i++)
117  for (size_t j=0; j<yy.size(); j++)
118  fout << setprecision(5) << setw(10) << right << xx[i] << " "
119  << setprecision(5) << setw(10) << right << yy[j] << " "
120  << setprecision(5) << setw(10) << right << median_model[i][j] << " "
121  << setprecision(5) << setw(10) << right << low_model[i][j] << " "
122  << setprecision(5) << setw(10) << right << up_model[i][j] << endl;
123 
124  fout.clear(); fout.close(); coutCBL << "I wrote the file: " << file << endl;
125 }
126 
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class Model2D.
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) override
write the model at xx, yy computing 16th, 50th and 84th percentiles from the chains.
Definition: Model2D.cpp:102
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) override
compute the median and percentiles of the model from MCMC chains
Definition: Model2D.cpp:56
void set_function(const model_function_2D function)
set the model function
Definition: Model2D.cpp:44
void write_at_bestfit(const std::string output_dir, const std::string output_file, const std::vector< double > xx, const std::vector< double > yy) override
write the model at xx, yy with best-fit parameters obtained from likelihood maximization
Definition: Model2D.cpp:92
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) override
write the model at xx, yy for given parameters
Definition: Model2D.cpp:65
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
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160