RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
levy.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <span>
4
9
10namespace randlib
11{
12
27template <typename RealType>
28class LevyRand final : public StableDistribution<RealType>
29{
30 RealType variate(detail::EngineRef g) const override { return (*this)(g); }
31
32public:
33 LevyRand(double location = 0, double scale = 1);
34 [[nodiscard]] std::string Name() const override;
35
36 [[nodiscard]] double f(const RealType &x) const override;
37 [[nodiscard]] double logf(const RealType &x) const override;
38 [[nodiscard]] double F(const RealType &x) const override;
39 [[nodiscard]] double S(const RealType &x) const override;
40 template <std::uniform_random_bit_generator G>
41 [[nodiscard]] RealType operator()(G &g) const;
42
43 template <std::uniform_random_bit_generator G>
44 [[nodiscard]] static RealType StandardVariate(G &g);
45
46private:
47 RealType quantileImpl(double p) const override;
48 RealType quantileImpl1m(double p) const override;
49
50 std::complex<double> CFImpl(double t) const override;
51
52public:
57 RANDLIB_DEPRECATED_FIT_MEMBER void FitScale(std::span<const RealType> sample);
58};
59
60} // namespace randlib
61
63
64namespace randlib
65{
66
67template <typename RealType>
68LevyRand<RealType>::LevyRand(double location, double scale) : StableDistribution<RealType>(0.5, 1, scale, location)
69{
70}
71
72template <typename RealType>
73std::string LevyRand<RealType>::Name() const
74{
75 return "Levy(" + this->toStringWithPrecision(this->GetLocation()) + ", " +
76 this->toStringWithPrecision(this->GetScale()) + ")";
77}
78
79template <typename RealType>
80double LevyRand<RealType>::f(const RealType &x) const
81{
82 return this->pdfLevy(x);
83}
84
85template <typename RealType>
86double LevyRand<RealType>::logf(const RealType &x) const
87{
88 return this->logpdfLevy(x);
89}
90
91template <typename RealType>
92double LevyRand<RealType>::F(const RealType &x) const
93{
94 return this->cdfLevy(x);
95}
96
97template <typename RealType>
98double LevyRand<RealType>::S(const RealType &x) const
99{
100 return this->cdfLevyCompl(x);
101}
102
103template <typename RealType>
104template <std::uniform_random_bit_generator G>
106{
107 RealType rv = detail::standardNormal<RealType>(g);
108 rv *= rv;
109 rv = this->gamma / rv;
110 return this->mu + rv;
111}
112
113template <typename RealType>
114template <std::uniform_random_bit_generator G>
116{
117 RealType rv = detail::standardNormal<RealType>(g);
118 return 1.0 / (rv * rv);
119}
120
121template <typename RealType>
122RealType LevyRand<RealType>::quantileImpl(double p) const
123{
124 return this->quantileLevy(p);
125}
126
127template <typename RealType>
128RealType LevyRand<RealType>::quantileImpl1m(double p) const
129{
130 return this->quantileLevy1m(p);
131}
132
133template <typename RealType>
134std::complex<double> LevyRand<RealType>::CFImpl(double t) const
135{
136 return this->cfLevy(t);
137}
138
139template <typename RealType>
140void LevyRand<RealType>::FitScale(std::span<const RealType> sample)
141{
143 if (!this->allElementsAreNotSmallerThan(this->mu, sample))
144 throw this->fitError(FitErrorCode::WrongSample,
145 this->LOWER_LIMIT_VIOLATION + this->toStringWithPrecision(this->mu));
146 long double invSum = 0.0;
147 for (RealType var : sample)
148 invSum += 1.0 / (var - this->mu);
149 invSum = 1.0 / invSum;
150 this->SetScale(sample.size() * invSum);
151}
152
153} // namespace randlib
The LevyRand class Levy distribution.
Definition levy.hpp:29
void FitScale(std::span< const RealType > sample)
Fit scale using maximum-likelihoood estimator.
Definition levy.hpp:140
LevyRand(double location=0, double scale=1)
Definition levy.hpp:68
static RealType StandardVariate(G &g)
Definition levy.hpp:115
double S(const RealType &x) const override
Definition levy.hpp:98
RealType operator()(G &g) const
Definition levy.hpp:105
double F(const RealType &x) const override
Definition levy.hpp:92
double logf(const RealType &x) const override
Definition levy.hpp:86
double f(const RealType &x) const override
Definition levy.hpp:80
std::string Name() const override
Definition levy.hpp:73
virtual void sample(detail::EngineRef g, std::span< T > outputData) const
Fills the output with variate(g); distributions with a faster bulk algorithm override it.
Definition distribution.hpp:66
The StableDistribution class Abstract class for Stable distribution.
Definition stable.hpp:74
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