Search

Search papers and lessons

Gamma Distribution

Level 3 · 11 min read

The gamma distribution models a positive quantity built from several waiting times added together; a shape and a rate fix its form and its scale.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Sign in to see your progress

TL;DR

The gamma distribution describes a positive quantity that is the total of several independent waiting times. It has two parameters: a shape α>0\alpha \gt 0, which sets how many waits are being added, and a rate β>0\beta \gt 0, which sets the time scale. Its mean is α/β\alpha/\beta. With shape 1 it is the exponential distribution, and it generalises that model to any number of events.

Where you'll see it: the time until the α\alpha-th event in a Poisson process, the Bayesian model of an unknown event rate, rainfall and insurance claim sizes, and the chi-squared distribution used in statistical tests.

Intuition

Waiting for several buses

The Exponential Distribution answers one question: how long until the next event, when events arrive at a steady average rate? Now ask a second question. Buses arrive at random, on average one every 15 minutes, and a friend says, "I will know you are safe when the third bus has passed." How long do you wait for the third bus?

The answer is the sum of three waits. The first wait is an exponential time. After the first bus, the second wait starts afresh, and it is also exponential with the same rate. The same holds for the third. The total time is the sum of three independent exponential waits, and that sum has a gamma distribution. The shape counts the waits: shape 3 means three events. The rate says how fast the events come.

This is why the gamma is the natural model for "time until the kk-th event". It is also why it is skewed. A sum of positive quantities cannot go below zero, and a few unusually slow waits stretch the right tail. Increasing the shape makes the sum more symmetric, and for large shape it looks almost like a bell curve, as the central limit theorem predicts.

A family, not one curve

Gamma densities come in a family. Changing the shape changes the form, from a curve that starts at its highest point (shape 1) to a hump that moves away from zero as the shape grows. Changing the rate only stretches the horizontal axis. The plot below fixes the rate at 1 and varies the shape.

02468101200.20.40.60.81xdensity
  • shape 1
  • shape 2
  • shape 5
Rate fixed at 1. Shape 1 is the exponential. Larger shape moves the peak away from zero and makes the curve more bell-like. The marker shows the peak of shape 2.

How the pieces connect

The gamma sits at the centre of a small web of models. The diagram below shows the links that the rest of this lesson uses.

flowchart LR
  A["Exponential waiting time"] -->|"add n of them"| B["Gamma with shape n"]
  B -->|"shape equals 1"| A
  C["Chi-squared with k degrees of freedom"] -->|"shape k over 2, rate 1 over 2"| B
  D["Poisson event count with unknown rate"] -->|"Gamma prior on the rate"| E["Gamma posterior"]

The math

Definition

Definition: Gamma distribution

A continuous random variable X>0X \gt 0 is gamma distributed with shape α>0\alpha \gt 0 and rate β>0\beta \gt 0, written X∼Gamma(α,β)X \sim \mathrm{Gamma}(\alpha, \beta), if its density is

f(x)=βαΓ(α) xα−1e−βx,x>0.f(x) = \frac{\beta^{\alpha}}{\Gamma(\alpha)}\, x^{\alpha-1} e^{-\beta x}, \qquad x \gt 0.

Here xx is a possible value of the variable. The factor xα−1x^{\alpha-1} grows with xx when α>1\alpha \gt 1 and makes the density zero at x=0x = 0. The factor e−βxe^{-\beta x} is the exponential decay that controls the right tail. The constant Γ(α)\Gamma(\alpha) is the gamma function, which extends the factorial: Γ(n)=(n−1)!\Gamma(n) = (n-1)! for positive whole numbers nn. The factor βα/Γ(α)\beta^{\alpha}/\Gamma(\alpha) is there to make the total area equal to 1.

Some texts write the density with a scale θ=1/β\theta = 1/\beta instead of a rate. The two conventions describe the same family. This lesson uses the rate, because it matches the Exponential Distribution and the Bayesian updates below.

