Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

MAP Estimation in Linear Regression

Recommended resources

Welcome to the next lesson in our journey through the probabilistic foundations of machine learning. In our previous session, we derived the Maximum Likelihood Estimators (MLE) for a Gaussian distribution, establishing a core principle of frequentist statistics: finding the parameters that make the observed data most likely.

Today, we shift our perspective back to the Bayesian school of thought to explore Maximum A Posteriori (MAP) estimation. Your goal for this lesson is to apply MAP estimation to a linear regression problem. This will not only introduce you to a new estimation technique but will also reveal a profound and elegant connection between Bayesian priors and the regularization methods, like Ridge regression, that you're already familiar with from your data science work. This lesson is a perfect example of bridging mathematical theory with practical, widely-used machine learning models.

From Likelihood to Posterior: The Bayesian Shift

In MLE, our objective was to maximize the likelihood function, , where is the data and is the parameter set. The question was, "What parameters make our data most probable?"

MAP estimation takes this one step further by asking, "Given our data, what parameters are most probable?" This corresponds to maximizing the posterior probability, . To do this, we use Bayes' theorem:

The key insight is that MAP incorporates a prior distribution, , which represents our belief about the parameters before we see any data. The likelihood, , then updates this prior belief to form the posterior. The term in the denominator, , is the probability of the data itself. Since it doesn't depend on , we can treat it as a constant normalizer and ignore it for the purpose of maximization.

This leads to the core objective of MAP:

The process is visually summarized in the diagram below. We start with our prior knowledge and observed data, combine them via Bayesian update to get the posterior distribution, and then find the parameter value that maximizes this posterior.

This flowchart shows the steps in MAP estimation. It begins with two inputs: Prior Knowledge and Observed Data. These are used to calculate the likelihood and are then combined to form the posterior distribution. The final step is an optimization process to find the parameter value that maximizes this posterior, yielding the MAP estimate.

The Log-Posterior and the Role of the Prior

Just as with MLE, working with products of probabilities is cumbersome. We can simplify the objective by taking the logarithm. Since the logarithm is a monotonically increasing function, maximizing the posterior is equivalent to maximizing the log-posterior:

Compare this to the log-likelihood objective from our last lesson:

The only difference is the addition of the log-prior term, . This term acts as a "guide" or a penalty, pulling the final estimate towards regions of the parameter space that we believed were more likely to begin with.

For a more formal breakdown of this formulation, you can consult the following lecture notes.

[PDF] Maximum Aposteriori (MAP) Estimate 5.1 Gradient Descent

These lecture notes from IIT Bombay provide a concise mathematical introduction to MAP.

Focus on section 5.5, starting from the definition of MAP. Then, review the definitions of Prior, Likelihood, and Posterior. Finally, pay close attention to the derivation that shows how the log-posterior is formed and compares it directly to the MLE objective in the paragraph starting with "Taking the logarithm".


MAP for Linear Regression: The Emergence of Regularization

Now for the main event. Let's apply this framework to a linear regression model. You'll see how choosing a specific prior for our regression weights naturally leads to the L2 regularization you know as Ridge Regression. This connection is one of the most elegant results in introductory machine learning theory.

The following video from the channel ritvikmath provides an outstanding explanation of this entire process. It begins with a review of regularized regression and then masterfully builds the Bayesian argument that leads to the same place.

Bayesian Linear Regression : Data Science Concepts

This video, "Bayesian Linear Regression," will walk us through the derivation and intuition connecting MAP estimation to Ridge Regression.

First, watch the introduction and review of the standard linear regression model and regularization from the setup. This will frame the problem that the Bayesian approach solves. Next, focus on how the posterior probability is defined and simplified using Bayes' theorem from the Bayesian approach. Pay close attention to the crucial step where a Gaussian prior is chosen for the model's weights from the prior. Notice the argument for why this encourages smaller weights. Finally, watch the conclusion of the derivation from the result, where the log-posterior objective is shown to be identical to the Ridge Regression objective function. It's also worth watching the "sanity check" from the sanity check, which explains the relationship between the prior's variance and the regularization strength \lambda.


Unpacking the Derivation

Let's summarize the beautiful result from the video.

  1. Likelihood: We assume the standard linear regression model , with Gaussian noise . This gives us a Gaussian likelihood . Maximizing the log of this likelihood alone leads to the ordinary least squares (OLS) solution, which is the MLE for linear regression. The objective is to minimize the Residual Sum of Squares (RSS):

  2. Prior: We place a Gaussian prior on our weights, assuming they are centered at zero: . This expresses our prior belief that smaller weights are more likely than larger ones. The log of this prior is:

  3. Posterior: Combining the log-likelihood and the log-prior, the MAP objective becomes minimizing the negative log-posterior:

This is exactly the objective function for Ridge Regression, where the regularization parameter is equal to the ratio of the noise variance to the prior variance, .

This gives us a powerful Bayesian interpretation of L2 regularization: it is equivalent to performing MAP estimation with a zero-mean Gaussian prior on the model weights. The strength of the regularization, , is determined by how strongly we believe the weights should be close to zero (a small prior variance ) relative to the amount of noise in the data ().

The following article provides a practical code demonstration, showing how sklearn.linear_model.Ridge is, in fact, an implementation of this MAP estimate.

Maximum A Posteriori (MAP) Estimation - Clearly Explained

This article from MachineLearning+, "Maximum A Posteriori (MAP) Estimation - Clearly Explained", demonstrates the connection with Python code.

Please read section 6, "MAP in Linear Regression...". You can skim the code, but focus on the comparison between the coefficients found by LinearRegression (MLE) and Ridge (MAP). This shows the practical outcome of adding the prior.

As the video and other resources hint, choosing a different prior leads to a different form of regularization. For instance, if we had chosen a Laplace prior on the weights, which is more "peaked" at zero, the MAP estimate would have corresponded to Lasso (L1) regression.


Conclusion

In this lesson, you have connected several fundamental concepts. You've learned that Maximum A Posteriori (MAP) estimation extends MLE by incorporating prior beliefs about parameters. Most importantly, you have seen the direct mathematical link between MAP estimation for linear regression and Ridge (L2) regularization. A Gaussian prior on the weights is not just an arbitrary choice; it is the Bayesian justification for the L2 penalty term that you have likely used many times in practice. This insight elevates regularization from a mere "trick" to prevent overfitting to a principled application of Bayesian inference.

In our next lesson, we will dive deeper into the practicalities of Bayesian updates by exploring the concept of conjugate priors. You will learn how choosing a specific mathematical pairing of a prior and a likelihood can make calculating the posterior distribution surprisingly simple. We will demonstrate this by deriving the posterior for a classic Beta-Binomial model.