BinomialDistribution

class BinomialDistribution(val trials: Int, val probability: Double) : DiscreteDistribution(source)

Represents the binomial distribution, defined by the number of trials and the probability of success on each trial.

The binomial distribution models the number of successes in a fixed number of independent trials, where each trial has the same probability of success. The classic example is counting the number of heads in n coin flips: if you flip a fair coin 10 times, the number of heads follows a binomial distribution with trials = 10 and probability = 0.5. More generally, it applies to any repeated yes/no experiment such as defective items in a batch, patients responding to a treatment, or successful network requests. The support is the set of integers from 0 to trials.

The CDF and survival function use the regularized incomplete beta function for numerical stability. Sampling uses direct simulation for small trial counts and a normal approximation for large trial counts.

Example:

val dist = BinomialDistribution(trials = 10, probability = 0.3)
dist.pmf(3) // 0.2668... (probability of exactly 3 successes)
dist.cdf(3) // 0.6496... (probability of 3 or fewer successes)
dist.quantileInt(0.5) // 3 (median number of successes)
dist.mean // 3.0
dist.variance // 2.1

Constructors

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

Properties

Link copied to clipboard
open override val entropy: Double

Returns the Shannon entropy of this binomial distribution in nats.

Link copied to clipboard
open override val kurtosis: Double

Returns the excess kurtosis (Fisher definition) of this binomial distribution.

Link copied to clipboard
open override val mean: Double

The mean of this distribution, equal to trials * probability.

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

Returns the skewness of this binomial distribution.

Link copied to clipboard
val trials: Int

the number of independent trials. Must be non-negative.

Link copied to clipboard
open override val variance: Double

The variance of this distribution, equal to trials * probability * (1 - probability).

Functions

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

Returns the cumulative distribution function value at k for this binomial distribution.

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

Returns the natural logarithm of the probability mass at k for this binomial distribution.

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

Returns the probability mass at k for this binomial distribution.

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

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

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

Draws a single random integer from this binomial distribution.

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

Returns the survival function value at k for this binomial distribution.