RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
wigner_semicircle.hpp
Go to the documentation of this file.
1#pragma once
2
8
9namespace randlib
10{
11
21template <typename RealType>
22class WignerSemicircleRand final : public ContinuousDistribution<RealType>
23{
24 RealType variate(detail::EngineRef g) const override { return (*this)(g); }
25
26 RealType R = 1;
27 double RSq = 1;
28 double logRSq = 0;
29 BetaRand<RealType> X{1.5, 1.5};
30
31 double lowerTail(double x) const;
32
33public:
34 explicit WignerSemicircleRand(double radius);
35
36 [[nodiscard]] std::string Name() const override;
37 [[nodiscard]] Support SupportType() const override { return Support::Finite; }
38 [[nodiscard]] RealType MinValue() const override { return -R; }
39 [[nodiscard]] RealType MaxValue() const override { return R; }
40
41 void SetRadius(double radius);
42 [[nodiscard]] inline double GetRadius() const noexcept { return R; }
43
44 [[nodiscard]] double f(const RealType &x) const override;
45 [[nodiscard]] double logf(const RealType &x) const override;
46 [[nodiscard]] double S(const RealType &x) const override;
47 [[nodiscard]] double F(const RealType &x) const override;
48 template <std::uniform_random_bit_generator G>
49 [[nodiscard]] RealType operator()(G &g) const;
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
58 [[nodiscard]] long double Entropy() const;
59};
60
61} // namespace randlib
62
63namespace randlib
64{
65
66template <typename RealType>
68{
69 SetRadius(radius);
70}
71
72template <typename RealType>
74{
75 return "Wigner Semicircle(" + this->toStringWithPrecision(GetRadius()) + ")";
76}
77
78template <typename RealType>
80{
81 if (!(radius > 0.0))
82 throw std::invalid_argument("Wigner-Semicircle distribution: radius should be positive");
83 R = radius;
84 RSq = R * R;
85 logRSq = std::log(RSq);
86}
87
88template <typename RealType>
89double WignerSemicircleRand<RealType>::f(const RealType &x) const
90{
91 double xSq = x * x;
92 if (xSq >= RSq)
93 return 0.0;
94 double y = RSq - xSq;
95 y = std::sqrt(y);
96 y *= randlib::consts::inv_pi / RSq;
97 return 2 * y;
98}
99
100template <typename RealType>
101double WignerSemicircleRand<RealType>::logf(const RealType &x) const
102{
103 double xSq = x * x;
104 if (xSq >= RSq)
105 return -INFINITY;
106 return randlib::consts::ln2 + 0.5 * std::log(RSq - xSq) - randlib::consts::lnpi - logRSq;
107}
108
109template <typename RealType>
111{
114 double delta = (R + x) / R;
115 double phi = 2 * std::asin(std::sqrt(0.5 * delta));
116 double y = 2 * phi, ySq = y * y;
117 double yMinusSinY =
118 (y < 0.5) ? y * ySq / 6 * (1 - ySq / 20 * (1 - ySq / 42 * (1 - ySq / 72 * (1 - ySq / 110)))) : y - std::sin(y);
119 return yMinusSinY / (2 * randlib::consts::pi);
120}
121
122template <typename RealType>
123double WignerSemicircleRand<RealType>::F(const RealType &x) const
124{
125 if (x <= -R)
126 return 0.0;
127 if (x >= R)
128 return 1.0;
129 return (x <= 0) ? lowerTail(x) : 1.0 - lowerTail(-x);
130}
131
132template <typename RealType>
133double WignerSemicircleRand<RealType>::S(const RealType &x) const
134{
135 if (x <= -R)
136 return 1.0;
137 if (x >= R)
138 return 0.0;
139 return (x >= 0) ? lowerTail(-x) : 1.0 - lowerTail(x);
140}
141
142template <typename RealType>
143template <std::uniform_random_bit_generator G>
145{
146 RealType x = X(g);
147 x += x - 1;
148 return R * x;
149}
150
151template <typename RealType>
153{
154 return 0.0;
155}
156
157template <typename RealType>
159{
160 return 0.25 * RSq;
161}
162
163template <typename RealType>
165{
166 return 0.0;
167}
168
169template <typename RealType>
171{
172 return 0.0;
173}
174
175template <typename RealType>
177{
178 return 0.0;
179}
180
181template <typename RealType>
183{
184 return -1.0;
185}
186
187template <typename RealType>
189{
190 return randlib::consts::lnpi + 0.5 * logRSq - 0.5;
191}
192
193} // namespace randlib
The BetaRand class Beta distribution.
Definition continuous/beta.hpp:246
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
The WignerSemicircleRand class Wigner-Semicircle distribution.
Definition wigner_semicircle.hpp:23
long double ExcessKurtosis() const override
Definition wigner_semicircle.hpp:182
WignerSemicircleRand(double radius)
Definition wigner_semicircle.hpp:67
double S(const RealType &x) const override
Definition wigner_semicircle.hpp:133
double logf(const RealType &x) const override
Definition wigner_semicircle.hpp:101
double GetRadius() const noexcept
Definition wigner_semicircle.hpp:42
RealType MaxValue() const override
Definition wigner_semicircle.hpp:39
std::string Name() const override
Definition wigner_semicircle.hpp:73
long double Mean() const override
Definition wigner_semicircle.hpp:152
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition wigner_semicircle.hpp:170
long double Skewness() const override
Definition wigner_semicircle.hpp:176
double f(const RealType &x) const override
Definition wigner_semicircle.hpp:89
double F(const RealType &x) const override
Definition wigner_semicircle.hpp:123
RealType operator()(G &g) const
Definition wigner_semicircle.hpp:144
Support SupportType() const override
Definition wigner_semicircle.hpp:37
RealType MinValue() const override
Definition wigner_semicircle.hpp:38
RealType Median() const override
Definition wigner_semicircle.hpp:164
void SetRadius(double radius)
Definition wigner_semicircle.hpp:79
long double Variance() const override
Definition wigner_semicircle.hpp:158
long double Entropy() const
Definition wigner_semicircle.hpp:188
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
constexpr long double inv_pi
Definition constants.hpp:24
constexpr long double ln2
Definition constants.hpp:18
constexpr long double pi
Definition constants.hpp:22
constexpr long double lnpi
Definition constants.hpp:21
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15