# Normal-gamma distribution

In probability theory and statistics, the normal-gamma distribution (or Gaussian-gamma distribution) is a bivariate four-parameter family of continuous probability distributions. It is the conjugate prior of a normal distribution with unknown mean and precision.

## Definition

For a pair of random variables, (X,T), suppose that the conditional distribution of X given T is given by
    $X\mid T\sim N(\mu ,1/(\lambda T))\,\!,$
meaning that the conditional distribution is a normal distribution with mean $\mu$ and precision $\lambda T$ — equivalently, with variance $1/(\lambda T).$
Suppose also that the marginal distribution of T is given by
    $T\mid \alpha ,\beta \sim \operatorname {Gamma} (\alpha ,\beta ),$
where this means that T has a gamma distribution. Here λ, α and β are parameters of the joint distribution.
Then (X,T) has a normal-gamma distribution, and this is denoted by
    $(X,T)\sim \operatorname {NormalGamma} (\mu ,\lambda ,\alpha ,\beta ).$

## Properties
### Probability density function

The joint probability density function of (X,T) is
    $f(x,\tau \mid \mu ,\lambda ,\alpha ,\beta )={\frac {\beta ^{\alpha }{\sqrt {\lambda }}}{\Gamma (\alpha ){\sqrt {2\pi }}}}\,\tau ^{\alpha -{\frac {1}{2}}}\,e^{-\beta \tau }\exp \left(-{\frac {\lambda \tau (x-\mu )^{2}}{2}}\right),$
where the conditional probability for $f(x,\tau \mid \mu ,\lambda ,\alpha ,\beta )=f(x\mid \tau ,\mu ,\lambda ,\alpha ,\beta )f(\tau \mid \mu ,\lambda ,\alpha ,\beta )$ was used.

### Marginal distributions

By construction, the marginal distribution of $\tau$ is a gamma distribution, and the conditional distribution of $x$ given $\tau$ is a Gaussian distribution. The marginal distribution of $x$ is a three-parameter non-standardized Student's t-distribution with parameters $(\nu ,\mu ,\sigma ^{2})=(2\alpha ,\mu ,\beta /(\lambda \alpha ))$.

### Exponential family

The normal-gamma distribution is a four-parameter exponential family with natural parameters $\alpha -1/2,-\beta -\lambda \mu ^{2}/2,\lambda \mu ,-\lambda /2$ and natural statistics $\ln \tau ,\tau ,\tau x,\tau x^{2}$.

### Moments of the natural statistics

The following moments can be easily computed using the moment generating function of the sufficient statistic:
    $\operatorname {E} (\ln T)=\psi \left(\alpha \right)-\ln \beta ,$
where $\psi \left(\alpha \right)$ is the digamma function,
    ${\begin{aligned}\operatorname {E} (T)&={\frac {\alpha }{\beta }},\\[5pt]\operatorname {E} (TX)&=\mu {\frac {\alpha }{\beta }},\\[5pt]\operatorname {E} (TX^{2})&={\frac {1}{\lambda }}+\mu ^{2}{\frac {\alpha }{\beta }}.\end{aligned}}$

### Scaling

If $(X,T)\sim \mathrm {NormalGamma} (\mu ,\lambda ,\alpha ,\beta ),$ then for any $b>0,(bX,bT)$ is distributed as ${\rm {NormalGamma}}(b\mu ,\lambda /b^{3},\alpha ,\beta /b).$

## Posterior distribution of the parameters

Assume that x is distributed according to a normal distribution with unknown mean $\mu$ and precision $\tau$.
    $x\sim {\mathcal {N}}(\mu ,\tau ^{-1})$
and that the prior distribution on $\mu$ and $\tau$, $(\mu ,\tau )$, has a normal-gamma distribution
    $(\mu ,\tau )\sim {\text{NormalGamma}}(\mu _{0},\lambda _{0},\alpha _{0},\beta _{0}),$
for which the density π satisfies
    $\pi (\mu ,\tau )\propto \tau ^{\alpha _{0}-{\frac {1}{2}}}\,\exp[-\beta _{0}\tau ]\,\exp \left[-{\frac {\lambda _{0}\tau (\mu -\mu _{0})^{2}}{2}}\right].$
Suppose
    $x_{1},\ldots ,x_{n}\mid \mu ,\tau \sim \operatorname {{i.}{i.}{d.}} \operatorname {N} \left(\mu ,\tau ^{-1}\right),$
