Introduction
Welcome to the seventh module of our course on Data Science. In the previous lesson, we established a remarkable connection between Ordinary Least Squares (OLS) and Maximum Likelihood Estimation (MLE) under the assumption of Gaussian noise. We saw that what begins as a geometric problem of minimizing squared distances (OLS) becomes equivalent to a probabilistic problem of maximizing likelihood (MLE).
Today, we take the next logical step into a new paradigm of statistical thinking: Bayesian Inference. The philosophical shift is profound. In the frequentist world of OLS and MLE, we treated parameters like and as fixed, unknown constants. In the Bayesian world, we treat parameters as random variables about which we can have beliefs that we update as we gather evidence.
This lesson will introduce the core mechanics of Bayesian inference through a foundational example. Our learning outcome is to derive the posterior distribution for a model with a Gaussian likelihood and a Gaussian prior. This specific case, where the prior and posterior are in the same distributional family, is known as using a conjugate prior, and it provides a beautifully clear illustration of the Bayesian updating process.
1. The Core of Bayesian Inference: Updating Beliefs
At its heart, Bayesian inference is a formal system for updating our beliefs in light of new data. It combines what we knew before seeing the data with what the data tells us. The process uses three key ingredients, all connected by Bayes' theorem.
-
Prior Distribution, : This distribution represents our belief about a parameter before we observe any data. A "strong" prior might be a narrow distribution, indicating high certainty, while a "weak" or "uninformative" prior might be very broad, indicating low certainty.
-
Likelihood, : This is the same likelihood function we used in MLE. It tells us the probability of observing our data for a given value of the parameter . It represents the evidence from the data.
-
Posterior Distribution, : This is our updated belief about the parameter after observing the data . It is the result we want to compute.
Bayes' theorem elegantly combines these parts:
The term in the denominator, , is called the marginal likelihood or the evidence. It acts as a normalization constant, ensuring the posterior integrates to 1. In many cases, we can ignore it and work with the proportionality:
This simple formula is the engine of all Bayesian inference: Posterior belief is proportional to Likelihood of data times Prior belief.
2. Inferring a Gaussian Mean with a Gaussian Prior
Let's make this concrete. Our goal is to infer the unknown mean of a Gaussian distribution, assuming we know its variance .
The Setup:
- Data: We have independent and identically distributed (i.i.d.) observations drawn from a Gaussian distribution: .
- Likelihood: As we saw in previous lessons, the likelihood function is the product of the individual probability densities. As a function of , its exponent is a quadratic in .
- Prior: We need to specify a prior belief for . A mathematically convenient choice is a prior that has the same functional form as the likelihood. Since the likelihood's exponent is quadratic in , we choose a Gaussian prior for :
Here, is our prior guess for the mean, and is the variance of our prior belief (how certain we are). This is a conjugate prior because, as we will see, the resulting posterior will also be a Gaussian distribution.
3. Deriving the Posterior Distribution
We now have all the ingredients. The posterior is proportional to the product of the likelihood and the prior. Since both are exponentials, their product is the exponential of the sum of their arguments.
Your interest in mathematical rigor will be well-served here. The key step in this derivation involves an algebraic technique called "completing the square" to rearrange the exponent into the form of a new Gaussian distribution.
The following reading provides a detailed, step-by-step derivation.
Bayesian Inference for the Gaussian
This article, 'Bayesian Inference for the Gaussian' by Gregory Gundersen, provides a clear and detailed mathematical derivation for our exact problem. It starts with the likelihood and prior, multiplies them, and carefully works through the algebra of completing the square to find the parameters of the posterior distribution.
Please read the article from the beginning ('Likelihood, prior, and posterior') down to the end of the full derivation (just before the 'Case 2' heading). Pay very close attention to the section 'Completing the square', as this is the central mathematical trick. Follow the algebra to see how the expression for p(μ|D) is manipulated to identify the posterior mean μ_N and posterior variance σ_N^2.
After working through the derivation, you will find that the posterior distribution is indeed a Gaussian:
with the following parameters for the posterior mean and variance :
- Posterior Variance:
- Posterior Mean:
where is the sample mean of the data.
4. Interpreting the Result: Precision and Weighted Averages
The derived formulas are mathematically correct, but their true beauty lies in their interpretation. The following video provides an excellent conceptual breakdown.
2.3.6 Bayesian Inference for the Gaussian - The Mean
This video, 'Bayesian Inference for the Gaussian - The Mean', gives an outstanding intuitive explanation of the posterior parameters we just derived. It explains the concepts of conjugate priors, posterior mean as a compromise, and the additivity of precisions.
Watch from 3:37 to 23:36. The video will walk you through the concepts and interpretation. Focus on: Why the Gaussian prior is conjugate (3:37 - 7:33). The form of the posterior mean and variance (7:33 - 9:04). The detailed, intuitive explanation of the posterior mean as a 'compromise' and the posterior precision as 'additive' (9:04 - 23:36). The presenter's derivation of the precision-weighted average is particularly insightful.
Let's summarize the key intuitions from the video.
4.1. The Additivity of Precision
The formula for the posterior variance is much clearer if we think in terms of precision, which is the inverse of variance (). High precision means low variance (high certainty).
In terms of precision, this is simply:
This is a beautiful result! It states that:
The posterior precision is the sum of the prior precision and the total precision contributed by the data.
Every data point we observe adds to our certainty about the parameter , causing the posterior distribution to become narrower (higher precision, lower variance).
4.2. The Posterior Mean as a Weighted Average
The formula for the posterior mean, , can be rewritten to reveal a compelling story. It is a precision-weighted average of the prior mean () and the sample mean (, which is the MLE of ).
The posterior mean is a compromise, or a point on the line between the prior mean and the data's sample mean.
- If our prior is very strong ( is large), the posterior mean will be pulled closer to .
- If our data is very strong (large ), the posterior mean will be pulled closer to the sample mean .
- As , the weight on the prior goes to zero, and the posterior mean converges to the sample mean. The Bayesian estimate converges to the frequentist (MLE) estimate.
- If we have a very uninformative prior (), the posterior mean is simply the sample mean.
This second short video reinforces this weighted-average intuition with clear algebraic steps.
33 - Normal prior conjugate to normal likelihood - intuition
This video, 'Normal prior conjugate to normal likelihood - intuition', focuses specifically on explaining the posterior mean and variance as weighted averages and combinations of precision. It's a great supplement to solidify the interpretation.
Watch the full video. Notice how it derives the same weighted-average form for the posterior mean and discusses how the weights shift depending on the prior certainty (sigma_theta_squared), sample size (n), and data variance (sigma_x_squared).
5. Sequential Nature of Bayesian Inference
One of the most powerful aspects of the Bayesian framework is its natural ability to handle data sequentially. The posterior distribution after observing data points can serve as the prior distribution for the -th data point.

