Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 57 ยท Common Distributions

The Binomial Distribution

Learn the binomial distribution: Bernoulli trials, the probability mass function, mean and variance, cumulative probabilities and when it applies, with runnable Python.

Flip a coin ten times and count the heads. Send a mailing to 500 customers and count the replies. Test 40 chips and count the defective ones. In every case you repeat the same yes/no experiment a fixed number of times and count the "yes" outcomes. The count follows one of the most useful models in statistics: the binomial distribution.

This lesson builds the distribution from its smallest piece, the Bernoulli trial, derives the probability mass function, and shows how to read means, variances and tail probabilities from it.

Bernoulli trials

A Bernoulli trial is an experiment with exactly two outcomes, usually called success and failure. The chance of success is p, the chance of failure is 1 - p. Coding success as 1 and failure as 0 gives a random variable with mean p and variance p(1 - p).

A binomial experiment is a sequence of n Bernoulli trials that satisfy four conditions:

  1. The number of trials n is fixed in advance.
  2. Each trial has two outcomes (success or failure).
  3. The probability of success p is the same on every trial.
  4. The trials are independent: one result does not change the next.

If X counts the successes, we write X ~ Binomial(n, p).

The probability mass function

Suppose n = 5 and you want exactly k = 2 successes. One particular arrangement, say SSFFF, has probability p * p * (1-p) * (1-p) * (1-p) = p^2 (1-p)^3, because trials are independent. But there are many arrangements with two successes: you choose which 2 of the 5 trials succeed, and there are C(5, 2) = 10 ways. Each has the same probability, so:

TEXT
P(X = k) = C(n, k) * p^k * (1 - p)^(n - k),   k = 0, 1, ..., n

where C(n, k) = n! / (k! (n - k)!) is the binomial coefficient.

Python Binomial PMF by hand and with SciPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two columns agree, and the probabilities add up to 1, as every PMF must.

Mean and variance

Because X is a sum of n independent Bernoulli variables, expectations and variances simply add:

TEXT
E[X]   = n * p
Var(X) = n * p * (1 - p)
SD(X)  = sqrt(n * p * (1 - p))

For 10 trials with p = 0.3 you expect 3 successes, with variance 2.1. The variance is largest when p = 0.5 and shrinks toward 0 as p approaches 0 or 1, because outcomes become more predictable.

Cumulative probabilities

Questions are often phrased with "at most" or "at least". The cumulative distribution function (CDF) gives P(X <= k). For "at least" use the complement:

TEXT
P(X >= k) = 1 - P(X <= k - 1)

In SciPy, binom.cdf(k, n, p) gives P(X <= k) and binom.sf(k, n, p) ("survival function") gives P(X > k). sf is more accurate than 1 - cdf for tiny tail probabilities.

Python A mailing campaign

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Simulating a binomial

You can check a formula by simulating the experiment many times. With NumPy, rng.binomial(n, p, size) draws repeated counts. The relative frequencies approach the PMF as the number of repetitions grows.

Python Simulation versus theory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Shape of the distribution

The shape depends on p. With p = 0.5 the distribution is symmetric. With p < 0.5 it is skewed right (a long tail toward larger counts), and with p > 0.5 it is skewed left. As n grows, the distribution looks more and more like a bell curve, which is the idea behind the normal distribution approximation.

flowchart LR
    A["Fixed n trials"] --> B["Two outcomes each"]
    B --> C["Constant p"]
    C --> D["Independent trials"]
    D --> E["X ~ Binomial(n, p)"]

The four conditions that make a count binomial.

When it does not apply

Check the conditions before reaching for the formula. Drawing cards without replacement from a small deck breaks independence (use the hypergeometric distribution instead). Counting the trials until the first success has no fixed n (that is the geometric distribution). And if p changes, for example because people who already replied are not asked again, the binomial is only an approximation.

Sampling without replacement from a large population is still very close to binomial. A common rule of thumb: the sample should be less than 10% of the population.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A fair die is rolled 12 times. What is the expected number of sixes?

    E[X] = n * p = 12 * (1/6) = 2.
  2. 2. Which situation is NOT binomial?

    The number of flips until the first success is not fixed in advance, so it is geometric, not binomial.
  3. 3. For X ~ Binomial(20, 0.1), what is Var(X)?

    Var = n p (1 - p) = 20 * 0.1 * 0.9 = 1.8.
  4. 4. How do you compute P(X >= 5) from the CDF?

    P(X >= 5) = 1 - P(X <= 4).

Summary

  • A Bernoulli trial has two outcomes; a binomial counts successes in n independent trials with constant p.
  • P(X = k) = C(n, k) p^k (1 - p)^(n - k).
  • Mean n p, variance n p (1 - p).
  • Use the CDF for "at most" and 1 - cdf(k - 1) for "at least".
  • Always check the four conditions, especially independence and fixed n.

Next: The Poisson Distribution