BinomialDistribution
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.1Functions
Returns the quantile (inverse CDF) for the given probability p as an integer.