Lesson 13 of 16 · Distributions
Bernoulli and Binomial
The Bernoulli and binomial distributions: success-or-failure trials, the binomial formula, mean and variance, an interactive chart and an A/B test in Python.
- 7 min read
- Intermediate
- Reviewed
- Describe a Bernoulli trial and its mean and variance
- Recognise situations that follow a binomial distribution
- Calculate binomial probabilities with math.comb
- Use the binomial mean and standard deviation to judge whether a result is surprising
One trial: Bernoulli
Many random events have just two outcomes: a visitor clicks or doesn't, an email is spam or isn't, a part is faulty or fine. Call one outcome “success” (1) and the other “failure” (0).
Where do those come from? E[X] = 1 × p + 0 × (1 − p) = p. And since X² = X when X is 0 or 1, Var(X) = E[X²] − (E[X])² = p − p² = p(1 − p). The variance is largest at p = 0.5, when the outcome is hardest to predict.
Many trials: binomial
Now repeat a Bernoulli trial n times and count the successes.
Each piece of the formula has a job:
- pᵏ × (1 − p)ⁿ⁻ᵏ is the probability of one particular sequence with k successes and n − k failures, such as SSFFF…, by the multiplication rule for independent events.
- C(n, k), “n choose k”, counts how many different sequences have exactly k successes. Python's
math.comb(n, k)calculates it.
Here's the formula for 10 coin flips, checked against 20,000 simulated experiments:
import random
from math import comb
def binomial_pmf(k, n, p):
"""P(exactly k successes in n independent trials with success probability p)."""
return comb(n, k) * p ** k * (1 - p) ** (n - k)
n, p = 10, 0.5
random.seed(13)
experiments = 20_000
# One experiment: flip 10 coins and count the heads
heads = [sum(random.random() < p for _ in range(n)) for _ in range(experiments)]
print(" k formula simulated")
for k in range(n + 1):
print(f"{k:2} {binomial_pmf(k, n, p):.4f} {heads.count(k) / experiments:.4f}")
print(f"mean: {n * p} (formula) {sum(heads) / experiments:.3f} (simulated)")Because the binomial is a sum of n independent Bernoulli trials, linearity and the variance rules from the last two lessons give its mean and variance straight away:
mean = n × p variance = n × p × (1 − p)Explore the shape. Change n and p, and use k to highlight the probability of at least k successes:
Notice that with p = 0.5 the bars are symmetric, with small p they lean left, and with large n the shape becomes a smooth hump. That hump is the normal distribution, the next lesson.
An A/B test
A website's “Sign up” button is clicked by 10% of visitors. A redesigned button is shown to 200 visitors and gets 30 clicks. Is the new design better, or could that be luck?
from math import comb, sqrt
def binomial_pmf(k, n, p):
return comb(n, k) * p ** k * (1 - p) ** (n - k)
n, p = 200, 0.10 # 200 visitors, each clicking with probability 0.10
print(f"expected clicks: {n * p:.0f}, standard deviation: {sqrt(n * p * (1 - p)):.2f}")
# The new design got 30 clicks. How surprising is that, if the click rate is still 10%?
p_30_or_more = sum(binomial_pmf(k, n, p) for k in range(30, n + 1))
print(f"P(30 or more clicks) = {p_30_or_more:.4f}")If the click rate were still 10%, we'd expect about 20 ± 4.24 clicks, and 30 or more would happen only about 1.6% of the time. So 30 clicks is good evidence that the new design really is better. This way of thinking, “how surprising would this result be if nothing had changed?”, is the heart of hypothesis testing in the Statistics course.
Common mistakes
A 10-question multiple choice quiz has 4 options per question. What's the chance of passing (5 or more right) by guessing every answer?
from math import comb n = 10 # questions in the quiz p = 0.25 # chance of guessing one right: 1 of 4 options pass_mark = 5 # you need at least 5 right to pass # Guessing every answer, what is P(at least 5 right)? # Add up the binomial probabilities for k = 5 to 10 and print the total rounded to 4 decimals.
A factory makes parts that are faulty with probability 0.02, independently. In a batch of 500, what are the mean and variance of the number of faulty parts?
Summary
- A Bernoulli trial gives 1 with probability p and 0 otherwise: mean p, variance p(1 − p).
- The binomial distribution counts successes in n independent trials with the same p.
- P(X = k) = C(n, k) × pᵏ × (1 − p)ⁿ⁻ᵏ, with
math.combfor C(n, k). - Its mean is np and its variance is np(1 − p).
- Comparing a result with the mean and standard deviation shows whether it's surprising.
Next: the normal distribution.
Finished reading? Tick it off to track your progress.