Checking that it is a density

The area must be 1. Substitute u=βxu = \beta x, so that dx=du/βdx = du/\beta:

∫0∞xα−1e−βx dx=1βα∫0∞uα−1e−u du=Γ(α)βα.\int_0^\infty x^{\alpha-1} e^{-\beta x}\, dx = \frac{1}{\beta^{\alpha}} \int_0^\infty u^{\alpha-1} e^{-u}\, du = \frac{\Gamma(\alpha)}{\beta^{\alpha}}.

The last step uses the definition of the gamma function, Γ(α)=∫0∞uα−1e−u du\Gamma(\alpha) = \int_0^\infty u^{\alpha-1} e^{-u}\,du. Multiplying by βα/Γ(α)\beta^{\alpha}/\Gamma(\alpha) gives 1, as required.

Mean, variance and mode

For a gamma variable:

E[X]=αβ,Var(X)=αβ2,mode=α−1β when α≥1.\mathbb{E}[X] = \frac{\alpha}{\beta}, \qquad \mathrm{Var}(X) = \frac{\alpha}{\beta^2}, \qquad \text{mode} = \frac{\alpha - 1}{\beta} \text{ when } \alpha \ge 1.

The mean is the shape times the mean of one exponential wait, 1/β1/\beta. The variance adds in the same way, since each exponential wait has variance 1/β21/\beta^2 and the waits are independent. The mode is the most likely value. For shape 1 it sits at zero, and for shape below 1 the density is unbounded at zero.

Sums and scaling

If X1,…,XnX_1, \dots, X_n are independent exponentials with rate β\beta, their sum is gamma:

X1+⋯+Xn∼Gamma(n,β).X_1 + \cdots + X_n \sim \mathrm{Gamma}(n, \beta).

More generally, two independent gammas with the same rate add their shapes: Gamma(α1,β)+Gamma(α2,β)=Gamma(α1+α2,β)\mathrm{Gamma}(\alpha_1, \beta) + \mathrm{Gamma}(\alpha_2, \beta) = \mathrm{Gamma}(\alpha_1 + \alpha_2, \beta). Scaling a gamma variable by a positive constant cc divides the rate: cX∼Gamma(α,β/c)cX \sim \mathrm{Gamma}(\alpha, \beta/c).

A chi-squared variable with kk degrees of freedom is a gamma with shape k/2k/2 and rate 1/21/2. That is why the two distributions share so many formulas. Working with the gamma directly gives the same results with fewer special cases.

The cumulative distribution for whole-number shape

When α=n\alpha = n is a whole number, the tail probability has a finite form. For X∼Gamma(n,β)X \sim \mathrm{Gamma}(n, \beta),

P(X>t)=e−βt∑k=0n−1(βt)kk!.P(X \gt t) = e^{-\beta t} \sum_{k=0}^{n-1} \frac{(\beta t)^k}{k!}.

This is the probability that fewer than nn events happen in time tt for a Poisson process of rate β\beta. The equality is a useful bridge: the time to the nn-th event is longer than tt exactly when the count by time tt is still below nn. The worked example below uses it.

Bayesian update with a Poisson likelihood

Suppose a count NN follows a Poisson distribution with unknown rate λ\lambda, and the prior belief about λ\lambda is Gamma(α,β)\mathrm{Gamma}(\alpha, \beta). After observing counts x1,…,xnx_1, \dots, x_n with sum s=∑xis = \sum x_i, the posterior is

λ∣data∼Gamma(α+s, β+n).\lambda \mid \text{data} \sim \mathrm{Gamma}(\alpha + s,\ \beta + n).

The prior and posterior belong to the same family. This property is called conjugacy, and it turns a Bayesian update into adding numbers. The shape grows by the total count, and the rate grows by the number of observation periods. The posterior mean is (α+s)/(β+n)(\alpha + s)/(\beta + n), which sits between the prior mean α/β\alpha/\beta and the sample average s/ns/n.

