23template <
typename IntType>
35 [[nodiscard]] std::string
Name()
const override;
37 [[nodiscard]] IntType
MinValue()
const override {
return 0; }
38 [[nodiscard]] IntType
MaxValue()
const override {
return n; }
40 void SetParameters(IntType number,
double shape1,
double shape2);
41 [[nodiscard]]
inline IntType
GetNumber() const noexcept {
return n; }
45 [[nodiscard]]
double P(
const IntType &k)
const override;
46 [[nodiscard]]
double logP(
const IntType &k)
const override;
47 [[nodiscard]]
double F(
const IntType &k)
const override;
50 template <std::uniform_random_bit_generator G>
51 IntType VariateUniform(G &g)
const;
52 template <std::uniform_random_bit_generator G>
53 IntType VariateBeta(G &g)
const;
56 template <std::uniform_random_bit_generator G>
59 [[nodiscard]]
long double Mean()
const override;
60 [[nodiscard]]
long double Variance()
const override;
61 [[nodiscard]] IntType
Mode()
const override;
62 [[nodiscard]]
long double Skewness()
const override;
74template <
typename IntType>
77 SetParameters(number, shape1, shape2);
80template <
typename IntType>
83 return "Beta-Binomial(" + this->toStringWithPrecision(GetNumber()) +
", " +
84 this->toStringWithPrecision(GetAlpha()) +
", " + this->toStringWithPrecision(GetBeta()) +
")";
87template <
typename IntType>
90 if (!(shape1 > 0.0 && shape2 > 0.0))
91 throw std::invalid_argument(
"Beta-Binomial distribution: shape parameters should be positive");
93 throw std::invalid_argument(
"Beta-Binomial distribution: number should be positive");
95 B.SetShapes(shape1, shape2);
97 pmfCoef -= std::lgammal(B.GetAlpha() + B.GetBeta() + n);
98 pmfCoef -= B.GetLogBetaFunction();
101template <
typename IntType>
104 return (k < 0 || k > n) ? 0.0 : std::exp(logP(k));
107template <
typename IntType>
112 double y = std::lgammal(k + B.GetAlpha());
113 y += std::lgammal(n - k + B.GetBeta());
119template <
typename IntType>
134template <
typename IntType>
135template <std::uniform_random_bit_generator G>
141template <
typename IntType>
142template <std::uniform_random_bit_generator G>
143IntType BetaBinomialRand<IntType>::VariateBeta(G &g)
const
149template <
typename IntType>
150template <std::uniform_random_bit_generator G>
153 return (B.GetAlpha() == 1 && B.GetBeta() == 1) ? VariateUniform(g) : VariateBeta(g);
156template <
typename IntType>
159 if (B.GetAlpha() == 1 && B.GetBeta() == 1) {
160 for (IntType &var : outputData)
161 var = VariateUniform(g);
163 for (IntType &var : outputData)
164 var = VariateBeta(g);
168template <
typename IntType>
172 double beta = B.GetBeta();
173 return n * alpha / (alpha + beta);
176template <
typename IntType>
179 double alpha = B.GetAlpha();
180 double beta = B.GetBeta();
181 double alphaPBeta = alpha + beta;
182 double numerator = n * alpha * beta * (alphaPBeta + n);
183 double denominator = alphaPBeta * alphaPBeta;
184 denominator *= (alphaPBeta + 1);
185 return numerator / denominator;
188template <
typename IntType>
191 IntType mode = (IntType)(n * B.Mode());
192 double logPmode = this->logP(mode);
193 if (this->logP(mode + 1) > logPmode)
195 if (this->logP(mode - 1) > logPmode)
200template <
typename IntType>
203 long double alpha = B.GetAlpha();
204 long double beta = B.GetBeta();
205 long double alphaPBeta = alpha + beta;
206 long double res = (1 + alphaPBeta) / (n * alpha * beta * (alphaPBeta + n));
207 res = std::sqrt(res);
208 res *= (alphaPBeta + 2 * n) * (beta - alpha);
209 res /= alphaPBeta + 2;
213template <
typename IntType>
216 long double alpha = B.GetAlpha();
217 long double beta = B.GetBeta();
218 long double alphaPBeta = alpha + beta;
219 long double alphaBetaN = alpha * beta * n;
220 long double res = alpha * beta * (n - 2);
221 res += 2 * (double)n * n;
222 res -= alphaBetaN * (6 - n) / alphaPBeta;
223 res -= 6 * alphaBetaN * n / (alphaPBeta * alphaPBeta);
225 res += alphaPBeta * (alphaPBeta - 1 + 6 * n);
226 res *= alphaPBeta * alphaPBeta * (1 + alphaPBeta);
227 res /= (alphaBetaN * (alphaPBeta + 2) * (alphaPBeta + 3) * (alphaPBeta + n));
The BetaBinomialRand class Beta-Binomial distribution.
Definition beta_binomial.hpp:25
IntType GetNumber() const noexcept
Definition beta_binomial.hpp:41
std::string Name() const override
Definition beta_binomial.hpp:81
long double ExcessKurtosis() const override
Definition beta_binomial.hpp:214
double GetAlpha() const
Definition beta_binomial.hpp:42
BetaBinomialRand(IntType number=1, double shape1=1, double shape2=1)
Definition beta_binomial.hpp:75
void SetParameters(IntType number, double shape1, double shape2)
Definition beta_binomial.hpp:88
long double Variance() const override
Definition beta_binomial.hpp:177
double GetBeta() const
Definition beta_binomial.hpp:43
double logP(const IntType &k) const override
Definition beta_binomial.hpp:108
IntType operator()(G &g) const
Definition beta_binomial.hpp:151
IntType MaxValue() const override
Definition beta_binomial.hpp:38
IntType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition beta_binomial.hpp:189
double F(const IntType &k) const override
Definition beta_binomial.hpp:120
IntType MinValue() const override
Definition beta_binomial.hpp:37
long double Mean() const override
Definition beta_binomial.hpp:169
Support SupportType() const override
Definition beta_binomial.hpp:36
long double Skewness() const override
Definition beta_binomial.hpp:201
double P(const IntType &k) const override
Definition beta_binomial.hpp:102
double GetAlpha() const noexcept
Definition continuous/beta.hpp:128
double GetBeta() const noexcept
Definition continuous/beta.hpp:132
The BetaRand class Beta distribution.
Definition continuous/beta.hpp:246
The DiscreteDistribution class Abstract class for all discrete distributions.
Definition discrete.hpp:21
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