RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
irwin_hall.hpp
Go to the documentation of this file.
1#pragma once
2
8
9namespace randlib
10{
11
23template <typename RealType>
24class IrwinHallRand final : public ContinuousDistribution<RealType>
25{
26 RealType variate(detail::EngineRef g) const override { return (*this)(g); }
27
28 int n = 1;
30
31public:
32 explicit IrwinHallRand(size_t number);
33
34 [[nodiscard]] std::string Name() const override;
35 [[nodiscard]] Support SupportType() const override { return Support::Finite; }
36 [[nodiscard]] RealType MinValue() const override { return 0; }
37 [[nodiscard]] RealType MaxValue() const override { return n; }
38
39 void SetNumber(int number);
40 [[nodiscard]] inline int GetNumber() const noexcept { return n; }
41
42 [[nodiscard]] double f(const RealType &x) const override;
43 [[nodiscard]] double logf(const RealType &x) const override;
44 [[nodiscard]] double F(const RealType &x) const override;
45 [[nodiscard]] double S(const RealType &x) const override;
46 template <std::uniform_random_bit_generator G>
47 [[nodiscard]] RealType operator()(G &g) const;
48
49 [[nodiscard]] long double Mean() const override;
50 [[nodiscard]] long double Variance() const override;
51 [[nodiscard]] RealType Median() const override;
52 [[nodiscard]] RealType Mode() const override;
53 [[nodiscard]] long double Skewness() const override;
54 [[nodiscard]] long double ExcessKurtosis() const override;
55
56private:
58 double lowerHalfSum(double x, int power) const;
59 std::complex<double> CFImpl(double t) const override;
60};
61
62} // namespace randlib
63
64namespace randlib
65{
66
67template <typename RealType>
69{
70 SetNumber(number);
71}
72
73template <typename RealType>
75{
76 return "Irwin-Hall(" + this->toStringWithPrecision(GetNumber()) + ")";
77}
78
79template <typename RealType>
81{
82 if (!(number > 0))
83 throw std::invalid_argument("Irwin-Hall distribution: number should be positive");
84 n = number;
85}
86
87template <typename RealType>
88double IrwinHallRand<RealType>::lowerHalfSum(double x, int power) const
89{
90 double sum = 0.0;
91 int last = std::floor(x);
92 for (int i = 0; i <= last; ++i) {
93 double add = power * std::log(x - i);
94 add -= math::lfact(i);
95 add -= math::lfact(n - i);
96 add = std::exp(add);
97 sum += (i % 2) ? -add : add;
98 }
99 return sum;
100}
101
102template <typename RealType>
103double IrwinHallRand<RealType>::f(const RealType &x) const
104{
105 if (x < 0 || x > n)
106 return 0.0;
107
109 if (n == 1)
110 return 1.0;
111 if (n == 2)
112 return (x <= 1.0) ? x : 2.0 - x;
114 return n * lowerHalfSum(std::min<double>(x, n - x), n - 1);
115}
116
117template <typename RealType>
118double IrwinHallRand<RealType>::logf(const RealType &x) const
119{
120 return std::log(f(x));
121}
122
123template <typename RealType>
124double IrwinHallRand<RealType>::F(const RealType &x) const
125{
126 if (x <= 0)
127 return 0.0;
128 if (x >= n)
129 return 1.0;
130
132 if (n == 1)
133 return x;
134 if (n == 2) {
135 if (x <= 1.0)
136 return 0.5 * x * x;
137 double temp = 2.0 - x;
138 return 1.0 - 0.5 * temp * temp;
139 }
141 return (2 * x <= n) ? lowerHalfSum(x, n) : 1.0 - lowerHalfSum(n - x, n);
142}
143
144template <typename RealType>
145double IrwinHallRand<RealType>::S(const RealType &x) const
146{
147 if (x <= 0)
148 return 1.0;
149 if (x >= n)
150 return 0.0;
151 if (n <= 2)
152 return F(n - x);
153 return (2 * x >= n) ? lowerHalfSum(n - x, n) : 1.0 - lowerHalfSum(x, n);
154}
155
156template <typename RealType>
157template <std::uniform_random_bit_generator G>
159{
160 RealType sum = 0.0;
161 for (int i = 0; i != n; ++i)
162 sum += U(g);
163 return sum;
164}
165
166template <typename RealType>
168{
169 return 0.5 * n;
170}
171
172template <typename RealType>
174{
175 return n / 12.0l;
176}
177
178template <typename RealType>
179std::complex<double> IrwinHallRand<RealType>::CFImpl(double t) const
180{
181 return std::pow(U.CF(t), n);
182}
183
184template <typename RealType>
186{
187 return 0.5 * n;
188}
189
190template <typename RealType>
192{
193 return 0.5 * n;
194}
195
196template <typename RealType>
198{
199 return 0.0l;
200}
201
202template <typename RealType>
204{
205 return -1.2 / n;
206}
207
208} // namespace randlib
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
The IrwinHallRand class Irwin-Hall distribution.
Definition irwin_hall.hpp:25
double S(const RealType &x) const override
Definition irwin_hall.hpp:145
RealType operator()(G &g) const
Definition irwin_hall.hpp:158
long double ExcessKurtosis() const override
Definition irwin_hall.hpp:203
int GetNumber() const noexcept
Definition irwin_hall.hpp:40
double F(const RealType &x) const override
Definition irwin_hall.hpp:124
void SetNumber(int number)
Definition irwin_hall.hpp:80
std::string Name() const override
Definition irwin_hall.hpp:74
RealType MaxValue() const override
Definition irwin_hall.hpp:37
RealType Median() const override
Definition irwin_hall.hpp:185
RealType MinValue() const override
Definition irwin_hall.hpp:36
Support SupportType() const override
Definition irwin_hall.hpp:35
double f(const RealType &x) const override
Definition irwin_hall.hpp:103
IrwinHallRand(size_t number)
Definition irwin_hall.hpp:68
double logf(const RealType &x) const override
Definition irwin_hall.hpp:118
long double Mean() const override
Definition irwin_hall.hpp:167
long double Variance() const override
Definition irwin_hall.hpp:173
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition irwin_hall.hpp:191
long double Skewness() const override
Definition irwin_hall.hpp:197
The UniformRand class Uniform continuous distribution.
Definition continuous/uniform.hpp:30
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
Real lfact(size_t n)
Calculate log(n!) using table values for small n <= 255 and built-in lgamma function for large n > 25...
Definition math/gamma.hpp:591
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15