01 / 21

Ridge & Lasso
Regularization

Learn how penalty terms tame overfitting by shrinking coefficients: the \(\ell_2\) circle of Ridge and the \(\ell_1\) diamond of Lasso.

Machine Learning Graduate Level

The Overfitting Problem

In ordinary least squares (OLS), we minimize the residual sum of squares: \(\text{RSS} = \sum_{i=1}^{n}(y_i - \hat{y}_i)^2\). With many predictors, OLS fits the training data perfectly but generalizes poorly.

Regularization adds a penalty to the loss function, forcing coefficients toward zero. This trades a small increase in bias for a large decrease in variance.

What Is Regularization?

Penalized Loss
Add a term \(\lambda \cdot P(\boldsymbol{\beta})\) to the loss, where \(\lambda \geq 0\) controls penalty strength.
Shrinkage
Coefficients are "shrunk" toward zero. Larger \(\lambda\) means stronger shrinkage, simpler models.
Bias-Variance
We accept a small bias increase to dramatically reduce variance and improve out-of-sample prediction.

Two Norms, Two Methods

\(\ell_2\) Norm: Ridge
Penalty = \(\lambda \sum \beta_j^2\)
Constraint region is a circle (or hypersphere). Shrinks coefficients smoothly toward zero but never exactly to zero.
\(\ell_1\) Norm: Lasso
Penalty = \(\lambda \sum |\beta_j|\)
Constraint region is a diamond. Can shrink coefficients exactly to zero, performing automatic feature selection.
\(\ell_2\) (Ridge)
\(\beta_1\)
\(\beta_2\)
\(\ell_1\) (Lasso)
\(\beta_1\)
\(\beta_2\)

Ridge Regression: The \(\ell_2\) Penalty

Ridge has a closed-form solution: $$\hat{\boldsymbol{\beta}}^{\text{ridge}} = \left(\mathbf{X}^T\mathbf{X} + \lambda \mathbf{I}\right)^{-1}\mathbf{X}^T\mathbf{y}$$

1
When \(\lambda = 0\): reduces to ordinary least squares (OLS).
2
As \(\lambda \to \infty\): all \(\hat{\beta}_j \to 0\), leaving only the intercept.
3
Key property: \(\lambda\mathbf{I}\) makes the matrix invertible even when \(\mathbf{X}^T\mathbf{X}\) is singular (multicollinearity).

Lasso Regression: The \(\ell_1\) Penalty

Unlike Ridge, Lasso has no closed-form solution because the \(|\beta_j|\) terms are not differentiable at zero. It requires iterative algorithms like coordinate descent.

1
Sparsity: Lasso drives coefficients exactly to zero, selecting a subset of features automatically.
2
Interpretability: The resulting model is simpler: irrelevant predictors are removed entirely.
3
Limitation: With correlated predictors, Lasso tends to pick one and zero out the rest arbitrarily.

Geometric Intuition: Why Lasso Zeros Out

The OLS solution sits at the center of elliptical contours of constant RSS. Regularization constrains \(\boldsymbol{\beta}\) to lie within a region. The solution is where the contours first touch the constraint region.

Ridge (\(\ell_2\))
OLS Ridge
\(\beta_1\)
\(\beta_2\)
Lasso (\(\ell_1\))
OLS Lasso
\(\beta_1\)
\(\beta_2\)
Diamond corners sit on axes: \(\beta_1 = 0\) or \(\beta_2 = 0\)

Ridge vs. Lasso: Key Distinction

Ridge (\(\ell_2\)) Lasso (\(\ell_1\))
Penalty \(\lambda \sum \beta_j^2\) \(\lambda \sum |\beta_j|\)
Constraint Shape Circle / sphere Diamond / cross-polytope
Sparsity No: shrinks, never zeros Yes: exact zeros
Feature Selection No Yes
Closed Form Yes No (iterative)
Correlated Features Handles well Unstable selection
Best When Many small effects Few large effects

Tuning \(\lambda\): The Bias-Variance Trade-off

\(\lambda\) (regularization strength)
Error Train Test
Optimal \(\lambda\)
Low \(\lambda\) (overfit) High \(\lambda\) (underfit)

Coefficient Paths as \(\lambda\) Grows

Ridge Paths
\(\lambda \to\)
\(\beta_j\)
Lasso Paths
\(\lambda \to\)
\(\beta_j\)

Elastic Net: Best of Both

