Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Ridge Regression: Formulation and Solution

Hello! Welcome to our next lesson.

In our previous session, we explored the coefficient of determination () as a measure of model fit. We concluded by noting a significant drawback: tends to increase as we add more variables, regardless of their actual predictive power. This can encourage overly complex models that fit the training data's noise rather than its underlying signal—a phenomenon known as overfitting.

Today, we will directly address this problem by introducing regularization, a powerful technique for creating simpler, more robust models. Our learning outcome is to formulate L2 (Ridge) regularization for linear regression and derive its closed-form solution. This will be our first step in moving beyond OLS to build models that generalize better to unseen data.

1. The Need for Regularization: Overfitting and Multicollinearity

The OLS estimator finds the coefficients that minimize the Residual Sum of Squares (RSS). While this is optimal under certain assumptions (as per the Gauss-Markov theorem), it can lead to problems in practice.

  1. Overfitting: When a model has many parameters (i.e., many features), it can become too flexible. It may start fitting random fluctuations in the training data, leading to large estimated coefficient values. Such a model performs poorly on new data because it has learned the noise, not the true relationship.
  2. Multicollinearity: As you know from your experience building regression models, features are often correlated. When features are highly correlated (high multicollinearity), the matrix becomes "ill-conditioned" or nearly singular. As we saw in the derivation of the OLS solution , this means its inverse is unstable. Small changes in the input data can cause massive swings in the estimated coefficients, increasing their variance and making the model unreliable.

Regularization provides a way to constrain the learning algorithm to prevent these issues. Specifically, Ridge Regression tackles this by adding a penalty for large coefficient values.

Ridge Regression (L2 Regularization): Complete Guide with ...

To begin, let's formally define the Ridge regression objective function. The article 'Ridge Regression (L2 Regularization): Complete Guide' provides a clear introduction.

Please read the sections 'L2 Regularization (Ridge)', 'Formula', and 'Summation Notation'. Focus on how the Ridge objective function is constructed by adding a penalty term to the standard Sum of Squared Errors (SSE/RSS). Pay close attention to the definition of the L2 penalty and the role of the regularization parameter λ.

2. Formulating the Ridge Regression Objective Function

As you just read, Ridge regression modifies the OLS cost function. Where OLS seeks only to minimize the sum of squared residuals, Ridge regression adds a second term: a penalty on the size of the coefficients.

The OLS cost function is:

Ridge regression adds an L2 penalty term, which is the squared L2-norm (Euclidean norm) of the coefficient vector, scaled by a hyperparameter (lambda). The L2-norm of is , so the penalty is .

The full Ridge Regression objective function is therefore:

  • The term pushes the model to fit the data well.
  • The term pushes the coefficients towards zero, penalizing complexity. This is often called shrinkage.
  • The regularization parameter controls the trade-off.
    • If , we recover the OLS objective function.
    • As , the penalty for non-zero coefficients becomes overwhelming, and the coefficients are shrunk to zero.

Note that the intercept term, , is typically not included in the penalty. Penalizing the intercept would mean shrinking it towards zero, which is undesirable as it is simply a measure of the baseline prediction when all features are zero. In practice, data is often centered before fitting, which makes the intercept represent the mean of the target variable.

3. Deriving the Closed-Form Solution

Your goal is to derive the solution with high mathematical rigor. Fortunately, like OLS, the Ridge objective function is convex and has a unique, closed-form solution. The derivation process is very similar to what we did for OLS: expand the objective function, take its gradient with respect to , set it to zero, and solve.

Let's write the objective function in matrix form:

Now, let's watch a video that walks through the derivation step-by-step.

Ridge Regression Derivation

The video 'Ridge Regression Derivation' provides an excellent and clear walkthrough of the algebra and matrix calculus needed to find the Ridge estimator.

Watch the entire video (00:00 to 05:24). The presenter methodically expands the objective function, takes the derivative with respect to beta, sets it to zero, and solves for beta. Follow each step closely, especially the matrix algebra.

Let's summarize and formalize the steps from the video.

Step 1: Expand the objective function.
As we did for OLS, we expand the RSS term:

So the full objective function is:

Step 2: Take the gradient with respect to .
We apply the rules of matrix calculus to each term:

Combining these gives the gradient:

Step 3: Set the gradient to zero and solve for .

Divide by 2 and move the term to the other side:

Now, we factor out . Crucially, as shown in the video, we cannot simply add the matrix and the scalar . We must multiply by the identity matrix of the same dimension () to ensure the matrix addition is conformable.

Step 4: Isolate .
To isolate , we pre-multiply both sides by the inverse of .
This gives us the final closed-form solution for the Ridge regression estimator:

The following image provides a concise visual summary of this derivation.

Derivation of Ridge Regression Closed-Form Solution
This diagram outlines the complete derivation of the Ridge regression estimator. It starts from the vectorized error function including the L2 penalty, expands it, computes the derivative with respect to the weight vector (here denoted 'w'), and solves for the optimal weights by setting the derivative to zero.

A key mathematical point for you: the Hessian of the Ridge objective function is . For , this matrix is guaranteed to be positive definite, which proves that our objective function is strictly convex. This ensures that the solution we found is a unique global minimum. This is also why the inverse always exists, even if itself is singular.

Test your understanding!

Suppose a design matrix has two columns that are perfectly collinear (e.g., one is twice the other).

  1. What is the problem with computing the OLS solution in this case?
  2. How does the Ridge formulation with resolve this problem mathematically?
Show answer
  1. With perfect collinearity, the matrix does not have full column rank. Consequently, the matrix is singular (its determinant is zero) and is not invertible. Therefore, the OLS solution is not unique and cannot be computed using the standard inverse formula.
  2. The Ridge formulation adds the term to . The resulting matrix is guaranteed to be invertible for any . Mathematically, is a positive semi-definite matrix, meaning its eigenvalues are . With collinearity, at least one eigenvalue is exactly 0, making it singular. Adding adds to every eigenvalue of . If , all eigenvalues of the new matrix become strictly positive, making the matrix positive definite and thus invertible. This provides a stable, unique solution even in the presence of severe multicollinearity.

Conclusion

In this lesson, we have formally introduced L2 regularization as a method to address the shortcomings of OLS, particularly overfitting and instability due to multicollinearity.

Key Takeaways:

  • Ridge regression adds an L2 penalty term, , to the OLS cost function. This penalizes large coefficient values, effectively "shrinking" them towards zero.
  • The objective is to minimize .
  • This optimization problem has a closed-form solution, the Ridge estimator: .
  • The addition of the term ensures that the matrix is always invertible for , providing a stable solution even when is singular.

Preview of the Next Lesson:
We have derived the Ridge estimator, but what are its properties? We've traded the OLS objective for a penalized one, which must have consequences. In the next lesson, we will explore the effect of L2 regularization on model parameters and the bias-variance trade-off. We will see that by introducing a small amount of bias into our estimates, Ridge regression can achieve a significant reduction in variance, often leading to a model with better overall predictive performance.

Can't find a good explanation? Sign up and we'll make it for you

Sign up