Lesson 15 of 16 · Distributions
Other Useful Distributions
The Poisson distribution for counts, the geometric for waiting for a first success, the exponential for waiting times, and how to choose a distribution.
- 8 min read
- Intermediate
- Reviewed
- Use the Poisson distribution for counts of events in an interval
- Use the geometric distribution for the number of tries until a first success
- Use the exponential distribution for continuous waiting times
- Explain the memoryless property
- Choose a sensible distribution for a new situation
Counting random events: Poisson
How many emails arrive in the next hour? How many errors will a server log today? How many typos are on a page? These count events that happen at random, at some average rate.
Change λ and watch the shape: for small rates it's piled up near 0, and for larger rates it becomes a hump, heading towards a bell curve:
from math import comb, exp, factorial
def poisson_pmf(k, lam):
"""P(exactly k events) when events happen at an average rate of lam per interval."""
return exp(-lam) * lam ** k / factorial(k)
lam = 4 # a help desk gets 4 tickets an hour on average
print(f"P(no tickets in an hour) = {poisson_pmf(0, lam):.4f}")
print(f"P(exactly 4 tickets) = {poisson_pmf(4, lam):.4f}")
print(f"P(8 or more tickets) = {1 - sum(poisson_pmf(k, lam) for k in range(8)):.4f}")
# The Poisson is the binomial with many trials and a tiny p: 1,000 customers, each with a 0.004 chance
n, p = 1000, 0.004
print(f"binomial P(exactly 4) = {comb(n, 4) * p ** 4 * (1 - p) ** (n - 4):.4f}")A help desk that averages 4 tickets an hour gets 8 or more in about 5% of hours, which is useful to know when planning staff. The last line shows where the Poisson comes from: it's the binomial with a very large number of trials, each with a tiny chance. A thousand customers who each contact the help desk with probability 0.004 give almost exactly the same answer.
Waiting for a first success: geometric
The formula says: fail k − 1 times, then succeed once. Waiting for a 6 on a die takes 6 rolls on average, because p = 1/6:
Waiting times: exponential
The geometric counts whole tries. For waiting time, which is continuous, the matching distribution is the exponential.
If help desk tickets arrive as a Poisson process at 4 an hour, the gaps between them are exponential with rate 4, and the average gap is 1/4 hour, or 15 minutes. Drag the slider to see the chance that the next ticket arrives within t hours:
Simulation agrees with both waiting-time distributions:
import random
from math import exp
from statistics import fmean
random.seed(15)
# Geometric: roll a die until the first 6. How many rolls does it take?
def rolls_until_six():
rolls = 1
while random.randint(1, 6) != 6:
rolls += 1
return rolls
counts = [rolls_until_six() for _ in range(20_000)]
print(f"average rolls until a 6: {fmean(counts):.2f} (theory: 1/p = 6)")
print(f"longest wait: {max(counts)} rolls")
# Exponential: tickets arrive at 4 an hour, so the gaps between them are exponential with rate 4
gaps = [random.expovariate(4) for _ in range(20_000)]
print(f"average gap: {fmean(gaps) * 60:.1f} minutes (theory: 1/rate = 15 minutes)")
print(f"share of gaps over 30 minutes: {sum(g > 0.5 for g in gaps) / len(gaps):.3f} (theory: {exp(-4 * 0.5):.3f})")Choosing a distribution
| Situation | Distribution | Example |
|---|---|---|
| One yes/no trial | Bernoulli(p) | Does this visitor click? |
| Successes in n independent tries | Binomial(n, p) | Clicks from 200 visitors |
| Tries until the first success | Geometric(p) | Calls until a sale |
| Events in an interval, at rate λ | Poisson(λ) | Emails per hour |
| Waiting time between random events | Exponential(λ) | Minutes until the next email |
| Equally likely values in a range | Uniform | A random time in the next 15 minutes |
| Sums and averages of many effects | Normal(μ, σ²) | Heights, measurement errors |
| One of several labels | Categorical | A classifier's output |
Common mistakes
A website logs 3 errors a day on average. Use the Poisson distribution to find the chance of a day with no errors, and of a day with at most 2.
from math import exp, factorial lam = 3 # a website logs 3 errors a day on average, at random times # Using the Poisson PMF, P(X = k) = exp(-lam) * lam ** k / factorial(k), find: # 1. P(no errors tomorrow) # 2. P(at most 2 errors tomorrow) # Print both on one line, rounded to 4 decimals.
A salesperson closes a sale on each call with probability 0.2, independently. On average, how many calls does it take to make the first sale?
Summary
- Poisson(λ): counts of independent events in an interval; mean = variance = λ.
- Geometric(p): tries until the first success; mean 1/p.
- Exponential(λ): continuous waiting time between random events; mean 1/λ and P(wait > t) = e^(−λt).
- Geometric and exponential waits are memoryless.
- Pick a distribution by what you're counting or measuring, then check it against data.
Next: sampling in Python.
Finished reading? Tick it off to track your progress.