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?
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.
Ridge regression adds a penalty on the size of the weights themselves to the squared-error loss being minimized:
- SSE() — the sum of squared errors of the fit, same as ordinary least squares.
- — the penalty strength. recovers plain OLS; larger shrinks weights harder.
- — the model's -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 added to the diagonal of the normal equations: .
- Collinearity makes X^TX nearly singular
When two predictors are highly correlated, is close to singular — its inverse blows up, and tiny changes in the data produce wildly different "optimal" weights.
- Adding λ to the diagonal fixes that directly
Adding to every diagonal entry pulls away from singular, however close the raw predictors are to collinear — the matrix stays invertible and the solution stays finite and stable.
- 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 grows it pushes their weights toward being equal, sharing the job between them instead of letting one dominate.
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.
Two predictors, and — correlated at — fit to with no intercept.
- Unregularized: an unstable, sign-flipped fit
The normal equations , with the numbers plugged in:
Solving by Cramer's rule: the determinant is , so:
Both predictors rise together, yet one gets a strongly negative weight — a symptom of collinearity, not a real pattern in the data.
- A modest penalty (λ = 1) already fixes the sign
Adding to the diagonal:
Determinant: . Solving:
Both positive, and much closer together.
- A strong penalty (λ = 5) makes them nearly equal
At :
Determinant: . Solving:
Ridge has split the credit between the two near-identical predictors almost exactly evenly, exactly as the theory predicts.
Increase λ until both weights are positive — undoing the sign flip caused by the two nearly-identical predictors.
Ridge regression's job is stability, not sparsity: by adding 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.