TExcelTAD
Home › Probability Theory › Bernoulli Trials and the Binomial Distribution

Bernoulli Trials and the Binomial Distribution

A Bernoulli trial is an experiment with two outcomes, success (probability pp) and failure (probability q=1−pq = 1 - p). 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 nn independent trials and let SS be the number of successes. A particular sequence with kk successes has probability pkqn−kp^k q^{n-k}, and there are (nk)\binom{n}{k} such sequences (choose which trials succeed), so

b(n,p,k)=Pr⁡[S=k]=(nk)pkqn−k,k=0,1,…,nb(n, p, k) = \Pr[S = k] = \binom{n}{k} p^k q^{n-k}, \qquad k = 0, 1, \ldots, n

By the binomial theorem these sum to (p+q)n=1(p + q)^n = 1. The mean is npnp and the variance npqnpq (shown in Variance).

python · try it yourself
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 np=3np = 3. The most likely value is ⌊(n+1)p⌋\lfloor (n+1)p \rfloor.

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?

Pr⁡[S≤1]=q50+50 p q49=0.9850+50⋅0.02⋅0.9849≈0.364+0.372=0.736\Pr[S \le 1] = q^{50} + 50\,p\,q^{49} = 0.98^{50} + 50 \cdot 0.02 \cdot 0.98^{49} \approx 0.364 + 0.372 = 0.736

For large nn and small pp, the binomial is close to a Poisson distribution with mean npnp; for large nn and moderate pp, it is close to a normal curve.

Simulation agrees

python · try it yourself
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, Pr⁡[S≥60]=∑k=60100b(100,0.5,k)≈0.028\Pr[S \ge 60] = \sum_{k=60}^{100} b(100, 0.5, k) \approx 0.028. 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.

python · try it yourself
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 1−0.99100≈63%1 - 0.99^{100} \approx 63\%").

Exercises

Exercise 1: Exactly three sixes

Roll a die 10 times. What is the probability of exactly three sixes?

Answer

About 0.1550.155.

Solution

(103)(1/6)3(5/6)7=120⋅0.00463⋅0.279≈0.155\binom{10}{3} (1/6)^3 (5/6)^7 = 120 \cdot 0.00463 \cdot 0.279 \approx 0.155.

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

np=20⋅0.25=5np = 20 \cdot 0.25 = 5; npq=20⋅0.25⋅0.75=3.75≈1.94\sqrt{npq} = \sqrt{20 \cdot 0.25 \cdot 0.75} = \sqrt{3.75} \approx 1.94.

Exercise 3: Provisioning (code)

200 users are each active with probability 0.1, independently. Find the smallest capacity cc with Pr⁡[S>c]<0.01\Pr[S > c] < 0.01.

Answer

c=30c = 30.

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 20+2.33⋅4.24≈3020 + 2.33 \cdot 4.24 \approx 30, and the exact computation confirms 30.

python · try it yourself
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 nn and pp?

  1. npnp
  2. np(1−p)np(1-p)
  3. n2pn^2 p
  4. np\sqrt{np}

Why is the factor (nk)\binom{n}{k} in the binomial formula?

  1. It normalises the distribution
  2. It counts the sequences with exactly kk successes
  3. It is the mean
  4. It corrects for dependence