BetaBinomialDistribution
Represents the beta-binomial distribution, a compound distribution arising when the success probability of a binomial distribution is itself random and follows a beta distribution.
The beta-binomial distribution models overdispersed count data where the assumption of a fixed success probability is too restrictive. Instead of a single probability, each observation draws its own success probability from a Beta(alpha, beta) distribution, and the count of successes in trials independent trials then follows a binomial. This two-stage model captures extra variability beyond what a simple binomial allows, making it suitable for scenarios such as the number of defective items in batches with varying quality, or the number of positive responses in surveys where response rates vary across groups.
The support is {0, 1, ..., trials}. Sampling works by first drawing a random probability from Beta(alpha, beta), then drawing from Binomial(trials, p). The PMF, CDF, and survival function are computed in log-space using the log-beta function and the log-sum-exp technique for numerical stability.
Example:
val dist = BetaBinomialDistribution(trials = 10, alpha = 2.0, beta = 5.0)
dist.pmf(3) // probability of exactly 3 successes
dist.cdf(3) // probability of 3 or fewer successes
dist.quantileInt(0.5) // median number of successes
dist.mean // 2.857... (10 * 2 / (2 + 5))
dist.variance // 3.316... (overdispersed relative to binomial)
dist.sample(Random(42)) // a single random drawProperties
The skewness of this distribution, computed from the shape parameters and trial count. Returns Double.NaN when trials is zero.
Functions
Returns the quantile (inverse CDF) for the given probability p as an integer.