Univariate
Preliminaries
Consider the case of observing independent samples from a distribution. Suppose we know the true value of , and we are interested in determining the posterior distribution of . It is conventional to place a conjugate normal prior on . Our model is:
In Bayesian statistics, the posterior is proportional to the likelihood times the prior. Because all observations are independent, our likelihood is the product of Normal density functions: one for each . 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
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 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
We now distribute the sum, yielding
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
We can combine the terms together, and the terms together:
Finally, for ease of notation, we can use the fact that :
After all of this algebraic simplification, inside the parentheses what we have looks something like . We can now “complete the square” to obtain something of the form .
Competing the square
Our first step is to make the notation easier to follow. Let and . Using the new, simplified notation, we have
We can move the coefficient on outside the parentheses:
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:
In fact, neither term is a function of , so we can simply drop the term colored in red.
The terms within the parentheses are of the form , which, from the rules learned in algebra, can be simplified to . Applying this to our terms, we obtain:
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:
By completing the square, we have revealed that the posterior distribution of has the form of a normal distribution with a mean of and a variance of , or
where