Gamma distribution

In probability theory and statistics, the gamma distribution is a two-parameter family of continuous probability distributions. It has a scale parameter &theta; and a shape parameter k. If k is an integer then the distribution represents the sum of k exponentially distributed random variables, each of which has mean &theta;.

Characterization
That a random variable X is gamma-distributed with scale &theta; and shape k is denoted


 * $$X \sim \Gamma(k, \theta) \,\,\mathrm{ or }\,\, X \sim \textrm{Gamma}(k, \theta). $$

Probability density function
The probability density function of the gamma distribution can be expressed in terms of the gamma function:


 * $$ f(x;k,\theta) = x^{k-1} \frac{e^{-x/\theta}}{\theta^k \, \Gamma(k)}

\ \mathrm{ for }\ x > 0\,\, \mathrm{ and }\,\, k, \theta > 0.$$

(This parameterization is used in the infobox and the plots.)

Alternatively, the gamma distribution can be parameterized in terms of a shape parameter $$\alpha = k$$ and an inverse scale parameter $$\beta = 1/\theta$$, called a rate parameter:


 * $$ g(x;\alpha,\beta) = x^{\alpha-1} \frac{\beta^{\alpha} \, e^{-\beta\,x} }{\Gamma(\alpha)}  \ \mathrm{for}\ x > 0 \,\!.$$


 * If $$\alpha$$ is a positive integer, then $$ {\Gamma(\alpha)}=(\alpha - 1)! $$

Both parameterizations are common because either can be more convenient depending on the situation.

Cumulative distribution function
The cumulative distribution function can be expressed in terms of the incomplete gamma function,


 * $$ F(x;mk,\theta) = \int_0^x f(u;mk,\theta)\,du

=\frac{\gamma(k, x/\theta)}{\Gamma(k)} \,\!$$

Summation
If Xi has a &Gamma;(&alpha;i, &beta;) distribution for i = 1, 2, ..., N, then



\sum_{i=1}m^N X_i \sim \Gamma \left( \sum_{i=1}^N \alpha_i, \beta \right) \,\! $$

provided all Xi are independent.

The gamma distribution exhibits infinite divisibility.

Scaling
For any t > 0 it holds that tX is distributed &Gamma;(k, t&theta;), demonstrating that &theta; is a scale parameter.

Exponential family
The Gamma distribution is a two-parameter exponential family with natural parameters $$k-1$$ and $$1/\theta$$, and natural statistics $$X$$ and $$\ln(X)$$.

Information entropy
The information entropy is given by


 * $$\frac{-1}{\theta^k \Gamma(k)} \int_0^{\infty} \frac{x^{k-1}}{e^{x/\theta}} \left[ (k-1)\ln x - x/\theta - k \ln\theta - \ln\Gamma(k) \right] \,dx \!$$


 * $$= -\left[ (k-1) (\ln\theta + \psi(k)) - k - k \ln\theta - \ln\Gamma(k) \right] \!$$


 * $$= k + \ln\theta + \ln\Gamma(k) + (1-k)\psi(k) \!$$

where &psi;(k) is the digamma function.

Kullback-Leibler divergence
The directed Kullback-Leibler divergence between &Gamma;(&alpha;0, &beta;0) ('true' distribution) and &Gamma;(&alpha;, &beta;) ('approximating' distribution) is given by



D_{\mathrm{KL}}(\alpha,\beta || \alpha_0, \beta_0) = \log\left(\frac{\Gamma({\alpha_0})\beta_0^{\alpha_0}}{\Gamma(\alpha)\beta^{\alpha_0}}\right)+(\alpha-{\alpha_0})\psi(\alpha)+\alpha\frac{\beta-\beta_0}{\beta_0} $$

Laplace Transform
The Laplace transformation of the gamma distribution is



F(s)=\frac{\beta^\alpha}{(s+\beta)^\alpha} $$

Maximum likelihood estimation
The likelihood function for N iid observations $$(x_1,\ldots,x_N)$$ is


 * $$L(\theta)=\prod_{i=1}^N f(x_i;k,\theta)\,\!$$

from which we calculate the log-likelihood function


 * $$\ell(\theta) = (k-1) \sum_{i=1}^N \ln{(x_i)} - \sum x_i/\theta - Nk\ln{(\theta)} - N\ln{\Gamma(k)}.$$

Finding the maximum with respect to $$\theta$$ by taking the derivative and setting it equal to zero yields the maximum likelihood estimate of the &theta; 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)} \,\!$$

