Skip to content
nonconvexcard

Linear, Ridge and Lasso Regression

The normal equations as orthogonal projection, the exact bias and variance of ridge, and why the unbiased estimator is rarely the best one.

Classical MLCore⏱ 3 hours12 min readleast squaresnormal equationsridgelassoprojection
5 problems →

Least squares

Given X∈Rn×pX\in\R^{n\times p} with rank⁡X=p\rank X = p and y∈Rny\in\R^n, minimise L(β)=12∥y−Xβ∥22L(\beta) = \tfrac12\norm{y - X\beta}_2^2.

TheoremNormal equations and the projection interpretation

LL is minimised uniquely at

β^=(X⊤X)−1X⊤y,\hat\beta = (X^\top X)^{-1}X^\top y,

characterised by the normal equations X⊤(y−Xβ^)=0X^\top(y - X\hat\beta)=0. The fitted values are y^=Xβ^=Hy\hat y = X\hat\beta = Hy with H=X(X⊤X)−1X⊤H = X(X^\top X)^{-1}X^\top the orthogonal projector onto col⁡(X)\operatorname{col}(X), and the residual y−y^y-\hat y is orthogonal to every column of XX.

Key idea

Least squares is projection; no calculus is needed. The normal equations are the statement "the residual is orthogonal to the model space", which is the projection theorem. Everything else — the Pythagorean decomposition of the sum of squares, the R2R^2 statistic, the geometry of ANOVA — follows from the same picture.

TheoremGauss–Markov theorem

Suppose y=Xβ⋆+εy = X\beta^\star+\varepsilon with Eε=0\E\varepsilon = 0 and Cov⁡(ε)=σ2In\Cov(\varepsilon)=\sigma^2I_n. Then β^\hat\beta is unbiased, Cov⁡(β^)=σ2(X⊤X)−1\Cov(\hat\beta) = \sigma^2(X^\top X)^{-1}, and for every other estimator of the form β~=Ay\tilde\beta = Ay that is unbiased for all β⋆\beta^\star,

Cov⁡(β~)⪰Cov⁡(β^).\Cov(\tilde\beta) \succeq \Cov(\hat\beta) .

Ridge regression

β^λ=arg min⁡β{12∥y−Xβ∥22+λ2∥β∥22}.\hat\beta_\lambda = \argmin_\beta\left\{\frac{1}{2}\norm{y-X\beta}_2^2 + \frac{\lambda}{2}\norm\beta_2^2\right\}.
PropositionRidge in closed form and in the SVD basis
β^λ=(X⊤X+λI)−1X⊤y,\hat\beta_\lambda = (X^\top X+\lambda I)^{-1}X^\top y ,

and if X=UΣV⊤X = U\Sigma V^\top is the SVD with singular values σj\sigma_j, then

β^λ=∑jσjσj2+λ ⟨uj,y⟩ vj,y^λ=∑jσj2σj2+λ⟨uj,y⟩uj.\hat\beta_\lambda = \sum_{j}\frac{\sigma_j}{\sigma_j^2+\lambda}\,\ip{u_j}{y}\,v_j, \qquad \hat y_\lambda = \sum_j \frac{\sigma_j^2}{\sigma_j^2+\lambda}\ip{u_j}{y}u_j .

The solution exists and is unique for every λ>0\lambda>0, even when rank⁡X<p\rank X<p.

Key idea

The shrinkage factor σj2/(σj2+λ)\sigma_j^2/(\sigma_j^2+\lambda) is near 11 for σj2≫λ\sigma_j^2\gg\lambda and near 00 for σj2≪λ\sigma_j^2\ll\lambda. Ridge leaves the well-determined directions alone and suppresses the poorly-determined ones — precisely the directions where, by Marchenko–Pastur, the sample covariance is least trustworthy. The effective number of parameters is ∑jσj2/(σj2+λ)\sum_j\sigma_j^2/(\sigma_j^2+\lambda), continuously interpolating between pp at λ=0\lambda=0 and 00 as λ→∞\lambda\to\infty.

TheoremThere is always a ridge parameter that beats least squares

Under the Gauss–Markov assumptions with rank⁡X=p\rank X = p, define m(λ)=E∥β^λ−β⋆∥22m(\lambda) = \E\norm{\hat\beta_\lambda - \beta^\star}_2^2. Then mm is differentiable at 00 with

m′(0)=−2σ2tr⁡((X⊤X)−2)<0,m'(0) = -2\sigma^2\tr\big((X^\top X)^{-2}\big) < 0 ,

so m(λ)<m(0)m(\lambda)<m(0) for all sufficiently small λ>0\lambda>0.

Key idea

Read the two terms. Bias grows like λ2\lambda^2 — second order — while variance falls like λ\lambda — first order. So at λ=0\lambda = 0 there is always a free lunch, and the unbiased estimator is inadmissible in mean squared error. The effect is largest when some σj\sigma_j is small, since the derivative scales as σj−4\sigma_j^{-4}, which is exactly the ill-conditioned case. This computation is the regression counterpart of the shrinkage result on the covariance estimation page, and both are instances of the James–Stein phenomenon.

Ridge versus lasso

PropositionWhy lasso is sparse and ridge is not

Consider the orthogonal design X⊤X=nIX^\top X = nI, where both problems decouple across coordinates. With z=X⊤y/nz = X^\top y/n,

β^jridge=zj1+λ,β^jlasso=sign⁡(zj)max⁡(∣zj∣−λ, 0).\hat\beta^{\text{ridge}}_j = \frac{z_j}{1+\lambda}, \qquad \hat\beta^{\text{lasso}}_j = \sign(z_j)\max\big(|z_j|-\lambda,\ 0\big) .

Ridge multiplies; lasso subtracts and truncates. Only the second can return exactly zero.

RidgeLasso
Penaltyλ∥β∥22\lambda\norm\beta_2^2λ∥β∥1\lambda\norm\beta_1
Closed formyesonly for orthogonal design
Sparsenoyes
Strictly convex objectiveyesno (unique solution not guaranteed)
Works when p>np>nyesyes
Rate under sparsityp/np/nslog⁡p/ns\log p/n
Prox operatorscalingsoft threshold

The rate comparison is the content of the lasso theorem: when the truth is sparse, lasso's slog⁡p/ns\log p/n beats ridge's p/np/n; when it is dense, ridge wins.

Problems

Least squares as orthogonal projectionProof · Foundation · 15 min Prove the Gauss–Markov theoremProof · Intermediate · 30 min Ridge regression in the SVD basisProof · Intermediate · 30 min There is always a ridge parameter beating least squaresProof · Advanced · 1 hour Implement ridge and find the optimal lambdaCoding · Intermediate · 1 hour

References

  • BookTrevor Hastie, Robert Tibshirani and Jerome Friedman (2009). The Elements of Statistical Learning
  • PaperArthur E. Hoerl and Robert W. Kennard (1970). Ridge Regression: Biased Estimation for Nonorthogonal Problems
  • PaperRobert Tibshirani (1996). Regression Shrinkage and Selection via the Lasso

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page

Problems (5)

All problems

Papers that use this mathematics