ENع
←Distributions
Lesson 16 · Probability and statistics

Distributions: Bernoulli and binomial

14 min9 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 16 · Interactive
01 / 09

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]
Loading figure…
or ← →
Read the lesson step by step
  1. 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]
  2. 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'))
  3. 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))
    16
  4. The 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]
  5. 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]
  6. 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)
  7. 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)
  8. 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]
  9. 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

Quiz Unlocks after the last step.