Regularization
Regularization fits a linear model by least squares plus a penalty on the size of the coefficients. The three methods below share one interface. Each takes standardized predictors , a centered response and a strength , and solves
Here is a penalty that grows with the size of . The intercept is not penalized, and it equals after centering. Each matches a budget in the constrained form, which minimizes the squared error subject to . Setting gives OLS, and letting sends every coefficient to zero. Cross-validation over a logarithmic grid chooses . The three penalties are:
- Ridge: . It shrinks every coefficient and has a closed form.
- Lasso: . It sets some coefficients exactly to zero.
- Elastic net: with . It selects predictors and keeps correlated ones together.
The decision rule is as follows:
- Ridge is the default when many predictors each carry some signal, especially when they are correlated.
- Lasso fits when the true model is sparse or the fitted model must name a few predictors.
- Elastic net fits when predictors come in correlated groups and the model must still select.
Standardize Before Penalizing
The penalty treats every coefficient alike, so the units of each predictor change the fit. In OLS, multiplying column by divides by and leaves the predictions unchanged. In ridge, the same change multiplies the -th diagonal entry of by . The penalty then has less effect on that coefficient. Center each column, scale it to unit variance, center , fit, and convert the coefficients back to the original units. Compute the means and scales on the training folds only. glmnet standardizes by default, while scikit-learn does not, so a scikit-learn pipeline needs a
StandardScaler.
Intuition
OLS is unbiased. With correlated predictors, or with close to , its coefficients can still vary wildly from sample to sample. A penalty makes the fit prefer small coefficients. Raising adds bias and removes variance, and the test error usually falls before it rises again (2.3.1. Bias-Variance Decomposition). The word comes from Tikhonov’s work on ill-posed problems, where a penalty makes an unstable solution unique and stable.
The methods differ in the shape of the penalty. The squared penalty has slope , which vanishes near zero. A small coefficient then costs almost nothing, so ridge never removes it. The absolute penalty has slope at every nonzero value, so the lasso sets weak coefficients exactly to zero. The two penalties therefore trade stability against selection. Ridge spreads the weight over a group of correlated predictors and gives a stable fit. The lasso keeps a few predictors and gives a readable fit that can change with small changes in the data. Elastic net moves between the two as goes from to .
Both panels use the same OLS estimate for two predictors. The RSS contours grow until they first meet the allowed region. The disk is met where both coefficients are nonzero. The diamond is met at a corner, so the lasso sets .
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw[->] (-1.65,0) -- (2.75,0) node[below left] {$\beta_1$};
\draw[->] (0.00,-1.59) -- (0.00,4.94) node[right] {$\beta_2$};
\node[anchor=north west, inner sep=1pt] at (1.27,-0.06) {\footnotesize $1$};
\draw[->] (5.75,0) -- (10.14,0) node[below left] {$\beta_1$};
\draw[->] (7.40,-1.59) -- (7.40,4.94) node[right] {$\beta_2$};
\node[anchor=north west, inner sep=1pt] at (8.67,-0.06) {\footnotesize $1$};
\draw[cold, very thick] (0.00,0.00) circle (1.22);
\draw[cold, very thick] (8.62,0.00) -- (7.40,1.22) -- (6.18,0.00) -- (7.40,-1.22) -- cycle;
\node at (0.43,5.64) {ridge: $\beta_1^2 + \beta_2^2 \le 1$};
\node at (7.83,5.64) {lasso: $|\beta_1| + |\beta_2| \le 1$};
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.66s = "true", 13.48s = "true", repeats}}}]
\draw[mid, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (0.27 and 0.20);
\draw[mid, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (0.27 and 0.20);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.92s = "true", 13.48s = "true", repeats}}}]
\draw[mid, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (0.55 and 0.40);
\draw[mid, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (0.55 and 0.40);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.18s = "true", 13.48s = "true", repeats}}}]
\draw[mid, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (0.82 and 0.60);
\draw[mid, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (0.82 and 0.60);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.44s = "true", 13.48s = "true", repeats}}}]
\draw[mid, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (1.10 and 0.80);
\draw[mid, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.10 and 0.80);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.7s = "true", 13.48s = "true", repeats}}}]
\draw[mid, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (1.37 and 1.01);
\draw[mid, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.37 and 1.01);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.96s = "true", 13.48s = "true", repeats}}}]
\draw[mid, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (1.64 and 1.21);
\draw[mid, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.64 and 1.21);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.96s = "true", 13.48s = "true", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (1.92 and 1.41);
\filldraw[draw=black, fill=hot, thick] (0.45,1.14) circle (3.4pt);
\node at (0.55,-2.09) {$\beta = (0.37, 0.93)$, \ RSS $= 5.04$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.76s = "true", 13.48s = "true", repeats}}}]
\draw[mid, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.92 and 1.41);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.28s = "true", 13.48s = "true", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (2.09 and 1.53);
\filldraw[draw=black, fill=hot, thick] (7.40,1.22) circle (3.4pt);
\node at (7.95,-2.09) {$\beta = (0.00, 1.00)$, \ RSS $= 5.36$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.4s = "false", 13.48s = "false", repeats}}}]
\node[anchor=west] at (0.75,2.80) {$\hat\beta$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.4s = "false", 13.48s = "false", repeats}}}]
\node[anchor=west] at (8.15,2.80) {$\hat\beta$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.4s = "false", 13.48s = "false", repeats}}}]
\node[anchor=west] at (-1.65,-2.79) {The OLS estimate $\hat\beta = (0.50, 2.20)$ lies outside both allowed regions.};
\node at (0.55,-2.09) {RSS $= 3.31$};
\node at (7.95,-2.09) {RSS $= 3.31$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.4s = "true", 2.66s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (0.27 and 0.20);
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (0.27 and 0.20);
\node at (0.55,-2.09) {RSS $= 3.34$};
\node at (7.95,-2.09) {RSS $= 3.34$};
\node[anchor=west] at (-1.65,-2.79) {The RSS contours grow out from $\hat\beta$. A larger contour means a larger RSS.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.66s = "true", 2.92s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (0.55 and 0.40);
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (0.55 and 0.40);
\node at (0.55,-2.09) {RSS $= 3.45$};
\node at (7.95,-2.09) {RSS $= 3.45$};
\node[anchor=west] at (-1.65,-2.79) {The RSS contours grow out from $\hat\beta$. A larger contour means a larger RSS.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.92s = "true", 3.18s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (0.82 and 0.60);
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (0.82 and 0.60);
\node at (0.55,-2.09) {RSS $= 3.63$};
\node at (7.95,-2.09) {RSS $= 3.63$};
\node[anchor=west] at (-1.65,-2.79) {The RSS contours grow out from $\hat\beta$. A larger contour means a larger RSS.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.18s = "true", 3.44s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (1.10 and 0.80);
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.10 and 0.80);
\node at (0.55,-2.09) {RSS $= 3.87$};
\node at (7.95,-2.09) {RSS $= 3.87$};
\node[anchor=west] at (-1.65,-2.79) {The RSS contours grow out from $\hat\beta$. A larger contour means a larger RSS.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.44s = "true", 3.7s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (1.37 and 1.01);
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.37 and 1.01);
\node at (0.55,-2.09) {RSS $= 4.19$};
\node at (7.95,-2.09) {RSS $= 4.19$};
\node[anchor=west] at (-1.65,-2.79) {The RSS contours grow out from $\hat\beta$. A larger contour means a larger RSS.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.7s = "true", 3.96s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(0.61,2.68)}] (0.61,2.68) ellipse (1.64 and 1.21);
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.64 and 1.21);
\node at (0.55,-2.09) {RSS $= 4.58$};
\node at (7.95,-2.09) {RSS $= 4.58$};
\node[anchor=west] at (-1.65,-2.79) {The RSS contours grow out from $\hat\beta$. A larger contour means a larger RSS.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 3.96s = "true", 6.76s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.92 and 1.41);
\node at (7.95,-2.09) {RSS $= 5.04$};
\node[anchor=west] at (-1.65,-2.79) {Left: the contour first touches the disk off the axes. Both coefficients stay nonzero.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 6.76s = "true", 7.02s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (1.98 and 1.45);
\node at (7.95,-2.09) {RSS $= 5.14$};
\node[anchor=west] at (-1.65,-2.79) {Right: the diamond is smaller, so the contour keeps growing.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.02s = "true", 7.28s = "false", 13.48s = "false", repeats}}}]
\draw[hot, very thick, rotate around={-45.0:(8.01,2.68)}] (8.01,2.68) ellipse (2.03 and 1.49);
\node at (7.95,-2.09) {RSS $= 5.25$};
\node[anchor=west] at (-1.65,-2.79) {Right: the diamond is smaller, so the contour keeps growing.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.28s = "true", 10.08s = "false", 13.48s = "false", repeats}}}]
\node[anchor=west] at (-1.65,-2.79) {Right: the contour first touches the diamond at its corner $(0, 1)$, so $\beta_1 = 0$.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.08s = "true", 13.48s = "false", 13.48s = "false", repeats}}}]
\node[anchor=west] at (-1.65,-2.79) {The diamond has its corners on the axes. Here the contour hits one, so the lasso drops $x_1$.};
\end{scope}
\filldraw[draw=black, fill=white, thick] (0.61,2.68) circle (2.6pt);
\filldraw[draw=black, fill=white, thick] (8.01,2.68) circle (2.6pt);
\node[anchor=west] at (-1.65,-3.39) {\footnotesize white dot: OLS estimate $\hat\beta$, \ red: RSS contours, \ blue: allowed region, \ $\mathrm{corr}(x_1, x_2) = 0.3$};
\end{tikzpicture}
\end{document}Comparison Table
| Method | Penalty | Closed form | Exact zeros | Correlated predictors | Bayesian prior | Region for | Hyperparameters |
|---|---|---|---|---|---|---|---|
| Ridge | yes, | no, all stay nonzero | shares the weight and shrinks them toward each other | Gaussian | disk | ||
| Lasso | no, coordinate descent | yes, at most nonzero when | keeps one of a group, often arbitrarily | Laplace | diamond with corners on the axes | ||
| Elastic net | no, coordinate descent or a lasso on augmented data | yes, up to nonzero | keeps the group with similar coefficients | between Gaussian and Laplace | diamond with outward-curved edges, corners kept | , |
When to Use Which
Start with ridge when the goal is prediction and many predictors should each matter a little. Distributed-lag models are the standard case (2.4 Time Series Regression). Adjacent lags are highly correlated, and each carries some signal. Ridge also handles with one linear solve. Switch to the lasso when the model must name a few predictors. A screening study and a model that people will read are two examples. The lasso works best when the true model is sparse and the active predictors are only weakly correlated. Switch to elastic net when you want selection and the predictors form correlated groups, like genes in one pathway. Elastic net is also the safer choice when and more than predictors may matter. When unsure, tune on a small grid that includes and . Cross-validation then compares all three methods on the same folds. Time series need folds that respect time order (2.4.5 Time-Series Cross-Validation). None of the three repairs a model with the wrong form. A missing curve calls for new features, as in 3.1b. Basis Function Regression.
Examples
(Medium) A Duplicated Column
A dataset of days of electricity demand contains the same standardized temperature column twice by mistake. So , with and .
What do OLS, ridge, the lasso and elastic net return with and, for elastic net, ? The fit depends only on the sum , since . OLS has no unique answer, because every pair with fits equally well. Ridge is unique, and by symmetry . The lasso penalty equals whenever the two signs agree. Its objective then depends on alone and is minimized at . Every same-sign split of is optimal, such as or . Coordinate descent from zero returns , and a different start returns a different split. Elastic net is strictly convex, so its answer is unique and symmetric. With its objective is plus a constant, which gives . Ridge and elastic net split the weight evenly, while the lasso's split depends on the solver.
See Also
- 3.2.1. Ridge Regression, 3.2.2. Lasso and 3.2.3. Elastic Net, the three methods compared here.
- 3.1a. Multiple Linear Regression, the unpenalized fit at .
- 2.3.1. Bias-Variance Decomposition, for why a biased estimator can predict better.
- 2.3.2. Cross-Validation, for choosing and .
- 3.8.1 Bayesian Linear Regression, where the penalties become priors.
- 4.4. Neural Network Regularization, for weight decay and other penalties in neural networks.
- 3.3. Nonlinear Regression, where the same penalties apply to nonlinear least squares.