# Gamma distributions

October 14, 2019 — July 29, 2024

Lévy processes
probability
stochastic processes
time series

$\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 .

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 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 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 and Lognormal distributions. Those Thorin papers introduced the idea originally; possibly it is easy to start from one of the textbooks or overviews .

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 Parameter estimation

The method of moments is obvious. The Maximum likelihood version is surprisingly fiddly and has no clsoed form, but a low-bias closed-form approximation is given by Ye and Chen (2017). Wikipedia’s summary:

The estimate for the shape $$k$$ is

$\hat{k} = \frac{N \sum_{i=1}^N x_i}{N \sum_{i=1}^N x_i \ln x_i - \sum_{i=1}^N x_i \sum_{i=1}^N \ln x_i}$

and the estimate for the scale $$\theta$$ is

$\hat{\theta} = \frac{1}{N^2} \left(N \sum_{i=1}^N x_i \ln x_i - \sum_{i=1}^N x_i \sum_{i=1}^N \ln x_i\right)$

Using the sample mean of $$x$$, the sample mean of $$\ln x$$, and the sample mean of the product $$x\ln x$$ simplifies the expressions to:

$\hat{k} = \bar{x} / \hat{\theta}$ $\hat{\theta} = \overline{x\ln{x}} - \bar{x} \overline{\ln{x}}.$

If the rate parameterization is used, the estimate of $$\hat{\beta} = 1/\hat{\theta}$$.

These estimators are not strictly maximum likelihood estimators, but are instead referred to as mixed type log-moment estimators. They have however similar efficiency as the maximum likelihood estimators.

Although these estimators are consistent, they have a small bias. A bias-corrected variant of the estimator for the scale $$\theta$$ is

$\tilde{\theta} = \frac{N}{N - 1} \hat{\theta}$

A bias correction for the shape parameter $$k$$ is given as

$\tilde{k} = \hat{k} - \frac{1}{N} \left(3 \hat{k} - \frac{2}{3} \left(\frac{\hat{k}}{1 + \hat{k}}\right) - \frac{4}{5} \frac{\hat{k}}{(1 + \hat{k})^2} \right)$

A Bayesian update is given with a similar form in Louzada and Ramos (2018) but they have a slightly different parameterisation, so I will need to come back to that when I have time to translate that.

## 8 Simulating Gamma variates

### 8.1 Univariate

A Gamma variate can be generated by many methods , e.g. a transformed normal and a uniform random variable , 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{z} \rv{x}$$

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

## 9 References

Ahrens, and Dieter. 1974. Computing.
———. 1982. Communications of the ACM.
Barndorff-Nielsen, Maejima, and Sato. 2006. Bernoulli.
Bondesson. 1979. The Annals of Probability.
———. 2012. Generalized Gamma Convolutions and Related Classes of Distributions and Densities. Lecture Notes in Statistics 76.
Damsleth. 1975. Scandinavian Journal of Statistics.
Devroye. 1986. Non-uniform random variate generation.
Dufresne. 1998. Advances in Applied Mathematics.
Fink. 1997.
James, Roynette, and Yor. 2008. Probability Surveys.
Lin. 2016. “On The Dirichlet Distribution.”
Louzada, and Ramos. 2018. Journal of Statistical Computation and Simulation.
Louzada, Ramos, and Ramos. 2019. The American Statistician.
Mathai. 1982. Annals of the Institute of Statistical Mathematics.
Miller. 1980. Technometrics.
Morris. 1982. The Annals of Statistics.
———. 1983. The Annals of Statistics.
Morris, and Lock. 2009. The American Statistician.
Moschopoulos. 1985. Annals of the Institute of Statistical Mathematics.
Pérez-Abreu, and Stelzer. 2014. Journal of Multivariate Analysis.
Rezende, Mohamed, and Wierstra. 2015. In Proceedings of ICML.
Roychowdhury, and Kulis. 2015. In Artificial Intelligence and Statistics.
Steutel, and van Harn. 2003. Infinite Divisibility of Probability Distributions on the Real Line.
Thorin. 1977a. Scandinavian Actuarial Journal.
———. 1977b. Scandinavian Actuarial Journal.
Ye, and Chen. 2017. The American Statistician.
Yor. 2007. In Advances in Mathematical Finance. Applied and Numerical Harmonic Analysis.