i.e. the components of $\mathbf {X} =(x_{1},\ldots ,x_{n})$ are conditionally independent given $\mu ,\tau$ and the conditional distribution of each of them given $\mu ,\tau$ is normal with expected value $\mu$ and variance $1/\tau .$ The posterior distribution of $\mu$ and $\tau$ given this dataset $\mathbb {X}$ can be analytically determined by Bayes' theorem explicitly,
    $\mathbf {P} (\tau ,\mu \mid \mathbf {X} )\propto \mathbf {L} (\mathbf {X} \mid \tau ,\mu )\pi (\tau ,\mu ),$
where $\mathbf {L}$ is the likelihood of the parameters given the data.
Since the data are i.i.d, the likelihood of the entire dataset is equal to the product of the likelihoods of the individual data samples:
    $\mathbf {L} (\mathbf {X} \mid \tau ,\mu )=\prod _{i=1}^{n}\mathbf {L} (x_{i}\mid \tau ,\mu ).$
This expression can be simplified as follows:
    ${\begin{aligned}\mathbf {L} (\mathbf {X} \mid \tau ,\mu )&\propto \prod _{i=1}^{n}\tau ^{1/2}\exp \left[{\frac {-\tau }{2}}(x_{i}-\mu )^{2}\right]\\[5pt]&\propto \tau ^{n/2}\exp \left[{\frac {-\tau }{2}}\sum _{i=1}^{n}(x_{i}-\mu )^{2}\right]\\[5pt]&\propto \tau ^{n/2}\exp \left[{\frac {-\tau }{2}}\sum _{i=1}^{n}(x_{i}-{\bar {x}}+{\bar {x}}-\mu )^{2}\right]\\[5pt]&\propto \tau ^{n/2}\exp \left[{\frac {-\tau }{2}}\sum _{i=1}^{n}\left((x_{i}-{\bar {x}})^{2}+({\bar {x}}-\mu )^{2}\right)\right]\\[5pt]&\propto \tau ^{n/2}\exp \left[{\frac {-\tau }{2}}\left(ns+n({\bar {x}}-\mu )^{2}\right)\right],\end{aligned}}$
where ${\bar {x}}={\frac {1}{n}}\sum _{i=1}^{n}x_{i}$, the mean of the data samples, and $s={\frac {1}{n}}\sum _{i=1}^{n}(x_{i}-{\bar {x}})^{2}$, the sample variance.
The posterior distribution of the parameters is proportional to the prior times the likelihood.
    ${\begin{aligned}\mathbf {P} (\tau ,\mu \mid \mathbf {X} )&\propto \mathbf {L} (\mathbf {X} \mid \tau ,\mu )\pi (\tau ,\mu )\\&\propto \tau ^{n/2}\exp \left[{\frac {-\tau }{2}}\left(ns+n({\bar {x}}-\mu )^{2}\right)\right]\tau ^{\alpha _{0}-{\frac {1}{2}}}\,\exp[{-\beta _{0}\tau }]\,\exp \left[-{\frac {\lambda _{0}\tau (\mu -\mu _{0})^{2}}{2}}\right]\\&\propto \tau ^{{\frac {n}{2}}+\alpha _{0}-{\frac {1}{2}}}\exp \left[-\tau \left({\frac {1}{2}}ns+\beta _{0}\right)\right]\exp \left[-{\frac {\tau }{2}}\left(\lambda _{0}(\mu -\mu _{0})^{2}+n({\bar {x}}-\mu )^{2}\right)\right]\end{aligned}}$
The final exponential term is simplified by completing the square.
    ${\begin{aligned}\lambda _{0}(\mu -\mu _{0})^{2}+n({\bar {x}}-\mu )^{2}&=\lambda _{0}\mu ^{2}-2\lambda _{0}\mu \mu _{0}+\lambda _{0}\mu _{0}^{2}+n\mu ^{2}-2n{\bar {x}}\mu +n{\bar {x}}^{2}\\&=(\lambda _{0}+n)\mu ^{2}-2(\lambda _{0}\mu _{0}+n{\bar {x}})\mu +\lambda _{0}\mu _{0}^{2}+n{\bar {x}}^{2}\\&=(\lambda _{0}+n)(\mu ^{2}-2{\frac {\lambda _{0}\mu _{0}+n{\bar {x}}}{\lambda _{0}+n}}\mu )+\lambda _{0}\mu _{0}^{2}+n{\bar {x}}^{2}\\&=(\lambda _{0}+n)\left(\mu -{\frac {\lambda _{0}\mu _{0}+n{\bar {x}}}{\lambda _{0}+n}}\right)^{2}+\lambda _{0}\mu _{0}^{2}+n{\bar {x}}^{2}-{\frac {\left(\lambda _{0}\mu _{0}+n{\bar {x}}\right)^{2}}{\lambda _{0}+n}}\\&=(\lambda _{0}+n)\left(\mu -{\frac {\lambda _{0}\mu _{0}+n{\bar {x}}}{\lambda _{0}+n}}\right)^{2}+{\frac {\lambda _{0}n({\bar {x}}-\mu _{0})^{2}}{\lambda _{0}+n}}\end{aligned}}$
