The Binomial Distribution

Intuition

Flip a biased coin nn times. The number of heads is random; the binomial distribution says how likely each count is. Every count kk can happen in (nk)\binom{n}{k} orders, each with the same probability.

Precise explanation

For nn independent trials with success probability pp:

P(X=k)=(nk)pk(1−p)n−k,k=0,…,n.P(X=k) = \binom{n}{k} p^k (1-p)^{n-k},\qquad k = 0,\dots,n.

Cumulative: P(X≤k)=∑j=0kP(X=j)P(X \le k) = \sum_{j=0}^{k} P(X=j). Tail: P(X≥k)=1−P(X≤k−1)P(X \ge k) = 1 - P(X \le k-1). Mean npnp, variance np(1−p)np(1-p). Values of kk outside 0..n0..n have probability 0.

math.comb(n, k) gives the binomial coefficient (you implemented it in MA-07). In Python 0 ** 0 == 1, which correctly handles p=0p = 0 or p=1p = 1 at the boundaries.

Worked example

n=4n = 4, p=0.5p = 0.5: P(X=2)=6⋅0.54=0.375P(X=2) = 6 \cdot 0.5^4 = 0.375. P(X≥3)=P(3)+P(4)=4/16+1/16=0.3125P(X \ge 3) = P(3) + P(4) = 4/16 + 1/16 = 0.3125.

Common mistakes

  • Forgetting the (nk)\binom{n}{k} factor (computing one specific sequence).
  • Computing P(X≥k)P(X\ge k) as 1−P(X≤k)1 - P(X \le k) (off by one).
  • Not validating 0≤p≤10 \le p \le 1.

Why this matters

Accuracy on a test set of nn examples is a binomial proportion; A/B tests, dropout masks (DL-21) and client participation (FL-08) are Bernoulli processes.

Library implementation

scipy.stats.binom(n, p) provides .pmf(k), .cdf(k) and .sf(k - 1) (the survival function, P(X≥k)P(X \ge k)), computed in a numerically careful way for large n. The comparison runs only when SciPy is installed.

References