CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Model.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 "Model.h"
35 
36 using namespace std;
37 
38 using namespace cbl;
39 
40 // ======================================================================================
41 
42 
43 vector<vector<double>> cbl::statistics::Model::operator() (const std::vector<std::vector<double>> xx, std::vector<double> &parameters) const
44 {
45  parameters = m_parameters->full_parameter(parameters);
46  return m_function(xx, m_inputs, parameters);
47 }
48 
49 
50 // ======================================================================================
51 
52 
53 void cbl::statistics::Model::set_parameters (const size_t nparameters, std::vector<ParameterType> parameterTypes, std::vector<std::string> parameterNames)
54 {
55  m_parameters = make_shared<cbl::statistics::ModelParameters>(cbl::statistics::ModelParameters(nparameters, parameterTypes, parameterNames));
56 }
57 
58 
59 // ======================================================================================
60 
61 
62 void cbl::statistics::Model::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, const int thin)
63 {
64  int sz1, sz2;
65  if (m_dimension==Dim::_1D_) {
66  sz1 = 1;
67  sz2 = int(xx[0].size());
68  }
69  else if (m_dimension==Dim::_2D_) {
70  sz1 = int(xx[0].size());
71  sz2 = int(xx[1].size());
72  }
73  else
74  ErrorCBL("wrong size for xx vector!", "stats_from_chains", "Model.cpp");
75 
76  vector<double> _median(sz1*sz2, 0);
77  vector<double> _low(sz1*sz2, 0);
78  vector<double> _up(sz1*sz2, 0);
79 
80  vector<vector<double>> models(floor((m_parameters->chain_size()-start)/thin)*m_parameters->chain_nwalkers(), vector<double>(0));
81 
82 #pragma omp parallel num_threads(omp_get_max_threads())
83  {
84 #pragma omp for schedule(static,2)
85  for (size_t j=start; j<m_parameters->chain_size(); j+=thin) {
86 
87  for (size_t i=0; i<m_parameters->chain_nwalkers(); i++) {
88 
89  vector<double> parameters(m_parameters->nparameters());
90  for (size_t k=0; k<m_parameters->nparameters(); k++)
91  parameters[k] = m_parameters->chain_value(k, j, i);
92 
93  models[(j-start)/thin*m_parameters->chain_nwalkers()+i] = flatten(this->operator()(xx, parameters));
94  }
95  }
96  }
97 
98  vector<vector<double>> tr_models = transpose(models);
99 
100  for (size_t i=0; i<tr_models.size(); i++) {
101  vector<double> vv = tr_models[i];
102  sort(vv.begin(), vv.end());
103  int low = vv.size()*0.16;
104  int up = vv.size()*0.84;
105  int median = vv.size()*0.5;
106  _median[i] = vv[median];
107  _low[i] = vv[low];
108  _up[i] = vv[up];
109  }
110 
111  median_model = reshape(_median, sz1, sz2);
112  low_model = reshape(_low, sz1, sz2);
113  up_model = reshape(_up, sz1, sz2);
114 }
The class Model.
The class ModelParameters.
void set_parameters(const std::shared_ptr< ModelParameters > parameters)
set the model parameters
Definition: Model.h:182
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
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
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
std::vector< T > flatten(std::vector< std::vector< T >> matrix)
flatten a matrix in a vector of size
Definition: Kernel.h:1686
std::vector< std::vector< T > > transpose(std::vector< std::vector< T >> matrix)
transpose a matrix
Definition: Kernel.h:1729
std::vector< std::vector< T > > reshape(std::vector< T > vec, const int size1, const int size2)
reshape a vector into a matrix of given number of rows and columns
Definition: Kernel.h:1707