TL;DR
The gamma distribution describes a positive quantity that is the total of several independent waiting times. It has two parameters: a shape , which sets how many waits are being added, and a rate , which sets the time scale. Its mean is . 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 -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 -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.
- shape 1
- shape 2
- shape 5
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
A continuous random variable is gamma distributed with shape and rate , written , if its density is
Here is a possible value of the variable. The factor grows with when and makes the density zero at . The factor is the exponential decay that controls the right tail. The constant is the gamma function, which extends the factorial: for positive whole numbers . The factor is there to make the total area equal to 1.
Some texts write the density with a scale 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 , so that :
The last step uses the definition of the gamma function, . Multiplying by gives 1, as required.
Mean, variance and mode
For a gamma variable:
The mean is the shape times the mean of one exponential wait, . The variance adds in the same way, since each exponential wait has variance 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 are independent exponentials with rate , their sum is gamma:
More generally, two independent gammas with the same rate add their shapes: . Scaling a gamma variable by a positive constant divides the rate: .
Link to the chi-squared distribution
A chi-squared variable with degrees of freedom is a gamma with shape and rate . 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 is a whole number, the tail probability has a finite form. For ,
This is the probability that fewer than events happen in time for a Poisson process of rate . The equality is a useful bridge: the time to the -th event is longer than exactly when the count by time is still below . The worked example below uses it.
Bayesian update with a Poisson likelihood
Suppose a count follows a Poisson distribution with unknown rate , and the prior belief about is . After observing counts with sum , the posterior is
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 , which sits between the prior mean and the sample average .
Worked examples
Let . The density is for , because and .
- Mean: .
- Variance: , so the standard deviation is about 1.41.
- Probability that : . Integrating by parts gives .
- Numerically, .
- Check with the tail formula: . 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 (). About a quarter of the probability sits below 1, and the rest is spread over larger values.
Calls reach a help desk as a Poisson process at 2 per hour. You want the distribution of the time until the third call arrives. The sum of three independent exponential waits, each with rate 2, gives .
- Mean wait: hours.
- Standard deviation: hours.
- Probability that the third call takes more than 2 hours: .
- Use the tail formula with and : .
- Numerically, , so .
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.
A support team wants to know how many errors per day its system produces. Before any data, a prior of is chosen: the prior mean rate is errors per day. Over three days, the error counts are 3, 1 and 4. So and .
- Posterior shape: .
- Posterior rate: .
- Posterior mean: errors per day.
- Sample average: .
- 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 grows with every new day.
Tradeoffs and failure modes
The gamma is flexible, but it is not always the right choice.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Positive data with a long right tail, such as insurance claims | A gamma fits the shape, but heavy tails beyond the gamma's reach are missed | A log-normal or Pareto model, and check the tail with data |
| A count of events in a fixed window | The gamma models the waiting time, not the count | The Poisson or negative binomial distribution for the count itself |
| Shape near zero | The density explodes at zero, and sampling or fitting becomes unstable | Check the data for zeros and small values, and consider a different model |
| Data that are symmetric and centred | A gamma with large shape is close to Gaussian, but it still forces a skew | A Gaussian model, which has fewer parameters |
| Estimating the shape from small samples | The shape and rate are correlated, so the estimates are noisy | Use 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 or a scale , since passing a rate where a scale is expected changes the mean from to , a factor of 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 means the variable is the time to the -th event, so is one exponential wait. Equivalently, a gamma with integer shape is an Erlang distribution.
QuestionComputation: . Find the mean and the variance.Show answer
Mean is . Variance is . The standard deviation is about 0.433. The mean is the shape times the mean of one exponential wait, which is , and the variance adds across the three independent waits, each with variance .
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 in the rate . The Poisson likelihood for counts contributes a factor , where is the total count and the number of periods. Multiplying the two gives , which is the gamma kernel with shape and rate . 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 days. The exponential would badly underestimate how long it takes to reach five failures.