Hello! Welcome to the final lesson in our module on Bayesian methods for machine learning.
In our previous lessons, we've built a strong foundation in Bayesian thinking. We started by using Bayes' theorem to update our beliefs, explored how to use conjugate priors to simplify calculations, applied these ideas to linear regression, and most recently, learned about Maximum A Posteriori (MAP) estimation. MAP gave us a way to find the single "best" parameter value by balancing the data (likelihood) with our prior beliefs.
Today, we're taking the final step in our Bayesian journey by asking: what if we have entirely different models? For example, is a linear model better than a quadratic model for our data? Or is a model with a narrow prior better than one with a wide prior? This is the task of model comparison.
Your learning outcome for this lesson is to use Bayesian methods for model comparison (e.g., using Bayes factors or posterior odds). By the end, you will understand the principles behind the Bayesian answer to model selection and be able to interpret the results.
1. From Parameter Posterior to Model Posterior
So far, we have focused on the posterior distribution of a model's parameters, , given the data, : . For model comparison, we elevate this idea to the level of the models themselves. Our goal is to calculate the posterior probability of a model, , given the data: .
Using Bayes' theorem once again, we can write this as:
- Model Prior : Our prior belief that model is the correct one, before seeing any data. For example, we might assign a higher prior probability to a simpler model.
- Model Evidence : The probability of observing the data given the model . This is the central quantity in Bayesian model comparison. It's also called the marginal likelihood.
The marginal likelihood is the key. To understand how it's calculated and what it represents, let's watch a short video.
Introducing Bayes factors and marginal likelihoods
This video from Ben Lambert's channel formally introduces the concept of comparing models by comparing their posterior probabilities and defines the crucial term: the marginal likelihood.
Watch from the beginning to 06:40. Focus on how Bayes' rule is applied to models (not just parameters) and pay close attention to the definition of the marginal likelihood as an integral over the model's parameters.
As the video explained, the marginal likelihood is calculated by "averaging out" or "integrating out" the parameters of the model:
Don't worry too much about solving the integral. The key intuition is that the model evidence is the average probability of the data, averaged over all possible parameter values weighted by their prior probabilities. It answers the question: "How well does this model, as a whole, predict the observed data?"
2. Model Evidence as an Automatic Occam's Razor
This brings us to a beautiful and powerful aspect of Bayesian model comparison. The marginal likelihood has a built-in penalty for unnecessary complexity. This is often called a Bayesian "Occam's Razor," which favors the simplest explanation that fits the data well.
How does this work? Let's consider three models:
- Model 1 (Simple): A very simple model (e.g., a constant value). It can only explain a very narrow range of datasets.
- Model 2 (Just Right): A model with medium complexity that happens to be a good fit for our specific dataset.
- Model 3 (Complex): A very complex model (e.g., a high-order polynomial). It is so flexible it can explain a vast range of different datasets.
Because a complex model can generate many different possible datasets, its predictive probability is "spread thin" across all of them. So, for any one specific dataset, the probability is relatively low. The simple model is not flexible enough to explain our data, so its evidence is also low. The model that is "just complex enough" to explain the data and not much else will have its predictive probability concentrated on datasets like ours, resulting in the highest model evidence.
This image provides a great visual for this concept.

To deepen this intuition, the following video provides a brilliant explanation.
5.2 Bayesian Model Comparison (UvA - Machine Learning 1 - 2020)
This video by Erik Bekkers explains exactly why the model evidence penalizes complexity, using a clever approximation of the marginal likelihood integral.
Watch from 08:13 to 20:05. The first part (until 17:04) uses an approximation to show how a 'complexity penalty' term naturally arises in the log-model-evidence. The second part (from 17:04) provides the excellent thought experiment we just discussed.
3. Comparing Models: Bayes Factors and Posterior Odds
Now that we understand model evidence, comparing two models, and , is straightforward.
Bayes Factor
The most common way to compare two models is to compute the ratio of their model evidences. This ratio is called the Bayes Factor, .
The Bayes Factor tells you how much the data has shifted your belief in favor of one model over the other.
- If , the data provides more evidence for than for .
- If , the data provides more evidence for than for .
- If , the data does not distinguish between the two models.
To make interpretation easier, statisticians have proposed scales to categorize the strength of evidence.

