CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Modelling_Distribution.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 "Modelling_Distribution.h"
37 
38 using namespace std;
39 
40 using namespace cbl;
41 
42 
43 // ============================================================================================
44 
45 
46 void cbl::modelling::distribution::Modelling_Distribution::set_model_Distribution (const statistics::PriorDistribution mean_prior, const statistics::PriorDistribution std_prior, const std::string mean_name, const std::string std_name)
47 {
48  const int nParams = 2;
49  vector<statistics::ParameterType> Par_type(nParams, statistics::ParameterType::_Base_);
50  vector<string> Par_string(nParams);
51  std::vector<statistics::PriorDistribution> param_prior (nParams);
52 
53  // Set the parameter names and priors
54  Par_string[0] = mean_name;
55  Par_string[1] = std_name;
56  param_prior[0] = mean_prior;
57  param_prior[1] = std_prior;
58 
59  // set prior
60  m_set_prior(param_prior);
61 
62  // construct the model
63  m_model = make_shared<statistics::Model1D>(statistics::Model1D(&model_gaussian, nParams, Par_type, Par_string, NULL));
64 }
65 
66 
67 // ============================================================================================
68 
69 
70 void cbl::modelling::distribution::Modelling_Distribution::set_model_Distribution (const double k, const statistics::PriorDistribution mean_prior, const statistics::PriorDistribution std0_prior, const std::string mean_name, const std::string std0_name, const std::string std_name)
71 {
72  m_data_model.k = k;
73 
74  const int nParams = 3;
75  const int nParams_derived = 1;
76 
77  vector<statistics::ParameterType> Par_type(nParams, statistics::ParameterType::_Base_);
78  Par_type[2] = statistics::ParameterType::_Derived_;
79 
80  vector<string> Par_string(nParams);
81  std::vector<statistics::PriorDistribution> param_prior (nParams-nParams_derived);
82 
83  // Set the parameter names and priors
84  Par_string[0] = mean_name;
85  Par_string[1] = std0_name;
86  Par_string[2] = std_name;
87 
88  param_prior[0] = mean_prior;
89  param_prior[1] = std0_prior;
90 
91  // set prior
92  m_set_prior(param_prior);
93 
94  // set fixed parameters
95  auto inputs = make_shared<STR_Distr_model>(m_data_model);
96 
97  // construct the model
98  m_model = make_shared<statistics::Model1D>(statistics::Model1D(&model_gaussian2, nParams, Par_type, Par_string, inputs));
99 }
100 
101 
102 // ============================================================================================
103 
104 
105 std::vector<double> cbl::modelling::distribution::model_gaussian (const std::vector<double> x, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
106 {
107  (void)inputs;
108 
109  std::shared_ptr<void> ptr;
110  std::vector<double> values(x.size());
111 
112  for (size_t i=0; i<x.size(); i++)
113  values[i] = cbl::gaussian(x[i], ptr, {parameter[0], parameter[1]});
114 
115  return values;
116 }
117 
118 
119 // ============================================================================================
120 
121 
122 std::vector<double> cbl::modelling::distribution::model_gaussian2 (const std::vector<double> x, const std::shared_ptr<void> inputs, std::vector<double> &parameter)
123 {
124  shared_ptr<STR_Distr_model> pp = static_pointer_cast<STR_Distr_model>(inputs);
125 
126  std::shared_ptr<void> ptr;
127  std::vector<double> values(x.size());
128  parameter[2] = parameter[1]*pp->k;
129 
130  for (size_t i=0; i<x.size(); i++)
131  values[i] = cbl::gaussian(x[i], ptr, {parameter[0], parameter[2]});
132 
133  return values;
134 }
135 
The class Modelling_Distribution.
void set_model_Distribution(const statistics::PriorDistribution mean_prior, const statistics::PriorDistribution std_prior, const std::string mean_name, const std::string std_name)
set the parameters of a Gaussian PDF
The class Model1D.
Definition: Model1D.h:60
The class PriorDistribution.
std::vector< double > model_gaussian2(const std::vector< double > x, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
compute a Gaussian PDF, where the standard deviation, , is expressed as
std::vector< double > model_gaussian(const std::vector< double > x, const std::shared_ptr< void > inputs, std::vector< double > &parameter)
compute a Gaussian PDF
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
T gaussian(T xx, std::shared_ptr< void > pp, std::vector< double > par)
the Gaussian function
Definition: Func.h:1127