19template <
typename IntType>
29 static constexpr int DIRECT_TERMS = 16;
33 [[nodiscard]] std::string
Name()
const override;
36 [[nodiscard]] IntType
MinValue()
const override {
return 1; }
37 [[nodiscard]] IntType
MaxValue()
const override {
return std::numeric_limits<IntType>::max(); }
40 [[nodiscard]]
inline double GetExponent() const noexcept {
return s; }
42 [[nodiscard]]
double logP(
const IntType &k)
const override;
43 [[nodiscard]]
double F(
const IntType &k)
const override;
44 [[nodiscard]]
double S(
const IntType &k)
const override;
45 template <std::uniform_random_bit_generator G>
48 [[nodiscard]]
long double Mean()
const override;
49 [[nodiscard]]
long double Variance()
const override;
50 [[nodiscard]] IntType
Mode()
const override;
51 [[nodiscard]]
long double Skewness()
const override;
53 [[nodiscard]]
long double Moment(
int n)
const;
57 [[nodiscard]]
inline long double GetZetaFunction() const noexcept {
return zetaS; }
74 static constexpr long double B2jOverFact[] = {1.0L / 12,
79 -691.0L / 1307674368000.0L,
80 1.0L / 74724249600.0L,
81 -3617.0L / 10670622842880000.0L,
82 43867.0L / 5109094217170944000.0L,
83 -174611.0L / 802857662698291200000.0L};
87 for (
int j = 0;
j < 10; ++
j) {
97template <
typename IntType>
100 SetExponent(exponent);
103template <
typename IntType>
106 return "Zeta(" + this->toStringWithPrecision(GetExponent()) +
")";
109template <
typename IntType>
112 if (!(exponent > 1.0))
113 throw std::invalid_argument(
"Zeta distribution: exponent should be greater than 1, but it's equal to " +
114 std::to_string(exponent));
118 logZetaS = std::log(zetaS);
122template <
typename IntType>
125 return (k < 1) ? -INFINITY : -logZetaS - s * std::log(k);
128template <
typename IntType>
136template <
typename IntType>
142 long double tail = 0;
143 long long first =
static_cast<long long>(k) + 1;
144 for (; first < DIRECT_TERMS; ++first)
145 tail += std::pow(
static_cast<long double>(first), -
static_cast<long double>(s));
147 return static_cast<double>(tail / zetaS);
150template <
typename IntType>
151template <std::uniform_random_bit_generator G>
161 float T = std::pow(1.0 + 1.0 / X, sm1);
163 if (V * X * (T - 1) <= b * T)
166 long double logT = sm1 * std::log1pl(1.0 / X);
167 long double TM1 = std::expm1l(logT);
168 if (V * X * TM1 <= b * std::exp(logT))
169 return X < this->MaxValue() ? (IntType)X : this->MaxValue();
173 throw std::runtime_error(
"Zeta distribution: sampling failed");
176template <
typename IntType>
182template <
typename IntType>
192template <
typename IntType>
198template <
typename IntType>
206 long double z = zetaS, zSq = z * z;
207 long double logskew = zSq * z3;
208 logskew -= 3 * z2 * z1 * z;
209 logskew += 2 * z1 * z1Sq;
210 logskew = std::log(logskew);
211 logskew -= 1.5 * std::log(z * z2 - z1Sq);
212 logskew -= 2 * logZetaS;
213 return std::exp(logskew);
216template <
typename IntType>
221 long double mean = Mean();
222 long double secondMoment = this->SecondMoment();
223 long double thirdMoment = ThirdMoment();
224 long double fourthMoment = FourthMoment();
225 long double meanSq = mean * mean;
226 long double variance = secondMoment - meanSq;
227 long double numerator = fourthMoment - 4 * thirdMoment * mean + 6 * secondMoment * meanSq - 3 * meanSq * meanSq;
228 long double denominator = variance * variance;
229 return numerator / denominator - 3.0;
232template <
typename IntType>
The DiscreteDistribution class Abstract class for all discrete distributions.
Definition discrete.hpp:21
The ParetoRand class Pareto distribution.
Definition pareto.hpp:24
The ProbabilityDistribution class Abstract class for all probability distributions.
Definition distribution.hpp:28
The ZetaRand class Zeta distribution.
Definition zeta.hpp:21
Support SupportType() const override
Definition zeta.hpp:35
long double Moment(int n) const
Definition zeta.hpp:233
double GetExponent() const noexcept
Definition zeta.hpp:40
double S(const IntType &k) const override
Definition zeta.hpp:137
long double Skewness() const override
Definition zeta.hpp:199
long double GetLogZetaFunction() const noexcept
Definition zeta.hpp:58
void SetExponent(double exponent)
Definition zeta.hpp:110
long double GetZetaFunction() const noexcept
Definition zeta.hpp:57
IntType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition zeta.hpp:193
long double ThirdMoment() const override
Definition zeta.hpp:54
long double Variance() const override
Definition zeta.hpp:183
double F(const IntType &k) const override
Definition zeta.hpp:129
long double FourthMoment() const override
Definition zeta.hpp:55
ZetaRand(double exponent=2.0)
Definition zeta.hpp:98
long double Mean() const override
Definition zeta.hpp:177
std::string Name() const override
Definition zeta.hpp:104
IntType operator()(G &g) const
Definition zeta.hpp:152
IntType MinValue() const override
Definition zeta.hpp:36
long double ExcessKurtosis() const override
Definition zeta.hpp:217
IntType MaxValue() const override
Definition zeta.hpp:37
double logP(const IntType &k) const override
Definition zeta.hpp:123
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 pi_sq
Definition constants.hpp:39
constexpr long double ln3
Definition constants.hpp:19
constexpr long double lnpi
Definition constants.hpp:21
long double zetaTail(double exponent, long double firstTerm)
Definition zeta.hpp:72
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
Real harmonicNumber(Real exponent, int number)
Definition functions.hpp:533
Real riemannZeta(Real s)
Definition special_functions.hpp:340
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15