Statistics · random variates
Gamma Random Numbers (gamrnd)
A gamma random number generator, usually named gamrnd, draws values from the gamma distribution with shape a and scale θ. The mean of a draw is aθ and the variance is aθ², so shape 3 with scale 2 centers on 6 with variance 12. The distribution covers waiting times for several events in a row, skewed positive measurements, and, by a short detour, chi-square variables.
Two things decide the method: whether the shape is an integer, and whether it is below 1. The sections below cover each case, then the checks that expose a faulty implementation.
Integer shape: add exponentials
When a is a whole number, a gamma variable with scale θ is the sum of a independent exponential variables with mean θ. One exponential comes from one uniform number u as −θ ln u, so the sum collapses to a single logarithm of a product:
x = −θ · ln(u₁ · u₂ · … · u_a)
Take a = 3, θ = 2 and the uniforms 0.5, 0.25 and 0.8. Their product is 0.5 × 0.25 × 0.8 = 0.1, the logarithm is −2.302585, and the draw is 2 × 2.302585 = 4.60517. The cost is a uniform numbers per draw, which is fine for small a and wasteful for a = 500.
Any shape: rejection
The standard answer for general shape wraps a normal variate in a cubic transformation and accepts or rejects it. For a ≥ 1, set d = a − 1/3 and c = 1/√(9d). Repeat: draw a normal z and a uniform u, form v = (1 + cz)³, and discard the pair if v ≤ 0. Accept x = dv whenever
ln u < z²/2 + d − dv + d · ln v
then return xθ. The method of Marsaglia and Tsang accepts the vast majority of candidate values, so on average slightly more than one pair of random numbers is consumed per draw.
A worked trial for a = 3: d = 2.666667 and c = 0.204124. With z = 0.5 the quantity v is (1.102062)³ = 1.338499, and the candidate is dv = 3.569332. The right side of the test is 0.125 + 2.666667 − 3.569332 + 2.666667 × ln 1.338499, which comes to −0.000201. For u = 0.6 the left side is ln 0.6 = −0.510826, below the right side, so the candidate is accepted and the final draw is 3.5693 times the scale. A u close to 1, such as 0.9999, has a left side of about −0.0001, which is above the right side, so that candidate would be rejected.
Shape below 1
The cubic construction needs a ≥ 1. For smaller shapes, draw a gamma variate with shape a + 1 and multiply by u^(1/a) for a fresh uniform u. With a = 0.5 and u = 0.25 the multiplier is 0.25² = 0.0625. The correction is exact, and it shows why small-shape gamma draws pile up near zero: the multiplier shrinks a typical value by a large factor.
Related draws and checks
A chi-square variable with k degrees of freedom is a gamma variable with shape k/2 and scale 2, so the same function covers it. For a = 1 the distribution reduces to the exponential. And if the shape is an integer and the scale is the reciprocal of a rate, the result is the waiting time for a events in a Poisson process, which links this page to Poisson random numbers.
Validation follows the usual pattern. The sample mean and variance of a large batch should approach aθ and aθ². With a = 3 and θ = 2, for instance, a million draws should average within a few thousandths of 6. A histogram of the batch, scaled to unit area, should follow the density curve. As a further test, compare the fraction of draws below a chosen x with the regularized incomplete gamma function, which is the exact cumulative probability.
Guard the inputs. Shape and scale must be positive, and a scale given as a rate in some libraries needs inverting; confusing the two is a common reason an average comes out wrong by a large factor. The remaining distribution generators and tail functions sit on the statistics and special functions page, and the binomial random numbers page covers the discrete case.