Worked examples

Example
A small hand-computed case: shape 2, rate 1

Let X∼Gamma(2,1)X \sim \mathrm{Gamma}(2, 1). The density is f(x)=xe−xf(x) = x e^{-x} for x>0x \gt 0, because Γ(2)=1\Gamma(2) = 1 and βα=1\beta^{\alpha} = 1.

  1. Mean: α/β=2/1=2\alpha/\beta = 2/1 = 2.
  2. Variance: α/β2=2\alpha/\beta^2 = 2, so the standard deviation is about 1.41.
  3. Probability that X≤1X \le 1: ∫01xe−x dx\int_0^1 x e^{-x}\,dx. Integrating by parts gives [−xe−x−e−x]01=1−2e−1\left[-x e^{-x} - e^{-x}\right]_0^1 = 1 - 2e^{-1}.
  4. Numerically, 1−2×0.36788=0.264241 - 2 \times 0.36788 = 0.26424.
  5. Check with the tail formula: P(X≤1)=1−e−1(1+1)=1−2e−1P(X \le 1) = 1 - e^{-1}(1 + 1) = 1 - 2e^{-1}. The two methods agree.

Interpretation: the right tail is long, so a few large values pull the mean up to 2 while more than half of the probability lies below it (P(X≤2)=1−3e−2≈0.59P(X \le 2) = 1 - 3e^{-2} \approx 0.59). About a quarter of the probability sits below 1, and the rest is spread over larger values.

Example
Waiting for the third arrival

Calls reach a help desk as a Poisson process at 2 per hour. You want the distribution of the time TT until the third call arrives. The sum of three independent exponential waits, each with rate 2, gives T∼Gamma(3,2)T \sim \mathrm{Gamma}(3, 2).

  1. Mean wait: 3/2=1.53/2 = 1.5 hours.
  2. Standard deviation: 3/22=0.75≈0.866\sqrt{3/2^2} = \sqrt{0.75} \approx 0.866 hours.
  3. Probability that the third call takes more than 2 hours: P(T>2)P(T \gt 2).
  4. Use the tail formula with βt=2×2=4\beta t = 2 \times 2 = 4 and n=3n = 3: P(T>2)=e−4(1+4+422!)=e−4×13P(T \gt 2) = e^{-4}\left(1 + 4 + \frac{4^2}{2!}\right) = e^{-4} \times 13.
  5. Numerically, e−4=0.018316e^{-4} = 0.018316, so P(T>2)=0.2381P(T \gt 2) = 0.2381.

Interpretation: only about one time in four does the third call take more than two hours to arrive, even though the average is 1.5 hours. The right skew is visible in the numbers: the mean is 1.5, but the tail still reaches past 2 hours with nearly a quarter of the probability.

Example
Updating a belief about a call rate

A support team wants to know how many errors per day its system produces. Before any data, a prior of Gamma(2,1)\mathrm{Gamma}(2, 1) is chosen: the prior mean rate is 2/1=22/1 = 2 errors per day. Over three days, the error counts are 3, 1 and 4. So n=3n = 3 and s=3+1+4=8s = 3 + 1 + 4 = 8.

  1. Posterior shape: α+s=2+8=10\alpha + s = 2 + 8 = 10.
  2. Posterior rate: β+n=1+3=4\beta + n = 1 + 3 = 4.
  3. Posterior mean: 10/4=2.510/4 = 2.5 errors per day.
  4. Sample average: s/n=8/3≈2.67s/n = 8/3 \approx 2.67.
  5. The posterior mean 2.5 lies between the prior mean 2 and the sample average 2.67, as the general formula predicts.

Interpretation: three days of data pulled the estimate up from 2 to 2.5, but not all the way to the raw average. The prior still counts for something, and it counts for less as more days are observed, because the rate term β+n\beta + n grows with every new day.

Tradeoffs and failure modes

