12 · Binomial: count of successes in n trials
Stack n Bernoulli(p) trials and ask 'how many successes?' — the Binomial. The math behind A/B tests and classifier confidence intervals.
Sum n independent Bernoulli(p) trials and you get a Binomial(n, p) — the count of successes.
Without this:
Without it, you can't reason about 'X of N users converted' or compute the probability that a batch of K predictions has J correct.
If you flip a biased coin n times and count the heads, the result follows a Binomial(n, p) distribution. It is the natural extension of the Bernoulli from one trial to n trials.
Setup: n independent trials, each with P(success) = p. X = number of successes.
Probability Mass Function: P(X = k) = C(n, k) · p^k · (1 − p)^(n−k)
where C(n, k) = n! / (k! · (n−k)!) is the number of ways to choose k successes from n trials.
Mean and variance:
- E[X] = np
- Var(X) = np(1 − p)
This makes sense: each of the n Bernoulli trials contributes mean p and variance p(1−p), and because the trials are independent the contributions add.
The Binomial is the sum of n iid Bernoullis. If X₁, …, Xn are iid Bernoulli(p), then X = X₁ + … + Xn ~ Binomial(n, p). This is the key structural fact.
Normal approximation (Central Limit Theorem preview): When np > 5 AND n(1−p) > 5, the Binomial is well-approximated by Normal(np, np(1−p)). As n grows, the sum of independent RVs (Bernoullis here) converges to a Normal — this is the Central Limit Theorem in action, which we will study formally in Chapter 6.
Poisson approximation: When n is large and p is small, Binomial(n, p) ≈ Poisson(λ = np). The next lesson covers Poisson in depth.
Python (in browser)
Binomial(20, 0.3) has mean 6, variance 4.2. P(X=6) ≈ 0.179 is the mode (most likely single value). CDF(6) ≈ 0.608 means 'in about 61% of 20-trial experiments, we see 6 or fewer successes.' The ASCII bar chart shows the distribution shape.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
Three Binomial(20, p) PMFs on one figure. At p=0.5 the distribution is perfectly symmetric (looks like a bell curve). At p=0.1 it is right-skewed; at p=0.9, left-skewed — these are mirror images of each other. Open /tmp/binom_shapes.png to view.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
10 000 simulated batches of 20 coin flips. The gold histogram (relative frequencies) overlaps almost perfectly with the blue theoretical PMF bars. Empirical mean ≈ 10 and variance ≈ 5. Open /tmp/binom_sim.png to view.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
When n is large and p is small (rare events), Binomial(n, p) ≈ Poisson(λ = np). The Poisson formula is simpler (no combinatorial term) and numerically stable even for large n. The next lesson explores Poisson in full.
If X ~ Binomial(100, 0.5), what is E[X]?
- **PMF**: P(X=k) = C(n,k)·p^k·(1−p)^(n−k). **Mean** = np, **Var** = np(1−p). The Binomial is the sum of n iid Bernoullis.
- When np > 5 and n(1−p) > 5, Binomial ≈ Normal(np, np(1−p)) — a sneak peek of the Central Limit Theorem.
- A/B test significance, classifier accuracy confidence intervals, and conversion rate analysis are all Binomial calculations.
A/B test conversion analysis uses the Binomial; batch-level accuracy is a Binomial-driven uncertainty problem; confidence intervals on classifier accuracy are Wilson/Clopper-Pearson Binomial intervals.
If you remove it: You can't give a confidence interval on 'this classifier got 92% accuracy on a 500-example test set' — that interval is a Binomial calculation.