NegativeBinomialDistribution
Represents the negative binomial distribution, which models the number of failures before achieving a specified number of successes in a sequence of independent Bernoulli trials.
Each trial has the same success probability probability. The random variable counts how many failures occur before accumulating successes successes. For example, with successes = 3 and probability = 0.5, this distribution gives the probability of observing k tails before getting 3 heads in a series of fair coin flips.
The negative binomial generalizes the geometric distribution: setting successes = 1 yields a geometric distribution. The support is {0, 1, 2, ...} (all non-negative integers), where k = 0 means all required successes occurred with no failures.
The CDF and survival function are computed using the regularized incomplete beta function, which provides high accuracy without summing many individual PMF terms. Sampling is performed by summing successes independent geometric random variables.
Example:
// Number of failures before 5 successes, with p=0.4 per trial
val dist = NegativeBinomialDistribution(successes = 5, probability = 0.4)
dist.pmf(0) // 0.01024 (all 5 successes, no failures)
dist.pmf(5) // 0.1003 (5 failures before the 5th success)
dist.cdf(7) // 0.5765 (at most 7 failures)
dist.mean // 7.5 (expected number of failures)
dist.variance // 18.75
dist.quantileInt(0.5) // 7 (median number of failures)
dist.sample(Random(42)) // a single random drawProperties
The Shannon entropy of this distribution in nats, computed by summing over the support until convergence. Returns zero when probability is 1.0 (degenerate case).
the probability of success on each trial. Must be in (0, 1].