NegativeBinomialDistribution

class NegativeBinomialDistribution(val successes: Int, val probability: Double) : DiscreteDistribution(source)

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 draw

Constructors

Link copied to clipboard
constructor(successes: Int, probability: Double)

Properties

Link copied to clipboard
open override val entropy: Double

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).

Link copied to clipboard
open override val kurtosis: Double

The excess kurtosis of this distribution.

Link copied to clipboard
open override val mean: Double

The mean (expected number of failures before achieving successes successes).

Link copied to clipboard

the probability of success on each trial. Must be in (0, 1].

Link copied to clipboard
open override val skewness: Double

The skewness of this distribution.

Link copied to clipboard

the required number of successes (the "stopping count"). Must be positive.

Link copied to clipboard
open override val variance: Double

The variance of the number of failures before achieving successes successes.

Functions

Link copied to clipboard
open override fun cdf(k: Int): Double

Returns the cumulative distribution function value at k using the regularized incomplete beta function.

Link copied to clipboard
open override fun logPmf(k: Int): Double

Returns the natural logarithm of the probability mass at k.

Link copied to clipboard
open override fun pmf(k: Int): Double

Returns the probability mass at k, the probability of exactly k failures before the successes-th success.

Link copied to clipboard
open override fun quantileInt(p: Double): Int

Returns the quantile (inverse CDF) for the given probability p as an Int.

Link copied to clipboard
open override fun sample(random: Random): Int

Draws a single random value from this negative binomial distribution.

Link copied to clipboard
open override fun sf(k: Int): Double

Returns the survival function value at k using the regularized incomplete beta function.