Post · 6 min read· pp. 187–190

Conjugate Priors

Introduction

Conjugate priors have been widely used in many Bayesian inference techniques. Therefore it is very important to understand its basic concepts. In this blog post, I will introduce the concept of conjugate priors and give a concrete example of proving conjugate priors.

Definition

According to Bayes’ theorem, it is universally true that

p(θ∣x)⏟posterior=p(x∣θ)p(θ)p(x)∝p(x∣θ)⏟likelihoodp(θ)⏟prior∝p(x,θ)\underbrace{p(\theta|x)}_\text{posterior} = \frac{p(x|\theta) p(\theta)}{p(x)} \propto \underbrace{p(x|\theta)}_\text{likelihood} \underbrace{p(\theta)}_\text{prior} \propto p(x,\theta)

In Bayesian probability theory, if the posterior distributions p(θ∣x)p(\theta|x) are in the same probability distribution family as the prior probability distribution p(θ)p(\theta), the prior and posterior are then called conjugate distributions, and the prior is called a conjugate prior for the likelihood function p(x∣θ)p(x|\theta).

Example

If we have random variable μ\mu from normal distribution N(μ0,σ02)\mathcal{N}(\mu_0, \sigma_0^2), i.e.,

p(μ)=12πσ02e−(μ−μ0)22σ02p(\mu) = \frac{1}{\sqrt{2\pi\sigma_0^2}} e^{-\frac{(\mu-\mu_0)^2}{2\sigma_0^2}}

We also have random variable xx from normal distribution N(μ,σ2)\mathcal{N}(\mu, \sigma^2), i.e.,

p(x∣μ)=12πσ2e−(x−μ)22σ2p(x|\mu) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}

Note that μ0,σ02,σ2\mu_0, \sigma_0^2, \sigma^2 are not random variable but prefixed parameters in this case.

We have NN data points X={x1,x2,⋯ ,xN}X=\{x_1, x_2, \cdots, x_N\} independently sampled from normal distribution N(μ,σ2)\mathcal{N}(\mu, \sigma^2).

p(X∣μ)=[12πσ2]Ne−∑i=1N(xi−μ)22σ2p(X|\mu) = \Bigg[ \frac{1}{\sqrt{2\pi\sigma^2}} \Bigg]^N e^{-\frac{\sum_{i=1}^{N}(x_i-\mu)^2}{2\sigma^2}}

We will show that p(μ∣X)p(\mu|X) is also a normal distribution, thus the posterior p(μ∣X)p(\mu|X) and the prior p(μ)p(\mu) are in the same normal distribution family and they are conjugate. p(μ)p(\mu) is a conjugate prior for the likelihood p(X∣μ)p(X|\mu).

p(μ∣X)=p(X∣μ)p(μ)p(X)∝p(X∣μ)p(μ)∝exp⁡−[∑i=1N(xi−μ)22σ2+(μ−μ0)22σ02]\begin{aligned} p(\mu|X) &= \frac{p(X|\mu)p(\mu)}{p(X)} \\ &\propto p(X|\mu)p(\mu) \\ &\propto \exp{- \big[ \frac{\sum_{i=1}^{N}(x_i-\mu)^2}{2\sigma^2} + \frac{(\mu -\mu_0)^2}{2\sigma_0^2} \big] } \end{aligned}

where

