22template <
typename IntType>
30 [[nodiscard]] std::string
Name()
const override;
34 [[nodiscard]]
double P(
const IntType &k)
const override;
35 [[nodiscard]]
double logP(
const IntType &k)
const override;
36 [[nodiscard]]
double F(
const IntType &k)
const override;
37 [[nodiscard]]
double S(
const IntType &k)
const override;
38 template <std::uniform_random_bit_generator G>
41 template <std::uniform_random_bit_generator G>
42 [[nodiscard]]
static IntType
Variate(
double probability, G &g);
44 [[nodiscard]] IntType
Median()
const override;
46 [[nodiscard]]
long double Entropy()
const;
57template <
typename IntType>
60 return "Geometric(" + this->toStringWithPrecision(this->GetProbability()) +
")";
63template <
typename IntType>
66 if (!(probability > 0.0 && probability <= 1.0))
67 throw std::invalid_argument(
"Geometric distribution: probability parameter should be in interval (0, 1]");
68 this->SetParameters(1, probability);
71template <
typename IntType>
75 return (k == 0) ? 1.0 : 0.0;
76 return (k < 0) ? 0 : this->p * std::exp(k * this->log1mProb);
79template <
typename IntType>
83 return (k == 0) ? 0.0 : -INFINITY;
84 return (k < 0) ? -INFINITY : this->logProb + k * this->log1mProb;
87template <
typename IntType>
90 return (k < 0) ? 0 : -std::expm1l((k + 1) * this->log1mProb);
93template <
typename IntType>
96 return (k < 0) ? 1 : std::exp((k + 1) * this->log1mProb);
99template <
typename IntType>
100template <std::uniform_random_bit_generator G>
106 if (genId == this->EXPONENTIAL)
107 return this->variateGeometricThroughExponential(g);
108 if (genId == this->TABLE)
109 return this->variateGeometricByTable(g);
111 throw std::runtime_error(
"Geometric distribution: sampling failed");
114template <
typename IntType>
115template <std::uniform_random_bit_generator G>
118 if (!(probability <= 1.0 && probability > 0.0))
119 throw std::invalid_argument(
120 "Geometric distribution: probability parameter should be in interval (0, 1], but it's equal to " +
121 std::to_string(probability));
123 if (probability == 1.0)
127 if (probability < 0.05) {
128 double rate = -std::log1pl(-probability);
130 if (!std::isfinite(X) || X >=
static_cast<long double>(std::numeric_limits<IntType>::max()))
131 return std::numeric_limits<IntType>::max();
132 return static_cast<IntType
>(X);
137 double prod = probability, sum = prod, qprob = 1.0 - probability;
146template <
typename IntType>
149 if (this->p == 1.0) {
150 std::fill(outputData.begin(), outputData.end(), 0);
154 if (genId == this->EXPONENTIAL) {
155 for (IntType &var : outputData)
156 var = this->variateGeometricThroughExponential(g);
157 }
else if (genId == this->TABLE) {
158 for (IntType &var : outputData)
159 var = this->variateGeometricByTable(g);
163template <
typename IntType>
169 double flooredMedian = std::floor(median);
173template <
typename IntType>
178 double a = -this->q * this->log1mProb;
179 double b = -this->p * this->logProb;
180 return (a + b) / this->p;
The GeometricRand class Geometric distribution.
Definition geometric.hpp:24
void SetProbability(double probability)
Definition geometric.hpp:64
static IntType Variate(double probability, G &g)
Definition geometric.hpp:116
std::string Name() const override
Definition geometric.hpp:58
double S(const IntType &k) const override
Definition geometric.hpp:94
IntType operator()(G &g) const
Definition geometric.hpp:101
GeometricRand(double probability=0.5)
Definition geometric.hpp:29
double P(const IntType &k) const override
Definition geometric.hpp:72
IntType Median() const override
Definition geometric.hpp:164
double F(const IntType &k) const override
Definition geometric.hpp:88
double logP(const IntType &k) const override
Definition geometric.hpp:80
long double Entropy() const
Definition geometric.hpp:174
GENERATOR_ID
Definition negative_binomial.hpp:69
The NegativeBinomialRand class Negative binomial distribution.
Definition negative_binomial.hpp:133
T Variate() const
Uses the engine of this object, so it is not thread-safe for one object; use operator() with one engi...
Definition distribution.hpp:133
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
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
bool areClose(RealType a, RealType b, RealType eps=1e-6)
Definition functions.hpp:38
Definition bivariate_normal.hpp:10