Ridge Regression, In Full Depth
Ordinary linear regression (see Linear Regression) has a failure mode: when features are highly correlated, or there are more features than examples, becomes unstable — small changes in the data cause huge swings in the fitted weights. Ridge regression fixes this by penalizing large weights directly in the loss function.
What Is Ridge Regression?
Ridge is linear regression with one addition: a penalty term that discourages weights from growing large. The hypothesis is identical to plain linear regression — — only the cost function changes.
Why penalize large weights at all? A model with huge, wildly-swinging coefficients has usually latched onto noise in the training data rather than a real signal — a hallmark of overfitting (see Model Evaluation & Metrics — Bias-Variance Tradeoff). Shrinking the weights trades a small amount of bias for a large reduction in variance, which usually wins on unseen data.
The Ridge Cost Function
The first term is exactly the MSE cost function from plain linear regression. The second term — , the squared L2 norm of the weight vector (see Linear Algebra — Norms) — is new. controls the tradeoff: recovers plain OLS exactly; larger shrinks weights more aggressively toward zero. Note the bias is not penalized — only shrinking the feature weights makes sense, since just sets the overall output level.
Deriving the Gradient
The MSE part of the gradient is unchanged from Linear Regression — Section 3. The new penalty term adds :
The gradient descent update becomes:
Written this way, the mechanism is obvious: every single update step first shrinks by a factor of before applying the usual OLS correction. This is why ridge is sometimes called "weight decay" in the deep learning literature (see Training Deep Networks — Regularization) — it's the exact same mechanism.
The Closed-Form Solution
Ridge has a closed form too, a small modification of the normal equation:
This is why ridge regression exists mathematically, not just statistically: adding to before inverting guarantees the matrix is invertible, even when alone is singular or near-singular (highly correlated features, or more features than examples). Plain OLS can fail outright in that situation; ridge never does, for any .
The Regularization Path
As increases from 0, every weight shrinks smoothly toward zero — but critically, never exactly reaches zero (except in the limit ):
Compare this to Lasso's path — the visual difference between the two curves is the core conceptual difference between L2 and L1 regularization.
Try it on a real (small, synthetic) multicollinear dataset — drag from 0 and watch two deliberately correlated features fight over shared credit, then settle down:
Choosing λ
is a hyperparameter, not learned from the training data directly — it's chosen via cross-validation (see ML Workflow Fundamentals): train with several candidate values, and pick whichever generalizes best on a held-out validation set.
When to Use Ridge
- Many correlated features (ridge handles multicollinearity gracefully — plain OLS coefficients become unstable, ridge's don't)
- More features than examples, or close to it
- You want to keep all features in the model, just with controlled magnitude (contrast with Lasso, which drops features entirely)
Minimal Implementation
Next: Lasso Regression — the L1 alternative that drops features entirely instead of just shrinking them.