Ridge & Lasso Regression

Complete derivation of penalized least squares: the ridge closed-form solution, spectral shrinkage, Lasso sparsity via subgradient conditions, coordinate descent, solution path behavior, and the geometry of constrained optimization.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Ridge Regression: Derivation
  5. Spectral Analysis of Ridge
  6. Ridge Bias-Variance Tradeoff
  7. Lasso Regression: Formulation
  8. Subgradient Optimality and Sparsity
  9. Coordinate Descent for Lasso
  10. The Regularization Path
  11. Geometric Interpretation
  12. Model Selection: Choosing Lambda
  13. Common Pitfalls
  14. Summary
  15. Exercises

Learning Objectives

  1. Derive the ridge regression estimator and its relationship to SVD.
  2. Quantify how ridge shrinks eigencomponents differentially.
  3. Derive the subgradient optimality condition for Lasso.
  4. Implement coordinate descent conceptually.
  5. Explain why L1 produces exactly zero coefficients but L2 does not.
  6. Describe the full regularization path as λ\lambda varies.

Notation

  • XRn×d\mathbf{X} \in \mathbb{R}^{n \times d} — design matrix (rows = samples)
  • yRn\mathbf{y} \in \mathbb{R}^n — response vector
  • w^λ\hat{\mathbf{w}}_\lambda — regularized coefficient estimate
  • σ1σd\sigma_1 \geq \cdots \geq \sigma_d — singular values of X\mathbf{X}
  • w1\partial\lVert\mathbf{w}\rVert_1 — subdifferential of L1 norm

Core Intuition

When features are collinear or d>nd > n, ordinary least squares is ill-conditioned or non-unique. Ridge regression adds a quadratic penalty that "rounds" the eigenvalues of XTX\mathbf{X}^T\mathbf{X}, stabilizing inversion. Lasso adds an absolute-value penalty that drives some coefficients to exactly zero, performing automatic feature selection.

Ridge vs Lasso Geometry

OLSRidgeLassow₁w₂
Ridge (0.67, 0.43)
Lasso (0.60, 0.10)
λ
0.80
L2 ballL1 diamond
Explore: Lasso hits diamond corners → sparse coefficients (one weight → 0). Ridge shrinks smoothly along the L2 circle.

Ridge Regression: Derivation

The ridge objective:

J(w)=12Xwy22+λ2w22.(1)J(\mathbf{w}) = \frac{1}{2}\lVert\mathbf{X}\mathbf{w} - \mathbf{y}\rVert_2^2 + \frac{\lambda}{2}\lVert\mathbf{w}\rVert_2^2. \tag{1}

Taking the gradient and setting to zero:

J=XTXwXTy+λw=0\nabla J = \mathbf{X}^T\mathbf{X}\mathbf{w} - \mathbf{X}^T\mathbf{y} + \lambda\mathbf{w} = \mathbf{0} w^ridge=(XTX+λI)1XTy.(2)\hat{\mathbf{w}}_{\text{ridge}} = (\mathbf{X}^T\mathbf{X} + \lambda\mathbf{I})^{-1}\mathbf{X}^T\mathbf{y}. \tag{2}

The matrix XTX+λI\mathbf{X}^T\mathbf{X} + \lambda\mathbf{I} is always invertible for λ>0\lambda > 0, even when XTX\mathbf{X}^T\mathbf{X} is singular.


Spectral Analysis of Ridge

Let X=UΣVT\mathbf{X} = \mathbf{U}\boldsymbol\Sigma\mathbf{V}^T with singular values σ1,,σmin(n,d)\sigma_1, \ldots, \sigma_{\min(n,d)}. Then:

w^ridge=Vdiag ⁣(σjσj2+λ)UTy=j=1dσj2σj2+λujTyσjvj.(3)\hat{\mathbf{w}}_{\text{ridge}} = \mathbf{V}\,\text{diag}\!\left(\frac{\sigma_j}{\sigma_j^2 + \lambda}\right)\mathbf{U}^T\mathbf{y} = \sum_{j=1}^d \frac{\sigma_j^2}{\sigma_j^2 + \lambda}\cdot\frac{\mathbf{u}_j^T\mathbf{y}}{\sigma_j}\cdot\mathbf{v}_j. \tag{3}

