Velamuvi AIAcademy
Probability

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
In this lesson you will learn
  • 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:

00.050.10.150.2P(X = 0) = 0.01830P(X = 1) = 0.07331P(X = 2) = 0.14652P(X = 3) = 0.19543P(X = 4) = 0.19544P(X = 5) = 0.15635P(X = 6) = 0.10426P(X = 7) = 0.05957P(X = 8) = 0.02988P(X = 9) = 0.01329P(X = 10) = 0.005310P(X = 11) = 0.001911P(X = 12) = 0.000612P(X = 13) = 0.000213P(X = 14) = 0.000114
P(X ≥ 8) = 0.0511mean = 4standard deviation = 2
The Poisson distribution. The highlighted bars show P(X ≥ 8): rare when λ = 4, common when λ = 10.
Try it yourselfPython
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:

00.050.10.150.2P(X = 1) = 0.151P(X = 2) = 0.1275P(X = 3) = 0.1084P(X = 4) = 0.09214P(X = 5) = 0.0783P(X = 6) = 0.0666P(X = 7) = 0.05667P(X = 8) = 0.0481P(X = 9) = 0.0409P(X = 10) = 0.034710P(X = 11) = 0.0295P(X = 12) = 0.0251P(X = 13) = 0.021313P(X = 14) = 0.0181P(X = 15) = 0.0154P(X = 16) = 0.013116P(X = 17) = 0.0111P(X = 18) = 0.0095P(X = 19) = 0.00819P(X = 20) = 0.0068P(X = 21) = 0.0058P(X = 22) = 0.004922P(X = 23) = 0.0042P(X = 24) = 0.0036P(X = 25) = 0.00325P(X = 26) = 0.0026P(X = 27) = 0.0022P(X = 28) = 0.001928P(X = 29) = 0.0016P(X = 30) = 0.0013P(X = 31) = 0.001131P(X = 32) = 0.001P(X = 33) = 0.0008P(X = 34) = 0.000734P(X = 35) = 0.0006P(X = 36) = 0.0005P(X = 37) = 0.000437P(X = 38) = 0.0004P(X = 39) = 0.0003P(X = 40) = 0.000340P(X = 41) = 0.0002P(X = 42) = 0.0002P(X = 43) = 0.000243
P(X ≤ 6) = 0.6229mean = 6.667standard deviation = 6.146
The geometric distribution: the number of tries until the first success. The highlighted bars show P(success within 6 tries).

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:

00.511.5202468
exponential density
area = 0.865
The shaded area is P(wait ≤ t) = 1 − e^(−λt). With λ = 4, the chance of a ticket within half an hour is about 0.865.

Simulation agrees with both waiting-time distributions:

Try it yourselfPython
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

SituationDistributionExample
One yes/no trialBernoulli(p)Does this visitor click?
Successes in n independent triesBinomial(n, p)Clicks from 200 visitors
Tries until the first successGeometric(p)Calls until a sale
Events in an interval, at rate λPoisson(λ)Emails per hour
Waiting time between random eventsExponential(λ)Minutes until the next email
Equally likely values in a rangeUniformA random time in the next 15 minutes
Sums and averages of many effectsNormal(μ, σ²)Heights, measurement errors
One of several labelsCategoricalA classifier's output

Common mistakes

Practice exercise

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.

Your solutionPython
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.
Knowledge check

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.