CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
CombinedModelling.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2021 by Federico Marulli and Giorgio Lesci *
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 
36 #include "CombinedModelling.h"
37 
38 using namespace std;
39 
40 using namespace cbl;
41 
42 
43 // ============================================================================================
44 
45 
46 cbl::modelling::CombinedModelling::CombinedModelling (std::vector<std::shared_ptr<modelling::Modelling>> modelling, std::vector<std::string> repeated_par, const std::vector<std::vector<std::vector<int>>> common_repeated_par)
47 {
48  std::vector<std::shared_ptr<statistics::Posterior>> posteriors(modelling.size());
49  for (size_t i=0; i<modelling.size(); i++) {
50  modelling[i]->m_set_posterior(321);
51  auto posterior_ptr = modelling[i]->posterior();
52  posteriors[i] = std::move(posterior_ptr);
53  }
54  m_combined_posterior = make_shared<statistics::CombinedPosterior>(statistics::CombinedPosterior(posteriors, repeated_par, common_repeated_par));
55 }
56 
57 
58 // ============================================================================================
59 
60 
61 cbl::modelling::CombinedModelling::CombinedModelling (std::vector<std::vector<std::shared_ptr<modelling::Modelling>>> modelling, std::vector<std::shared_ptr<data::CovarianceMatrix>> covariance, const std::vector<cbl::statistics::LikelihoodType> likelihood_types, const std::vector<std::string> repeated_par, const std::vector<std::vector<std::vector<int>>> common_repeated_par, const std::vector<std::shared_ptr<cosmology::SuperSampleCovariance>> SSC)
62 {
63  std::vector<std::vector<std::shared_ptr<statistics::Posterior>>> posteriors(modelling.size());
64  for (size_t i=0; i<modelling.size(); i++) {
65  posteriors[i].resize(modelling[i].size());
66  for (size_t j=0; j<modelling[i].size(); j++) {
67  modelling[i][j]->m_set_posterior(321);
68  auto posterior_ptr = modelling[i][j]->posterior();
69  posteriors[i][j] = std::move(posterior_ptr);
70  }
71  }
72  m_combined_posterior = make_shared<statistics::CombinedPosterior>(statistics::CombinedPosterior(posteriors, covariance, likelihood_types, repeated_par, common_repeated_par, SSC));
73 }
74 
75 
76 // ============================================================================================
77 
78 
79 void cbl::modelling::CombinedModelling::maximize_combined_posterior (const std::vector<double> start, const unsigned int max_iter, const double tol, const double epsilon)
80 {
81  m_combined_posterior->maximize(start, max_iter, tol, epsilon);
82 }
83 
84 
85 // ============================================================================================
86 
87 
88 void cbl::modelling::CombinedModelling::sample_combined_posterior (const int chain_size, const int nwalkers, const double aa, const bool parallel)
89 {
90  m_combined_posterior->initialize_chains(chain_size, nwalkers);
91  m_combined_posterior->sample_stretch_move(aa, parallel);
92 }
93 
94 
95 // ============================================================================================
96 
97 
98 void cbl::modelling::CombinedModelling::sample_combined_posterior (const int chain_size, const int n_walkers, const double radius, const std::vector<double> start, const unsigned int max_iter, const double tol, const double epsilon, const double aa, const bool parallel)
99 {
100  m_combined_posterior->initialize_chains(chain_size, n_walkers, radius, start, max_iter, tol, epsilon);
101  m_combined_posterior->sample_stretch_move(aa, parallel);
102 }
103 
104 
105 // ============================================================================================
106 
107 
108 void cbl::modelling::CombinedModelling::sample_combined_posterior (const int chain_size, const int nwalkers, const std::string input_dir, const std::string input_file, const int seed, const double aa, const bool parallel)
109 {
110  m_combined_posterior->initialize_chains(chain_size, nwalkers, input_dir, input_file, seed);
111  m_combined_posterior->sample_stretch_move(aa, parallel);
112 }
113 
114 
115 // ============================================================================================
116 
117 
118 void cbl::modelling::CombinedModelling::write_combined_results (const std::string output_dir, const std::string root_file, const int start, const int thin, const int nbins, const bool fits, const bool compute_mode, const int ns)
119 {
120  m_combined_posterior->write_results(output_dir, root_file, start, thin, nbins, fits, compute_mode, ns);
121 }
122 
123 
124 // ============================================================================================
125 
126 
127 void cbl::modelling::CombinedModelling::write_model_from_combined_chain (const std::string output_dir, const std::string output_file, const int start, const int thin, const std::vector<double> xx, const std::vector<double> yy)
128 {
129  m_combined_posterior->write_model_from_chain(output_dir, output_file, start, thin, xx, yy);
130 }
The class CombinedModelling.
void sample_combined_posterior(const int chain_size, const int nwalkers, const double aa=2, const bool parallel=true)
sample the posterior, initializing the chains by drawing from the prior distributions
CombinedModelling()=default
default constuctor
void write_combined_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
void write_model_from_combined_chain(const std::string output_dir, const std::string output_file, const int start, const int thin, const std::vector< double > xx={}, const std::vector< double > yy={})
write the model computing 16th, 50th and 84th percentiles from the MCMC
void maximize_combined_posterior(const std::vector< double > start, const unsigned int max_iter=10000, const double tol=1.e-6, const double epsilon=1.e-3)
function that maximizes the combined posterior, finds the best-fit parameters and stores them in the ...
The class CombinedPosterior.
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38