CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Distribution.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 "Distribution.h"
35 
36 using namespace std;
37 
38 using namespace cbl;
39 using namespace random;
40 
41 
42 // ======================================================================================
43 
44 
46 {
47  function<double(double)> f_moment = [this] (double x) {return this->operator()(x);};
48  return wrapper::gsl::GSL_integrate_cquad(f_moment, m_xmin, xx, 1.e-4);
49 }
50 
51 
52 // ======================================================================================
53 
54 
56 {
57  function<double(double)> f = bind(m_func, std::placeholders::_1, m_distribution_func_fixed_pars, m_distribution_func_pars);
58 
59  m_distribution_normalization = wrapper::gsl::GSL_integrate_cquad(f, m_xmin, m_xmax, 1.e-4);
60  m_log_distribution_normalization = log(m_distribution_normalization);
61 }
62 
63 
64 // ======================================================================================
65 
66 
67 cbl::glob::Distribution::Distribution (const cbl::glob::DistributionType distributionType, const double value)
68 {
69  if (distributionType!=glob::DistributionType::_Constant_)
70  ErrorCBL("this constructor only allows DistributionType::_Constant_ !", "Distribution", "Distribution.cpp");
71 
72  set_constant_distribution(value);
73 }
74 
75 
76 
77 // ======================================================================================
78 
79 
80 cbl::glob::Distribution::Distribution (const cbl::glob::DistributionType distributionType, const double xmin, const double xmax, const int seed)
81 {
82  if (distributionType != glob::DistributionType::_Uniform_)
83  ErrorCBL("this constructor only allows DistributionType::_Uniform_ !", "Distribution", "Distribution.cpp");
84 
85  set_uniform_distribution(xmin, xmax, seed);
86 }
87 
88 
89 // ======================================================================================
90 
91 
92 cbl::glob::Distribution::Distribution (const cbl::glob::DistributionType distributionType, const std::vector<double> distribution_params, const double xmin, const double xmax , const int seed)
93 {
94  set_limits(xmin, xmax);
95 
96  if (distributionType == glob::DistributionType::_Gaussian_) {
97  if (distribution_params.size() != 2)
98  ErrorCBL("wrong size of distribution_params: Gaussian distribution needs 2 parameters, the mean and the standard deviation!", "Distribution", "Distribution.cpp");
99 
100  set_gaussian_distribution(distribution_params[0], distribution_params[1], seed);
101  }
102 
103  else if (distributionType == glob::DistributionType::_Poisson_) {
104  if (distribution_params.size() != 1)
105  ErrorCBL("wrong size of distribution_params: poisson distribution needs 1 parameter, the mean", "Distribution", "Distribution.cpp");
106 
107  set_poisson_distribution(distribution_params[0], seed);
108  }
109 
110  else
111  ErrorCBL("no such type of distribution", "Distribution", "Distribution.cpp");
112 }
113 
114 
115 // ======================================================================================
116 
117 
118 cbl::glob::Distribution::Distribution (const DistributionType distributionType, const distribution_func func, const std::shared_ptr<void> distribution_fixed_pars, const std::vector<double> distribution_pars, const double xmin, const double xmax, const int seed)
119 {
120  set_limits(xmin, xmax);
121 
122  if (distributionType != glob::DistributionType::_Custom_)
123  ErrorCBL("this constructor only allows DistributionType::_Custom_ !", "Distribution", "Distribution.cpp");
124 
125  set_custom_distribution(func, distribution_fixed_pars, distribution_pars, seed);
126 }
127 
128 
129 // ======================================================================================
130 
131 
132 cbl::glob::Distribution::Distribution (const cbl::glob::DistributionType distributionType, const std::vector<double> discrete_values, const std::vector<double> weights, const int seed)
133 {
134  if (distributionType != glob::DistributionType::_Discrete_)
135  ErrorCBL("this constructor only allows DistributionType::_Discrete_ !", "Distribution", "Distribution.cpp");
136 
137  set_discrete_values(discrete_values, weights, seed);
138 }
139 
140 
141 // ======================================================================================
142 
143 
144 cbl::glob::Distribution::Distribution (const cbl::glob::DistributionType distributionType, const std::vector<double> var, const std::vector<double> dist, const int nbin, const std::string interpolationType, const int seed)
145 {
146  if (distributionType == glob::DistributionType::_Discrete_)
147  {
148  vector<double> vv, dd, edd;
149  get_distribution(vv, dd, edd, var, dist, nbin);
150 
151  set_binned_distribution(vv, dd, interpolationType, seed);
152  }
153  else if (distributionType == glob::DistributionType::_Interpolated_)
154  {
155  set_binned_distribution(var, dist, interpolationType, seed);
156  }
157  else
158  ErrorCBL("no such type of distribution", "Distribution", "Distribution.cpp");
159 }
160 
161 
162 // ======================================================================================
163 
164 
166 {
167  if (xx<m_xmin || xx>m_xmax) return 0;
168  else return m_func(xx, m_distribution_func_fixed_pars, m_distribution_func_pars)/m_distribution_normalization;
169 }
170 
171 
172 // ======================================================================================
173 
174 
176 {
177  if (xx<m_xmin || xx>m_xmax) return par::defaultDouble;
178  else return log(m_func(xx, m_distribution_func_fixed_pars, m_distribution_func_pars))-m_log_distribution_normalization;
179 }
180 
181 
182 // ======================================================================================
183 
184 
185 void cbl::glob::Distribution::set_limits(const double xmin, const double xmax)
186 {
187  m_xmin = xmin;
188  m_xmax = xmax;
189 }
190 
191 
192 // ======================================================================================
193 
194 
196 {
197  m_distributionType = glob::DistributionType::_Constant_;
198 
200 
201  m_distribution_random = make_shared<ConstantRandomNumbers> (ConstantRandomNumbers(value));
202 
203  m_func = &identity<double>;
204  m_distribution_normalization = 1.;
205  m_log_distribution_normalization = 0.;
206 }
207 
208 // ======================================================================================
209 
210 
211 void cbl::glob::Distribution::set_uniform_distribution (const double xmin, const double xmax, const int seed)
212 {
213 
214  m_distributionType = glob::DistributionType::_Uniform_;
215 
216  set_limits(xmin, xmax);
217  m_distribution_func_pars.erase(m_distribution_func_pars.begin(), m_distribution_func_pars.end());
218 
219  m_distribution_func_pars.push_back(m_xmax);
220  m_distribution_func_pars.push_back(m_xmax);
221 
222  m_distribution_random = make_shared<UniformRandomNumbers> (UniformRandomNumbers(m_xmin, m_xmax, seed));
223  m_func = &identity<double>;
224 
225  m_distribution_normalization = m_xmax-m_xmin;
226  m_log_distribution_normalization = log(m_xmax-m_xmin);
227 }
228 
229 
230 // ======================================================================================
231 
232 
233 void cbl::glob::Distribution::set_gaussian_distribution (const double mean, const double sigma, const int seed)
234 {
235  m_distributionType = glob::DistributionType::_Gaussian_;
236 
237  m_distribution_func_pars.erase(m_distribution_func_pars.begin(), m_distribution_func_pars.end());
238  m_distribution_func_pars.push_back(mean);
239  m_distribution_func_pars.push_back(sigma);
240 
241  m_distribution_random = make_shared<NormalRandomNumbers> (NormalRandomNumbers(mean, sigma, seed, m_xmin, m_xmax));
242  m_func = &gaussian<double>;
243  m_mean = mean;
244  m_sigma = sigma;
245  m_seed = seed;
246 
247  m_distribution_normalization = 0.5*(erf((m_xmax-mean)/sigma/sqrt(2.))-erf((m_xmin-mean)/sigma/sqrt(2.)));
248  m_log_distribution_normalization = log(m_distribution_normalization);
249 }
250 
251 
252 // ======================================================================================
253 
254 
255 void cbl::glob::Distribution::set_poisson_distribution (const double mean, const int seed)
256 {
257 
258  m_distributionType = glob::DistributionType::_Poisson_;
259 
260  m_xmin = nint(m_xmin);
261  m_xmax = nint(m_xmax);
262  int nbins = m_xmax-m_xmin;
263 
264  vector<double> poisson_values = linear_bin_vector(nbins, m_xmin, m_xmax);
265  vector<double> weights;
266  for(int i=0;i<nbins;i++)
267  weights.push_back(poisson(poisson_values[i],NULL,{mean}));
268 
269  m_distribution_random = make_shared<DiscreteRandomNumbers> (DiscreteRandomNumbers(poisson_values, weights, seed, m_xmin, m_xmax));
270 
271  glob::STR_closest_probability parameters;
272  parameters.values = poisson_values;
273  parameters.weights = weights;
274 
275  m_distribution_func_fixed_pars = make_shared<glob::STR_closest_probability>(parameters);
276  m_func = &closest_probability;
277 
278  m_distribution_normalization = accumulate(weights.begin(), weights.end(), 0);
279  m_log_distribution_normalization = log(m_distribution_normalization);
280 }
281 
282 
283 // =====================================================================================
284 
285 
286 void cbl::glob::Distribution::set_discrete_values (const std::vector<double> discrete_values, const std::vector<double> weights, const int seed)
287 {
288  m_distributionType = glob::DistributionType::_Discrete_;
289 
290  if (discrete_values.size()==0)
291  ErrorCBL("the input vector of discrete values is empty", "set_discrete_values", "Distribution.cpp");
292 
293  set_limits(Min(discrete_values), Max(discrete_values));
294 
295  m_distribution_random = make_shared<DiscreteRandomNumbers> (DiscreteRandomNumbers(discrete_values, weights, seed, m_xmin, m_xmax));
296 
297  vector<double> ww = weights;
298 
299  if (ww.size() == 0)
300  ww.resize(discrete_values.size(), 1);
301 
302  glob::STR_closest_probability parameters;
303  parameters.values = discrete_values;
304  parameters.weights = ww;
305 
306  m_distribution_func_fixed_pars = make_shared<glob::STR_closest_probability>(parameters);
307 
308  m_func = &closest_probability;
309  m_distribution_normalization = accumulate(ww.begin(), ww.end(), 0);
310  m_log_distribution_normalization = log(m_distribution_normalization);
311 }
312 
313 
314 // =====================================================================================
315 
316 
317 void cbl::glob::Distribution::set_custom_distribution (const distribution_func func, const shared_ptr<void> distribution_fixed_pars, const std::vector<double> distribution_pars, const int seed)
318 {
319 
320  m_distributionType = glob::DistributionType::_Custom_;
321 
322  m_func = func;
323  m_distribution_func_fixed_pars = distribution_fixed_pars;
324  m_distribution_func_pars = distribution_pars;
325 
326  m_distribution_random = make_shared<CustomDistributionRandomNumbers> (CustomDistributionRandomNumbers(m_func, m_distribution_func_fixed_pars, m_distribution_func_pars, seed, m_xmin, m_xmax));
327 
328  m_set_distribution_normalization();
329 
330 }
331 
332 
333 // =====================================================================================
334 
335 
336 void cbl::glob::Distribution::set_binned_distribution (const std::vector<double> var, const std::vector<double> dist, const std::string interpolationType, const int seed)
337 {
338  m_distributionType = glob::DistributionType::_Interpolated_;
339 
340  if (var.size()==0)
341  ErrorCBL("the input vector is empty", "set_binned_distribution", "Distribution.cpp");
342 
343  set_limits(Min(var), Max(var));
344  m_distribution_random = make_shared<DistributionRandomNumbers> (DistributionRandomNumbers(var, dist, interpolationType, seed));
345 
346  glob::STR_distribution_probability parameters;
347  parameters.func = make_shared<glob::FuncGrid>(glob::FuncGrid(var, dist, interpolationType));
348 
349  m_distribution_func_fixed_pars = make_shared<glob::STR_distribution_probability>(parameters);
350 
351  m_func = distribution_probability;
352  m_set_distribution_normalization();
353 }
354 
355 
356 // =====================================================================================
357 
358 
359 bool cbl::glob::Distribution::isIncluded (const double value) const
360 {
361  if (value > m_xmin && m_xmax > value)
362  return true;
363  else
364  return false;
365 }
366 
367 
368 // =====================================================================================
369 
370 
372 {
373  double value = m_distribution_random->operator()();
374  while (!isIncluded(value))
375  value = m_distribution_random->operator()();
376  return value;
377 }
378 
379 
380 // =====================================================================================
381 
382 
383 double cbl::glob::Distribution::sample (const int seed)
384 {
385  m_distribution_random->set_seed(seed);
386  return sample();
387 }
388 
389 
390 // =====================================================================================
391 
392 
393 vector<double> cbl::glob::Distribution::sample_vector (const int nvalues)
394 {
395  vector<double> values;
396 
397  for (int i=0; i<nvalues; i++)
398  values.push_back(sample());
399 
400  return values;
401 
402 }
403 
404 
405 // =====================================================================================
406 
407 
409 {
410  double val = 0.;
411 
412  if (m_distributionType == glob::DistributionType::_Discrete_) {
413  shared_ptr<STR_closest_probability> pp = static_pointer_cast<glob::STR_closest_probability>(m_distribution_func_fixed_pars);
414  val = Average(pp->values, pp->weights);
415  }
416  else
417  {
418  function<double(double)> f = bind(&Distribution::m_moments_integrator, this, std::placeholders::_1, 1);
419 
420  val = wrapper::gsl::GSL_integrate_cquad(f, m_xmin, m_xmax, 1.e-4);
421  }
422  m_mean = val;
423  return val;
424 }
425 
426 
427 // =====================================================================================
428 
429 
431 {
432  double val = 0.;
433 
434  if (m_distributionType == glob::DistributionType::_Discrete_) {
435  shared_ptr<STR_closest_probability> pp = static_pointer_cast<glob::STR_closest_probability>(m_distribution_func_fixed_pars);
436  val = pow(Sigma(pp->values, pp->weights),2);
437  }
438  else
439  {
440  mean();
441 
442  function<double(double)> f = [this] (double xx) {return this->m_central_moments_integrator(xx, 2);};
443 
444  val = wrapper::gsl::GSL_integrate_cquad(f, m_xmin, m_xmax, 1.e-4);
445  }
446  m_variance = val;
447  return val;
448 }
449 
450 
451 // =====================================================================================
452 
453 
455 {
456  return sqrt(variance());
457 }
458 
459 
460 // =====================================================================================
461 
462 
464 {
465  double val = 0.;
466 
467  if (m_distributionType == glob::DistributionType::_Discrete_)
468  ErrorCBL("", "skewness", "Distribution.cpp", glob::ExitCode::_workInProgress_);
469 
470  else {
471  variance();
472 
473  function<double(double)> f = [this] (double xx) { return this->m_central_moments_integrator(xx, 3); };
474 
475  val = sqrt(pow(wrapper::gsl::GSL_integrate_qag(f, m_xmin, m_xmax, 1.e-4),2)*pow(m_variance, -3));
476  }
477 
478  return val;
479 }
480 
481 
482 // =====================================================================================
483 
484 
486 {
487  double val = 0.;
488 
489  if (m_distributionType == glob::DistributionType::_Discrete_)
490  ErrorCBL("", "kurtosis", "Distribution.cpp", glob::ExitCode::_workInProgress_);
491 
492  else {
493  variance();
494 
495  function<double(double)> f = [this] (double xx) { return this->m_central_moments_integrator(xx, 4); };
496 
497  val= wrapper::gsl::GSL_integrate_qag(f, m_xmin, m_xmax, 1.e-4)*pow(m_variance, -2);
498  }
499 
500  return val;
501 }
502 
503 
504 // =====================================================================================
505 
506 
508 {
509  return {mean(), std(), skewness(), kurtosis()};
510 }
511 
512 
513 // =====================================================================================
514 
515 
516 double cbl::glob::Distribution::percentile (const unsigned int i)
517 {
518  double Area = double(i)/100.;
519  double val = 0.;
520 
521  if (m_distributionType == glob::DistributionType::_Discrete_) {
522  shared_ptr<STR_closest_probability> pp = static_pointer_cast<glob::STR_closest_probability>(m_distribution_func_fixed_pars);
523  vector<double> vv = pp->values; sort(vv.begin(), vv.end());
524  int perc = nint(double(i)/100.*pp->values.size());
525  val = vv[perc];
526  }
527 
528  else {
529  function<double(double)> f_integral = [this] (double xx) { return this->m_percentile_integrator(xx); };
530 
531  val = wrapper::gsl::GSL_root_brent(f_integral, Area, m_xmin, m_xmax);
532  }
533 
534  return val;
535 }
536 
537 
538 // =====================================================================================
539 
540 
542 {
543  double val = 0.;
544 
545  if (m_distributionType == glob::DistributionType::_Discrete_) {
546 
547  int digits = 4;
548  shared_ptr<STR_closest_probability> pp = static_pointer_cast<glob::STR_closest_probability>(m_distribution_func_fixed_pars);
549  vector<double> vv = pp->values;
550  for (size_t i=0; i<vv.size(); i++)
551  vv[i] = round_to_precision (vv[i], digits);
552 
553  vector<double> unique_vv = vv;
554  unique_unsorted(unique_vv);
555  sort(unique_vv.begin(), unique_vv.end());
556 
557  int counts = -1;
558  for (size_t i=0; i<unique_vv.size(); i++) {
559  int cc = std::count(vv.begin(), vv.end(), unique_vv[i]);
560  if (cc>counts) {
561  val = unique_vv[i];
562  counts = cc;
563  }
564  }
565  }
566 
567  else {
568  auto func = [&] (const double param) { return -this->operator()(param); };
569  double start = median(); //(m_xmax+m_xmin)*0.5;
570  val = wrapper::gsl::GSL_minimize_1D(func, start, m_xmin, m_xmax, 10000, false);
571  }
572 
573  return val;
574 }
575 
576 
577 // =====================================================================================
578 
579 
580 void cbl::glob::Distribution::get_distribution (vector<double> &xx, vector<double> &fx, vector<double> &err, const std::vector<double> FF, const std::vector<double> WW, const int nbin, const bool linear, const std::string file_out, const double fact, const double V1, const double V2, const bool bin_type, const bool conv, const double sigma)
581 {
582  if (xx.size()>0 || fx.size()>0 || FF.size()<=0 || nbin<=0) ErrorCBL("the following conditions have to be satisfied: xx.size()<=0, fx.size()<=0, FF.size()>0 and nbin>0. The values recived are instead: xx.size() = "+cbl::conv(xx.size(), par::fINT)+", fx.size() = "+cbl::conv(fx.size(), par::fINT)+", FF.size() = "+cbl::conv(FF.size(), par::fINT)+" and nbin = "+cbl::conv(nbin, par::fINT)+"!", "get_distribution", "Distribution.cpp");
583 
584  double minFF = (V1>cbl::par::defaultDouble) ? V1 : Min(FF)*0.9999;
585  double maxFF = (V2>cbl::par::defaultDouble) ? V2 : Max(FF)*1.0001;
586 
587 
588  // using GSL to create the histogram
589 
590  gsl_histogram *histo = gsl_histogram_alloc(nbin);
591 
592  if (linear) gsl_histogram_set_ranges_uniform(histo, minFF, maxFF);
593 
594  else {
595  vector<double> vv = logarithmic_bin_vector(nbin+1, minFF, maxFF);
596  double *vvv = new double[nbin+1]; for (int i=0; i<nbin+1; i++) vvv[i] = vv[i];
597  gsl_histogram_set_ranges(histo, vvv, nbin+1);
598  }
599 
600  vector<double> Weight = WW;
601  if (Weight.size()==0) Weight.resize(FF.size(), 1.);
602  checkDim(Weight, FF.size(), "WW");
603 
604  for (size_t i=0; i<FF.size(); i++)
605  gsl_histogram_accumulate(histo, FF[i], Weight[i]);
606 
607  double x1, x2;
608 
609  for (int i=0; i<nbin; i++) {
610 
611  gsl_histogram_get_range(histo, i, &x1, &x2);
612  double val = gsl_histogram_get(histo, i);
613 
614  if (linear) xx.push_back(0.5*(x1+x2));
615  else xx.push_back(pow(10., 0.5*(log10(x1)+log10(x2))));
616 
617  if (bin_type) {
618  fx.push_back(val/((x2-x1)*fact));
619  err.push_back(sqrt(val)/((x2-x1)*fact));
620  }
621  else {
622  fx.push_back(val/((log10(x2)-log10(x1))*fact));
623  err.push_back(sqrt(val)/((log10(x2)-log10(x1))*fact));
624  }
625 
626  }
627 
628 
629  // Gaussian convolution
630 
631  if (conv) {
632  coutCBL << "The distribution is smoothed with a Gaussian filter" << endl;
633  double *func;
634  fftw_complex *func_tr;
635 
636  if (!linear) ErrorCBL("", "get_distribution", "Distribution.cpp", ExitCode::_workInProgress_);
637  int nbinN = 2*nbin;
638  int i1 = nbin*0.5, i2 = 1.5*nbin;
639 
640  int nbinK = 0.5*nbinN+1;
641 
642  func = fftw_alloc_real(nbinN);
643  func_tr = fftw_alloc_complex(nbinK);
644 
645  for (int i=0; i<nbinN; i++)
646  func[i] = 0;
647 
648  for (int i=i1; i<i2; i++)
649  func[i] = fx[i-i1];
650 
651  for (int i=0; i<nbinK; i++) {
652  func_tr[i][0] = 0;
653  func_tr[i][1] = 0;
654  }
655 
656  fftw_plan real2complex;
657  real2complex = fftw_plan_dft_r2c_1d(nbinN, func, func_tr, FFTW_ESTIMATE);
658  fftw_execute(real2complex);
659  fftw_destroy_plan(real2complex);
660 
661  double delta = (maxFF-minFF)/nbin;
662  double SS = pow(sigma,2);
663 
664  double fact = 2*par::pi/(nbinN*delta);
665  for (int i=0; i<nbinK; i++) {
666  double kk = i*fact;
667  func_tr[i][0] = func_tr[i][0]*exp(-0.5*kk*kk*SS);
668  func_tr[i][1] = func_tr[i][1]*exp(-0.5*kk*kk*SS);
669  }
670 
671  fftw_plan complex2real;
672  complex2real = fftw_plan_dft_c2r_1d(nbinN, func_tr, func, FFTW_ESTIMATE);
673  fftw_execute(complex2real);
674  fftw_destroy_plan(complex2real);
675 
676  for (int i=i1; i<i2; i++)
677  fx[i-i1] = func[i]/nbinN;
678  }
679 
680 
681  if (file_out!=par::defaultString && file_out!="") {
682 
683  ofstream fout(file_out.c_str()); checkIO(fout, file_out);
684 
685  for (size_t i=0; i<xx.size(); i++)
686  fout << xx[i] << " " << fx[i] << " " << err[i] << endl;
687 
688  fout.clear(); fout.close(); coutCBL << "I wrote the file: " << file_out << endl;
689  }
690 
691  gsl_histogram_free(histo);
692  fftw_cleanup();
693 
694 }
The class Distribution.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
void set_gaussian_distribution(const double mean, const double sigma, const int seed=1)
set normal distribution
void set_custom_distribution(const distribution_func func, const std::shared_ptr< void > distribution_fixed_pars, const std::vector< double > distribution_pars, const int seed=1)
set a custom distribution
void m_set_distribution_normalization()
set distribution normalization
double operator()(double xx)
evaluate distribution
void set_poisson_distribution(const double mean, const int seed=1)
set poisson distribution
double mean()
return the mean value of the distribution
double mode()
return the distribution mode
void set_constant_distribution(const double value)
set a constant distribution
Distribution()
default constructor
Definition: Distribution.h:215
void set_limits(const double xmin, const double xmax)
set the distribution limits
double log_distribution(double xx)
evaluate log-distribution
double kurtosis()
return the kurtosis of the distribution
std::vector< double > moments()
return the moments of the distribution distribution
double variance()
return the standard deviation of the distribution
double skewness()
return the skewness of the distribution
double sample() const
sample a value from the distribution
void set_uniform_distribution(const double xmin, const double xmax, const int seed=1)
set an uniform distribution with input limits and seed
void set_discrete_values(const std::vector< double > discrete_values, const std::vector< double > weights, const int seed=1)
set discrete distribution values and weights
double std()
return the standard deviation of the distribution
void get_distribution(std::vector< double > &xx, std::vector< double > &fx, std::vector< double > &err, const std::vector< double > FF, const std::vector< double > WW, const int nbin, const bool linear=true, const std::string file_out=par::defaultString, const double fact=1., const double V1=par::defaultDouble, const double V2=par::defaultDouble, const bool bin_type=true, const bool conv=false, const double sigma=0.)
derive and store the number distribution of a given std::vector
double m_percentile_integrator(const double xx)
integrand of the percentile of the distribution
void set_binned_distribution(const std::vector< double > var, const std::vector< double > dist, const std::string interpolationType="Spline", const int seed=1)
set discrete distribution values and weights
bool isIncluded(const double value) const
check if a value is included in the distribution limits
double percentile(const unsigned int i)
return the i-th percentile of the distribution
std::vector< double > sample_vector(const int nvalues)
sample values from the distribution
The class FuncGrid.
Definition: FuncGrid.h:55
The class ConstantRandomNumbers.
The class CustomDistributionRandomNumbers.
The class DiscreteRandomNumbers.
The class DistributionRandomNumbers.
The class NormalRandomNumbers.
The class UniformRandomNumbers.
static const char fINT[]
conversion symbol for: int -> std::string
Definition: Constants.h:121
static const std::string defaultString
default std::string value
Definition: Constants.h:336
static const double defaultDouble
default double value
Definition: Constants.h:348
static const double pi
: the ratio of a circle's circumference to its diameter
Definition: Constants.h:199
static const double cc
: the speed of light in vacuum (the value is exact) [km/sec]
Definition: Constants.h:221
DistributionType
the distribution type
Definition: Distribution.h:51
double GSL_minimize_1D(FunctionDoubleDouble func, const double start, double min=par::defaultDouble, double max=-par::defaultDouble, const int max_iter=1000, const bool verbose=false)
minimize the provided function using GSL procedure
Definition: GSLwrapper.cpp:658
double GSL_integrate_cquad(gsl_function func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int nevals=100)
integral, using the gsl cquad method
Definition: GSLwrapper.cpp:159
double GSL_root_brent(gsl_function Func, const double low_guess, const double up_guess, const double rel_err=1.e-3, const double abs_err=0)
function to find roots using GSL qag method
Definition: GSLwrapper.cpp:430
double GSL_integrate_qag(gsl_function Func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000, const int rule=6)
integral, computed using the GSL qag method
Definition: GSLwrapper.cpp:180
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
T Min(const std::vector< T > vect)
minimum element of a std::vector
Definition: Kernel.h:1324
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
double Average(const std::vector< double > vect)
the average of a std::vector
Definition: Func.cpp:870
std::vector< T > logarithmic_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with logarithmically spaced values
Definition: Kernel.h:1621
std::vector< T > linear_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with linearly spaced values
Definition: Kernel.h:1604
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
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
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
void unique_unsorted(std::vector< int > &vv)
erase all the equal elements of the input std::vector
Definition: Kernel.cpp:284
T poisson(T xx, std::shared_ptr< void > pp, std::vector< double > par)
the poisson distribution
Definition: Func.h:1143
T Max(const std::vector< T > vect)
maximum element of a std::vector
Definition: Kernel.h:1336
double Sigma(const std::vector< double > vect)
the standard deviation of a std::vector
Definition: Func.cpp:925
std::function< double(double, std::shared_ptr< void >, std::vector< double >)> distribution_func
generic distribution function
Definition: RandomNumbers.h:50
double closest_probability(double xx, std::shared_ptr< void > pp, std::vector< double > par)
probability of the closest element to x from a list with weights
Definition: Func.cpp:47
int nint(const T val)
the nearest integer
Definition: Kernel.h:915
double round_to_precision(const double num, const int ndigits)
reduce the precision of an input double
Definition: Kernel.cpp:150
double distribution_probability(double xx, std::shared_ptr< void > pp, std::vector< double > par)
probability of an interpolated distribution
Definition: Func.cpp:58