On inserting this back into the expression above,
    ${\begin{aligned}\mathbf {P} (\tau ,\mu \mid \mathbf {X} )&\propto \tau ^{{\frac {n}{2}}+\alpha _{0}-{\frac {1}{2}}}\exp \left[-\tau \left({\frac {1}{2}}ns+\beta _{0}\right)\right]\exp \left[-{\frac {\tau }{2}}\left(\left(\lambda _{0}+n\right)\left(\mu -{\frac {\lambda _{0}\mu _{0}+n{\bar {x}}}{\lambda _{0}+n}}\right)^{2}+{\frac {\lambda _{0}n({\bar {x}}-\mu _{0})^{2}}{\lambda _{0}+n}}\right)\right]\\&\propto \tau ^{{\frac {n}{2}}+\alpha _{0}-{\frac {1}{2}}}\exp \left[-\tau \left({\frac {1}{2}}ns+\beta _{0}+{\frac {\lambda _{0}n({\bar {x}}-\mu _{0})^{2}}{2(\lambda _{0}+n)}}\right)\right]\exp \left[-{\frac {\tau }{2}}\left(\lambda _{0}+n\right)\left(\mu -{\frac {\lambda _{0}\mu _{0}+n{\bar {x}}}{\lambda _{0}+n}}\right)^{2}\right]\end{aligned}}$
This final expression is in exactly the same form as a Normal-Gamma distribution, i.e.,
    $\mathbf {P} (\tau ,\mu \mid \mathbf {X} )={\text{NormalGamma}}\left({\frac {\lambda _{0}\mu _{0}+n{\bar {x}}}{\lambda _{0}+n}},\lambda _{0}+n,\alpha _{0}+{\frac {n}{2}},\beta _{0}+{\frac {1}{2}}\left(ns+{\frac {\lambda _{0}n({\bar {x}}-\mu _{0})^{2}}{\lambda _{0}+n}}\right)\right)$

### Interpretation of parameters

The interpretation of parameters in terms of pseudo-observations is as follows:
* The new mean takes a weighted average of the old pseudo-mean and the observed mean, weighted by the number of associated (pseudo-)observations.
* The precision was estimated from $2\alpha$ pseudo-observations (i.e. possibly a different number of pseudo-observations, to allow the variance of the mean and precision to be controlled separately) with sample mean $\mu$ and sample variance ${\frac {\beta }{\alpha }}$ (i.e. with sum of squared deviations $2\beta$).
* The posterior updates the number of pseudo-observations ($\lambda _{0}$) simply by adding the corresponding number of new observations ($n$).
* The new sum of squared deviations is computed by adding the previous respective sums of squared deviations. However, a third "interaction term" is needed because the two sets of squared deviations were computed with respect to different means, and hence the sum of the two underestimates the actual total squared deviation.
As a consequence, if one has a prior mean of $\mu _{0}$ from $n_{\mu }$ samples and a prior precision of $\tau _{0}$ from $n_{\tau }$ samples, the prior distribution over $\mu$ and $\tau$ is
    $\mathbf {P} (\tau ,\mu \mid \mathbf {X} )=\operatorname {NormalGamma} \left(\mu _{0},n_{\mu },{\frac {n_{\tau }}{2}},{\frac {n_{\tau }}{2\tau _{0}}}\right)$
and after observing $n$ samples with mean $\mu$ and variance $s$, the posterior probability is
    $\mathbf {P} (\tau ,\mu \mid \mathbf {X} )={\text{NormalGamma}}\left({\frac {n_{\mu }\mu _{0}+n\mu }{n_{\mu }+n}},n_{\mu }+n,{\frac {1}{2}}(n_{\tau }+n),{\frac {1}{2}}\left({\frac {n_{\tau }}{\tau _{0}}}+ns+{\frac {n_{\mu }n(\mu -\mu _{0})^{2}}{n_{\mu }+n}}\right)\right)$
Note that in some programming languages, such as Matlab, the gamma distribution is implemented with the inverse definition of $\beta$, so the fourth argument of the Normal-Gamma distribution is $2\tau _{0}/n_{\tau }$.

## Generating normal-gamma random variates

Generation of random variates is straightforward:
* Sample $\tau$ from a gamma distribution with parameters $\alpha$ and $\beta$
* Sample $x$ from a normal distribution with mean $\mu$ and variance $1/(\lambda \tau )$

## Related distributions

* The normal-inverse-gamma distribution is the same distribution parameterized by variance rather than precision
* The normal-exponential-gamma distribution