RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
gumbel.hpp
Go to the documentation of this file.
1#pragma once
2
7
8namespace randlib
9{
10
20template <typename RealType>
21class GumbelRand final : public ContinuousDistribution<RealType>
22{
23 RealType variate(detail::EngineRef g) const override { return (*this)(g); }
24
25 double mu = 0;
26 double beta = 1;
27 double logBeta = 0;
28public:
29 GumbelRand(double location = 0, double scale = 1);
30
31 [[nodiscard]] std::string Name() const override;
32 [[nodiscard]] Support SupportType() const override { return Support::Infinite; }
33 [[nodiscard]] RealType MinValue() const override { return -INFINITY; }
34 [[nodiscard]] RealType MaxValue() const override { return INFINITY; }
35
36 void SetLocation(double location);
37 void SetScale(double scale);
38 [[nodiscard]] inline double GetLocation() const noexcept { return mu; }
39 [[nodiscard]] inline double GetScale() const noexcept { return beta; }
40
41 [[nodiscard]] double f(const RealType &x) const override;
42 [[nodiscard]] double logf(const RealType &x) const override;
43 [[nodiscard]] double F(const RealType &x) const override;
44 [[nodiscard]] double S(const RealType &x) const override;
45 template <std::uniform_random_bit_generator G>
46 [[nodiscard]] RealType operator()(G &g) const;
47
48 template <std::uniform_random_bit_generator G>
49 [[nodiscard]] static RealType StandardVariate(G &g);
50
51 [[nodiscard]] long double Mean() const override;
52 [[nodiscard]] long double Variance() const override;
53 [[nodiscard]] RealType Median() const override;
54 [[nodiscard]] RealType Mode() const override;
55 [[nodiscard]] long double Skewness() const override;
56 [[nodiscard]] long double ExcessKurtosis() const override;
57
58private:
59 RealType quantileImpl(double p) const override;
60 RealType quantileImpl1m(double p) const override;
61
62public:
63 [[nodiscard]] long double Entropy() const;
64};
65
66} // namespace randlib
67
69
70namespace randlib
71{
72
73template <typename RealType>
74GumbelRand<RealType>::GumbelRand(double location, double scale)
75{
76 SetLocation(location);
77 SetScale(scale);
78}
79
80template <typename RealType>
81std::string GumbelRand<RealType>::Name() const
82{
83 return "Gumbel(" + this->toStringWithPrecision(GetLocation()) + ", " + this->toStringWithPrecision(GetScale()) +
84 ")";
85}
86
87template <typename RealType>
89{
90 if (!std::isfinite(location))
91 throw std::invalid_argument("Gumbel distribution: location should be finite");
92 mu = location;
93}
94
95template <typename RealType>
97{
98 if (!(scale > 0.0))
99 throw std::invalid_argument("Gumbel distribution: scale should be positive");
100 beta = scale;
101 logBeta = std::log(beta);
102}
103
104template <typename RealType>
105double GumbelRand<RealType>::f(const RealType &x) const
106{
107 return std::exp(logf(x));
108}
109
110template <typename RealType>
111double GumbelRand<RealType>::logf(const RealType &x) const
112{
113 double z = (mu - x) / beta;
114 double y = std::exp(z);
115 return z - y - logBeta;
116}
117
118template <typename RealType>
119double GumbelRand<RealType>::F(const RealType &x) const
120{
121 double y = (mu - x) / beta;
122 y = std::exp(y);
123 return std::exp(-y);
124}
125
126template <typename RealType>
127double GumbelRand<RealType>::S(const RealType &x) const
128{
129 double y = (mu - x) / beta;
130 y = std::exp(y);
131 return -std::expm1l(-y);
132}
133
134template <typename RealType>
135template <std::uniform_random_bit_generator G>
137{
138 return mu + beta * GumbelRand<RealType>::StandardVariate(g);
139}
140
141template <typename RealType>
142template <std::uniform_random_bit_generator G>
144{
146 return -std::log(w);
147}
148
149template <typename RealType>
150long double GumbelRand<RealType>::Mean() const
151{
152 return mu + beta * randlib::consts::egamma;
153}
154
155template <typename RealType>
157{
158 double v = randlib::consts::pi * beta;
159 return v * v / 6;
160}
161
162template <typename RealType>
163RealType GumbelRand<RealType>::quantileImpl(double p) const
164{
165 return mu - beta * std::log(-std::log(p));
166}
167
168template <typename RealType>
169RealType GumbelRand<RealType>::quantileImpl1m(double p) const
170{
171 return mu - beta * std::log(-std::log1pl(-p));
172}
173
174template <typename RealType>
176{
177 static const double M_LN_LN2 = std::log(randlib::consts::ln2);
178 return mu - beta * M_LN_LN2;
179}
180
181template <typename RealType>
183{
184 return mu;
185}
186
187template <typename RealType>
189{
190 static constexpr long double skew = 12 * randlib::consts::sqrt2 * randlib::consts::sqrt3 * randlib::consts::apery /
192 return skew;
193}
194
195template <typename RealType>
197{
198 return 2.4l;
199}
200
201template <typename RealType>
203{
204 return logBeta + randlib::consts::egamma + 1.0;
205}
206
207} // namespace randlib
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
The GumbelRand class Gumbel distribution.
Definition gumbel.hpp:22
static RealType StandardVariate(G &g)
Definition gumbel.hpp:143
double GetScale() const noexcept
Definition gumbel.hpp:39
RealType Median() const override
Definition gumbel.hpp:175
std::string Name() const override
Definition gumbel.hpp:81
double f(const RealType &x) const override
Definition gumbel.hpp:105
long double ExcessKurtosis() const override
Definition gumbel.hpp:196
long double Mean() const override
Definition gumbel.hpp:150
double S(const RealType &x) const override
Definition gumbel.hpp:127
GumbelRand(double location=0, double scale=1)
Definition gumbel.hpp:74
void SetScale(double scale)
Definition gumbel.hpp:96
double GetLocation() const noexcept
Definition gumbel.hpp:38
Support SupportType() const override
Definition gumbel.hpp:32
RealType MaxValue() const override
Definition gumbel.hpp:34
void SetLocation(double location)
Definition gumbel.hpp:88
long double Variance() const override
Definition gumbel.hpp:156
RealType MinValue() const override
Definition gumbel.hpp:33
double logf(const RealType &x) const override
Definition gumbel.hpp:111
double F(const RealType &x) const override
Definition gumbel.hpp:119
long double Skewness() const override
Definition gumbel.hpp:188
long double Entropy() const
Definition gumbel.hpp:202
RealType operator()(G &g) const
Definition gumbel.hpp:136
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition gumbel.hpp:182
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
constexpr long double ln2
Definition constants.hpp:18
constexpr long double egamma
Definition constants.hpp:36
constexpr long double apery
Definition constants.hpp:37
constexpr long double pi
Definition constants.hpp:22
constexpr long double pi_sq
Definition constants.hpp:39
constexpr long double sqrt2
Definition constants.hpp:27
constexpr long double sqrt3
Definition constants.hpp:29
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15
@ Infinite
(-∞, ∞)