Shrinkage factor: sj=σj2σj2+λ(0,1)s_j = \frac{\sigma_j^2}{\sigma_j^2 + \lambda} \in (0, 1).

  • Directions with large σj\sigma_j (strong signal): sj1s_j \approx 1 → barely shrunk.
  • Directions with small σj\sigma_j (noise): sj0s_j \approx 0 → heavily shrunk.
  • Ridge acts as a soft spectral filter.

Ridge Bias-Variance Tradeoff

Bias. Bias(w^ridge)=λ(XTX+λI)1w\text{Bias}(\hat{\mathbf{w}}_{\text{ridge}}) = -\lambda(\mathbf{X}^T\mathbf{X} + \lambda\mathbf{I})^{-1}\mathbf{w}^* where w\mathbf{w}^* is the true parameter.

Variance. Var(w^ridge)=σ2(XTX+λI)1XTX(XTX+λI)1\text{Var}(\hat{\mathbf{w}}_{\text{ridge}}) = \sigma^2(\mathbf{X}^T\mathbf{X} + \lambda\mathbf{I})^{-1}\mathbf{X}^T\mathbf{X}(\mathbf{X}^T\mathbf{X} + \lambda\mathbf{I})^{-1}.

The MSE is minimized at λ>0\lambda^* > 0 whenever w0\mathbf{w}^* \neq \mathbf{0} — ridge always improves over OLS in terms of MSE (a consequence of the Gauss–Markov theorem not applying to MSE under bias).


Lasso Regression: Formulation

w^lasso=argminw12Xwy22+λw1.(4)\hat{\mathbf{w}}_{\text{lasso}} = \arg\min_{\mathbf{w}} \frac{1}{2}\lVert\mathbf{X}\mathbf{w} - \mathbf{y}\rVert_2^2 + \lambda\lVert\mathbf{w}\rVert_1. \tag{4}

The L1 penalty is convex but not differentiable at wj=0w_j = 0.


Subgradient Optimality and Sparsity

The optimality condition uses the subdifferential:

0XT(Xw^y)+λw^1,(5)\mathbf{0} \in \mathbf{X}^T(\mathbf{X}\hat{\mathbf{w}} - \mathbf{y}) + \lambda\,\partial\lVert\hat{\mathbf{w}}\rVert_1, \tag{5}

