Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
FlatMarginal.cpp
Go to the documentation of this file.
1#include "FlatMarginal.h"
2
3FlatMarginal::FlatMarginal(double a, double b, unsigned int seed)
4 : a(a), b(b)
5{
6 if (!std::isfinite(a) || !std::isfinite(b) || !(a < b)) {
7 throw std::invalid_argument("FlatMarginal requires finite bounds with a < b");
8 }
9 if (!eng_) {
10 throw std::runtime_error("FlatMarginal could not allocate its GSL RNG");
11 }
12 gsl_rng_set(eng_.get(), seed);
13}
14
15std::vector<double> FlatMarginal::rvs(std::size_t n) {
16 std::vector<double> z(n);
17 for (std::size_t i = 0; i < n; ++i)
18 z[i] = gsl_ran_flat(eng_.get(), a, b);
19 return z;
20}
21
22double FlatMarginal::logpdf(double x) {
23 // Use an explicit closed-support convention instead of inheriting GSL's
24 // half-open endpoint convention. Endpoints have measure zero, but keeping
25 // the convention explicit makes logpdf/f_df_ddf internally consistent.
26 return (x >= a && x <= b)
27 ? -std::log(b - a)
28 : -std::numeric_limits<double>::infinity();
29}
30
32 const double pdf = (x >= a && x <= b) ? 1.0 / (b - a) : 0.0;
33 return {pdf, 0.0, 0.0};
34}
35
36double FlatMarginal::cdf(double x) {
37 return gsl_cdf_flat_P(x, a, b);
38}
39
40double FlatMarginal::ppf(double p) {
41 return gsl_cdf_flat_Pinv(p, a, b);
42}
43
45 return 0.5 * (a + b);
46}
47
49 return (b - a) / std::sqrt(12.);
50}
double ppf(double p) override
Evaluates the quantile function (inverse CDF).
FlatMarginal(double a, double b, unsigned int seed=std::random_device{}())
Constructs a uniform marginal on [a,b].
double logpdf(double x) override
Evaluates the logarithm of the probability density at x.
std::vector< double > rvs(std::size_t n) override
Draws random samples from the marginal distribution.
double cdf(double x) override
Evaluates the cumulative distribution function at x.
double mean() override
Returns the mean of the distribution.
PDFDiff f_df_ddf(double x) override
double std() override
Returns the standard deviation of the distribution.