2.2.14Linear & Logistic Regression

L2 (Ridge) regularization

1,963 words9 min readdifficulty · medium1 backlinks

WHAT is it?

  • λ=0\lambda = 0 → ordinary least squares (no shrinkage).
  • λ\lambda \to \infty → all weights forced toward 00 (underfit).

WHY does it help? (Steel-manning the reason)


HOW: derive the closed form from scratch

We work in matrix form. Let XRn×dX\in\mathbb{R}^{n\times d} (rows = examples), yRn\mathbf{y}\in\mathbb{R}^n, weights wRd\mathbf{w}\in\mathbb{R}^d.

Step 1 — Write the cost. J(w)=(yXw)(yXw)+λwwJ(\mathbf{w}) = (\mathbf{y}-X\mathbf{w})^\top(\mathbf{y}-X\mathbf{w}) + \lambda\,\mathbf{w}^\top\mathbf{w} Why this step? yXw2=(yXw)(yXw)\|\mathbf{y}-X\mathbf{w}\|^2 = (\mathbf{y}-X\mathbf{w})^\top(\mathbf{y}-X\mathbf{w}), and wj2=ww\sum w_j^2 = \mathbf{w}^\top\mathbf{w}. Everything is now differentiable.

Step 2 — Expand. J=yy2wXy+wXXw+λwwJ = \mathbf{y}^\top\mathbf{y} - 2\mathbf{w}^\top X^\top \mathbf{y} + \mathbf{w}^\top X^\top X \mathbf{w} + \lambda \mathbf{w}^\top\mathbf{w} Why this step? Expanding lets us apply standard vector-derivative rules to each term.

Step 3 — Differentiate and set to zero. Using w(wAw)=2Aw\frac{\partial}{\partial \mathbf{w}}(\mathbf{w}^\top A \mathbf{w}) = 2A\mathbf{w} (for symmetric AA) and w(wb)=b\frac{\partial}{\partial \mathbf{w}}(\mathbf{w}^\top \mathbf{b}) = \mathbf{b}: wJ=2Xy+2XXw+2λw=0\nabla_{\mathbf{w}} J = -2X^\top\mathbf{y} + 2X^\top X\mathbf{w} + 2\lambda\mathbf{w} = 0 Why this step? At the minimum the gradient vanishes; the cost is convex (sum of convex quadratics), so this stationary point is the global minimum.

Step 4 — Solve. (XX+λI)w=Xy(X^\top X + \lambda I)\,\mathbf{w} = X^\top \mathbf{y}

Figure — L2 (Ridge) regularization

Shrinkage seen through the SVD (the deep view)

Let X=UΣVX = U\Sigma V^\top (SVD), with singular values σj\sigma_j. Then one can show the prediction is y^=jujσj2σj2+λujy.\hat{\mathbf{y}} = \sum_j \mathbf{u}_j \,\frac{\sigma_j^2}{\sigma_j^2 + \lambda}\, \mathbf{u}_j^\top \mathbf{y}.


Worked examples


Common mistakes


Constrained-optimization view


Flashcards

