Binomial Distribution
The binomial distribution counts successes in a fixed number of independent yes/no trials, each with the same success probability. How many heads in 10 coin tosses? How many of 50 emails are spam? How many of 20 penalty kicks are scored? All binomial.
It is a discrete distribution: the result is always a whole number from 0 to n.
| Parameter | Meaning |
|---|---|
n |
number of trials |
p |
probability of success in one trial |
size |
output shape |
Generating binomial data
import numpy as np
rng = np.random.default_rng(42)
# 10 coin tosses, repeated for 8 people: heads each person got
print(rng.binomial(n=10, p=0.5, size=8))
# A basketball player hits 70 % of free throws; 5 games of 12 attempts
print(rng.binomial(n=12, p=0.7, size=5))[6 5 7 6 3 8 6 6]
[10 9 9 6 8]
Mean and variance
For a binomial distribution the expected value is n·p and the variance is n·p·(1−p). Simulation agrees:
import numpy as np
rng = np.random.default_rng(42)
n, p = 20, 0.3
x = rng.binomial(n, p, size=100_000)
print(f"simulated mean {x.mean():.3f} theory {n * p}")
print(f"simulated var {x.var():.3f} theory {n * p * (1 - p):.1f}")
print("probability of 10 or more successes:", np.mean(x >= 10))simulated mean 6.005 theory 6.0
simulated var 4.207 theory 4.2
probability of 10 or more successes: 0.04851
The last line shows a powerful idea: to estimate a probability, simulate many times and count how often the event happens (Monte Carlo simulation).
Visualising the counts
Because the values are whole numbers, a bar chart with one bar per value shows the distribution best. np.bincount(x) counts how often each integer 0, 1, 2, … occurs (seaborn's equivalent is sns.histplot(x, discrete=True, stat="probability")):
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
plt.figure(figsize=(6, 3.5))
for p, color in [(0.2, "#e07a5f"), (0.5, "#4dabcf"), (0.8, "#81b29a")]:
x = rng.binomial(20, p, size=5000)
counts = np.bincount(x, minlength=21) # how often each value 0..20 occurs
plt.bar(np.arange(21), counts / x.size, color=color, alpha=0.5, label=f"p = {p}")
plt.xlabel("successes out of 20")
plt.legend()
plt.tight_layout()
plt.show()
print("Plotted binomial distributions with n=20 and three values of p")Plotted binomial distributions with n=20 and three values of p
With p = 0.5 the shape is symmetric around 10; smaller p shifts it left, larger p right.
Binomial vs normal
When n is large, the binomial distribution looks very much like a normal distribution with mean n·p and standard deviation √(n·p·(1−p)). The main difference: binomial is discrete, normal is continuous.
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(3)
n, p = 100, 0.5
binom = rng.binomial(n, p, size=10_000)
normal = rng.normal(n * p, np.sqrt(n * p * (1 - p)), size=10_000)
bins = np.arange(29.5, 71.5) # one bin per whole number
plt.figure(figsize=(6, 3.5))
plt.hist(normal, bins=bins, density=True, histtype="step", color="#4dabcf", label="normal")
plt.hist(binom, bins=bins, density=True, histtype="step", color="#e07a5f",
linestyle="--", label="binomial")
plt.legend()
plt.tight_layout()
plt.show()
print(f"binomial mean {binom.mean():.2f}, normal mean {normal.mean():.2f}")binomial mean 50.00, normal mean 50.01
The two outlines almost coincide, which is why statisticians often approximate a binomial with a normal distribution.
Note: A binomial experiment with a single trial (
n = 1) is called a Bernoulli trial: the result is just 0 or 1.rng.binomial(1, 0.3, size=10)simulates ten yes/no events with 30 % chance each.
Test yourself
What is the expected value of rng.binomial(n=40, p=0.25)?
- 0.25
- 10
- 25
- 40
Which values can rng.binomial(n=5, p=0.6) return?
- Any float from 0 to 5
- Integers 0 to 5
- Only 0 or 1
- Integers 1 to 5
When does a binomial distribution look like a normal distribution?
- When n is large
- When p = 0
- When n = 1
- Never