HypergeometricDistribution

class HypergeometricDistribution(val population: Int, val successes: Int, val draws: Int) : DiscreteDistribution(source)

Represents the hypergeometric distribution, which models the number of successes when drawing without replacement from a finite population.

Consider an urn containing population balls, of which successes are "success" balls (e.g., red) and the rest are "failure" balls (e.g., blue). You draw draws balls without putting any back. The hypergeometric distribution gives the probability of observing exactly k success balls among the drawn items.

Unlike the binomial distribution, the hypergeometric distribution accounts for the changing composition of the population as items are drawn. This makes it the correct model when sampling without replacement, such as quality control inspections from a finite lot, card games (e.g., the probability of being dealt a certain number of aces), or Fisher's exact test in statistics.

The support is {max(0, draws + successes - population), ..., min(draws, successes)}, which reflects the physical constraint that you cannot draw more success balls than exist or more than the total number of draws.

The CDF and survival function are computed using a numerically stable log-sum-exp technique over the PMF values. Sampling uses direct simulation of the drawing process.

Example:

// Urn with 50 balls: 15 red, 35 blue. Draw 10 without replacement.
val dist = HypergeometricDistribution(population = 50, successes = 15, draws = 10)
dist.pmf(3) // 0.2786 (probability of exactly 3 red balls)
dist.cdf(3) // 0.6749 (probability of 3 or fewer red balls)
dist.mean // 3.0 (expected number of red balls)
dist.quantileInt(0.5) // 3 (median)
dist.sample(Random(42)) // a single random draw

Constructors

Link copied to clipboard
constructor(population: Int, successes: Int, draws: Int)

Properties

Link copied to clipboard
val draws: Int

the number of items drawn without replacement. Must be in [0, population].

Link copied to clipboard
open override val entropy: Double

The Shannon entropy of this distribution in nats, computed by summing over the entire support.

Link copied to clipboard
open override val kurtosis: Double

The excess kurtosis of this distribution. Returns Double.NaN when population is less than 4 or the distribution is degenerate (single point support).

Link copied to clipboard
open override val mean: Double

The mean (expected number of success items drawn).

Link copied to clipboard

the total number of items in the population. Must be non-negative.

Link copied to clipboard
open override val skewness: Double

The skewness of this distribution. Returns Double.NaN when population is less than 3 or the distribution is degenerate (single point support).

Link copied to clipboard

the number of success items in the population. Must be in [0, population].

Link copied to clipboard
open override val variance: Double

The variance of the number of success items drawn.

Functions

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

Returns the cumulative distribution function value at k.

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 drawing exactly k success items.

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 hypergeometric distribution.

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

Returns the survival function value at k.