∑i=1N(xi−μ)22σ2+(μ−μ0)22σ02=12σ2∑i=1N(xi2−2xiμ+μ2)+12σ02(μ−μ0)2=12σ2∑i=1Nxi2+12σ22μ∑i=1Nxi+12σ2Nμ2+12σ02μ2−12σ022μμ0+12σ02μ02=(N2σ2+12σ02)μ2−2μ(∑i=1Nxi2σ2+μ02σ02)+const=1(N2σ2+12σ02)−1[μ−∑i=1Nxi2σ2+μ02σ02N2σ2+12σ02]2+const=12(Nσ2+1σ02)−1[μ−NXˉσ2+μ0σ02Nσ2+1σ02]2+const\begin{aligned} &\frac{\sum_{i=1}^{N}(x_i-\mu)^2}{2\sigma^2} + \frac{(\mu -\mu_0)^2}{2\sigma_0^2} \\ &= \frac{1}{2\sigma^2}\sum_{i=1}^{N}(x_i^2 - 2x_i\mu + \mu^2) + \frac{1}{2\sigma_0^2}(\mu - \mu_0)^2\\ &= \frac{1}{2\sigma^2}\sum_{i=1}^{N}x_i^2 + \frac{1}{2\sigma^2} 2\mu \sum_{i=1}^{N} x_i + \frac{1}{2\sigma^2} N \mu^2 + \frac{1}{2\sigma_0^2}\mu^2 - \frac{1}{2\sigma_0^2} 2\mu\mu_0 + \frac{1}{2\sigma_0^2} \mu_0^2 \\ &= (\frac{N}{2\sigma^2} + \frac{1}{2\sigma_0^2}) \mu^2 - 2\mu ( \frac{\sum_{i=1}^{N} x_i}{2\sigma^2} + \frac{\mu_0}{2\sigma_0^2} ) + \text{const} \\ &= \frac{1}{(\frac{N}{2\sigma^2} + \frac{1}{2\sigma_0^2})^{-1}} \Bigg[ \mu - \frac{\frac{\sum_{i=1}^{N} x_i}{2\sigma^2} + \frac{\mu_0}{2\sigma_0^2}}{\frac{N}{2\sigma^2} + \frac{1}{2\sigma_0^2}} \Bigg]^2 + \text{const} \\ &= \frac{1}{2(\frac{N}{\sigma^2} + \frac{1}{\sigma_0^2})^{-1}} \Bigg[ \mu - \frac{\frac{N\bar{X}}{\sigma^2} + \frac{\mu_0}{\sigma_0^2}}{\frac{N}{\sigma^2} + \frac{1}{\sigma_0^2}} \Bigg]^2 + \text{const} \\ \end{aligned}

Here const\text{const} are terms irrelevant to random variable μ\mu. For simplicity, we define:

μ′=NXˉσ2+μ0σ02Nσ2+1σ02σ′2=(Nσ2+1σ02)−1\mu^{\prime} = \frac{\frac{N\bar{X}}{\sigma^2} + \frac{\mu_0}{\sigma_0^2}}{\frac{N}{\sigma^2} + \frac{1}{\sigma_0^2}} \\ \sigma^{\prime2} = (\frac{N}{\sigma^2} + \frac{1}{\sigma_0^2})^{-1}

Therefore, p(μ∣X)p(\mu|X) follows normal distribution N(μ′,σ′2)\mathcal{N}(\mu^{\prime}, \sigma^{\prime2}). Since both the posterior p(μ∣X)p(\mu|X) and the prior p(μ)p(\mu) are in the normal distribution family, we say that p(μ)∼N(μ0,σ02)p(\mu) \sim \mathcal{N}(\mu_0, \sigma_0^2) is a conjugate prior for the likelihood p(X∣μ)p(X|\mu). It turns out that p(X∣μ)p(X|\mu) is a multivariate normal distribution by definition.

Caveats

By the way, the distribution of the mean of XX, p(Xˉ∣μ)p(\bar{X}|\mu), is an exact normal distribution. But the proof I found was oversimplified.

Here is what they did. They first define random variables:

Xˉ=1N∑i=1Nxis2=1N−1∑i=1N(xi−Xˉ)2\bar{X} = \frac{1}{N} \sum_{i=1}^{N} x_i\\ s^2 = \frac{1}{N-1} \sum_{i=1}^{N} (x_i - \bar{X})^2

Then they transform the probability density expression for p(X∣μ)p(X|\mu):

p(X∣μ)∝exp⁡−∑i=1N(xi−μ)22σ2∝exp⁡−∑i=1N[(xi−Xˉ)+(Xˉ−μ)]22σ2∝exp⁡−∑i=1N(xi−Xˉ)2+2∑i=1N(xi−Xˉ)(Xˉ−μ)+∑i=1N(Xˉ−μ)22σ2\begin{aligned} p(X|\mu) &\propto \exp{-\frac{\sum_{i=1}^{N}(x_i-\mu)^2}{2\sigma^2}} \\ &\propto \exp{-\frac{\sum_{i=1}^{N} \big[ (x_i - \bar{X}) + (\bar{X} - \mu) \big]^2}{2\sigma^2}}\\ &\propto \exp{-\frac{\sum_{i=1}^{N} (x_i - \bar{X})^2 + 2\sum_{i=1}^{N} (x_i - \bar{X})(\bar{X} - \mu) + \sum_{i=1}^{N} (\bar{X} - \mu)^2 }{2\sigma^2}}\\ \end{aligned}

