Gamma distributions

October 14, 2019 — July 8, 2024

Lévy processes
probability
stochastic processes
time series
Figure 1: The gammer distribution is a popular model for survival times.

\[\renewcommand{\var}{\operatorname{Var}} \renewcommand{\corr}{\operatorname{Corr}} \renewcommand{\dd}{\mathrm{d}} \renewcommand{\bb}[1]{\mathbb{#1}} \renewcommand{\vv}[1]{\boldsymbol{#1}} \renewcommand{\rv}[1]{\mathsf{#1}} \renewcommand{\vrv}[1]{\vv{\rv{#1}}} \renewcommand{\disteq}{\stackrel{d}{=}} \renewcommand{\gvn}{\mid} \renewcommand{\Ex}{\mathbb{E}} \renewcommand{\Pr}{\mathbb{P}}\]

The density \(g(x|\alpha, \lambda )\) of the univariate Gamma is \[ g(x| \alpha, \lambda)= \frac{ \lambda^{\alpha} }{ \Gamma (\alpha) } x^{\alpha\,-\,1}e^{-\lambda x}, x\geq 0. \] People refer to this as the shape-rate parameterisation, with rate \(\lambda\) and shape \(\alpha,\). I find it easier to remember as \[ \log g(x| \alpha, \lambda)= \alpha \log \lambda + ({\alpha{-}1})\log x -\lambda x -\log \Gamma (\alpha) \]

If \(\rv{x}\sim \operatorname{Gamma}(\alpha, \lambda)\) then \(\bb E(\rv{x})=\alpha/\lambda\) and \(\var(\rv{x})=\alpha/\lambda^2.\)

The Gamma distribution has lots of neat properties, such as divisibility. Some more are outlined in the Gamma-Dirichlet algebra section.

1 All moments

The moment generating function of the Gamma distribution is \[\Ex[\exp(\rv{x} s)]=\left(1-{\frac {s}{\lambda }}\right)^{-\alpha }{\text{ for }}s<\lambda\] which gives us expressions for all moments fairly easily: \[\begin{aligned} \Ex[\rv{x}]&=\left.\frac{\dd}{\dd s}\Ex[\exp(\rv{x} t)]\right|_{s=0}\\ &=\left.\frac{\dd}{\dd s}\left(1-{\frac {s}{\lambda }}\right)^{-\alpha }\right|_{s=0}\\ &=\left.\frac{\alpha}{\lambda}\left(1-{\frac {s}{\lambda }}\right)^{-\alpha -1}\right|_{s=0}\\ &=\alpha/\lambda\\ \Ex[\rv{x}^2] &=\left.\frac{\dd}{\dd s}\frac{\alpha}{\lambda}\left(1-{\frac {s}{\lambda }}\right)^{-\alpha -1}\right|_{s=0}\\ &=\left.\frac{\alpha^2+\alpha}{\lambda^2}\left(1-{\frac {s}{\lambda }}\right)^{-\alpha -2}\right|_{s=0}\\ &=\frac{\alpha(\alpha+1)}{\lambda^2}\\ \var[\rv{x}] &=\frac{\alpha}{\lambda^2} \quad\text{(handy later)}\\ \var[\rv{x}^2] &=\frac{\Gamma (\alpha) \Gamma(\alpha+4)-\Gamma^2 (\alpha+2)}{\Gamma^2 (\alpha)\lambda ^4 }\quad\text{(handy later)}\\ \Ex[\rv{x}^3] &=\left.\frac{\dd}{\dd s}\frac{\alpha^2+\alpha}{\lambda^2}\left(1-{\frac {s}{\lambda }}\right)^{-\alpha -2}\right|_{s=0}\\ &=\frac{\alpha(\alpha+1)(\alpha+2)}{\lambda^3}\left(1-{\frac {s}{\lambda }}\right)^{-\alpha -2}\\ &=\frac{\alpha(\alpha+1)(\alpha+2)}{\lambda^3}\\ \Ex[\rv{x}^4] &=\frac{\alpha(\alpha+1)(\alpha+2)(\alpha+3)}{\lambda^4}\\ &\dots\\ \Ex[\rv{x}^n] &=\frac{\langle \alpha \rangle_{n}}{\lambda^n}\\ \end{aligned} \]

Here \(\langle \alpha \rangle_{n}:=\frac{\Gamma(\alpha+n)}{\Gamma(\alpha)}\) is the rising factorial.

2 As exponential family

The gamma distribution is a two-parameter exponential family with natural parameters \([\theta_1, \theta_2]=[\alpha − 1,−\lambda]^\top\), and natural statistics \([x,\ln(x)]^\top.\) In particular, it is a natural exponential family with quadratic variance function (Morris 1983).

The log-partition function \(A(\theta)\) is given by: \[ A(\theta)=-\left(\theta_1+1\right) \log \left(-\theta_2\right)+\log \Gamma\left(\theta_1+1\right) \] So in canonical form the density is \(f\left(x ; \theta_1, \theta_2\right)=\frac{1}{x} \exp \left(\theta_1 \log (x)+\theta_2 x-A(\theta)\right).\)

In terms of the natural parameters, the mean and variance are \[ \begin{aligned} \Ex[\rv{x}]&=\frac{\theta_1+1}{-\theta_2}\\ \var[\rv{x}]&=\frac{\theta_1+1}{\theta_2^2}. \end{aligned} \]

2.1 Tempering

A tempered version of the density behaves how we would hope, as an exponential family. Lazily writing \(f ^\tau\) for the tempered density (which the pedantic might argue skips the normalizing constant), we have \[ \begin{aligned} f\left(x ; \theta_1, \theta_2\right)^{\tau} &=\left(\frac{1}{x} \exp \left(\theta_1 \log (x)+\theta_2 x-A(\theta)\right)^{\tau}\right)\\ &\propto\left(\frac{1}{x} \exp \left(\tau \theta_1 \log (x)+\tau\theta_2 x\right)\right)\\ &=f\left(x ; \tau\theta_1, \tau\theta_2\right). \end{aligned} \] No surprises there. How this acts on the moments is the different than e.g. Gaussians \[ \begin{aligned} \Ex_{\rv{x}\sim f^{\tau}}[\rv{x}] &=\frac{\tau\theta_1+1}{-\tau\theta_2}\\ &=\frac{\theta_1}{-\theta_2}+\frac{1}{-\tau\theta_2}\\ &=\Ex_{\rv{x}\sim f}[\rv{x}]-\frac{1}{\tau\theta_2}\\ \var_{\rv{x}\sim f^{\tau}}[\rv{x}^\tau] &=\frac{\tau\theta_1+1}{\tau^2\theta_2^2}\\ &=\frac{1}{\tau}\frac{\tau\theta_1+1}{\tau\theta_2^2}\\ &=\frac{1}{\tau}\Ex_{\rv{x}\sim f^{\tau}}[\rv{x}]. \end{aligned} \]

So if we weight the density down by \(\tau<1\), or equivalently, increase its temperature \(1/\tau\), the mean decreases and the relative variance increases.

3 Linear combinations of Gammas

Is the Gamma family closed under addition? For a fixed scale/rate parameters, yes. See Gamma-Dirichlet algebra.

If we are summing gamma variates which differ in the rate parameter \(\lambda\), the result is not in general Gamma distributed. It can be compactly expressed in terms of Gamma densities, for what that is worth. If we wish to know the distribution of the sum of a set of arbitrary Gamma random variables with different rate parameters, we can try to approximate the sum with moment matching (Mathai 1982; Moschopoulos 1985) if that feel worthwhile.

Note that multiplying a gamma RV by a scalar changes the rate, so gamma variates are not close under affine combination as Gaussians ones are. The moral is that we cannot assume the convenient algebra of linear combination additivity as in the Gaussian process community. What kind of algebra do we get?

4 Gamma-Dirichlet algebra

There are various operations which give us similar conveniences, however. For those, we need to also be aware of the Dirichlet and Beta distributions. Here are some useful properties, drawn from, or extrapolated from, Dufresne (1998), Lin (2016), and Pérez-Abreu and Stelzer (2014) which use those properties.

First, we fix some notation. From here on, all variables denoted \(\rv{x}_{a}\) (for some \(a>0\), with or without superscript) have a \(\operatorname{Gamma}(a, 1)\) distribution; all variables denoted \(\rv{b}_{a, b}\) (for some \(a, b>0\), with or without superscript) have a \(\operatorname{Beta}_{a, b}\) distribution. In all expressions, the variables \(\rv{x}_{a_{1}}, \rv{x}_{a_{2}}', \ldots, \rv{b}_{a_{1}, b_{1}}, \rv{b}_{a_{2}, b_{2}}', \ldots\) are independent unless I say otherwise.

4.1 Superposition

\(\rv{x}_{\alpha_1}+\rv{x}_{\alpha_2}\sim \operatorname{Gamma}(\alpha_1+\alpha_2, \lambda)\)

4.2 Multiplication

If \(\rv{x}\sim \operatorname{Gamma}(\alpha, \lambda)\) then \(c \rv{x}\sim \operatorname{Gamma}(\alpha, \lambda/c).\) This looks useful but in practice few constructions I handle vary \(\lambda\).

4.3 Beta Thinning

\(\frac{\rv{x}_{\alpha_1}}{\rv{x}_{\alpha_1}+\rv{x}_{\alpha_2}}\sim \operatorname{Beta}(\alpha_1, \alpha_2)\) independent of \(\rv{x}_{\alpha_1}+\rv{x}_{\alpha_2}.\) Equivalently, \(\rv{x}_{\alpha_1+\alpha_2} \rv{b}_{\alpha_1,\alpha_2}\sim\operatorname{Gamma}(\alpha_1)\) independent of \(\rv{b}_{\alpha_1,\alpha_2}.\)

The Gamma-bridge construction arises from this thinning procedure.

4.4 Dirichlet thinning

Grab a set of independent Gamma rvs, \(\{\rv{x}_{a_i}\}_{i=1,\dots,k}\), and define \(\rv{s}=\sum_i\rv{x}_{a_i}.\) We know that \(\rv{s}\sim\operatorname{Gamma}(\sum_i a_i,1).\) But wait! There is more. Define \[ \rv{d}_i=\frac{\rv{x}_{a_i}}{\rv{s}}. \] The \(\rv{d}_i\sim\operatorname{Dirichlet}(\vv{\alpha})\), independently of \(\rv{s}.\)

Conversely, take some arbitrary \(\rv{x}_{A}\) and some \(\vrv{d}\sim\operatorname{Dirichlet}(\vv{a})\) with \(\sum_i a_i=B.\) Then \(\rv{x}_{A}\vrv{d}_i\sim \operatorname{Gamma}(\frac{Aa_i}{B}, \lambda),\) and also the random variates \(\rv{x}_{A}\vrv{d}_i, i=1,\dots,k\) are jointly independent.

4.5 Beta thickening

Grab a set of independent Gamma rvs, \(\{\rv{x}_{a_i}\}_{i=1,\dots,k}.\) If we take a set of Beta rvs with arbitrary dependence, \(\{\rv{b}_{\kappa_i,a_i-\kappa_i}\}_{i=1,\dots,k}\) then the product rvs \(\{(\rv{x}_{a_i}\rv{b}_{\kappa_i,a_i-\kappa_i})\sim \operatorname{Gamma}(\kappa_i,\lambda)\}_{i=1,\dots,k}\) are jointly independent Gamma variates. Thus \(\sum_i\rv{x}_{a_i}\rv{b}_{\kappa_i,a_i-\kappa_i}\sim \operatorname{Gamma}(\sum_i \kappa_i,\lambda).\) As a special case, if \(\kappa_i\equiv \kappa,\) then \(\sum_i\rv{x}_{a_i}\rv{b}_{\kappa_i,a_i-\kappa_i}\sim \operatorname{Gamma}(\sum_i \kappa_i,\lambda).\)

TODO: Check this. Also, is it actually useful? I thought it was for coupling Gamma processes, but it turned out not to be necessary in my construction.

4.6 Other

There are many other nice properties and relations.

The properties I include in this section fail to define a formal algebraic structure, but they do define a bunch of operations that preserve membership of a certain distributional family, or pretty close to. 差不多. We can define the sets and operations if we really need an algebra.

Thematically, the operations that arise most often in this Gamma-“algebra” are not quite the same as in the Gaussian process “algebra”. In that case we are usually concerned with linear algebras in that many linear operations on many objects which are Gaussian in a very broad sense still end up being Gaussian and possessed of a closed-form solution. In this case we are mostly concerned with different operations, addition yes, but also thinning (Steutel and van Harn 2003) rather than multiplication.

Yor (2007) talks about the Gamma-Beta algebra of Dufresne (1998) which relates certain Markov chains of Gamma distribution and Beta distributions. Dufresne (1998)’s construction is a formal algebra, although one that I only pull a couple of trivial cases from. Read that paper for more than the following taster:

For any \(w, x, y, z>0\), \[ \rv{b}_{w, x} \rv{x}_{y}+\rv{x}_{z}' \disteq \rv{x}_{y+z}^{\prime \prime}\left(1-\rv{b}_{x, w} \rv{b}_{y, z}'\right) . \] In particular, for any \(w, x, y>0\), \[ \rv{b}_{w, x+y} \rv{x}_{x}+\rv{x}_{y}'=\rv{x}_{x+y} \rv{b}_{w+y, x} \disteq \rv{x}_{w+y} \rv{x}_{x+y, w} . \]

For more, see the Gamma-Beta notebook.

5 Conjugate prior for

Fink (1997) summarizes Miller (1980), which extends Damsleth (1975):

Suppose that data \(x_1, \ldots, x_n\) are independent and identically distributed from a gamma process where both the shape, \(\alpha\), and the reciprocal scale, \(\beta\), parameters are unknown. The likelihood function, \(L\left(\alpha, \beta \mid x_1, \ldots x_n\right)\), proportional to the parameters, is \[ \begin{aligned} & L\left(\alpha, \beta \mid x_1, \ldots, x_n\right) \propto\left\{\begin{array}{cc} \frac{P^{\alpha-1} \exp (-\beta S)}{\left(\Gamma(\alpha) \beta^{-\alpha}\right)^n} & \text { where } \quad x_i>0, i=1 \ldots n \\ 0 & \text { otherwise } \end{array}\right. \\ & \text { where } \quad S=\sum_{i=1}^n x_i \quad P=\prod_{i=1}^n x_i . \end{aligned} \]

The sufficient statistics are \(n\), the number of data points, \(P\), the product of the data, and \(S\), the sum of the data. The factors of [the equation] proportional to parameters \(\alpha\) and \(\beta\) make up the kernel of the conjugate prior, \(\pi(\cdot, \cdot)\). We specify the conjugate prior with hyperparameters \(p, q, r, s>0\) \[ \pi(\alpha, \beta \mid p, q, r, s)=\left\{\begin{array}{cc} \frac{1}{K} \frac{p^{\alpha-1} \exp (-\beta q)}{\Gamma(\alpha)^r \beta^{-\alpha s}} & \text { where } \quad \alpha, \beta>0 \\ 0 & \text { otherwise. } \end{array}\right. \]

[…] The posterior joint distribution of \(\alpha\) and \(\beta\) is specified by the hyperparameters \[ p^{\prime}=p P \quad q^{\prime}=q+S \quad r^{\prime}=r+n \quad s^{\prime}=s+n . \]

From this we find the predictive, \[ \begin{aligned} &L\left(x|\alpha, \beta \right) \pi(\alpha, \beta \mid p, q, r, s) \mathrm{d}\alpha \mathrm{d}\beta \\ &\propto \iint \frac{x^{\alpha-1} \exp (-\beta x)}{\Gamma(\alpha) \beta^{-\alpha}} \frac{p^{\alpha-1} \exp (-\beta q)}{\Gamma(\alpha)^r \beta^{-\alpha s}}\mathrm{d}\alpha \mathrm{d}\beta \\ % &\propto \iint \frac{P^{\alpha-1} p^{\alpha-1} \exp -\beta S \exp (-\beta q)}{\Gamma(\alpha)^r \beta^{-\alpha s}\left(\Gamma(\alpha) \beta^{-\alpha}\right)^n}\mathrm{d}\alpha \mathrm{d}\beta\\ &\propto \iint \frac{(x p)^{\alpha-1} \exp -\beta (x+q)}{\Gamma(\alpha)^{r+1} \beta^{-\alpha (x+n)}}\mathrm{d}\alpha \mathrm{d}\beta \end{aligned} \] The predictive is reasonably intractable e.g. because there is nothing obviously nice to do with the \(1/\Gamma(\alpha)\) term; even estimating the mean requires approximations.

Maybe if you really want to have a good conjugate prior relation for a non-negative variate we should consider something a little (but only a little) less messy, such as [inverse Gaussian] (./inverse_gaussian_distribution.qmd) or lognormal distributions.

6 Generalized Gamma Convolution

As noted under divisible distributions, the class of Generalized Gamma Convolution (GGC) is a construction that represents some startling (to me) processes as a certain type of generalization of Gamma distributions. This family includes Pareto (Thorin 1977a) and Lognormal (Thorin 1977b) distributions. Those Thorin papers introduced the idea originally; possibly it is easy to start from one of the textbooks or overviews (Bondesson 2012; James, Roynette, and Yor 2008; Steutel and van Harn 2003; Barndorff-Nielsen, Maejima, and Sato 2006).

AFAICT this allows us to prove lots of nice things about such distributions. It is less easy to get implementable computational methods this way.

The GGC convolves a Gamma distribution with some measure and makes a new divisible distribution. James, Roynette, and Yor (2008):

we say that a positive r.v. \(\Gamma\) is a generalized gamma convolution \((\mathrm{GGC})\) - … if there exists a positive Radon measure \(\mu\) on \(] 0, \infty[\) such that: \[ \begin{aligned} E\left[e^{-\lambda \Gamma}\right]=& \exp \left\{-\int_{0}^{\infty}\left(1-e^{-\lambda x}\right) \frac{d x}{x} \int_{0}^{\infty} e^{-x z} \mu(d z)\right\} \\ =& \exp \left\{-\int_{0}^{\infty} \log \left(1+\frac{\lambda}{z}\right) \mu(d z)\right\} \\ \text { with: } & \int_{] 0,1]}|\log x| \mu(d x)<\infty \text { and } \int_{[1, \infty[} \frac{\mu(d x)}{x}<\infty. \end{aligned} \]

Barndorff-Nielsen, Maejima, and Sato (2006) and Pérez-Abreu and Stelzer (2014) generalize the GGC to vector- and matrix-valued distributions.

7 Simulating Gamma variates

7.1 Univariate

A Gamma variate can be generated by many methods (Ahrens and Dieter 1974), e.g. a transformed normal and a uniform random variable (Ahrens and Dieter 1982), or two uniforms, depending on the parameter range. Most methods involve a rejection step. Here is Devroye (1986) summary for beta generators for \(0 < a, b\leq 1\) and Gamma generators for \(a<1\):

Johnk’s beta generator

REPEAT Generate iid uniform [0,1] random variates \(\rv{u}, \rv{v}\)

\[ \begin{array}{c} \rv{x} \leftarrow \rv{u}^{\frac{1}{a}}, \rv{y}-\rv{v}^{\frac{1}{b}} \\ \text { UNTL } \rv{x}+\rv{y} \leq 1 \end{array} \]

RETURN \(\frac{\rv{x}}{\rv{x}+\rv{y}}\)

\((\rv{x} \text { is beta }(a, b) \text { distributed })\)

Berman’s beta generator

REPEAT Generate iid uniform [0,1] random variates \(\rv{u}, \rv{v}\)

\[ \begin{array}{c} \rv{x} \leftarrow \rv{u}^{\frac{1}{4}}, \rv{y} \leftarrow \rv{v}^{\frac{1}{b}} \\ \text { UNTIL } \rv{x}+\rv{y} \leq \mathbf{1} \end{array} \]

RETURN \(\rv{x}\)

\((\rv{x} \text { is beta }(a, b+1) \text { distributed })\)

Johnk’s gamma generator

REPEAT Generate iid uniform [0,1] random variates \(\rv{u}, \rv{v}\)

\[ \begin{array}{r} \rv{x}+\rv{u}^{\frac{1}{a}}, \rv{y} \leftarrow \rv{v}^{\frac{1}{1-a}} \\ \text { UNTL } \rv{x}+\rv{y} \leq 1 \end{array} \]

Generate an exponential random variate \(E\) RETURN \(\frac{E \rv{x}}{\rv{x}+\rv{y}}\)

\((\rv{x} \text { is gamma ( } a\) ) distributed)

Berman’s gamma generator

REPEAT Generate iid uniform [0,1] random variates \(\rv{u}, \rv{v}\)

\[ \begin{array}{r} \rv{x} \leftarrow \rv{u}^{\frac{1}{4}}, \rv{y} \leftarrow \rv{v}^{\frac{1}{1-a}} \\ \text { UNTL } \rv{x}+\rv{y} \leq 1 \end{array} \]

Generate a gamma ( 2 ) random variate \(\rv{z}\) (either as the sum of two iid exponential random variates or as \(-\log (\rv{u} * \rv{v} *)\) where \(\rv{u} *, \rv{v} *\) are lid uniform [0,1] random variates

RETURN $ $

\((\rv{x} \text { is gamma }(a) \text { distributed })\)

8 References

Ahrens, and Dieter. 1974. Computer Methods for Sampling from Gamma, Beta, Poisson and Bionomial Distributions.” Computing.
———. 1982. Generating Gamma Variates by a Modified Rejection Technique.” Communications of the ACM.
Barndorff-Nielsen, Maejima, and Sato. 2006. Some Classes of Multivariate Infinitely Divisible Distributions Admitting Stochastic Integral Representations.” Bernoulli.
Bondesson. 1979. A General Result on Infinite Divisibility.” The Annals of Probability.
———. 2012. Generalized Gamma Convolutions and Related Classes of Distributions and Densities. Lecture Notes in Statistics 76.
Damsleth. 1975. Conjugate Classes for Gamma Distributions.” Scandinavian Journal of Statistics.
Devroye. 1986. Non-uniform random variate generation.
Dufresne. 1998. Algebraic Properties of Beta and Gamma Distributions, and Applications.” Advances in Applied Mathematics.
Fink. 1997. A Compendium of Conjugate Priors.”
James, Roynette, and Yor. 2008. Generalized Gamma Convolutions, Dirichlet Means, Thorin Measures, with Explicit Examples.” Probability Surveys.
Lin. 2016. “On The Dirichlet Distribution.”
Mathai. 1982. Storage Capacity of a Dam with Gamma Type Inputs.” Annals of the Institute of Statistical Mathematics.
Miller. 1980. Bayesian Analysis of the Two-Parameter Gamma Distribution.” Technometrics.
Morris. 1982. Natural Exponential Families with Quadratic Variance Functions.” The Annals of Statistics.
———. 1983. Natural Exponential Families with Quadratic Variance Functions: Statistical Theory.” The Annals of Statistics.
Morris, and Lock. 2009. Unifying the Named Natural Exponential Families and Their Relatives.” The American Statistician.
Moschopoulos. 1985. The Distribution of the Sum of Independent Gamma Random Variables.” Annals of the Institute of Statistical Mathematics.
Pérez-Abreu, and Stelzer. 2014. Infinitely Divisible Multivariate and Matrix Gamma Distributions.” Journal of Multivariate Analysis.
Rezende, Mohamed, and Wierstra. 2015. Stochastic Backpropagation and Approximate Inference in Deep Generative Models.” In Proceedings of ICML.
Roychowdhury, and Kulis. 2015. Gamma Processes, Stick-Breaking, and Variational Inference.” In Artificial Intelligence and Statistics.
Steutel, and van Harn. 2003. Infinite Divisibility of Probability Distributions on the Real Line.
Thorin. 1977a. On the Infinite Divisbility of the Pareto Distribution.” Scandinavian Actuarial Journal.
———. 1977b. On the Infinite Divisibility of the Lognormal Distribution.” Scandinavian Actuarial Journal.
Yor. 2007. Some Remarkable Properties of Gamma Processes.” In Advances in Mathematical Finance. Applied and Numerical Harmonic Analysis.