The gamma is flexible, but it is not always the right choice.

SituationWhat goes wrongBetter choice
Positive data with a long right tail, such as insurance claimsA gamma fits the shape, but heavy tails beyond the gamma's reach are missedA log-normal or Pareto model, and check the tail with data
A count of events in a fixed windowThe gamma models the waiting time, not the countThe Poisson or negative binomial distribution for the count itself
Shape near zeroThe density explodes at zero, and sampling or fitting becomes unstableCheck the data for zeros and small values, and consider a different model
Data that are symmetric and centredA gamma with large shape is close to Gaussian, but it still forces a skewA Gaussian model, which has fewer parameters
Estimating the shape from small samplesThe shape and rate are correlated, so the estimates are noisyUse the method of moments as a start, then refine with maximum likelihood and report intervals

A common mistake is to confuse the rate and the scale. Software libraries differ on which one they take as an argument. Before using a fitted gamma, check whether the parameter is a rate β\beta or a scale θ=1/β\theta = 1/\beta, since passing a rate where a scale is expected changes the mean from α/β\alpha/\beta to αβ\alpha\beta, a factor of β2\beta^2 off.

History

Euler introduced the gamma function in the 1720s and 1730s, while studying how to extend factorials to non-integers, and the function later became the normalising constant of the gamma density. Karl Pearson placed a version of the distribution among his family of curves in the 1890s, where it was called Pearson's type III. Agner Krarup Erlang, working on telephone traffic in the early twentieth century, used sums of exponential waits, and the whole-number case is still called the Erlang distribution. The conjugate Bayesian update for a Poisson rate, using a gamma prior, was worked out and popularised in the early 1960s, in the decision-theory work of Howard Raiffa and Robert Schlaifer. It caught on because it turns Bayesian updating into simple arithmetic, which mattered a great deal before computers made sampling routine.

Check yourself

QuestionRecall: what does the shape parameter count when the variable is a sum of waiting times?Show answer

The shape counts the number of independent exponential waits that are added together. A shape of nn means the variable is the time to the nn-th event, so Gamma(1,β)\mathrm{Gamma}(1, \beta) is one exponential wait. Equivalently, a gamma with integer shape is an Erlang distribution.

QuestionComputation: X∼Gamma(3,4)X \sim \mathrm{Gamma}(3, 4). Find the mean and the variance.Show answer

Mean is α/β=3/4=0.75\alpha/\beta = 3/4 = 0.75. Variance is α/β2=3/16=0.1875\alpha/\beta^2 = 3/16 = 0.1875. The standard deviation is about 0.433. The mean is the shape times the mean of one exponential wait, which is 1/41/4, and the variance adds across the three independent waits, each with variance 1/161/16.

QuestionWhy: why does the prior-to-posterior update for a Poisson rate just add numbers to the shape and the rate?Show answer

The gamma density has the factor λα−1e−βλ\lambda^{\alpha-1} e^{-\beta\lambda} in the rate λ\lambda. The Poisson likelihood for counts xix_i contributes a factor λse−nλ\lambda^{s} e^{-n\lambda}, where ss is the total count and nn the number of periods. Multiplying the two gives λα+s−1e−(β+n)λ\lambda^{\alpha + s - 1} e^{-(\beta + n)\lambda}, which is the gamma kernel with shape α+s\alpha + s and rate β+n\beta + n. The prior and posterior share a form, so the update is just adding.

QuestionApplied: a manufacturer models the time until the fifth failure of a machine with a gamma distribution, and the rate is 0.1 failures per day. Why is the exponential model a poor fit for this question?Show answer

The exponential models the time to the first failure. The question asks for the time to the fifth, which is a sum of five independent waits, and that is a gamma with shape 5 and rate 0.1. Using the exponential would give a mean of 10 days, while the correct mean is 5/0.1=505/0.1 = 50 days. The exponential would badly underestimate how long it takes to reach five failures.

Sources