L1 and L2 regularization

core 11 minfundamentalsregularization

Builds on:Overfitting and generalization

The idea

An unregularized linear model will use any freedom it has — including noise directions — to shave training error. Regularization charges the model rent for coefficient size, so it only keeps weights the data genuinely pay for:

min⁡β  12∥y−Xβ∥22+λ∥β∥1(lasso),min⁡β  12∥y−Xβ∥22+λ2∥β∥22(ridge).\begin{aligned} \min_\beta \;& \tfrac{1}{2}\|y - X\beta\|_2^2 + \lambda\|\beta\|_1 && \text{(lasso)}, \\ \min_\beta \;& \tfrac{1}{2}\|y - X\beta\|_2^2 + \tfrac{\lambda}{2}\|\beta\|_2^2 && \text{(ridge)}. \end{aligned}

Both shrink coefficients toward zero and trade a little bias for a lot of variance reduction. The interview-famous difference: lasso sets coefficients exactly to zero; ridge only shrinks them. Two ways to see why:

Geometry. Penalized form is equivalent to minimizing RSS subject to a budget ∥β∥1≤t\|\beta\|_1 \le t (a diamond) or ∥β∥22≤t\|\beta\|_2^2 \le t (a disk). The solution sits where the elliptical RSS contours first touch the budget region. A diamond has corners on the axes, and expanding ellipses tend to strike a corner first — and at a corner, some coordinates are exactly zero. A disk has no corners, so the tangent point is generically off-axis: every coefficient survives, smaller.

Subgradients. At βj=0\beta_j = 0, the L1 term contributes any value in [−λ,λ][-\lambda, \lambda] to the gradient. If the data's pull on that coefficient satisfies ∣∂RSS/∂βj∣≤λ|\partial \text{RSS}/\partial \beta_j| \le \lambda, zero is optimal and stays optimal — zero is sticky. The ridge penalty's force is λβj\lambda\beta_j, which vanishes as βj→0\beta_j \to 0: it can push a coefficient arbitrarily close to zero but never hold it there.

Bayesian reading: ridge is MAP estimation under a Gaussian prior on β\beta; lasso under a Laplace prior, whose sharp peak at zero encodes the belief "most coefficients are exactly irrelevant."

Practicalities that interviewers probe: standardize features first (the penalty is unit-blind, coefficients are not), do not penalize the intercept, and remember lasso's instability on correlated features — it picks one member of a correlated group nearly arbitrarily, while ridge splits weight across the group. Elastic net, λ1∥β∥1+λ22∥β∥22\lambda_1\|\beta\|_1 + \tfrac{\lambda_2}{2}\|\beta\|_2^2, buys sparsity and grouping together.

Worked example

With an orthonormal design (X⊤X=IX^{\top}X = I) the solutions are closed-form, so you can watch both penalties act on the same OLS fit. Take β^OLS=(3.0,  0.4,  −1.2)\hat{\beta}^{\text{OLS}} = (3.0,\; 0.4,\; -1.2) and λ=0.5\lambda = 0.5:

Ridge divides everything by 1+λ=1.51 + \lambda = 1.5:

β^ridge=(2.0,  0.27,  −0.8)\hat{\beta}^{\text{ridge}} = (2.0,\; 0.27,\; -0.8)

Lasso soft-thresholds — move each coefficient 0.50.5 toward zero, clip at zero:

β^lasso=(2.5,  0,  −0.7)\hat{\beta}^{\text{lasso}} = (2.5,\; 0,\; -0.7)

CoefficientOLSRidge (/1.5/1.5)Lasso (shift by 0.50.5)
β1\beta_13.02.02.5
β2\beta_20.40.270
β3\beta_3−1.2-1.2−0.8-0.8−0.7-0.7

The story in one row: β2=0.4\beta_2 = 0.4 is weaker than the threshold λ=0.5\lambda = 0.5, so lasso deletes it outright, while ridge keeps it at 0.270.27 — nonzero forever, no matter how large λ\lambda grows, it only approaches zero asymptotically. Also note ridge punished the largest coefficient hardest in absolute terms (3.0 lost 1.0; 0.4 lost 0.13): proportional shrinkage, versus lasso's flat "everyone pays 0.50.5".

Cheat sheet

Objectives: lasso 12∥y−Xβ∥22+λ∥β∥1\tfrac{1}{2}\|y-X\beta\|_2^2 + \lambda\|\beta\|_1; ridge 12∥y−Xβ∥22+λ2∥β∥22\tfrac{1}{2}\|y-X\beta\|_2^2 + \tfrac{\lambda}{2}\|\beta\|_2^2; elastic net adds both.

Orthonormal closed forms: ridge β^j/(1+λ)\hat{\beta}_j / (1+\lambda); lasso sign(β^j)max⁡(∣β^j∣−λ,0)\text{sign}(\hat{\beta}_j)\max(|\hat{\beta}_j| - \lambda, 0).

QuestionRidge (L2)Lasso (L1)
Exact zeros?neveryes — feature selection built in
Correlated featuresshares weight, stablepicks one arbitrarily, unstable
Closed form?yes: (X⊤X+λI)−1X⊤y(X^{\top}X + \lambda I)^{-1}X^{\top}yno — coordinate descent / LARS
PriorGaussianLaplace
Use whenmany small effects, correlated inputsfew strong effects, want sparsity