# Gamma distribution

Template:Distinguish{{ safesubst:#invoke:Unsubst||$N=Refimprove |date=__DATE__ |$B= {{#invoke:Message box|ambox}} }}

In probability theory and statistics, the gamma distribution is a two-parameter family of continuous probability distributions. The common exponential distribution and chi-squared distribution are special cases of the gamma distribution. There are three different parametrizations in common use:

1. With a shape parameter k and a scale parameter θ.
2. With a shape parameter α = k and an inverse scale parameter β = 1/θ, called a rate parameter.
3. With a shape parameter k and a mean parameter μ = k/β.

In each of these three forms, both parameters are positive real numbers.

The parameterization with k and θ appears to be more common in econometrics and certain other applied fields, where e.g. the gamma distribution is frequently used to model waiting times. For instance, in life testing, the waiting time until death is a random variable that is frequently modeled with a gamma distribution.

The parameterization with α and β is more common in Bayesian statistics, where the gamma distribution is used as a conjugate prior distribution for various types of inverse scale (aka rate) parameters, such as the λ of an exponential distribution or a Poisson distribution – or for that matter, the β of the gamma distribution itself. (The closely related inverse gamma distribution is used as a conjugate prior for scale parameters, such as the variance of a normal distribution.)

If k is an integer, then the distribution represents an Erlang distribution; i.e., the sum of k independent exponentially distributed random variables, each of which has a mean of θ (which is equivalent to a rate parameter of 1/θ).

The gamma distribution is the maximum entropy probability distribution for a random variable X for which E[X] = kθ = α/β is fixed and greater than zero, and E[ln(X)] = ψ(k) + ln(θ) = ψ(α) − ln(β) is fixed (ψ is the digamma function).

## Characterization using shape k and scale θ

A random variable X that is gamma-distributed with shape k and scale θ is denoted by

$X\sim \Gamma (k,\theta )\equiv {\textrm {Gamma}}(k,\theta )$ ### Probability density function Illustration of the gamma PDF for parameter values over k and x with θ set to 1, 2, 3, 4, 5 and 6. One can see each θ layer by itself here  as well as by k  and x. .

The probability density function using the shape-scale parametrization is

$f(x;k,\theta )={\frac {x^{k-1}e^{-{\frac {x}{\theta }}}}{\theta ^{k}\Gamma (k)}}\quad {\text{ for }}x>0{\text{ and }}k,\theta >0.$ Here Γ(k) is the gamma function evaluated at k.

### Cumulative distribution function

The cumulative distribution function is the regularized gamma function:

$F(x;k,\theta )=\int _{0}^{x}f(u;k,\theta )\,du={\frac {\gamma \left(k,{\frac {x}{\theta }}\right)}{\Gamma (k)}}$ where γ(k, x/θ) is the lower incomplete gamma function.

It can also be expressed as follows, if k is a positive integer (i.e., the distribution is an Erlang distribution):

$F(x;k,\theta )=1-\sum _{i=0}^{k-1}{\frac {1}{i!}}\left({\frac {x}{\theta }}\right)^{i}e^{-{\frac {x}{\theta }}}=e^{-{\frac {x}{\theta }}}\sum _{i=k}^{\infty }{\frac {1}{i!}}\left({\frac {x}{\theta }}\right)^{i}$ ## Characterization using shape α and rate β

Alternatively, the gamma distribution can be parameterized in terms of a shape parameter α = k and an inverse scale parameter β = 1/θ, called a rate parameter. A random variable X that is gamma-distributed with shape α and rate β is denoted

$X\sim \Gamma (\alpha ,\beta )\equiv {\textrm {Gamma}}(\alpha ,\beta )$ ### Probability density function

The corresponding density function in the shape-rate parametrization is

$g(x;\alpha ,\beta )={\frac {\beta ^{\alpha }x^{\alpha -1}e^{-x\beta }}{\Gamma (\alpha )}}\quad {\text{ for }}x\geq 0{\text{ and }}\alpha ,\beta >0$ Both parametrizations are common because either can be more convenient depending on the situation.

### Cumulative distribution function

The cumulative distribution function is the regularized gamma function:

$F(x;\alpha ,\beta )=\int _{0}^{x}f(u;\alpha ,\beta )\,du={\frac {\gamma (\alpha ,\beta x)}{\Gamma (\alpha )}}$ where γ(α, βx) is the lower incomplete gamma function.

If α is a positive integer (i.e., the distribution is an Erlang distribution), the cumulative distribution function has the following series expansion:

$F(x;\alpha ,\beta )=1-\sum _{i=0}^{\alpha -1}{\frac {(\beta x)^{i}}{i!}}e^{-\beta x}=e^{-\beta x}\sum _{i=\alpha }^{\infty }{\frac {(\beta x)^{i}}{i!}}$ ## Properties

### Skewness

The skewness is equal to $2/{\sqrt {k}}$ , it depends only on the shape parameter (k) and approaches a normal distribution when k is large (approximately when k > 10).

### Median calculation

Unlike the mode and the mean which have readily calculable formulas based on the parameters, the median does not have an easy closed form equation. The median for this distribution is defined as the value ν such that

${\frac {1}{\Gamma (k)\theta ^{k}}}\int _{0}^{\nu }x^{k-1}e^{-{\frac {x}{\theta }}}dx={\tfrac {1}{2}}.$ A formula for approximating the median for any gamma distribution, when the mean is known, has been derived based on the fact that the ratio μ/(μ − ν) is approximately a linear function of k when k ≥ 1. The approximation formula is

$\nu \approx \mu {\frac {3k-0.8}{3k+0.2}},$ ### Summation

If Xi has a Gamma(ki, θ) distribution for i = 1, 2, ..., N (i.e., all distributions have the same scale parameter θ), then

$\sum _{i=1}^{N}X_{i}\sim {\mathrm {Gamma} }\left(\sum _{i=1}^{N}k_{i},\theta \right)$ provided all Xi are independent.

For the cases where the Xi are independent but have different scale parameters see Mathai (1982) and Moschopoulos (1984).

The gamma distribution exhibits infinite divisibility.

### Scaling

If

$X\sim \mathrm {Gamma} (k,\theta ),$ then for any c > 0,

$cX\sim {\mathrm {Gamma} }(k,c\theta ).$ Hence the use of the term "scale parameter" to describe θ.

Equivalently, if

$X\sim {\mathrm {Gamma} }(\alpha ,\beta ),$ then for any c > 0,

$cX\sim {\mathrm {Gamma} }(\alpha ,\beta /c).$ Hence the use of the term "inverse scale parameter" to describe β.

### Exponential family

The gamma distribution is a two-parameter exponential family with natural parameters k − 1 and −1/θ (equivalently, α − 1 and −β), and natural statistics X and ln(X).

If the shape parameter k is held fixed, the resulting one-parameter family of distributions is a natural exponential family.

### Logarithmic expectation

One can show that

${\mathbf {E} }[\ln(X)]=\psi (\alpha )-\ln(\beta )$ or equivalently,

${\mathbf {E} }[\ln(X)]=\psi (k)+\ln(\theta )$ where ψ is the digamma function.

This can be derived using the exponential family formula for the moment generating function of the sufficient statistic, because one of the sufficient statistics of the gamma distribution is ln(x).

### Information entropy

The information entropy is

$\operatorname {H} (X)={\mathbf {E} }[-\ln(p(X))]={\mathbf {E} }[-\alpha \ln(\beta )+\ln(\Gamma (\alpha ))-(\alpha -1)\ln(X)+\beta X]=\alpha -\ln(\beta )+\ln(\Gamma (\alpha ))+(1-\alpha )\psi (\alpha ).$ In the k, θ parameterization, the information entropy is given by

$\operatorname {H} (X)=k+\ln(\theta )+\ln(\Gamma (k))+(1-k)\psi (k).$ ### Kullback–Leibler divergence Illustration of the Kullback–Leibler (KL) divergence for two gamma PDFs. Here β = β0 + 1 which are set to 1, 2, 3, 4, 5 and 6. The typical asymmetry for the KL divergence is clearly visible.

The Kullback–Leibler divergence (KL-divergence), of Gamma(αp, βp) ("true" distribution) from Gamma(αq, βq) ("approximating" distribution) is given by

$D_{\mathrm {KL} }(\alpha _{p},\beta _{p};\alpha _{q},\beta _{q})=(\alpha _{p}-\alpha _{q})\psi (\alpha _{p})-\log \Gamma (\alpha _{p})+\log \Gamma (\alpha _{q})+\alpha _{q}(\log \beta _{p}-\log \beta _{q})+\alpha _{p}{\frac {\beta _{q}-\beta _{p}}{\beta _{p}}}$ Written using the k, θ parameterization, the KL-divergence of Gamma(kp, θp) from Gamma(kq, θq) is given by

$D_{\mathrm {KL} }(k_{p},\theta _{p};k_{q},\theta _{q})=(k_{p}-k_{q})\psi (k_{p})-\log \Gamma (k_{p})+\log \Gamma (k_{q})+k_{q}(\log \theta _{q}-\log \theta _{p})+k_{p}{\frac {\theta _{p}-\theta _{q}}{\theta _{q}}}$ ### Laplace transform

The Laplace transform of the gamma PDF is

$F(s)=(1+\theta s)^{-k}={\frac {\beta ^{\alpha }}{(s+\beta )^{\alpha }}}.$ ## Parameter estimation

### Maximum likelihood estimation

The likelihood function for N iid observations (x1, ..., xN) is

$L(k,\theta )=\prod _{i=1}^{N}f(x_{i};k,\theta )$ from which we calculate the log-likelihood function

$\ell (k,\theta )=(k-1)\sum _{i=1}^{N}\ln {(x_{i})}-\sum _{i=1}^{N}{\frac {x_{i}}{\theta }}-Nk\ln(\theta )-N\ln(\Gamma (k))$ Finding the maximum with respect to θ by taking the derivative and setting it equal to zero yields the maximum likelihood estimator of the θ parameter:

${\hat {\theta }}={\frac {1}{kN}}\sum _{i=1}^{N}x_{i}$ Substituting this into the log-likelihood function gives

$\ell =(k-1)\sum _{i=1}^{N}\ln {(x_{i})}-Nk-Nk\ln {\left({\frac {\sum x_{i}}{kN}}\right)}-N\ln(\Gamma (k))$ Finding the maximum with respect to k by taking the derivative and setting it equal to zero yields

$\ln(k)-\psi (k)=\ln \left({\frac {1}{N}}\sum _{i=1}^{N}x_{i}\right)-{\frac {1}{N}}\sum _{i=1}^{N}\ln(x_{i})$ There is no closed-form solution for k. The function is numerically very well behaved, so if a numerical solution is desired, it can be found using, for example, Newton's method. An initial value of k can be found either using the method of moments, or using the approximation

$\ln(k)-\psi (k)\approx {\frac {1}{2k}}\left(1+{\frac {1}{6k+1}}\right)$ If we let

$s=\ln {\left({\frac {1}{N}}\sum _{i=1}^{N}x_{i}\right)}-{\frac {1}{N}}\sum _{i=1}^{N}\ln {(x_{i})}$ then k is approximately

$k\approx {\frac {3-s+{\sqrt {(s-3)^{2}+24s}}}{12s}}$ which is within 1.5% of the correct value. An explicit form for the Newton–Raphson update of this initial guess is:

$k\leftarrow k-{\frac {\ln(k)-\psi (k)-s}{{\frac {1}{k}}-\psi ^{\prime }(k)}}.$ ### Bayesian minimum mean squared error

With known k and unknown θ, the posterior density function for theta (using the standard scale-invariant prior for θ) is

$P(\theta |k,x_{1},\dots ,x_{N})\propto {\frac {1}{\theta }}\prod _{i=1}^{N}f(x_{i};k,\theta )$ Denoting

$y\equiv \sum _{i=1}^{N}x_{i},\qquad P(\theta |k,x_{1},\dots ,x_{N})=C(x_{i})\theta ^{-Nk-1}e^{-{\frac {y}{\theta }}}$ Integration over θ can be carried out using a change of variables, revealing that 1/θ is gamma-distributed with parameters α = Nk, β = y.

$\int _{0}^{\infty }\theta ^{-Nk-1+m}e^{-{\frac {y}{\theta }}}\,d\theta =\int _{0}^{\infty }x^{Nk-1-m}e^{-xy}\,dx=y^{-(Nk-m)}\Gamma (Nk-m)\!$ The moments can be computed by taking the ratio (m by m = 0)

${\mathbf {E} }[x^{m}]={\frac {\Gamma (Nk-m)}{\Gamma (Nk)}}y^{m}$ which shows that the mean ± standard deviation estimate of the posterior distribution for θ is

${\frac {y}{Nk-1}}\pm {\frac {y^{2}}{(Nk-1)^{2}(Nk-2)}}$ ## Generating gamma-distributed random variables

Given the scaling property above, it is enough to generate gamma variables with θ = 1 as we can later convert to any value of β with simple division.

Using the fact that a Gamma(1, 1) distribution is the same as an Exp(1) distribution, and noting the method of generating exponential variables, we conclude that if U is uniformly distributed on (0, 1], then −ln(U) is distributed Gamma(1, 1) Now, using the "α-addition" property of gamma distribution, we expand this result:

$\sum _{k=1}^{n}{-\ln U_{k}}\sim \Gamma (n,1)$ where Uk are all uniformly distributed on (0, 1] and independent. All that is left now is to generate a variable distributed as Gamma(δ, 1) for 0 < δ < 1 and apply the "α-addition" property once more. This is the most difficult part.

Random generation of gamma variates is discussed in detail by Devroye, noting that none are uniformly fast for all shape parameters. For small values of the shape parameter, the algorithms are often not valid. For arbitrary values of the shape parameter, one can apply the Ahrens and Dieter modified acceptance–rejection method Algorithm GD (shape k ≥ 1), or transformation method when 0 < k < 1. Also see Cheng and Feast Algorithm GKM 3 or Marsaglia's squeeze method.

The following is a version of the Ahrens-Dieter acceptance–rejection method:

1. Let m be 1.
2. Generate V3m−2, V3m−1 and V3m as independent uniformly distributed on (0, 1] variables.
3. If $V_{3m-2}\leq v_{0}$ , where $v_{0}={\frac {e}{e+\delta }}$ , then go to step 4, else go to step 5.
4. Let $\xi _{m}=V_{3m-1}^{1/\delta },\ \eta _{m}=V_{3m}\xi _{m}^{\delta -1}$ . Go to step 6.
5. Let $\xi _{m}=1-\ln {V_{3m-1}},\ \eta _{m}=V_{3m}e^{-\xi _{m}}$ .
6. If $\eta _{m}>\xi _{m}^{\delta -1}e^{-\xi _{m}}$ , then increment m and go to step 2.
7. Assume ξ = ξm to be the realization of Γ(δ, 1).

A summary of this is

$\theta \left(\xi -\sum _{i=1}^{\lfloor {k}\rfloor }{\ln(U_{i})}\right)\sim \Gamma (k,\theta )$ where

While the above approach is technically correct, Devroye notes that it is linear in the value of k and in general is not a good choice. Instead he recommends using either rejection-based or table-based methods, depending on context.

## Related distributions

### Conjugate prior

In Bayesian inference, the gamma distribution is the conjugate prior to many likelihood distributions: the Poisson, exponential, normal (with known mean), Pareto, gamma with known shape σ, inverse gamma with known shape parameter, and Gompertz with known scale parameter.

The gamma distribution's conjugate prior is:

$p(k,\theta |p,q,r,s)={\frac {1}{Z}}{\frac {p^{k-1}e^{-\theta ^{-1}q}}{\Gamma (k)^{r}\theta ^{ks}}},$ where Z is the normalizing constant, which has no closed-form solution. The posterior distribution can be found by updating the parameters as follows:

{\begin{aligned}p'&=p\prod \nolimits _{i}x_{i},\\q'&=q+\sum \nolimits _{i}x_{i},\\r'&=r+n,\\s'&=s+n,\end{aligned}} where n is the number of observations, and xi is the ith observation.

### Compound gamma

If the shape parameter of the gamma distribution is known, but the inverse-scale parameter is unknown, then a gamma distribution for the inverse-scale forms a conjugate prior. The compound distribution, which results from integrating out the inverse-scale has a closed form solution, known as the compound gamma distribution.

### Others

• If X ~ Gamma(1, λ), then X has an exponential distribution with rate parameter λ.
• If X ~ Gamma(ν/2, 2), then X is identical to χ2(ν), the chi-squared distribution with ν degrees of freedom. Conversely, if Q ~ χ2(ν) and c is a positive constant, then cQ ~ Gamma(ν/2, 2c).
• If k is an integer, the gamma distribution is an Erlang distribution and is the probability distribution of the waiting time until the kth "arrival" in a one-dimensional Poisson process with intensity 1/θ. If
$X\sim \Gamma (k\in {\mathbf {Z} },\theta ),\qquad Y\sim {\mathrm {Pois} }\left({\frac {x}{\theta }}\right),$ then
$P(X>x)=P(Y $X^{2}\sim \Gamma \left({\tfrac {3}{2}},2a^{2}\right)$ .

{{#invoke:Category handler|main}}{{#invoke:Category handler|main}}[citation needed] }} .

## Applications

The gamma distribution has been used to model the size of insurance claims and rainfalls. This means that aggregate insurance claims and the amount of rainfall accumulated in a reservoir are modelled by a gamma process. The gamma distribution is also used to model errors in multi-level Poisson regression models, because the combination of the Poisson distribution and a gamma distribution is a negative binomial distribution.

In neuroscience, the gamma distribution is often used to describe the distribution of inter-spike intervals. Although in practice the gamma distribution often provides a good fit, there is no underlying biophysical motivation for using it.

In bacterial gene expression, the copy number of a constitutively expressed protein often follows the gamma distribution, where the scale and shape parameter are, respectively, the mean number of bursts per cell cycle and the mean number of protein molecules produced by a single mRNA during its lifetime.

In genomics, the gamma distribution was applied in peak calling step (i.e. in recognition of signal) in ChIP-chip and ChIP-seq data analysis.

The gamma distribution is widely used as a conjugate prior in Bayesian statistics. It is the conjugate prior for the precision (i.e. inverse of the variance) of a normal distribution. It is also the conjugate prior for the exponential distribution.