17template <
typename RealType>
24 [[nodiscard]] std::string
Name()
const override;
26 [[nodiscard]] RealType
MinValue()
const override {
return 0; }
27 [[nodiscard]] RealType
MaxValue()
const override {
return INFINITY; }
30 static double L(RealType x);
31 static double K(RealType x);
34 [[nodiscard]]
double f(
const RealType &x)
const override;
35 [[nodiscard]]
double logf(
const RealType &x)
const override;
36 [[nodiscard]]
double F(
const RealType &x)
const override;
37 [[nodiscard]]
double S(
const RealType &x)
const override;
38 double logF(
const RealType &x)
const;
39 double logS(
const RealType &x)
const;
42 template <std::uniform_random_bit_generator G>
43 RealType truncatedGammaVariate(G &g)
const;
44 template <std::uniform_random_bit_generator G>
45 RealType variateForTheLeftMostInterval(G &g)
const;
46 template <std::uniform_random_bit_generator G>
47 RealType variateForTheRightMostInterval(G &g)
const;
50 template <std::uniform_random_bit_generator G>
53 [[nodiscard]]
long double Mean()
const override;
54 [[nodiscard]]
long double Variance()
const override;
55 [[nodiscard]] RealType
Mode()
const override;
56 [[nodiscard]] RealType
Median()
const override;
59 RealType quantileImpl(
double p)
const override;
60 RealType quantileImpl1m(
double p)
const override;
71template <
typename RealType>
76template <
typename RealType>
79 return "Kolmogorov-Smirnov";
82template <
typename RealType>
87 double sum = 0.0, addon = 0.0;
91 int temp = (2 * k - 1);
93 addon = std::exp(-temp * aux);
100template <
typename RealType>
101double KolmogorovSmirnovRand<RealType>::K(RealType x)
105 double sum = 0.0, addon = 0.0;
109 int temp = 2 * k * k;
110 addon = std::exp(-temp * xSq);
111 sum += (k & 1) ? addon : -addon;
117template <
typename RealType>
122 double sum = 0.0, addon = 0.0;
126 double aux = 0.125 / xSq;
130 addon = temp - 4 * xSq;
131 addon *= std::exp(-temp * aux);
140 addon = std::exp(-2 * temp * xSq);
142 sum += (k & 1) ? addon : -addon;
148template <
typename RealType>
151 return std::log(f(x));
154template <
typename RealType>
157 return (x > 1.0) ? 1.0 - K(x) : L(x);
160template <
typename RealType>
163 return (x > 1.0) ? K(x) : 1.0 - L(x);
166template <
typename RealType>
169 return (x > 1.0) ? std::log1pl(-K(x)) : std::log(L(x));
172template <
typename RealType>
175 return (x > 1.0) ? std::log(K(x)) : std::log1pl(-L(x));
178template <
typename RealType>
179template <std::uniform_random_bit_generator G>
183 static constexpr long double tp = 2.193245422464302l;
184 static constexpr long double rate = 1.2952909208355123l;
189 RealType gammaValue = tp + E0;
190 if (E0 * E0 <= tp * E1 * (gammaValue + tp))
192 RealType Wp = E0 / tp;
193 if (Wp - std::log1pl(Wp) <= E1)
195 }
while (++iter <= ProbabilityDistribution<RealType>::MAX_ITER_REJECTION);
196 throw std::runtime_error(
"Kolmogorov-Smirnov distribution: sampling failed");
199template <
typename RealType>
200template <std::uniform_random_bit_generator G>
201RealType KolmogorovSmirnovRand<RealType>::variateForTheLeftMostInterval(G &g)
const
205 RealType gammaValue = truncatedGammaVariate(g);
208 RealType Z = 0.5 / gammaValue;
209 size_t n = 1, iter2 = 0;
212 while (U >= W && ++iter2 <= ProbabilityDistribution<RealType>::MAX_ITER_REJECTION) {
218 Q = std::exp(gammaValue - gammaValue * nSq);
221 }
while (++iter1 <= ProbabilityDistribution<RealType>::MAX_ITER_REJECTION);
222 throw std::runtime_error(
"Kolmogorov-Smirnov distribution: sampling failed");
225template <
typename RealType>
226template <std::uniform_random_bit_generator G>
227RealType KolmogorovSmirnovRand<RealType>::variateForTheRightMostInterval(G &g)
const
229 static constexpr double tSq = 0.5625;
234 RealType X = std::sqrt(tSq + 0.5 * E);
236 size_t n = 1, iter2 = 0;
237 RealType Z = -2 * X * X;
238 while (U > W && ++iter2 <= ProbabilityDistribution<RealType>::MAX_ITER_REJECTION) {
241 W += nSq * std::exp(Z * (nSq - 1));
246 W -= nSq * std::exp(Z * (nSq - 1));
248 }
while (++iter1 <= ProbabilityDistribution<RealType>::MAX_ITER_REJECTION);
249 throw std::runtime_error(
"Kolmogorov-Smirnov distribution: sampling failed");
252template <
typename RealType>
253template <std::uniform_random_bit_generator G>
259 return isLeft ? variateForTheLeftMostInterval(g) : variateForTheRightMostInterval(g);
262template <
typename RealType>
268template <
typename RealType>
271 long double mean = Mean();
275template <
typename RealType>
278 return 0.735467812776958l;
281template <
typename RealType>
284 return 0.82757355518990761l;
287template <
typename RealType>
294template <
typename RealType>
295RealType KolmogorovSmirnovRand<RealType>::quantileImpl1m(
double p)
const
297 RealType guess = std::sqrt(-0.5 * std::log(0.5 * p));
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
RealType quantileImpl(double p, RealType initValue) const override
Definition continuous.hpp:140
RealType quantileImpl1m(double p, RealType initValue) const override
Definition continuous.hpp:152
The KolmogorovSmirnovRand class Kolmogorov-Smirnov distribution.
Definition kolmogorov_smirnov.hpp:19
double logf(const RealType &x) const override
Definition kolmogorov_smirnov.hpp:149
Support SupportType() const override
Definition kolmogorov_smirnov.hpp:25
long double Variance() const override
Definition kolmogorov_smirnov.hpp:269
RealType operator()(G &g) const
Definition kolmogorov_smirnov.hpp:254
std::string Name() const override
Definition kolmogorov_smirnov.hpp:77
long double Mean() const override
Definition kolmogorov_smirnov.hpp:263
double S(const RealType &x) const override
Definition kolmogorov_smirnov.hpp:161
double logS(const RealType &x) const
Definition kolmogorov_smirnov.hpp:173
RealType MinValue() const override
Definition kolmogorov_smirnov.hpp:26
double f(const RealType &x) const override
Definition kolmogorov_smirnov.hpp:118
RealType Median() const override
Definition kolmogorov_smirnov.hpp:282
RealType MaxValue() const override
Definition kolmogorov_smirnov.hpp:27
KolmogorovSmirnovRand()
Definition kolmogorov_smirnov.hpp:72
double logF(const RealType &x) const
Definition kolmogorov_smirnov.hpp:167
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition kolmogorov_smirnov.hpp:276
double F(const RealType &x) const override
Definition kolmogorov_smirnov.hpp:155
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
constexpr long double ln2
Definition constants.hpp:18
constexpr long double sqrtpi
Definition constants.hpp:31
constexpr long double pi
Definition constants.hpp:22
constexpr long double inv_sqrt2
Definition constants.hpp:28
constexpr long double pi_sq
Definition constants.hpp:39
constexpr long double sqrt2pi
Definition constants.hpp:32
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
constexpr long double minimumTolerance
Definition constants.hpp:44
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15