Distributions: Bernoulli and binomial
About this lesson
Some patterns of chance come up so often they have names. Start from one yes/no try (Bernoulli), repeat it four times, and count the ways to get k successes. Watch the bars 1, 4, 6, 4, 1 build the binomial distribution, add up into its CDF, and reshape when p changes from 0.5 to 0.3. Then draw 10,000 samples in Python and see them land close to the formula.
One try: Bernoulli
Some distributions come up so often they have names. The simplest is one yes/no try that succeeds with chance p. X is 1 on success, 0 otherwise: a Bernoulli(p) variable. A fair coin flip, heads = 1, is Bernoulli(1/2).
>>> import random >>> random.seed(1) >>> [int(random.random() < 0.5) for _ in range(10)] # ten Bernoulli(1/2) draws [1, 0, 0, 1, 1, 1, 0, 0, 1, 1]
Read the lesson step by step
One try: Bernoulli
Some distributions come up so often they have names. The simplest is one yes/no try that succeeds with chance p. X is 1 on success, 0 otherwise: a Bernoulli(p) variable. A fair coin flip, heads = 1, is Bernoulli(1/2).
>>> import random >>> random.seed(1) >>> [int(random.random() < 0.5) for _ in range(10)] # ten Bernoulli(1/2) draws [1, 0, 0, 1, 1, 1, 0, 0, 1, 1]Four tries, 16 sequences
Now repeat the try n = 4 times, each independent of the others. Every try has 2 results, so there are 2 × 2 × 2 × 2 = 16 sequences, from HHHH to TTTT. With p = 1/2 each sequence has chance 1/16.
>>> from itertools import product >>> S = list(product('HT', repeat=4)) >>> len(S), S[0], S[-1] (16, ('H', 'H', 'H', 'H'), ('T', 'T', 'T', 'T'))Count the ways
Let X count the heads. How many sequences have exactly k heads? Choose which k of the 4 tries they are: C(4, k) ways, "4 choose k". That gives 1, 4, 6, 4, 1 sequences for k = 0 to 4: 16 in all.
>>> from math import comb >>> [comb(4, k) for k in range(5)] [1, 4, 6, 4, 1] >>> sum(comb(4, k) for k in range(5)) 16The binomial formula
Each sequence with k heads has chance pᵏ(1 − p)ⁿ⁻ᵏ. Multiply by the number of ways: P(X = k) = C(n, k) pᵏ(1 − p)ⁿ⁻ᵏ. That is the Binomial(n, p) distribution. Here P(X = 2) = 6 × 1/16 = 0.375.
>>> from math import comb >>> n, p = 4, 0.5 >>> [comb(n, k) * p**k * (1-p)**(n-k) for k in range(n + 1)] [0.0625, 0.25, 0.375, 0.25, 0.0625]Cumulative: P(X ≤ k)
The cumulative distribution function F(k) = P(X ≤ k) adds the bars from the left: 0.0625, 0.3125, 0.6875, 0.9375 and 1. It never goes down, and it ends at 1 because some value always happens.
>>> from itertools import accumulate >>> pmf = [0.0625, 0.25, 0.375, 0.25, 0.0625] >>> list(accumulate(pmf)) [0.0625, 0.3125, 0.6875, 0.9375, 1.0]Mean and variance
X is a sum of 4 Bernoulli tries. Each has mean p and variance p(1 − p), and independent variances add. So E[X] = np = 2 and Var(X) = np(1 − p) = 1. The bars balance at 2.
>>> pmf = [0.0625, 0.25, 0.375, 0.25, 0.0625] >>> E = sum(k * q for k, q in enumerate(pmf)) >>> V = sum((k - E)**2 * q for k, q in enumerate(pmf)) >>> E, V (2.0, 1.0)Change p, change the shape
n and p are the parameters: one formula, many shapes. Set p = 0.3 and the bars become 0.2401, 0.4116, 0.2646, 0.0756 and 0.0081. The peak moves to 1, and the mean drops to np = 1.2.
>>> from math import comb >>> n, p = 4, 0.3 >>> [round(comb(n, k) * p**k * (1-p)**(n-k), 4) for k in range(n + 1)] [0.2401, 0.4116, 0.2646, 0.0756, 0.0081] >>> n * p, round(n * p * (1-p), 2) (1.2, 0.84)Samples follow the shape
Python can draw from it: random.binomialvariate(4, 0.3), new in Python 3.12. With random.seed(1), 10,000 draws land 4,069 times on 1, a share of 0.4069. The formula said 0.4116: close, as expected.
>>> import random >>> from collections import Counter >>> random.seed(1) >>> c = Counter(random.binomialvariate(4, 0.3) for _ in range(10_000)) >>> [c[k] for k in range(5)] [2445, 4069, 2660, 758, 68] >>> [c[k] / 10_000 for k in range(5)] [0.2445, 0.4069, 0.266, 0.0758, 0.0068]The formula in code
In code, math.comb(n, k) counts the ways, and the formula does the rest. For n = 4, p = 0.3 and k = 1 there are 4 ways, so P(X = 1) = 4 × 0.3 × 0.7³ = 0.4116.
>>> from math import comb >>> n, p, k = 4, 0.3, 1 >>> ways = comb(n, k) >>> ways 4 >>> round(ways * p**k * (1-p)**(n-k), 4) 0.4116
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A test has 5 yes/no questions and you guess each with chance 1/2. How many answer patterns get exactly 2 right?
Choose which 2 of the 5 questions are right: C(5, 2) = 5 × 4 / 2 = 10. There are 2⁵ = 32 patterns in all, so P(X = 2) = 10/32.
02X ~ Binomial(10, 0.2). What are E[X] and Var(X)?
E[X] = np = 10 × 0.2 = 2 and Var(X) = np(1 − p) = 10 × 0.2 × 0.8 = 1.6.
03For X ~ Binomial(4, 1/2), the PMF is 0.0625, 0.25, 0.375, 0.25, 0.0625. What is P(X ≤ 2)?
The CDF adds the bars up to 2: 0.0625 + 0.25 + 0.375 = 0.6875.
04Which situation is NOT well modelled by a binomial distribution?
A binomial counts successes in a fixed number n of independent tries with the same p. Counting tries until the first success has no fixed n: that is a geometric distribution.
05With random.seed(1), 10,000 draws of random.binomialvariate(4, 0.3) gave the value 1 a share of 0.4069, but the formula says 0.4116. What does this show?
Random samples wobble around the true probability. A gap of about 0.005 after 10,000 draws is normal, and it tends to shrink as the number of draws grows.
Glossary
- Distribution
- The list of every value a random variable can take, with the probability of each. It describes the whole pattern of chance, not one result.
- PMF (probability mass function)
- For a variable with whole-number values, the function k → P(X = k): the height of each bar.
- CDF (cumulative distribution function)
- F(k) = P(X ≤ k), the running total of the PMF from the left. It never decreases and ends at 1.
- Bernoulli(p)
- One yes/no try: X = 1 with probability p and X = 0 with probability 1 − p.
- Binomial(n, p)
- The number of successes in n independent Bernoulli(p) tries. P(X = k) = C(n, k) pᵏ(1 − p)ⁿ⁻ᵏ.
- C(n, k), 'n choose k'
- The number of ways to choose k items from n, n! / (k! (n − k)!). In Python: math.comb(n, k).
- Parameter
- A number that picks one member of a family of distributions, such as n and p for the binomial.
- Independent
- Tries are independent when the result of one does not change the chances of another.
Notes and sources
- X ~ Binomial(n, p) is read "X follows a binomial distribution with n tries and success chance p". C(n, k) = n! / (k! (n − k)!) is the number of ways to choose k items from n.
- The binomial needs independent tries with the same p each time. Variances add when the variables are independent (more generally, whenever they are uncorrelated); means always add.
- For a variable with whole-number values, the list of P(X = k) is called its probability mass function (PMF). The CDF holds the same information as running totals.
- For large n the binomial bars hug a bell curve, the normal distribution with mean np and standard deviation √(np(1 − p)). This is the de Moivre–Laplace theorem, a special case of the central limit theorem.
- random.binomialvariate was added in Python 3.12. On older versions, sum(random.random() < p for _ in range(n)) draws the same distribution.
- Blitzstein & Hwang, Introduction to Probability, 2nd ed., ch. 3 (Bernoulli, binomial, CDF) and ch. 4 (expectation, variance)
- MIT 18.05 Introduction to Probability and Statistics (Orloff & Bloom), class notes on discrete random variables
- MIT 6.042J Mathematics for Computer Science (Lehman, Leighton & Meyer), chapters 19 and 20
- NIST/SEMATECH e-Handbook of Statistical Methods: Binomial distribution
- Python library: math.comb
- Python library: random (seed, binomialvariate)
Quiz Unlocks after the last step.