CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
PosteriorParameters.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 __PPARAM__
34 #define __PPARAM__
35 
36 #include "ModelParameters.h"
37 #include "PosteriorDistribution.h"
38 #include "Prior.h"
39 
40 namespace cbl {
41 
42  namespace statistics {
43 
54 
55  protected:
56 
58  size_t m_nparameters_free = 0;
59 
61  size_t m_nparameters_fixed = 0;
62 
64  std::vector<unsigned int> m_fixed_parameter;
65 
67  std::vector<unsigned int> m_free_parameter;
68 
70  std::vector<std::shared_ptr<PriorDistribution>> m_parameter_prior;
71 
73  std::vector<std::shared_ptr<PriorDistribution>> m_correlated_parameter_prior;
74 
76  std::vector<std::shared_ptr<PosteriorDistribution>> m_posterior_distribution;
77 
79  std::vector<double> m_parameter_bestfit_value;
80 
82  std::vector<std::vector<double>> m_parameter_covariance;
83 
85  size_t m_chain_size;
86 
89 
91  std::vector<size_t> m_multipriors;
92 
94  std::vector<std::vector<double>> m_chain_value;
95 
101  void m_set_parameter_type () override;
102 
113  int m_inds_to_index (const int pp, const int ww) const
114  { return pp*m_chain_nwalkers+ww; }
115 
116 
117  public:
118 
123 
129  PosteriorParameters () = default;
130 
144  PosteriorParameters (const size_t nparameters, const std::vector<std::shared_ptr<PriorDistribution>> priorDistributions, std::vector<ParameterType> parameterTypes, std::vector<std::string> parameterNames);
145 
151  ~PosteriorParameters () = default;
152 
154 
155 
161  size_t nparameters_free () const override { return m_nparameters_free; }
162 
170  std::string status (const int p) const;
171 
177  std::vector<std::string> status () const;
178 
184  std::vector<unsigned int> free_parameter () const override { return m_free_parameter; }
185 
192  size_t nparameters_fixed () const override { return m_nparameters_fixed; }
193 
199  std::vector<unsigned int> fixed_parameter () const { return m_fixed_parameter; }
200 
208  std::vector<double> full_parameter (const std::vector<double> parameter_value) const override;
209 
224  void set_parameters (const size_t nparameters, const std::vector<std::shared_ptr<PriorDistribution>> priorDistributions, std::vector<ParameterType> parameterTypes, std::vector<std::string> parameterNames) override;
225 
226 
231 
241  void set_parameter_covariance (const int start=0, const int thin=1);
242 
252  double parameter_covariance (const int i, const int j) const;
253 
259  std::vector<std::vector<double>> parameter_covariance () const;
260 
262 
263 
268 
279  void set_prior_distribution (const int p, const std::shared_ptr<PriorDistribution> priorDistribution);
280 
289  void set_prior_distribution (const std::vector<std::shared_ptr<PriorDistribution>> priorDistribution);
290 
299  void set_prior_distribution_seed (const std::shared_ptr<random::UniformRandomNumbers_Int> ran_generator);
300 
310  std::shared_ptr<PriorDistribution> prior_distribution (const int p) const { return m_parameter_prior[p]; }
311 
318  std::vector<std::shared_ptr<PriorDistribution>> prior_distribution () const { return m_parameter_prior; }
319 
325  std::shared_ptr<Prior> prior () const;
326 
328 
329 
334 
342  double bestfit_value (const int p) const;
343 
349  std::vector<double> bestfit_value () const;
350 
358  void set_bestfit_values (const std::vector<double> bestfit_value);
359 
373  void set_bestfit_values (const int start, const int thin, const int nbins, const int seed) override;
374 
380  void write_bestfit_info ();
381 
383 
384 
389 
406  void set_posterior_distribution (const int start, const int thin, const int nbins, const int seed=34121, const std::vector<double> weight={});
407 
416  std::shared_ptr<PosteriorDistribution> posterior_distribution (const int par) const
417  { cbl::checkDim(m_posterior_distribution, par, "m_posterior_distribution", false); return m_posterior_distribution[par]; }
418 
420 
421 
466  void show_results (const int start, const int thin, const int nbins, const int seed=34121, const bool show_mode=false, const int ns=-1, const int nb=-1, const std::vector<double> weight={});
467 
522  void write_results (const std::string dir, const std::string file, const int start, const int thin, const int nbins, const int seed=34121, const bool compute_mode=false, const int ns=-1, const int nb=-1, const std::vector<double> weight={});
523 
529  size_t chain_size () const { return m_chain_size; }
530 
536  size_t chain_nwalkers () const { return m_chain_nwalkers; }
537 
547  void set_chain (const size_t size, const size_t nwalkers);
548 
554  void reset_chain ();
555 
563  void expand_chain (const int append);
564 
577  double chain_value (const int param, const int pos, const int ww) const
578  { return m_chain_value[param][m_inds_to_index(pos, ww)]; }
579 
590  std::vector<double> chain_value_parameter (const int pos, const int ww) const;
591 
603  std::vector<double> parameter_chain_values (const int param, const int start=0, const int thin = 1) const;
604 
619  void set_chain_value (const int param, const int pos, const int ww, const double value)
620  { m_chain_value[param][m_inds_to_index(pos, ww)] = value; }
621 
631  void set_chain_values (const std::vector<std::vector<double>> values, const int nwalkers);
632 
640  void set_chain_values (const std::vector<std::vector<std::vector<double>>> values);
641 
649  void initialize_chain (const std::vector<std::vector<double>> values);
650 
658 
670  void initialize_chain_ball (const std::vector<double> center, const double radius, const double seed);
671 
682  void initialize_chain_ball_bestfit (const double radius, const double seed);
683  };
684  }
685 }
686 
687 #endif
The class ModelParameters.
The class PosteriorDistribution.
The class Prior.
The class ModelParameters.
size_t nparameters() const
return the total number of parameters
The class PosteriorParameters.
std::vector< unsigned int > fixed_parameter() const
return the private member m_fixed_parameter
void set_parameters(const size_t nparameters, const std::vector< std::shared_ptr< PriorDistribution >> priorDistributions, std::vector< ParameterType > parameterTypes, std::vector< std::string > parameterNames) override
set the parameter
void initialize_chain_ball_bestfit(const double radius, const double seed)
initialize the chain values around bestfit values
size_t chain_size() const
return the private member m_chain_size
size_t nparameters_free() const override
return the number of free parameters
void write_results(const std::string dir, const std::string file, const int start, const int thin, const int nbins, const int seed=34121, const bool compute_mode=false, const int ns=-1, const int nb=-1, const std::vector< double > weight={})
store the results to file
void reset_chain()
reset the chain using m_size and m_nwalkers
void initialize_chain(const std::vector< std::vector< double >> values)
initialize the chain values
void set_chain_value(const int param, const int pos, const int ww, const double value)
set the private member m_chain_value at the pp-th step for the ww-th step
void set_prior_distribution(const int p, const std::shared_ptr< PriorDistribution > priorDistribution)
set the prior distribution for the p-th parameter
int m_inds_to_index(const int pp, const int ww) const
get the position in the vector m_values from position index and walker index
std::vector< std::vector< double > > m_parameter_covariance
the function parameter covariance matrix
std::shared_ptr< PosteriorDistribution > posterior_distribution(const int par) const
get the posterior distribution for the chosen parameter
std::vector< double > m_parameter_bestfit_value
the best-fit parameter values, either the medians of the MCMC chain or the maximum of the posterior (...
std::vector< unsigned int > m_fixed_parameter
the indexes of fixed parameters
size_t nparameters_fixed() const override
return the number of fixed parameters
std::vector< double > parameter_chain_values(const int param, const int start=0, const int thin=1) const
return all the chain values for a parameter
size_t m_chain_size
the lenght of the chain
void set_chain(const size_t size, const size_t nwalkers)
set the chain
std::vector< double > full_parameter(const std::vector< double > parameter_value) const override
return all the model parameters
size_t m_chain_nwalkers
the number of parallel walkers
size_t m_nparameters_fixed
the number of fixed parameters
void set_prior_distribution_seed(const std::shared_ptr< random::UniformRandomNumbers_Int > ran_generator)
set the prior distributions for the parameters
void set_bestfit_values(const std::vector< double > bestfit_value)
set the protected member m_bestfit_value
void show_results(const int start, const int thin, const int nbins, const int seed=34121, const bool show_mode=false, const int ns=-1, const int nb=-1, const std::vector< double > weight={})
show the results on the standard output
size_t chain_nwalkers() const
return the private member m_chain_nwalkers
std::vector< std::string > status() const
return all the model parameter status
std::vector< double > chain_value_parameter(const int pos, const int ww) const
return the private member m_values at the pp-th step for the ww-th step for all the parameters
void expand_chain(const int append)
expand the already existing chain
std::vector< std::shared_ptr< PriorDistribution > > m_correlated_parameter_prior
the correlated parameter prior distributions
std::shared_ptr< Prior > prior() const
get the prior function
PosteriorParameters()=default
default constructor
double chain_value(const int param, const int pos, const int ww) const
return the private member m_chain_value at the pos step for the ww-th walker, for the chosen paramete...
size_t m_nparameters_free
the numbers of free parameters
std::vector< double > bestfit_value() const
get the protected member m_parameter_bestfit_value
std::shared_ptr< PriorDistribution > prior_distribution(const int p) const
get the prior distribution for the p-th parameter
void set_posterior_distribution(const int start, const int thin, const int nbins, const int seed=34121, const std::vector< double > weight={})
set the posterior distribution from the chains
void set_chain_values(const std::vector< std::vector< double >> values, const int nwalkers)
set the chain values
std::vector< size_t > m_multipriors
the lenght of each set of multidimensional prior
std::vector< std::shared_ptr< PriorDistribution > > m_parameter_prior
the parameter prior distributions
~PosteriorParameters()=default
default destructor
void initialize_chain_from_prior()
initialize the chain values random sampling the parameter priors
std::vector< std::shared_ptr< PriorDistribution > > prior_distribution() const
get the prior distribution for the p-th parameter
void set_parameter_covariance(const int start=0, const int thin=1)
set the internal method m_parameter_covariance
std::vector< std::shared_ptr< PosteriorDistribution > > m_posterior_distribution
the parameter posterior distributions
std::vector< unsigned int > m_free_parameter
the indexes of the free parameters
std::vector< unsigned int > free_parameter() const override
return the private member m_free_parameter
std::vector< std::vector< double > > m_chain_value
content of the chain
void m_set_parameter_type() override
private member to set the parameter types
void initialize_chain_ball(const std::vector< double > center, const double radius, const double seed)
initialize the chain values
std::vector< std::vector< double > > parameter_covariance() const
return the protected member m_parameter_covariance
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void checkDim(const std::vector< T > vect, const int val, const std::string vector, bool equal=true)
check if the dimension of a std::vector is equal/lower than an input value
Definition: Kernel.h:1532