This "online learning" property is not an add-on; it falls directly out of the mathematics. The product of the likelihoods for data points can be grouped in any way we choose:
This shows that the posterior after observations is what you get when you update the posterior after observations with the final data point. The Bayesian framework provides a natural and elegant way to continuously learn as data arrives.
Conclusion
This lesson served as your formal entry into Bayesian inference, demonstrating its core mechanics with the canonical example of a Gaussian model.
Key Takeaways:
- Bayesian Framework: The posterior belief about a parameter is derived by multiplying a prior belief with the likelihood of observed data.
- Conjugate Priors: Choosing a prior from a distributional family that is "conjugate" to the likelihood (e.g., Gaussian prior for a Gaussian likelihood) results in a posterior from the same family, which simplifies the analysis immensely.
- Posterior for Gaussian Mean: The posterior distribution for the mean of a Gaussian, given a Gaussian prior, is another Gaussian distribution, .
- Intuitive Posterior Parameters: The posterior parameters have clear interpretations:
- The posterior precision is the sum of the prior precision and the data's precision.
- The posterior mean is a precision-weighted average of the prior mean and the sample mean.
- Sequential Updating: Bayesian inference is naturally sequential; the posterior from one set of data can serve as the prior for the next.
Preview of the Next Lesson:
Now that we have derived a full posterior distribution for our parameter, a natural question arises: how do we get a single point estimate from it? In the next lesson, we will explore two of the most common ways to do this. We will derive the Minimum Mean Square Error (MMSE) estimator, which corresponds to the mean of the posterior distribution, and the Maximum A Posteriori (MAP) estimator, which corresponds to its mode. You will also see the fascinating connection between MAP estimation, MLE, and regularization.