RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
continuous/uniform.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <span>
4
5#include <algorithm>
6#include <limits>
7
12
13namespace randlib
14{
15
28template <typename RealType>
29class UniformRand final : public BetaDistribution<RealType>
30{
31 RealType variate(detail::EngineRef g) const override { return (*this)(g); }
32
33public:
34 UniformRand(double minValue = 0, double maxValue = 1);
35 [[nodiscard]] std::string Name() const override;
36
37 using BetaDistribution<RealType>::SetSupport;
38
39 [[nodiscard]] Support SupportType() const override { return Support::Finite; }
40 [[nodiscard]] RealType MinValue() const override { return this->a; }
41 [[nodiscard]] RealType MaxValue() const override { return this->b; }
42
43 [[nodiscard]] double f(const RealType &x) const override;
44 [[nodiscard]] double logf(const RealType &x) const override;
45 [[nodiscard]] double F(const RealType &x) const override;
46 [[nodiscard]] double S(const RealType &x) const override;
47 template <std::uniform_random_bit_generator G>
48 [[nodiscard]] RealType operator()(G &g) const;
49
54 template <std::uniform_random_bit_generator G>
55 [[nodiscard]] static RealType StandardVariate(G &g);
56
61 template <std::uniform_random_bit_generator G>
62 [[nodiscard]] static RealType StandardVariateClosed(G &g);
63
68 template <std::uniform_random_bit_generator G>
69 [[nodiscard]] static RealType StandardVariateHalfClosed(G &g);
70
71 [[nodiscard]] long double Mean() const override;
72 [[nodiscard]] long double Variance() const override;
73 [[nodiscard]] RealType Median() const override;
74 [[nodiscard]] RealType Mode() const override;
75 [[nodiscard]] long double Skewness() const override;
76 [[nodiscard]] long double ExcessKurtosis() const override;
77
78private:
79 RealType quantileImpl(double p) const override;
80 RealType quantileImpl1m(double p) const override;
81
82 std::complex<double> CFImpl(double t) const override;
83
84 static constexpr char TOO_LARGE_A[] =
85 "Minimum element of the sample is smaller than lower boundary returned by method: ";
86 static constexpr char TOO_SMALL_B[] =
87 "Maximum element of the sample is greater than upper boundary returned by method: ";
88
89public:
90 [[nodiscard]] long double Entropy() const;
91
92 [[nodiscard]] double LikelihoodFunction(std::span<const RealType> sample) const override;
93 [[nodiscard]] double LogLikelihoodFunction(std::span<const RealType> sample) const override;
94
101 RANDLIB_DEPRECATED_FIT_MEMBER void FitMinimum(std::span<const RealType> sample, bool unbiased = false);
108 RANDLIB_DEPRECATED_FIT_MEMBER void FitMaximum(std::span<const RealType> sample, bool unbiased = false);
115 RANDLIB_DEPRECATED_FIT_MEMBER void Fit(std::span<const RealType> sample, bool unbiased = false);
116
125 FitMaximumBayes(std::span<const RealType> sample, const ParetoRand<RealType> &priorDistribution, bool MAP = false);
126};
127
128} // namespace randlib
129
131
132namespace randlib
133{
134
135template <typename RealType>
136UniformRand<RealType>::UniformRand(double minValue, double maxValue)
137 : BetaDistribution<RealType>(1, 1, minValue, maxValue)
138{
139}
140
141template <typename RealType>
143{
144 return "Uniform(" + this->toStringWithPrecision(MinValue()) + ", " + this->toStringWithPrecision(MaxValue()) + ")";
145}
146
147template <typename RealType>
148double UniformRand<RealType>::f(const RealType &x) const
149{
150 return (x < this->a || x > this->b) ? 0.0 : this->bmaInv;
151}
152
153template <typename RealType>
154double UniformRand<RealType>::logf(const RealType &x) const
155{
156 return (x < this->a || x > this->b) ? -INFINITY : -this->logbma;
157}
158
159template <typename RealType>
160double UniformRand<RealType>::F(const RealType &x) const
161{
162 if (x < this->a)
163 return 0.0;
164 return (x > this->b) ? 1.0 : this->bmaInv * (x - this->a);
165}
166
167template <typename RealType>
168double UniformRand<RealType>::S(const RealType &x) const
169{
170 if (x < this->a)
171 return 1.0;
172 return (x > this->b) ? 0.0 : this->bmaInv * (this->b - x);
173}
174
175template <typename RealType>
176template <std::uniform_random_bit_generator G>
178{
179 return this->a + StandardVariate(g) * this->bma;
180}
181
182template <typename RealType>
183template <std::uniform_random_bit_generator G>
188
189template <typename RealType>
190template <std::uniform_random_bit_generator G>
195
196template <typename RealType>
197template <std::uniform_random_bit_generator G>
202
203template <typename RealType>
205{
206 return 0.5 * (this->b + this->a);
207}
208
209template <typename RealType>
211{
212 return this->bma * this->bma / 12;
213}
214
215template <typename RealType>
217{
218 return 0.5 * (this->b + this->a);
219}
220
221template <typename RealType>
223{
225 return 0.5 * (this->b + this->a);
226}
227
228template <typename RealType>
230{
231 return 0.0;
232}
233
234template <typename RealType>
236{
237 return -1.2;
238}
239
240template <typename RealType>
241RealType UniformRand<RealType>::quantileImpl(double p) const
242{
243 return this->a + this->bma * p;
244}
245
246template <typename RealType>
247RealType UniformRand<RealType>::quantileImpl1m(double p) const
248{
249 return this->b - this->bma * p;
250}
251
252template <typename RealType>
253std::complex<double> UniformRand<RealType>::CFImpl(double t) const
254{
255 double cosX = std::cos(t * this->b), sinX = std::sin(t * this->b);
256 double cosY = std::cos(t * this->a), sinY = std::sin(t * this->a);
257 std::complex<double> numerator(cosX - cosY, sinX - sinY);
258 std::complex<double> denominator(0, t * this->bma);
259 return numerator / denominator;
260}
261
262template <typename RealType>
264{
265 return (this->b == this->a) ? -INFINITY : std::log(this->bma);
266}
267
268template <typename RealType>
269double UniformRand<RealType>::LikelihoodFunction(std::span<const RealType> sample) const
270{
271 bool sampleIsInsideInterval =
272 this->allElementsAreNotSmallerThan(this->a, sample) && this->allElementsAreNotGreaterThan(this->b, sample);
273 return sampleIsInsideInterval ? std::pow(this->bma, -static_cast<double>(sample.size())) : 0.0;
274}
275
276template <typename RealType>
277double UniformRand<RealType>::LogLikelihoodFunction(std::span<const RealType> sample) const
278{
279 bool sampleIsInsideInterval =
280 this->allElementsAreNotSmallerThan(this->a, sample) && this->allElementsAreNotGreaterThan(this->b, sample);
281 int sample_size = sample.size();
282 return sampleIsInsideInterval ? -sample_size * this->logbma : -INFINITY;
283}
284
285template <typename RealType>
287template <typename RealType>
289
290template <typename RealType>
291void UniformRand<RealType>::FitMinimum(std::span<const RealType> sample, bool unbiased)
292{
293 if (!this->allElementsAreNotGreaterThan(this->b, sample))
294 throw this->fitError(FitErrorCode::WrongSample,
295 this->UPPER_LIMIT_VIOLATION + this->toStringWithPrecision(this->b));
296 RealType minVar = *std::min_element(sample.begin(), sample.end());
297
298 if (unbiased == true) {
299 int n = sample.size();
301 RealType minVarAdj = (minVar * (n + 1) - this->b) / n;
302 if (!this->allElementsAreNotSmallerThan(minVarAdj, sample))
303 throw this->fitError(FitErrorCode::WrongReturn, TOO_LARGE_A + this->toStringWithPrecision(minVarAdj));
304 SetSupport(minVarAdj, this->b);
305 } else {
306 SetSupport(minVar, this->b);
307 }
308}
309
310template <typename RealType>
311void UniformRand<RealType>::FitMaximum(std::span<const RealType> sample, bool unbiased)
312{
313 if (!this->allElementsAreNotSmallerThan(this->a, sample))
314 throw this->fitError(FitErrorCode::WrongSample,
315 this->LOWER_LIMIT_VIOLATION + this->toStringWithPrecision(this->a));
316 RealType maxVar = *std::max_element(sample.begin(), sample.end());
317
318 if (unbiased == true) {
319 int n = sample.size();
321 RealType maxVarAdj = (maxVar * (n + 1) - this->a) / n;
322 if (!this->allElementsAreNotGreaterThan(maxVarAdj, sample))
323 throw this->fitError(FitErrorCode::WrongReturn, TOO_SMALL_B + this->toStringWithPrecision(maxVarAdj));
324 SetSupport(this->a, maxVarAdj);
325 } else {
326 SetSupport(this->a, maxVar);
327 }
328}
329
330template <typename RealType>
331void UniformRand<RealType>::Fit(std::span<const RealType> sample, bool unbiased)
332{
333 double minVar = *std::min_element(sample.begin(), sample.end());
334 double maxVar = *std::max_element(sample.begin(), sample.end());
335 if (unbiased == true) {
336 int n = sample.size();
338 RealType minVarAdj = (minVar * n - maxVar) / (n - 1);
340 RealType maxVarAdj = (maxVar * n - minVar) / (n - 1);
341 if (!this->allElementsAreNotSmallerThan(minVarAdj, sample))
342 throw this->fitError(FitErrorCode::WrongReturn, TOO_LARGE_A + this->toStringWithPrecision(minVarAdj));
343 if (!this->allElementsAreNotGreaterThan(maxVarAdj, sample))
344 throw this->fitError(FitErrorCode::WrongReturn, TOO_SMALL_B + this->toStringWithPrecision(maxVarAdj));
345 SetSupport(minVarAdj, maxVarAdj);
346 } else {
347 SetSupport(minVar, maxVar);
348 }
349}
350
351template <typename RealType>
353 const ParetoRand<RealType> &priorDistribution, bool MAP)
354{
355 if (!this->allElementsAreNotSmallerThan(this->a, sample))
356 throw this->fitError(FitErrorCode::WrongSample,
357 this->LOWER_LIMIT_VIOLATION + this->toStringWithPrecision(this->a));
358 double maxVar = *std::max_element(sample.begin(), sample.end());
359 int n = sample.size();
360 double newShape = priorDistribution.GetShape() + n;
361 double newScale = std::max(priorDistribution.GetScale(), maxVar - this->a);
362 ParetoRand<RealType> posteriorDistribution(newShape, newScale);
363 double theta = MAP ? posteriorDistribution.Mode() : posteriorDistribution.Mean();
364 SetSupport(this->a, this->a + theta);
365 return posteriorDistribution;
366}
367
368} // namespace randlib
The BetaDistribution class Abstract class for Beta distribution.
Definition continuous/beta.hpp:33
double b
max bound
Definition continuous/beta.hpp:41
void SetSupport(double minValue, double maxValue)
Definition continuous/beta.hpp:446
double a
min bound
Definition continuous/beta.hpp:40
The ParetoRand class Pareto distribution.
Definition pareto.hpp:24
double GetScale() const noexcept
Definition pareto.hpp:44
double GetShape() const noexcept
Definition pareto.hpp:43
long double Mean() const override
Definition pareto.hpp:244
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition pareto.hpp:267
The UniformRand class Uniform continuous distribution.
Definition continuous/uniform.hpp:30
long double Entropy() const
Definition continuous/uniform.hpp:263
void Fit(std::span< const RealType > sample, bool unbiased=false)
fit support with maximum-likelihood estimator if unbiased == false, fit support using UMVU estimator ...
Definition continuous/uniform.hpp:331
std::string Name() const override
Definition continuous/uniform.hpp:142
double F(const RealType &x) const override
Definition continuous/uniform.hpp:160
long double Mean() const override
Definition continuous/uniform.hpp:204
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition continuous/uniform.hpp:222
long double Variance() const override
Definition continuous/uniform.hpp:210
Support SupportType() const override
Definition continuous/uniform.hpp:39
long double Skewness() const override
Definition continuous/uniform.hpp:229
RealType operator()(G &g) const
Definition continuous/uniform.hpp:177
RealType Median() const override
Definition continuous/uniform.hpp:216
double LogLikelihoodFunction(std::span< const RealType > sample) const override
Definition continuous/uniform.hpp:277
RealType MinValue() const override
Definition continuous/uniform.hpp:40
RealType MaxValue() const override
Definition continuous/uniform.hpp:41
double logf(const RealType &x) const override
Definition continuous/uniform.hpp:154
UniformRand(double minValue=0, double maxValue=1)
Definition continuous/uniform.hpp:136
ParetoRand< RealType > FitMaximumBayes(std::span< const RealType > sample, const ParetoRand< RealType > &priorDistribution, bool MAP=false)
fit maximum parameter, using Bayesian estimation
Definition continuous/uniform.hpp:352
void FitMinimum(std::span< const RealType > sample, bool unbiased=false)
fit minimum with maximum-likelihood estimator if unbiased == false, fit minimum using UMVU estimator ...
Definition continuous/uniform.hpp:291
static RealType StandardVariateHalfClosed(G &g)
Definition continuous/uniform.hpp:198
double LikelihoodFunction(std::span< const RealType > sample) const override
Definition continuous/uniform.hpp:269
static RealType StandardVariateClosed(G &g)
Definition continuous/uniform.hpp:191
long double ExcessKurtosis() const override
Definition continuous/uniform.hpp:235
double S(const RealType &x) const override
Definition continuous/uniform.hpp:168
static RealType StandardVariate(G &g)
Definition continuous/uniform.hpp:184
void FitMaximum(std::span< const RealType > sample, bool unbiased=false)
fit maximum with maximum-likelihood estimator if unbiased == false, fit maximum using UMVU estimator ...
Definition continuous/uniform.hpp:311
double f(const RealType &x) const override
Definition continuous/uniform.hpp:148
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
#define RANDLIB_DEPRECATED_FIT_MEMBER
Definition fwd.hpp:9
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
@ WrongSample
the sample could not have been drawn from this distribution
@ WrongReturn
the method returned invalid parameters
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15