Bernoulli Trials and the Binomial Distribution
A Bernoulli trial is an experiment with two outcomes, success (probability ) and failure (probability ). Coin tosses, a packet arriving intact, a user clicking an ad, a unit passing quality control: anything yes/no. Repeating independent Bernoulli trials leads to the most important discrete distribution.
The binomial distribution
Run independent trials and let be the number of successes. A particular sequence with successes has probability , and there are such sequences (choose which trials succeed), so
By the binomial theorem these sum to . The mean is and the variance (shown in Variance).
from math import comb
def b(n, p, k):
return comb(n, k) * p ** k * (1 - p) ** (n - k)
n, p = 10, 0.3
for k in range(n + 1):
pk = b(n, p, k)
print(f"k={k:2d} {pk:.4f} " + "#" * round(pk * 100))
print("sum:", round(sum(b(n, p, k) for k in range(n + 1)), 10))
print("mean:", round(sum(k * b(n, p, k) for k in range(n + 1)), 10), " np =", n * p)k= 0 0.0282 ###
k= 1 0.1211 ############
k= 2 0.2335 #######################
k= 3 0.2668 ###########################
k= 4 0.2001 ####################
k= 5 0.1029 ##########
k= 6 0.0368 ####
k= 7 0.0090 #
k= 8 0.0014
k= 9 0.0001
k=10 0.0000
sum: 1.0
mean: 3.0 np = 3.0
The distribution peaks near . The most likely value is .
Example: quality control
A machine produces parts that are defective with probability 2%, independently. In a box of 50, what is the probability of at most one defect?
For large and small , the binomial is close to a Poisson distribution with mean ; for large and moderate , it is close to a normal curve.
Simulation agrees
import numpy as np
from math import comb
rng = np.random.default_rng(1)
S = rng.binomial(50, 0.02, size=200_000)
print("simulated P(S <= 1):", round((S <= 1).mean(), 4))
print("exact:", round(sum(comb(50, k) * 0.02 ** k * 0.98 ** (50 - k) for k in range(2)), 4))
print("sample mean and variance:", round(S.mean(), 3), round(S.var(), 3), " np, npq:", 1.0, 0.98)simulated P(S <= 1): 0.7353
exact: 0.7358
sample mean and variance: 0.999 0.979 np, npq: 1.0 0.98
Tail probabilities and decisions
A coin lands heads 60 times in 100 tosses. Is it fair? Under fairness, . Results this extreme happen less than 3% of the time with a fair coin, which is evidence against fairness (a one-sided test at the 5% level). The Statistics course turns this reasoning into hypothesis tests.
from math import comb
from fractions import Fraction
tail = sum(Fraction(comb(100, k), 2 ** 100) for k in range(60, 101))
print("P(S >= 60) =", round(float(tail), 5))
for heads in [55, 60, 65, 70]:
t = sum(comb(100, k) for k in range(heads, 101)) / 2 ** 100
print(f"P(S >= {heads}) = {t:.6f}")P(S >= 60) = 0.02844
P(S >= 55) = 0.184101
P(S >= 60) = 0.028444
P(S >= 65) = 0.001759
P(S >= 70) = 0.000039
In software
Binomial models size systems: if each of 200 users is active with probability 0.1, how many concurrent sessions should you provision so that overflow happens less than 1% of the time? They also give A/B testing its basic arithmetic, model packet loss and retries, and quantify flaky tests ("a test that fails 1% of the time fails at least once in 100 CI runs with probability ").
Exercises
Exercise 1: Exactly three sixes
Roll a die 10 times. What is the probability of exactly three sixes?
Answer
About .
Solution
.
Exercise 2: A multiple-choice test
A test has 20 questions with 4 options each. Guessing at random, what are the mean and standard deviation of the number of correct answers?
Answer
Mean 5, standard deviation about 1.94.
Solution
; .
Exercise 3: Provisioning (code)
200 users are each active with probability 0.1, independently. Find the smallest capacity with .
Answer
.
Solution
Accumulate the binomial probabilities until the CDF reaches 0.99. The mean is 20 and the standard deviation about 4.24, so the answer should be roughly , and the exact computation confirms 30.
from math import comb
n, p = 200, 0.1
cdf, c = 0.0, -1
while 1 - cdf >= 0.01:
c += 1
cdf += comb(n, c) * p ** c * (1 - p) ** (n - c)
print(c, round(1 - cdf, 5))30 0.00951
Test yourself
What is the variance of a binomial variable with parameters and ?
Why is the factor in the binomial formula?
- It normalises the distribution
- It counts the sequences with exactly successes
- It is the mean
- It corrects for dependence