Ridge cost function
i(yiwxi)2+λjwj2\sum_i (y_i-\mathbf{w}^\top\mathbf{x}_i)^2 + \lambda\sum_j w_j^2
Ridge closed-form solution
w^=(XX+λI)1Xy\hat{\mathbf{w}}=(X^\top X+\lambda I)^{-1}X^\top\mathbf{y}
Why add λI\lambda I?
Makes XX+λIX^\top X+\lambda I invertible/positive-definite even when XXX^\top X is singular
What happens as λ\lambda\to\infty?
All weights shrink toward 00 (max bias, min variance, underfit)
What happens at λ=0\lambda=0?
Recovers ordinary least squares
Does Ridge give sparse weights?
No — it shrinks smoothly but rarely zeros weights; that's Lasso (L1)
How does Ridge treat correlated features?
Spreads weight evenly among them (smallest-norm split) instead of huge cancelling values
Bias–variance effect of increasing λ\lambda
Increases bias, decreases variance
How should λ\lambda be chosen?
Cross-validation on held-out data, not training error
Preprocessing needed before Ridge
Standardize features so the penalty is scale-fair
Should the intercept be penalized?
No — center the data / exclude w0w_0 from the penalty
SVD shrinkage factor per direction
σj2/(σj2+λ)\sigma_j^2/(\sigma_j^2+\lambda) — keeps strong directions, shrinks weak ones
1D Ridge weight formula
w=xiyixi2+λw=\frac{\sum x_iy_i}{\sum x_i^2+\lambda}
Constraint-form equivalent of Ridge
Minimize yXw2\|\mathbf{y}-X\mathbf{w}\|^2 s.t. w2t\|\mathbf{w}\|^2\le t

Recall Feynman: explain to a 12-year-old

Imagine building a paper tower to reach a mark. You could stack a crazy tall wobbly tower that just barely touches the mark — but it falls over if the wind (new data) blows. Ridge is a rule: "Every extra block you use costs money." So you build the shortest, sturdiest tower that still reaches close to the mark. It won't be perfect, but it won't topple when things change. The "cost per block" is λ\lambda: charge nothing and you build a monster; charge too much and you build almost nothing.

Connections

Concept Map

adds penalty

charges fee for weight size

controls

lambda=0

lambda to infinity

shrinks weights

raises slightly

cuts a lot

trade improves

trade improves

handles

spreads weight

convex cost solved

Ordinary Least Squares

Ridge Regression

Penalty lambda sum w squared

Lambda regularization strength

All weights toward 0

Smaller calmer weights

Bias

Variance

Lower test error

Correlated features

Closed form w = X^T X + lambda I inverse X^T y

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, plain least-squares regression ka ek problem hai: agar data me thoda noise ho ya features aapas me correlated ho, to model bahut bade-bade weights bana leta hai training data ko exactly fit karne ke chakkar me. Ye weights new data pe faad khaate hain — yani overfitting. Ridge (L2) regularization is problem ka ilaaj hai: hum cost function me ek extra "fee" jodte hain, λwj2\lambda \sum w_j^2. Matlab har weight ke square ke hisaab se penalty. Ab model chhote, shaant weights prefer karta hai jo generalize accha karte hain.

Formula seedha derive hota hai: cost ko w\mathbf{w} ke respect me differentiate karke zero rakho, to milta hai w^=(XX+λI)1Xy\hat{\mathbf{w}} = (X^\top X + \lambda I)^{-1} X^\top \mathbf{y}. Yaha λI\lambda I ka magic ye hai ki agar XXX^\top X singular (invert nahi ho raha) bhi ho, to +λI+\lambda I usko positive-definite bana deta hai — hamesha invertible. Isiliye naam "ridge" pada, kyunki diagonal ke upar ek ridge (mound) add karte ho.

Do cheezein yaad rakhna. Pehli — Ridge weights ko shrink karta hai, exactly zero nahi karta; sparsity chahiye to Lasso (L1) use karo. Dusri — λ\lambda choose karne ke liye cross-validation karo, training error se nahi, kyunki training error hamesha λ=0\lambda=0 bolega. Aur haan, features ko pehle standardize karo (zero mean, unit variance), warna meter vs millimeter wale features ko galat penalty milegi.

Simple analogy: paper ka tower banana hai jo ek mark tak pahunche. Bina rule ke tum ek lambi wobbly tower bana loge jo hawa aane pe gir jayegi. Ridge kehta hai "har block ka paisa lagega" — to tum sabse chhoti, mazboot tower banaoge jo hawa (new data) me bhi khadi rahe. λ\lambda hi wo "paisa" hai — balance zaroori hai.

Go deeper — visual, from zero

Test yourself — Linear & Logistic Regression

Connections