It is not hard to see that

∑i=1N(xi−Xˉ)2=(N−1)s2∑i=1N(Xˉ−μ)2=N(Xˉ−μ)2\sum_{i=1}^{N} (x_i - \bar{X})^2 = (N-1)s^2\\ \sum_{i=1}^{N} (\bar{X} - \mu)^2 = N(\bar{X} - \mu)^2

Moreover,

∑i=1N(xi−Xˉ)(Xˉ−μ)=∑i=1N(xiXˉ−xiμ−Xˉ2+Xˉμ)=Xˉ∑i=1Nxi−μ∑i=1Nxi−NXˉ2+NXˉμ=XˉNXˉ−μNXˉ−NXˉ2+NXˉμ=0\begin{aligned} \sum_{i=1}^{N} (x_i - \bar{X})(\bar{X} - \mu) &= \sum_{i=1}^{N}(x_i\bar{X} -x_i\mu - {\bar{X}}^2 + \bar{X}\mu)\\ &= \bar{X}\sum_{i=1}^{N} x_i - \mu \sum_{i=1}^{N} x_i - N {\bar{X}}^2 + N \bar{X}\mu\\ &= \bar{X}N\bar{X} - \mu N\bar{X} - N {\bar{X}}^2 + N \bar{X}\mu\\ &= 0 \end{aligned}

Therefore,

p(X∣μ)∝exp⁡−∑i=1N(xi−μ)22σ2∝exp⁡N(Xˉ−μ)22σ2exp⁡(N−1)s22σ2\begin{aligned} p(X|\mu) &\propto \exp{-\frac{\sum_{i=1}^{N}(x_i-\mu)^2}{2\sigma^2}} \\ &\propto \exp{\frac{N(\bar{X} - \mu)^2}{2\sigma^2}} \exp{\frac{(N-1)s^2}{2\sigma^2}}\\ \end{aligned}

This means

p(Xˉ∣μ)∝exp⁡N(Xˉ−μ)22σ2exp⁡(N−1)s22σ2\begin{aligned} p(\bar{X}|\mu) &\propto \exp{\frac{N(\bar{X} - \mu)^2}{2\sigma^2}} \exp{\frac{(N-1)s^2}{2\sigma^2}}\\ \end{aligned}

Is random variable ss related to Xˉ\bar{X}? It has been proved that ss and Xˉ\bar{X} are independent, given the samples are normally distributed. One of the proofs could be found here, which surprised me because I never thought it would be non-trivial. We may come back to this proof in the future.

Therefore, we could remove exp⁡{(N−1)s22σ2}\exp\{\frac{(N-1)s^2}{2\sigma^2}\} from the right side since ss and Xˉ\bar{X} are independent.

p(Xˉ∣μ)∝exp⁡N(Xˉ−μ)22σ2exp⁡(N−1)s22σ2∝exp⁡N(Xˉ−μ)22σ2\begin{aligned} p(\bar{X}|\mu) &\propto \exp{\frac{N(\bar{X} - \mu)^2}{2\sigma^2}} \exp{\frac{(N-1)s^2}{2\sigma^2}}\\ &\propto \exp{\frac{N(\bar{X} - \mu)^2}{2\sigma^2}} \end{aligned}

Therefore, p(Xˉ∣μ)p(\bar{X}|\mu) strictly follows the exact normal distribution N(Xˉ∣μ;σ2N)\mathcal{N}(\bar{X} | \mu; \frac{\sigma^2}{N}).

This has something related to the central limit theorem. The central limit theorem states as the sample size become sufficiently large, the distribution of sample means approximates to normal distribution, regardless of what distribution the samples come from. It approximates normal distribution, but it might not be the exact normal distribution. In our case, with our derivation, we showed that the distribution of sample means is an exact normal distribution if the samples come from a normal distribution.

Final Remarks

Deriving and proving conjugate priors could be complex and tedious. Fortunately, Wikipedia has documented all the known conjugate prior for certain likelihoods so far.

One last important property, all members of the exponential family have conjugate priors. This property is very useful in variational inference. Normal distribution is one of the members of the exponential family. For example, as we have shown, in our case p(x∣μ)p(x|\mu) (there is only one sample in XX) does have a conjugate prior which follows normal distribution.

Acknowledgement

I would like to thank CrazyStat from Hacker News for providing critical comments and corrections to this post.

References