where wj={{+1}wj>0{1}wj<0[1,+1]wj=0\partial|w_j| = \begin{cases}\{+1\} & w_j > 0 \\ \{-1\} & w_j < 0 \\ [-1, +1] & w_j = 0\end{cases}.

For coordinate jj, let rj=xjT(yXjw^j)r_j = \mathbf{x}_j^T(\mathbf{y} - \mathbf{X}_{-j}\hat{\mathbf{w}}_{-j}) (partial residual correlation):

w^j={rjλxj2if rj>λrj+λxj2if rj<λ0if rjλ(6)\hat{w}_j = \begin{cases} \frac{r_j - \lambda}{\lVert\mathbf{x}_j\rVert^2} & \text{if } r_j > \lambda \\ \frac{r_j + \lambda}{\lVert\mathbf{x}_j\rVert^2} & \text{if } r_j < -\lambda \\ 0 & \text{if } |r_j| \leq \lambda \end{cases} \tag{6}

Sparsity mechanism: If the correlation rjr_j between feature jj and the residual is smaller than λ\lambda in magnitude, the coefficient is set to exactly zero.


Coordinate Descent for Lasso

Algorithm. Repeat until convergence:

For j=1,,dj = 1, \ldots, d:

  1. Compute partial residual: r(j)=ykjxkw^k\mathbf{r}^{(j)} = \mathbf{y} - \sum_{k \neq j}\mathbf{x}_k\hat{w}_k
  2. Update: w^jSλ(xjTr(j))/xj2\hat{w}_j \leftarrow S_\lambda(\mathbf{x}_j^T\mathbf{r}^{(j)}) / \lVert\mathbf{x}_j\rVert^2

where Sλ(z)=sign(z)max(zλ,0)S_\lambda(z) = \text{sign}(z)\max(|z| - \lambda, 0) is the soft-thresholding operator.

Convergence. Coordinate descent converges to the global minimum because the Lasso objective is convex (but not strictly convex when d>nd > n, so the solution may not be unique).


The Regularization Path

As λ\lambda decreases from λmax=XTy\lambda_{\max} = \lVert\mathbf{X}^T\mathbf{y}\rVert_\infty (all coefficients zero) to 0:

  • Coefficients enter the model one by one at breakpoints.
  • The path is piecewise linear (LARS algorithm exploits this).
  • Ridge path: coefficients shrink continuously, never reach zero.
  • Lasso path: coefficients shrink and hit zero at finite λ\lambda.

Geometric Interpretation

Constrained form (equivalent by Lagrangian duality):

  • Ridge: minimize Xwy2\lVert\mathbf{X}\mathbf{w} - \mathbf{y}\rVert^2 subject to w22t\lVert\mathbf{w}\rVert_2^2 \leq t
  • Lasso: minimize Xwy2\lVert\mathbf{X}\mathbf{w} - \mathbf{y}\rVert^2 subject to w1t\lVert\mathbf{w}\rVert_1 \leq t

The L2 constraint is a sphere — the ellipsoidal loss contours touch it smoothly, rarely at coordinate axes. The L1 constraint is a diamond — contact occurs at vertices (coordinate axes), producing zeros.


Model Selection: Choosing Lambda

The optimal λ\lambda is selected via cross-validation:

  1. For a grid of λ\lambda values λ1>λ2>>λK\lambda_1 > \lambda_2 > \cdots > \lambda_K
  2. For each λ\lambda, compute KK-fold CV error
  3. Select λ\lambda minimizing CV error (or "one-SE rule": largest λ\lambda within one standard error of minimum)

Information criteria (AIC, BIC) can also be used:

BIC(λ)=nlog(R^n(w^λ))+w^λ0logn.(7)\text{BIC}(\lambda) = n\log(\hat{R}_n(\hat{\mathbf{w}}_\lambda)) + \lVert\hat{\mathbf{w}}_\lambda\rVert_0 \cdot \log n. \tag{7}

Common Pitfalls

Pitfall 1. Applying Lasso without standardizing features. L1 penalizes wj|w_j| equally for all jj; features on larger scales get penalized more in absolute terms.

Pitfall 2. Expecting Lasso to handle groups of correlated features. It tends to select one and zero out the rest. Use Elastic Net for grouped selection.

Pitfall 3. Comparing ridge and Lasso MSE without context. Ridge is better when all features contribute; Lasso wins when true model is sparse.


Summary

  • Ridge adds λw22\lambda\lVert\mathbf{w}\rVert_2^2: always has closed-form, shrinks all coefficients, stabilizes ill-conditioning.
  • Lasso adds λw1\lambda\lVert\mathbf{w}\rVert_1: produces sparse solutions, solved by coordinate descent.
  • Sparsity comes from the geometry of the L1 ball (corners at coordinate axes).
  • Both can be seen as constrained optimization with different constraint sets.
  • Choose λ\lambda via cross-validation.

Exercises

Exercise 1. Starting from the SVD X=UΣVT\mathbf{X} = \mathbf{U}\boldsymbol\Sigma\mathbf{V}^T, derive equation (3) for the ridge solution.

Exercise 2. Show that ridge always reduces variance relative to OLS: tr(Var(w^ridge))tr(Var(w^OLS))\text{tr}(\text{Var}(\hat{\mathbf{w}}_{\text{ridge}})) \leq \text{tr}(\text{Var}(\hat{\mathbf{w}}_{\text{OLS}})).

Exercise 3. For orthonormal X\mathbf{X} (XTX=I\mathbf{X}^T\mathbf{X} = \mathbf{I}), derive the closed-form Lasso solution and compare with ridge.

Exercise 4. Prove that λmax=XTy\lambda_{\max} = \lVert\mathbf{X}^T\mathbf{y}\rVert_\infty is the smallest λ\lambda for which w^lasso=0\hat{\mathbf{w}}_{\text{lasso}} = \mathbf{0}.

Exercise 5. Show that Elastic Net can be reformulated as a Lasso on an augmented dataset.