\(\alpha = 1\)
Pure Lasso: maximum sparsity, feature selection
\(\alpha = 0\)
Pure Ridge: no sparsity, handles multicollinearity
\(0 < \alpha < 1\)
Elastic Net: selects groups of correlated features together

Elastic Net overcomes Lasso's instability with correlated predictors by adding an \(\ell_2\) component that encourages grouping: correlated variables enter or leave the model together.

How to Apply Regularization

1
Standardize predictors: Regularization penalizes coefficient magnitude. If features have different scales, the penalty is unfair. Center and scale each \(x_j\) to mean 0, variance 1.
2
Choose the method: Ridge if you expect many small effects and correlated features. Lasso if you expect few relevant features. Elastic Net for correlated groups.
3
Select \(\lambda\) via cross-validation: Fit the model for a grid of \(\lambda\) values. Use \(k\)-fold CV (typically \(k = 10\)) to pick the \(\lambda\) minimizing CV error.
4
Refit on full data: Once \(\lambda\) is chosen, refit the model using all training data with that \(\lambda\) to get final coefficient estimates.
5
Interpret results: For Lasso, note which coefficients are exactly zero (dropped features). For Ridge, examine the relative magnitude of coefficients.

Example 1: Ridge with Two Predictors

Suppose we have standardized predictors \(x_1, x_2\) and the OLS estimates are \(\hat{\beta}_1^{\text{OLS}} = 3.2\) and \(\hat{\beta}_2^{\text{OLS}} = -1.8\). We apply Ridge with \(\lambda = 2\).

\(\hat{\beta}_1^{\text{ridge}}\)
\(\frac{3.2}{1+2} = \frac{3.2}{3} \approx 1.07\)
\(\hat{\beta}_2^{\text{ridge}}\)
\(\frac{-1.8}{1+2} = \frac{-1.8}{3} = -0.60\)

Example 2: Lasso Soft Thresholding

For a single predictor (orthonormal design), the Lasso solution is the soft-thresholding operator:

Using \(\hat{\beta}_1^{\text{OLS}} = 3.2\), \(\hat{\beta}_2^{\text{OLS}} = -1.8\), and \(\lambda = 4\):

\(\hat{\beta}_1^{\text{lasso}}\)
\(\text{sign}(3.2) \cdot \max(3.2 - \frac{4}{2},\, 0)\)
= \(+1 \cdot 1.2 = 1.2\)
\(\hat{\beta}_2^{\text{lasso}}\)
\(\text{sign}(-1.8) \cdot \max(1.8 - \frac{4}{2},\, 0)\)
= \(-1 \cdot 0 = 0\)

Example 3: Comparing Ridge vs. Lasso

A dataset has \(p = 5\) predictors with OLS coefficients \(\hat{\boldsymbol{\beta}}^{\text{OLS}} = (4.1,\; 0.3,\; -2.5,\; 0.1,\; 1.8)\). We set \(\lambda = 1\) for both methods (standardized design).

Predictor OLS Ridge Lasso
\(x_1\) 4.1 2.05 3.6
\(x_2\) 0.3 0.15 0
\(x_3\) -2.5 -1.25 -2.0
\(x_4\) 0.1 0.05 0
\(x_5\) 1.8 0.90 1.3

Common Mistakes

✕ Forgetting to Standardize
If predictors are on different scales, the penalty disproportionately shrinks features with larger units. Always standardize before regularizing.
✕ Penalizing the Intercept
The intercept \(\beta_0\) should not be penalized: it simply shifts the prediction. Most software excludes it by default, but always verify.
✕ Using Lasso for Grouped Features
Lasso arbitrarily picks one variable from a correlated group and zeros the rest. Use Elastic Net or Ridge when predictors are highly correlated.

Question 1 of 5

Which regularization method can set coefficients exactly to zero, thereby performing automatic feature selection?

Question 2 of 5

The Ridge regression closed-form solution is \(\hat{\boldsymbol{\beta}} = (\mathbf{X}^T\mathbf{X} + \lambda\mathbf{I})^{-1}\mathbf{X}^T\mathbf{y}\). What happens as \(\lambda \to \infty\)?

Question 3 of 5

Given OLS estimate \(\hat{\beta}^{\text{OLS}} = 2.5\) and \(\lambda = 5\), what is the Lasso estimate using the soft-thresholding formula for an orthonormal design?

Question 4 of 5

Why must predictors be standardized before applying Ridge or Lasso regression?

Question 5 of 5

Elastic Net combines \(\ell_1\) and \(\ell_2\) penalties. When is it most advantageous over using pure Lasso?