RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
inverse_gamma.hpp
Go to the documentation of this file.
1#pragma once
2
8
9namespace randlib
10{
11
21template <typename RealType>
22class InverseGammaRand final : public ContinuousDistribution<RealType>
23{
24 RealType variate(detail::EngineRef g) const override { return (*this)(g); }
25 void sample(detail::EngineRef g, std::span<RealType> outputData) const override;
26
27 double alpha = 1;
28 double beta = 1;
29 double pdfCoef = 0;
30
32
33public:
34 InverseGammaRand(double shape = 1, double rate = 1);
35
37 [[nodiscard]] static InverseGammaRand FromShapeRate(double shape, double rate)
38 {
39 return InverseGammaRand(shape, rate);
40 }
41
42 [[nodiscard]] std::string Name() const override;
43 [[nodiscard]] Support SupportType() const override { return Support::RightSemiFinite; }
44 [[nodiscard]] RealType MinValue() const override { return 0; }
45 [[nodiscard]] RealType MaxValue() const override { return INFINITY; }
46
47 void SetParameters(double shape, double rate);
48 [[nodiscard]] inline double GetShape() const noexcept { return alpha; }
49 [[nodiscard]] inline double GetRate() const noexcept { return beta; }
50 [[nodiscard]] inline double GetLogShape() const { return X.GetLogShape(); }
51 [[nodiscard]] inline double GetLogRate() const { return X.GetLogRate(); }
52
53 [[nodiscard]] double logf(const RealType &x) const override;
54 [[nodiscard]] double F(const RealType &x) const override;
55 [[nodiscard]] double S(const RealType &x) const override;
56 template <std::uniform_random_bit_generator G>
57 [[nodiscard]] RealType operator()(G &g) const;
58
59 [[nodiscard]] long double Mean() const override;
60 [[nodiscard]] long double Variance() const override;
61 [[nodiscard]] RealType Median() const override;
62 [[nodiscard]] RealType Mode() const override;
63 [[nodiscard]] long double Skewness() const override;
64 [[nodiscard]] long double ExcessKurtosis() const override;
65
66private:
67 RealType quantileImpl(double p) const override;
68 RealType quantileImpl1m(double p) const override;
69 std::complex<double> CFImpl(double t) const override;
70
71public:
72 [[nodiscard]] double GetLogGammaShape() const { return X.GetLogGammaShape(); }
73};
74
75} // namespace randlib
76
77namespace randlib
78{
79
80template <typename RealType>
82{
83 SetParameters(shape, rate);
84}
85
86template <typename RealType>
88{
89 return "Inverse-Gamma(" + this->toStringWithPrecision(GetShape()) + ", " + this->toStringWithPrecision(GetRate()) +
90 ")";
91}
92
93template <typename RealType>
94void InverseGammaRand<RealType>::SetParameters(double shape, double rate)
95{
96 if (!(shape > 0.0))
97 throw std::invalid_argument("Inverse-Gamma distribution: shape should be positive");
98 if (!(rate > 0.0))
99 throw std::invalid_argument("Inverse-Gamma distribution: rate should be positive");
100 X.SetParameters(shape, rate);
101 alpha = X.GetShape();
102 beta = X.GetRate();
103 pdfCoef = alpha * X.GetLogRate() - X.GetLogGammaShape();
104}
105
106template <typename RealType>
107double InverseGammaRand<RealType>::logf(const RealType &x) const
108{
109 if (x <= 0.0)
110 return -INFINITY;
111 double logX = std::log(x);
112 double y = -(alpha - 1.0) * logX;
113 y -= beta / x;
114 y += pdfCoef;
115 return y - 2 * logX;
116}
117
118template <typename RealType>
119double InverseGammaRand<RealType>::F(const RealType &x) const
120{
121 return (x > 0.0) ? X.S(1.0 / x) : 0.0;
122}
123
124template <typename RealType>
125double InverseGammaRand<RealType>::S(const RealType &x) const
126{
127 return (x > 0.0) ? X.F(1.0 / x) : 1.0;
128}
129
130template <typename RealType>
131template <std::uniform_random_bit_generator G>
133{
134 return 1.0 / X(g);
135}
136
137template <typename RealType>
138void InverseGammaRand<RealType>::sample(detail::EngineRef g, std::span<RealType> outputData) const
139{
140 X.Sample(g, outputData);
141 for (RealType &var : outputData)
142 var = 1.0 / var;
143}
144
145template <typename RealType>
147{
148 return (alpha > 1) ? beta / (alpha - 1) : INFINITY;
149}
150
151template <typename RealType>
153{
154 if (alpha <= 2)
155 return INFINITY;
156 double var = beta / (alpha - 1);
157 var *= var;
158 return var / (alpha - 2);
159}
160
161template <typename RealType>
163{
164 return 1.0 / X.Median();
165}
166
167template <typename RealType>
169{
170 return beta / (alpha + 1);
171}
172
173template <typename RealType>
174RealType InverseGammaRand<RealType>::quantileImpl(double p) const
175{
176 return 1.0 / X.Quantile1m(p);
177}
178
179template <typename RealType>
180RealType InverseGammaRand<RealType>::quantileImpl1m(double p) const
181{
182 return 1.0 / X.Quantile(p);
183}
184
185template <typename RealType>
186std::complex<double> InverseGammaRand<RealType>::CFImpl(double t) const
187{
188 return this->integrateCharacteristicFunction(t);
189}
190
191template <typename RealType>
193{
194 return (alpha > 3) ? 4 * std::sqrt(alpha - 2) / (alpha - 3) : INFINITY;
195}
196
197template <typename RealType>
199{
200 if (alpha <= 4)
201 return INFINITY;
202 long double numerator = 30 * alpha - 66.0;
203 long double denominator = (alpha - 3) * (alpha - 4);
204 return numerator / denominator;
205}
206
207} // namespace randlib
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
The GammaRand class Gamma distribution.
Definition continuous/gamma.hpp:295
The InverseGammaRand class Inverse-Gamma distribution.
Definition inverse_gamma.hpp:23
void SetParameters(double shape, double rate)
Definition inverse_gamma.hpp:94
InverseGammaRand(double shape=1, double rate=1)
Definition inverse_gamma.hpp:81
double GetLogRate() const
Definition inverse_gamma.hpp:51
double GetRate() const noexcept
Definition inverse_gamma.hpp:49
RealType MaxValue() const override
Definition inverse_gamma.hpp:45
double F(const RealType &x) const override
Definition inverse_gamma.hpp:119
double GetShape() const noexcept
Definition inverse_gamma.hpp:48
double GetLogShape() const
Definition inverse_gamma.hpp:50
std::string Name() const override
Definition inverse_gamma.hpp:87
RealType Median() const override
Definition inverse_gamma.hpp:162
double S(const RealType &x) const override
Definition inverse_gamma.hpp:125
long double Mean() const override
Definition inverse_gamma.hpp:146
long double Variance() const override
Definition inverse_gamma.hpp:152
static InverseGammaRand FromShapeRate(double shape, double rate)
Definition inverse_gamma.hpp:37
double logf(const RealType &x) const override
Definition inverse_gamma.hpp:107
Support SupportType() const override
Definition inverse_gamma.hpp:43
double GetLogGammaShape() const
Definition inverse_gamma.hpp:72
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition inverse_gamma.hpp:168
long double ExcessKurtosis() const override
Definition inverse_gamma.hpp:198
RealType MinValue() const override
Definition inverse_gamma.hpp:44
long double Skewness() const override
Definition inverse_gamma.hpp:192
RealType operator()(G &g) const
Definition inverse_gamma.hpp:132
void Sample(std::span< T > outputData) const
Fills the output with variates drawn with the engine of this object.
Definition distribution.hpp:139
T Quantile1m(double p) const
Definition univariate.hpp:324
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15
@ RightSemiFinite
[a, ∞)