where


 * $$\psi(k) = \frac{\Gamma'(k)}{\Gamma(k)} \!$$

is the digamma function.

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}{k}\left(\frac{1}{2} + \frac{1}{12k+2}\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 given by Choi and Wette (1969) as the following expression:


 * $$k \leftarrow k - \frac{ \ln k - \psi\left(k\right) - s }{ 1/k - \psi'\left(k\right) }$$

where $$\psi'\left(\cdot\right)$$ denotes the trigamma function (the derivative of the digamma function).

The digamma and trigamma functions can be difficult to calculate with high precision. However, approximations known to be good to several significant figures can be computed using the following approximation formulae:



\psi\left(k\right) = \begin{cases} \ln(k) - ( 1 + ( 1 - ( 1/10 - 1 / ( 21 k^2 ) ) / k^2 ) / ( 6 k ) ) / ( 2 k ), \quad k \geq 8 \\ \psi\left( k + 1 \right) - 1/k, \quad k < 8 \end{cases} $$

and



\psi'\left(k\right) = \begin{cases} ( 1 + ( 1 + ( 1 - ( 1/5 - 1 / ( 7 k^2 ) ) / k^2 ) / ( 3 k ) ) / ( 2 k ) ) / k, \quad k \geq 8, \\ \psi'\left( k + 1 \right) + 1/k^2, \quad k < 8. \end{cases} $$

For details, see Choi and Wette (1969).

Bayesian minimum mean-squared error
With known k and unknown $$\theta$$, the posterior PDF for theta (using the standard scale-invariant prior for $$\theta$$) is



P(\theta | k, x_1, ..., x_N) \propto 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^{-N k-1} e^{-y / \theta}. \! $$

Integration over &theta; can be carried out using a change of variables, revealing that 1/&theta; is gamma-distributed with parameters $$\scriptstyle \alpha = N k,\ \ \beta = y$$.



\int_0^{\infty} \theta^{-N k-1+m} e^{-y / \theta}\, d\theta = \int_0^{\infty} x^{N k -1 -m} e^{-x y} \, dx = y^{-(N k -m)} \Gamma(N k -m). \! $$

The moments can be computed by taking the ratio (m by m = 0)



E(x^m) = \frac {\Gamma (N k -m)} {\Gamma(N k)} y^m, \! $$

which shows that the mean +/- standard deviation estimate of the posterior distribution for theta is


 * $$ \frac {y} {N k -1}$$ +/- $$\frac {y^2} {(N k-1)^2 (N k-2)}. $$

Generating gamma-distributed random variables
Given the scaling property above, it is enough to generate gamma variables with &beta; = 1 as we can later convert to any value of &beta; 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 &minus;ln(U) is distributed &Gamma;(1, 1). Now, using the "&alpha;-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;(&delta;, 1) for 0 < &delta; < 1 and apply the "&alpha;-addition" property once more. This is the most difficult part.

We provide an algorithm without proof. It is an instance of the acceptance-rejection method:

Now, to summarize,
 * 1) Let m be 1.
 * 2) Generate $$V_{3m - 2}$$, $$V_{3m - 1}$$ and $$V_{3m}$$ &mdash; independent uniformly distributed on (0, 1 ] variables.
 * 3) If $$V_{3m - 2} \le 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 $$\xi = \xi_m$$ to be the realization of $$\Gamma  (\delta, 1)$$


 * $$ \theta \left( \xi - \sum _{i=1} ^{[k]} {\ln U_i} \right) \sim \Gamma (k, \theta),$$

where [k] is the integral part of k, and &xi; has been generated using the algorithm above with &delta; = {k} (the fractional part of k), Uk and Vl are distributed as explained above and are all independent.

The GNU Scientific Library (which has ports for Visual Studio) has robust routines for sampling many distributions including the Gamma distribution.

Specializations

 * If $$X \sim {\Gamma}(k=1, \theta=1/\lambda)\,$$, then X has an exponential distribution with rate parameter &lambda;.
 * If $$X \sim {\Gamma}(k=v/2, \theta=2)\,$$, then X is identical to &chi;2(&nu;), the chi-square distribution with &nu; degrees of freedom.
 * If $$k$$ is an integer, the gamma distribution is an Erlang distribution and is the probability distribution of the waiting time until the $$k$$-th "arrival" in a one-dimensional Poisson process with intensity 1/&theta;.
 * If $$X^2 \sim {\Gamma}(3/2, 2a^2)\,$$, then X has a Maxwell-Boltzmann distribution with parameter a.
 * $$X \sim \mathrm{SkewLogistic}(\theta)\,$$, then $$\mathrm{log}(1 + e^{-X}) \sim \Gamma (1,\theta)\,$$

Others

 * If X has a &Gamma;(k, &theta;) distribution, then 1/X has an inverse-gamma distribution with parameters k and &theta;-1.
 * If X and Y are independently distributed &Gamma;(&alpha;, &theta;) and &Gamma;(&beta;, &theta;) respectively, then X / (X + Y) has a beta distribution with parameters &alpha; and &beta;.
 * If Xi are independently distributed &Gamma;(&alpha;i,&theta;) respectively, then the vector (X1 / S, ..., Xn / S), where S = X1 + ... + Xn, follows a Dirichlet distribution with parameters &alpha;1, ..., &alpha;n.