1. Home
  2. Statistics & special functions

Statistics · random variates

Poisson Random Numbers (poissrnd)

Eigenfield editorial team ·

A routine like poissrnd returns random counts from the Poisson distribution with mean λ. The model fits events that arrive independently at a steady average rate: calls in a minute, defects on a length of wire, particles in a detector. Mean and variance are both λ, which gives the first check on any generator, since a sample whose variance differs noticeably from its mean is not Poisson.

The probabilities follow P(k) = e^(−λ) λᵏ / k!. For λ = 2 they start at e^(−2) = 0.135335, then 0.270671, 0.270671, 0.180447, 0.090224 and 0.036089 for k = 0 to 5. The cumulative sums are 0.13534, 0.40601, 0.67668, 0.85712, 0.94735 and 0.98344, and the tail beyond k = 5 holds the remaining 1.7 percent.

The multiplication method

The classical algorithm for small λ multiplies uniform random numbers together until the product drops to e^(−λ) or below, and returns the number of factors used minus one:

L = exp(−λ);  k = 0;  p = 1
repeat:  k = k + 1;  p = p · u      # u is a fresh uniform number
until p ≤ L
return k − 1

To see it work, let λ = 2, so L = 0.135335, and suppose the uniform numbers arrive as 0.7, 0.5, 0.4 and 0.9. The running products are 0.7, then 0.35, then 0.14, which is still above L, then 0.126, which is below. The loop stops after four factors and the result is 4 − 1 = 3.

Why it works: the logarithm of a uniform number, negated, is an exponential variable with mean 1, so the loop counts how many unit-mean exponential waiting times fit inside the interval of length λ. That is the definition of a Poisson process, and it connects directly to the sum-of-exponentials trick on the gamma random numbers page.

Inversion with a table

When the same λ is used many times, a table is faster. Build the cumulative column above once, draw one uniform number u, and return the first k whose cumulative value is at least u. With λ = 2, a draw u = 0.70 falls between 0.67668 and 0.85712, so k = 3, while u = 0.30 gives k = 1.

The probabilities come from the recurrence P(k + 1) = P(k) · λ/(k + 1), which needs no factorials. The same device appears for the discrete case on the binomial random numbers page.

Large means

The multiplication method needs about λ + 1 uniform numbers per draw, so it slows down as λ grows. It also fails outright when e^(−λ) underflows, which in double precision happens near λ = 745. Two remedies are common.

The first is splitting: a Poisson variable with mean λ is the sum of m independent Poisson variables each with mean λ/m, so one large draw becomes m small ones. The second is a rejection method with a constant cost per draw, typically built on a normal approximation with corrections. The details are more delicate than for the gamma case, and a published, tested algorithm is the safe choice over a hand-made one.

Rounding a normal variate with mean λ and variance λ is tempting and wrong in the tails. It returns negative counts for small λ and misstates the probability of rare large values.

Checking output

Run a large batch and compare four things. The sample mean and the sample variance should both be near λ. The frequencies of 0, 1, 2 and so on should match the table, which a histogram with unit-width bins shows at a glance. The count of zeros alone is a sharp test, because its expected share e^(−λ) is exact. And for λ = 2, the share of draws above 5 should be near 1.7 percent.

The exact cumulative probabilities are also available from the incomplete gamma function: P(Poisson ≤ k) equals Q(k + 1, λ). Check it against the table: Q(3, 2) = 0.676676, the third cumulative entry. Other generators and tail functions are listed under statistics and special functions.

Back to top