18template <
typename IntType>
31 [[nodiscard]] std::string
Name()
const override;
33 [[nodiscard]] IntType
MinValue()
const override {
return 0; }
34 [[nodiscard]] IntType
MaxValue()
const override {
return N - M; }
36 void SetParameters(IntType totalSize, IntType totalSuccessesNum, IntType limitSuccessesNum);
41 [[nodiscard]]
double P(
const IntType &k)
const override;
42 [[nodiscard]]
double logP(
const IntType &k)
const override;
43 [[nodiscard]]
double F(
const IntType &k)
const override;
44 template <std::uniform_random_bit_generator G>
47 [[nodiscard]]
long double Mean()
const override;
48 [[nodiscard]]
long double Variance()
const override;
52 static constexpr double SMALL_URN = 16;
53 template <std::uniform_random_bit_generator G>
54 IntType variateByUrn(G &g)
const;
65template <
typename IntType>
67 IntType limitSuccessesNum)
69 SetParameters(totalSize, totalSuccessesNum, limitSuccessesNum);
72template <
typename IntType>
75 return "Negative hypergeometric(" + this->toStringWithPrecision(N) +
", " + this->toStringWithPrecision(M) +
", " +
76 this->toStringWithPrecision(m) +
")";
79template <
typename IntType>
81 IntType limitSuccessesNum)
83 if (!(totalSize > 0 && totalSuccessesNum > 0 && limitSuccessesNum > 0))
84 throw std::invalid_argument(
"Negative-HyperGeometric distribution: all parameters should be positive");
85 if (!(totalSuccessesNum <= totalSize))
86 throw std::invalid_argument(
87 "Negative-HyperGeometric distribution: total size shouldn't be smaller than total successes number");
88 if (!(limitSuccessesNum <= totalSuccessesNum))
89 throw std::invalid_argument(
"Negative-HyperGeometric distribution: total successes number shouldn't be smaller "
90 "than limit successes number");
93 M = totalSuccessesNum;
94 m = limitSuccessesNum;
96 successFraction.SetShapes(m, M - m + 1);
104template <
typename IntType>
107 return (k < MinValue() || k > MaxValue()) ? 0.0 : std::exp(logP(k));
110template <
typename IntType>
113 if (k < MinValue() || k > MaxValue())
122template <
typename IntType>
128 IntType maxVal = MaxValue();
131 if (k <= 0.5 * maxVal) {
134 for (IntType i = 0; i <= k; ++i)
140 for (IntType i = k + 1; i <= maxVal; ++i)
145template <
typename IntType>
146template <std::uniform_random_bit_generator G>
150 const double expectedDraws = m * (
static_cast<double>(N) + 1) / (
static_cast<double>(M) + 1);
151 if (expectedDraws < SMALL_URN)
152 return variateByUrn(g);
156template <
typename IntType>
157template <std::uniform_random_bit_generator G>
160 double p =
static_cast<double>(M) / N;
161 IntType successesNum = 0;
163 while (successesNum < m) {
167 p = M - successesNum;
170 return num - successesNum;
173template <
typename IntType>
176 long double mean = m;
178 mean /=
static_cast<long double>(M) + 1.0L;
182template <
typename IntType>
185 long double Mp1 =
static_cast<long double>(M) + 1.0L;
186 long double var = 1.0L - m / Mp1;
187 var *=
static_cast<long double>(N) + 1.0L;
189 var /= Mp1 * (Mp1 + 1);
The BetaRand class Beta distribution.
Definition continuous/beta.hpp:246
The DiscreteDistribution class Abstract class for all discrete distributions.
Definition discrete.hpp:21
The NegativeHyperGeometricRand class Negative hypergeometric distribution.
Definition negative_hypergeometric.hpp:20
Support SupportType() const override
Definition negative_hypergeometric.hpp:32
IntType operator()(G &g) const
Definition negative_hypergeometric.hpp:147
IntType GetLimitSuccessesNum()
Definition negative_hypergeometric.hpp:39
double P(const IntType &k) const override
Definition negative_hypergeometric.hpp:105
NegativeHyperGeometricRand(IntType totalSize=1, IntType totalSuccessesNum=1, IntType limitSuccessesNum=1)
Definition negative_hypergeometric.hpp:66
IntType GetTotalSuccessesNum()
Definition negative_hypergeometric.hpp:38
long double Variance() const override
Definition negative_hypergeometric.hpp:183
double logP(const IntType &k) const override
Definition negative_hypergeometric.hpp:111
IntType MinValue() const override
Definition negative_hypergeometric.hpp:33
double F(const IntType &k) const override
Definition negative_hypergeometric.hpp:123
void SetParameters(IntType totalSize, IntType totalSuccessesNum, IntType limitSuccessesNum)
Definition negative_hypergeometric.hpp:80
IntType GetTotalSize()
Definition negative_hypergeometric.hpp:37
long double Mean() const override
Definition negative_hypergeometric.hpp:174
std::string Name() const override
Definition negative_hypergeometric.hpp:73
IntType MaxValue() const override
Definition negative_hypergeometric.hpp:34
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
Real lfact(size_t n)
Calculate log(n!) using table values for small n <= 255 and built-in lgamma function for large n > 25...
Definition math/gamma.hpp:591
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15