Univariate

Preliminaries

Consider the case of observing N independent samples from a Normal(μ,σ2) distribution. Suppose we know the true value of σ2, and we are interested in determining the posterior distribution of μ. It is conventional to place a conjugate normal prior on μ. Our model is:

yiiidNormal(μ,σ2)μNormal(μ0,τ2)

In Bayesian statistics, the posterior is proportional to the likelihood times the prior. Because all observations are independent, our likelihood is the product of N Normal density functions: one for each yi. The prior then provides another Normal density function term. After simplifying and dropping terms that are not functions of μ, we end up with a posterior distribution for μ proportional to

exp{12σ2i=1N(yiμ)2}exp{12τ2(μμ0)2}

The first term is the likelihood, and the second term is the prior. Because we desire a posterior that is a simple function of μ, we need to gather all the terms that include μ together; as the posterior is written above, μ is scattered across N+1 terms. Completing the square is the trick that will allow us to gather all the μ terms into one.

The first step is to expand the squares containing μ. This yields

exp{12σ2i=1N(yi22μyiμ2)}exp{12τ2(μ22μμ0+μ02)}

We now distribute the sum, yielding

exp{12σ2(i=1Nyi22μi=1Nyi+Nμ2)}exp{12τ2(μ22μμ0+μ02)}

There are several terms above that do not involve μ; these are highlighted in red. When we drop those terms, combine all remaining terms within one exponent, and then focus only on what is in the exponent, what remains is

12(Nσ2μ2+1τ2μ22μi=1Nyiσ22μ1τ2μ0)

We can combine the μ2 terms together, and the μ terms together:

12((Nσ2+1τ2)μ22(i=1Nyiσ2+1τ2μ0)μ)

Finally, for ease of notation, we can use the fact that i=1Nyi=Ny¯:

12((Nσ2+1τ2)μ22(Nσ2y¯+1τ2μ0)μ)

After all of this algebraic simplification, inside the parentheses what we have looks something like ax22bx. We can now “complete the square” to obtain something of the form (xc)2.

Competing the square

Our first step is to make the notation easier to follow. Let a=Nσ2+1τ2 and b=Nσ2y¯+1τ2μ0. Using the new, simplified notation, we have

12(aμ22bμ)

We can move the coefficient a on μ outside the parentheses:

a2(μ22baμ)

We now add and subtract the same value inside the parentheses. This doesn’t change the value at all, since the terms sum to 0:

a2(μ22baμ+b2a2b2a2)

In fact, neither term is a function of μ, so we can simply drop the term colored in red.

a2(μ22baμ+b2a2)

The terms within the parentheses are of the form x22xc+c2, which, from the rules learned in algebra, can be simplified to (xc)2. Applying this to our terms, we obtain:

a2(μba)2

We have thus completed the square. We are not done, however: this was only the portion of the posterior distribution that was in the exponent. Replacing the terms in the exponent yields:

exp{a2(μba)2}

By completing the square, we have revealed that the posterior distribution of μ has the form of a normal distribution with a mean of b/a and a variance of 1/a, or

μyNormal(μn,σn2) where

σn2=1a=(Nσ2+1τ2)1,μn=ba=σn2(Nσ2y¯+1τ2μ0).