2.2.15Linear & Logistic Regression

Elastic Net regularization

1,860 words8 min readdifficulty · medium3 backlinks

WHAT is it?


WHY combine them? (first-principles reasoning)

Why L1 gives sparsity: the L1 ball has corners on the axes. When the loss contour first touches this ball, it tends to touch at a corner → some coordinates become exactly 00.

Why L1 alone fails on correlated features: if x1x2x_1 \approx x_2, the loss is nearly flat along the direction w1+w2=constw_1 + w_2 = \text{const}. L1's penalty w1+w2|w_1|+|w_2| is also constant along that direction (as long as signs match), so the solution is not unique — it wobbles arbitrarily.

Why adding L2 fixes it: the L2 penalty w12+w22w_1^2 + w_2^2 is strictly convex and is minimized (for fixed sum) when w1=w2w_1 = w_2. So the L2 term breaks the tie and spreads the weight evenly across correlated features. This is the grouping effect.


HOW to derive the update (soft-thresholding)

We derive the coordinate-descent update for one weight wjw_j. Assume features are standardized so 1nixij2=1\frac{1}{n}\sum_i x_{ij}^2 = 1.

Step 1 — isolate wjw_j. Fix all other weights. Define the partial residual (target minus everyone else's contribution): ri=yikjwkxik.r_i = y_i - \sum_{k\ne j} w_k x_{ik}. Why this step? Coordinate descent optimizes one variable at a time; everything not involving wjw_j is a constant.

Step 2 — write the 1-D objective. f(wj)=12ni(riwjxij)2+λαwj+λ(1α)2wj2f(w_j) = \frac{1}{2n}\sum_i (r_i - w_j x_{ij})^2 + \lambda\alpha |w_j| + \frac{\lambda(1-\alpha)}{2}w_j^2 Why this step? This is exactly JJ with only wjw_j free.

Step 3 — take the subgradient and set to 0. Let ρj=1nixijri\rho_j = \frac{1}{n}\sum_i x_{ij} r_i (correlation of feature jj with the partial residual). Using 1nxij2=1\frac{1}{n}\sum x_{ij}^2 = 1: 0ρj+wj+λ(1α)wj+λαwj0 \in -\rho_j + w_j + \lambda(1-\alpha)w_j + \lambda\alpha\,\partial|w_j| Why this step? The quadratic parts differentiate cleanly; wj|w_j| has subgradient sign(wj)\text{sign}(w_j) for wj0w_j\ne 0 and [1,1][-1,1] at 00.

Step 4 — solve, giving the soft-threshold. The minimizer is wj=S(ρj, λα)1+λ(1α),S(z,γ)=sign(z)max(zγ, 0)\boxed{\,w_j = \frac{S(\rho_j,\ \lambda\alpha)}{1 + \lambda(1-\alpha)}\,}, \qquad S(z,\gamma)=\text{sign}(z)\,\max(|z|-\gamma,\ 0) Why this step? SS is the soft-thresholding operator: the L1 term shrinks ρj\rho_j toward zero by λα\lambda\alpha (and kills it if ρjλα|\rho_j|\le\lambda\alpha), while the L2 term divides by 1+λ(1α)1+\lambda(1-\alpha), an extra proportional shrink.

Figure — Elastic Net regularization

Worked examples



Active recall

Recall Forecast-then-Verify: predict before revealing

Q: If α=0\alpha=0, which method do you get, and does it produce zeros? A: Pure Ridge; it shrinks but never sets weights exactly to zero.

Q: Two perfectly correlated features — what does Elastic Net do with the weight? A: Splits it (roughly) equally between them (grouping effect), thanks to the L2 term.

Recall Feynman: explain to a 12-year-old

Imagine you're packing a backpack for a trip and you must pick which gadgets to bring. Lasso is a strict friend: "pick ONE flashlight, throw the other two away" — even if all three are equally good, and next time he might pick a different one at random. Ridge is a soft friend: "bring smaller versions of everything, nobody gets left home." Elastic Net is the wise friend: "throw out the truly useless junk (like Lasso), but for the three equally-good flashlights, just bring three small ones so it's fair and stable (like Ridge)." That fairness for equally-good items is why it doesn't panic when things look alike.


The 80/20 core

  1. J=MSE+λ(αw1+1α2w22)J = \text{MSE} + \lambda(\alpha\|w\|_1 + \tfrac{1-\alpha}{2}\|w\|_2^2).
  2. Update = soft-threshold (numerator, L1) over 1+λ(1α)1+\lambda(1-\alpha) (denominator, L2).
  3. L1 → sparsity; L2 → grouping/stability; Elastic Net → both.
  4. Tune λ\lambda (strength) and α\alpha (flavor) by CV; standardize first.

Connections

  • Ridge Regression — the α=0\alpha=0 special case (L2 only).
  • Lasso Regression — the α=1\alpha=1 special case (L1 only).
  • Soft-Thresholding Operator — the proximal operator powering the update.
  • Coordinate Descent — the algorithm used to solve it.
  • Bias-Variance Tradeoff — regularization trades variance for bias.
  • Feature Correlation & Multicollinearity — the problem Elastic Net specifically tames.
  • Cross-Validation — how λ,α\lambda,\alpha are chosen.

What penalty does Elastic Net add to the loss?
A convex combination λ(αw1+1α2w22)\lambda(\alpha\|w\|_1 + \tfrac{1-\alpha}{2}\|w\|_2^2) — both L1 and L2.
What does α\alpha control in Elastic Net?
The mixing ratio between L1 and L2; α=1\alpha=1 is Lasso, α=0\alpha=0 is Ridge.
What does λ\lambda control?
The overall strength of regularization (independent of the L1/L2 flavor).
Why does Lasso struggle with correlated features?
Its objective is flat along the correlated direction, so selection is non-unique/unstable.
How does the L2 term fix correlated features?
L2 is strictly convex and minimized when equal weights, so it splits weight evenly (grouping effect).
Write the coordinate-descent update for wjw_j.
wj=S(ρj,λα)/(1+λ(1α))w_j = S(\rho_j,\lambda\alpha)/(1+\lambda(1-\alpha)) where SS is soft-thresholding.
What is the soft-thresholding operator S(z,γ)S(z,\gamma)?
sign(z)max(zγ,0)\text{sign}(z)\max(|z|-\gamma,0) — shrinks toward zero, sets to zero if zγ|z|\le\gamma.
Which part of the update gives sparsity vs stability?
Numerator (L1 soft-threshold) → sparsity; denominator 1+λ(1α)1+\lambda(1-\alpha) (L2) → stability/grouping.
Must you standardize features before Elastic Net?
Yes — penalties are not scale-invariant, so unstandardized scales bias which features get penalized.
If ρj=0.3\rho_j=0.3, λ=1\lambda=1, α=0.5\alpha=0.5, what is wjw_j?
00, because 0.3<λα=0.5|0.3|<\lambda\alpha=0.5 so the soft-threshold is zero.

Concept Map

gives

fails on

shrinks but

strictly convex fixes

produces

convex combination of

convex combination of

controlled by

alpha=1

alpha=0

scaled by

solved via

yields

achieves

achieves

L1 Lasso penalty

Sparsity / zero weights

Correlated features

L2 Ridge penalty

Never zeroes weights

Grouping effect

Elastic Net

alpha mixing ratio 0 to 1

lambda overall strength

Coordinate descent

Soft-thresholding update

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Elastic Net basically Lasso (L1) aur Ridge (L2) ka combo hai. Problem yeh hai ki Lasso akela to features ko zero kar deta hai (sparsity milti hai, achhi baat), lekin jab do features aapas mein bahut correlated ho — jaise ek hi cheez do baar aa gayi — to Lasso confuse ho jaata hai aur randomly kisi ek ko rakh ke baaki ko phenk deta hai. Ridge iss situation ko handle kar leta hai, weight ko fairly baant deta hai, lekin Ridge kabhi kisi weight ko exactly zero nahi karta, so feature selection nahi hoti.

Elastic Net kehta hai "dono ka faayda le lo": loss mein αw1+1α2w22\alpha\|w\|_1 + \tfrac{1-\alpha}{2}\|w\|_2^2 dono add kar do. Yahan λ\lambda decide karta hai kitna regularization, aur α\alpha decide karta hai kaunsa flavor (α=1 pura Lasso, α=0 pura Ridge). Iska magic result hai grouping effect: agar do features ek jaise hain to L2 term unke beech weight ko barabar baant deta hai, aur L1 term bekaar features ko poori tarah zero kar deta hai. Best of both worlds.

Update rule bhi simple hai: wj=S(ρj,λα)/(1+λ(1α))w_j = S(\rho_j,\lambda\alpha)/(1+\lambda(1-\alpha)). Upar wala soft-thresholding SS chhote correlation waale features ko maar deta hai (sparsity), aur neeche wala 1+λ(1α)1+\lambda(1-\alpha) baaki ko thoda dabaa deta hai (stability). Ek important baat — features ko pehle standardize zaroor karo, warna kilometre vs metre jaise scale differences se penalty galat lag jaayegi. Aur λ\lambda, α\alpha dono ko cross-validation se tune karo, guess se nahi.

Go deeper — visual, from zero

Test yourself — Linear & Logistic Regression

Connections