46 m_function = [
this,
function](vector<vector<double>> xx, shared_ptr<void> inputs, vector<double> ¶meters)
48 vector<vector<double>> res(1,
function(xx[0], inputs, parameters));
59 m_function = [
this,
function](vector<vector<double>> xx, shared_ptr<void> inputs, vector<double> ¶meters)
62 vector<vector<double>> res(1,
function(xx[0], parameters));
73 vector<vector<double>> _median, _low, _up;
75 Model::stats_from_chains({1, xx}, _median, _low, _up, start, thin);
77 median_model = _median[0];
88 vector<double> pp = parameters;
91 if (xx.size() % xx_unique.size()!=0)
92 ErrorCBL(
"model.size() is not a multiple of xx.size()!",
"write",
"Model1D.cpp");
94 const int nmodels = xx.size()/xx_unique.size();
95 vector<double> model = this->operator()(xx, pp);
97 const string mkdir =
"mkdir -p "+output_dir;
98 if (system(mkdir.c_str())) {}
100 const string file = output_dir+output_file;
102 ofstream fout(file.c_str());
checkIO(fout, file);
105 for (
int nn=0; nn<nmodels; nn++) fout <<
" # [" << nn+2 <<
"] y_" << nn <<
"(x)";
106 fout <<
" ###" << endl;
108 for (
size_t i=0; i<xx_unique.size(); ++i) {
109 fout << setprecision(5) << setw(10) << right << xx_unique[i];
110 for (
int nn=0; nn<nmodels; nn++)
111 fout <<
" " << setprecision(5) << setw(10) << right << model[i+nn*xx_unique.size()];
115 fout.clear(); fout.close();
coutCBL <<
"I wrote the file: " << file << endl << endl;
124 vector<double> bf = m_parameters->bestfit_value();
125 write(output_dir, output_file, xx, bf);
136 if (xx.size() % xx_unique.size()!=0)
137 ErrorCBL(
"model.size() is not a multiple of xx.size()!",
"write_from_chains",
"Model1D.cpp");
139 const int nmodels = xx.size()/xx_unique.size();
141 vector<double> median_model, low_model, up_model;
142 stats_from_chains(xx, median_model, low_model, up_model, start, thin);
144 const string mkdir =
"mkdir -p "+output_dir;
145 if (system(mkdir.c_str())) {}
147 const string file = output_dir+output_file;
149 ofstream fout(file.c_str());
checkIO(fout, file);
152 for (
int nn=0; nn<nmodels; nn++) fout <<
" # [" << nn+2 <<
"] median y(x) # [" << nn+3 <<
"] 16% percentile y(x)# [" << nn+4 <<
"] 84% percentile y(x)";
153 fout <<
" ###" << endl;
155 for (
size_t i=0; i<xx_unique.size(); i++) {
156 fout << setprecision(5) << setw(10) << right << xx_unique[i] <<
" ";
157 for (
int nn=0; nn<nmodels; nn++)
158 fout << setprecision(5) << setw(10) << right << median_model[i+nn*xx_unique.size()] <<
" "
159 << setprecision(5) << setw(10) << right << low_model[i+nn*xx_unique.size()] <<
" "
160 << setprecision(5) << setw(10) << right << up_model[i+nn*xx_unique.size()] <<
" ";
164 fout.clear(); fout.close();
coutCBL <<
"I wrote the file: " << file << endl;
#define coutCBL
CBL print message.
void write(const std::string output_dir, const std::string output_file, const std::vector< double > xx, const std::vector< double > parameters) override
write the model at xx for given parameters
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) override
write the model at xx computing 16th, 50th and 84th percentiles from the chains.
void write_at_bestfit(const std::string output_dir, const std::string output_file, const std::vector< double > xx) override
write the model at xx with best-fit parameters obtained from likelihood maximization
void set_function(const model_function_1D function)
set the model function
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) override
compute the median and percentiles of the model from MCMC chains
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 ...
The global namespace of the CosmoBolognaLib
std::vector< T > different_elements(const std::vector< T > vect_input)
get the unique elements of a std::vector
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
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