For a more detailed interpretation scale, let's consult this guide.
Bayes Factors and Marginal Likelihood
The PyMC documentation provides an excellent summary of Bayes factors and includes the widely used Jeffreys' scale for interpretation.
Read the section 'Bayesian model selection'. Pay attention to the definition of the Bayes factor and the table of values for Jeffreys' scale. This gives you a practical tool for reporting your findings.
Posterior Odds
The Bayes Factor tells us what the data says. But what if we had prior beliefs about the models themselves? This is where Posterior Odds come in.
The relationship is simple and elegant:
If we have no reason to prefer one model over the other before seeing the data, we can set the prior odds to 1 (). In this common case, the posterior odds are equal to the Bayes factor. This is why the Bayes factor is so often the primary focus of analysis.
Posterior odds | Bayesian Statistics Class Notes
This short study guide from Fiveable clearly explains the components and calculation of posterior odds.
Read the sections 'Components of posterior odds' and 'Calculation of posterior odds' to solidify the relationship between the Bayes factor and prior/posterior odds.
4. Practical Computation and Challenges
This all sounds great in theory, but as we noted, computing the marginal likelihood integral is often very difficult. Let's briefly look at how it's done in practice.
Bayes Factors and Marginal Likelihood
Let's return to the PyMC documentation to see how Bayes factors can be computed and what challenges we face.
Read the sections 'Some remarks', 'Analytically', and 'Savage-Dickey Density Ratio'. Focus on understanding the three key points: The Good, Bad, and Ugly: The pros and cons of using marginal likelihood. Analytical Example: How to compute the BF for a simple coin-flip model. This provides a concrete example that you can follow. Savage-Dickey Ratio: A clever computational shortcut for a special but common case: comparing a model against a simplified, 'nested' version of itself.
To summarize the practical aspects:
- The Challenge: The marginal likelihood is hard to compute because it involves a high-dimensional integral. It's also very sensitive to the choice of priors on the parameters ().
- Analytical Solutions: For simple conjugate models (like the Beta-Binomial coin-flip example), we can sometimes calculate the Bayes factor exactly.
- Numerical Methods: For most real-world models in machine learning, we rely on numerical methods like Sequential Monte Carlo (SMC), which can estimate the marginal likelihood as part of their sampling process.
- Shortcuts: For nested models (e.g., testing if a regression coefficient is exactly zero), the Savage-Dickey density ratio provides an easy way to compute the Bayes factor by comparing the prior and posterior distributions at that specific value.
Test your understanding!
You are comparing two models to predict house prices. Model A is a simple linear regression. Model B is a complex Gradient Boosting model. After analyzing your data, you compute a Bayes factor .
According to the Jeffreys' scale, what is the strength of evidence for Model A? What does this result tell you about the two models, keeping the principle of Occam's Razor in mind?
Show answer
A Bayes factor of 25 falls into the "strong" evidence category (between 10-30). This means the data provides strong evidence in favor of the simple linear model (Model A) over the complex Gradient Boosting model (Model B).
This is a classic example of the Bayesian Occam's Razor at work. Even though the complex model could likely fit the training data better, the marginal likelihood penalizes its complexity. The result suggests that the simple model provides a sufficient explanation for the data, and the extra complexity of the boosting model is not justified by the evidence.
Conclusion
Congratulations on completing the final lesson of the Bayesian methods module! You've moved from the fundamentals of Bayes' theorem to the sophisticated application of Bayesian model comparison.
Key Takeaways:
- Bayesian model comparison is framed as calculating the posterior probability of a model, .
- The key quantity is the marginal likelihood or model evidence, , which measures how well a model predicts the data on average over all its possible parameter values.
- Model evidence naturally embodies Occam's Razor, automatically penalizing models for unnecessary complexity.
- The Bayes factor is the ratio of two models' evidences and quantifies the support from the data for one model over another.
- Posterior odds combine the Bayes factor with our prior beliefs about the models, giving a complete Bayesian comparison.
Preview of the Next Module:
In this module, we have explored a comprehensive framework for reasoning about uncertainty and evidence. While powerful, full Bayesian inference can be computationally intensive. Many of the most widely used machine learning models today are trained not by analyzing a full posterior, but by optimizing a single objective.
In our next module, Optimization for Machine Learning, we will dive into this world. We'll begin by exploring the cornerstone of this process: the loss function. You will see deep connections to what you've learned here; for example, minimizing certain loss functions is mathematically equivalent to performing the Maximum Likelihood or MAP estimations we've already studied. This will bridge your knowledge from the probabilistic world to the optimization-centric view of machine learning.