Part IV — Supervised Learning: Regression & Linear Classifiers · Chapter 3

Ridge regression (L2 regularization)

Hook

Two predictors that move together almost perfectly — a company's revenue in dollars and in thousands of dollars, say — shouldn't confuse a model. But ordinary least squares can hand one of them a huge positive weight and the other a huge negative one, and still call it the "best" fit. What's going on, and how do you stop it?

Intuition

These two predictors are correlated at nearly 0.998 — practically the same signal, twice. At λ=0, watch the fitted weights: one strongly positive, one strongly negative, even though both features move with the target in the same direction. Push λ up and watch that flip resolve into two ordinary, positive weights instead.

Formalize

Ridge regression adds a penalty on the size of the weights themselves to the squared-error loss being minimized:

SSE(w)+λjwj2\text{SSE}(\mathbf{w}) + \lambda \sum_j w_j^2
  • SSE(w\mathbf{w}) — the sum of squared errors of the fit, same as ordinary least squares.
  • λ\lambda — the penalty strength. λ=0\lambda=0 recovers plain OLS; larger λ\lambda shrinks weights harder.
  • wjw_j — the model's jj-th weight (the intercept, if there is one, is conventionally left unpenalized).

Minimizing this is still a quadratic problem, so it still has a closed form — just with λ\lambda added to the diagonal of the normal equations: (XX+λI)w=Xy(\mathbf{X}^\top\mathbf{X} + \lambda I)\mathbf{w} = \mathbf{X}^\top\mathbf{y}.

  1. Collinearity makes X^TX nearly singular

    When two predictors are highly correlated, XX\mathbf{X}^\top\mathbf{X} is close to singular — its inverse blows up, and tiny changes in the data produce wildly different "optimal" weights.

  2. Adding λ to the diagonal fixes that directly

    Adding λ\lambda to every diagonal entry pulls XX+λI\mathbf{X}^\top\mathbf{X} + \lambda I away from singular, however close the raw predictors are to collinear — the matrix stays invertible and the solution stays finite and stable.

  3. With correlated predictors, ridge splits credit evenly

    Ridge has no way to prefer one of two near-identical predictors over the other — its penalty treats them symmetrically — so as λ\lambda grows it pushes their weights toward being equal, sharing the job between them instead of letting one dominate.

Play

Watch the sum of squared errors (SSE) as you move λ: it barely changes, even while the two weights swing from wildly different to nearly identical. Near-perfect collinearity means many different weight combinations fit the data almost equally well — OLS just happens to land on an unstable one.

Worked example

Two predictors, x1=[1,2,3,4]x_1 = [1,2,3,4] and x2=[1,2,3,4.5]x_2 = [1,2,3,4.5] — correlated at 0.998\approx 0.998 — fit to y=[2,4,7,8]y=[2,4,7,8] with no intercept.

  1. Unregularized: an unstable, sign-flipped fit

    The normal equations XXw=Xy\mathbf{X}^\top\mathbf{X}\mathbf{w}=\mathbf{X}^\top\mathbf{y}, with the numbers plugged in:

    (30323234.25)(w1w2)=(6367)\begin{pmatrix}30&32\\32&34.25\end{pmatrix}\begin{pmatrix}w_1\\w_2\end{pmatrix} = \begin{pmatrix}63\\67\end{pmatrix}

    Solving by Cramer's rule: the determinant is 30(34.25)32(32)=1027.51024=3.530(34.25)-32(32)=1027.5-1024=3.5, so:

    • w1=(63(34.25)67(32))/3.5=13.75/3.53.929w_1=(63(34.25)-67(32))/3.5=13.75/3.5\approx3.929
    • w2=(30(67)32(63))/3.5=6/3.51.714w_2=(30(67)-32(63))/3.5=-6/3.5\approx-1.714

    Both predictors rise together, yet one gets a strongly negative weight — a symptom of collinearity, not a real pattern in the data.

  2. A modest penalty (λ = 1) already fixes the sign

    Adding λ=1\lambda=1 to the diagonal:

    (31323235.25)(w1w2)=(6367)\begin{pmatrix}31&32\\32&35.25\end{pmatrix}\begin{pmatrix}w_1\\w_2\end{pmatrix} = \begin{pmatrix}63\\67\end{pmatrix}

    Determinant: 31(35.25)32(32)=1092.751024=68.7531(35.25)-32(32)=1092.75-1024=68.75. Solving:

    • w1=(63(35.25)67(32))/68.75=76.75/68.751.116w_1=(63(35.25)-67(32))/68.75=76.75/68.75\approx1.116
    • w2=(31(67)32(63))/68.75=61/68.750.887w_2=(31(67)-32(63))/68.75=61/68.75\approx0.887

    Both positive, and much closer together.

  3. A strong penalty (λ = 5) makes them nearly equal

    At λ=5\lambda=5:

    (35323239.25)(w1w2)=(6367)\begin{pmatrix}35&32\\32&39.25\end{pmatrix}\begin{pmatrix}w_1\\w_2\end{pmatrix} = \begin{pmatrix}63\\67\end{pmatrix}

    Determinant: 35(39.25)32(32)=1373.751024=349.7535(39.25)-32(32)=1373.75-1024=349.75. Solving:

    • w1=(63(39.25)67(32))/349.75=328.75/349.750.940w_1=(63(39.25)-67(32))/349.75=328.75/349.75\approx0.940
    • w2=(35(67)32(63))/349.75=329/349.750.941w_2=(35(67)-32(63))/349.75=329/349.75\approx0.941

    Ridge has split the credit between the two near-identical predictors almost exactly evenly, exactly as the theory predicts.

Checkpoint

Increase λ until both weights are positive — undoing the sign flip caused by the two nearly-identical predictors.

Move the λ slider to try it
Summary
SSE(w)+λjwj2,(XX+λI)w=Xy\text{SSE}(\mathbf{w}) + \lambda \sum_j w_j^2, \qquad (\mathbf{X}^\top\mathbf{X}+\lambda I)\mathbf{w} = \mathbf{X}^\top \mathbf{y}

Ridge regression's job is stability, not sparsity: by adding λ\lambda to the diagonal of the normal equations, it keeps a near-singular system invertible and turns wildly unstable, sign-flipped weights into a sane, shared split of credit among correlated predictors.