Two sensors measure nearly the same quantity. Least squares can give one of them a large positive coefficient and the other a large negative one, and a slightly different training sample can change that split completely while the predictions hardly move. A penalty on the size of the coefficients removes much of this freedom. This lesson states one convention for the ridge, lasso and elastic-net penalties, works out how each one shrinks coefficients and when the lasso sets them exactly to zero, and chooses the penalty strength with validation data rather than with the training fit.
Key terms
Regularization
Definition Regularization changes the training objective so that it prefers some parameter values over others. On this page the features are standardized with the training means and standard deviations, giving a matrix with rows and centered columns, and a linear model is fitted by minimizing
where is the ridge, lasso or elastic-net penalty defined below and the regularization strength sets its weight. The intercept is not penalized, and because the columns of are centered, its optimal value is the training mean for every . Setting gives least squares. As , the coefficients go to zero and every prediction approaches . Software differs in these constants. For example, an objective has the same minimizer as the ridge objective below when , so the same printed number can mean different penalties.
In plain words The penalty adds a cost for large coefficients, so the fit gives up some training accuracy in exchange for smaller and more stable coefficients. A penalty is a choice about how to fit, not a finding that small coefficients are true. Because one penalty weighs the coefficients of all features, their scales matter, which is why the features are standardized first, with statistics from the training rows only.
Example A length recorded in meters is re-recorded in centimeters, so its values are multiplied by . The coefficient that gives the same predictions is divided by , so its square falls by a factor of , and so does its ridge penalty, which then barely restrains that feature. After standardization both versions give the same column, because the standard deviation is multiplied by as well.
Ridge regression
Definition Ridge regression uses the penalty . With the intercept at and , the minimizing coefficient vector is the unique solution of
since is positive definite even when does not have full column rank. Because the columns of are centered, using or the centered targets on the right gives the same result.
In plain words Ridge pulls all coefficients toward zero smoothly and does not choose between correlated features. It tends to spread the weight among them, which keeps the solution stable when features nearly repeat each other.
Example Two constructed standardized features are identical, with training rows and and targets and , so . Predictions depend only on the sum , the data term of the objective is , and least squares fits perfectly with any pair whose sum is . For a given sum the ridge penalty is smallest when , which leaves
to minimize. Setting the derivative to zero gives , so each coefficient is . At both are . The training fit is worse than with least squares, but the coefficients are now unique.
Lasso
Definition The lasso uses the penalty . Its objective is convex but not differentiable where a coefficient is zero. Write for column of and for the residual vector. A coefficient vector is a minimizer exactly when, for every , the average product equals if and lies in if . So a coefficient can sit exactly at zero, and every coefficient is zero once . Because of the corner at zero, plain gradient descent is not a general lasso solver. Coordinate descent with soft thresholding and proximal gradient methods treat the corner exactly. When the columns of are linearly dependent, for example with identical features or with more features than rows, the minimizer need not be unique.
In plain words A coefficient stays at zero unless its feature reduces the squared error by more than the penalty charges, so the lasso fits and selects features at the same time. With correlated features the selection is unstable. Which of two similar features is kept can depend on the sample or even on the order in which an algorithm visits them, and a zero coefficient only says that the penalized fit did not use that feature, given the others, this scaling and this .
Example For the identical features with , the penalty for a given sum is smallest when both coefficients have the same sign, and then it equals . The objective for is smallest at , so every pair of nonnegative coefficients with sum is a minimizer, and all of them give the objective value . Coordinate descent that updates first returns , and updating first returns . Neither zero shows that the dropped feature is unrelated to the target, since both features have exactly the same relationship with it.
Elastic net
Definition The elastic net uses the penalty
with a mixing weight , so gives the lasso and gives ridge. For and the objective is strictly convex, so its minimizer is unique even with identical features. For it can set coefficients exactly to zero, as the lasso does, and like ridge it tends to give strongly correlated features similar coefficients.
In plain words The elastic net combines a threshold that can remove features with a smooth pull that shares weight among similar ones. It has two settings to choose, the strength and the mix .
Example For the identical features with and , the penalty for a given positive sum is smallest with equal coefficients, so the objective becomes
Its derivative is zero at , so the unique solution is . Unlike the lasso, the elastic net does not pick one of the two identical features.
Coefficient shrinkage
Definition Coefficient shrinkage is the movement of fitted coefficients toward zero, relative to least squares, as the regularization strength grows. Take one standardized feature, with average square and average product with the centered target. The data term is plus a constant, least squares gives , and the three penalized minimizers are
where is soft thresholding at level . With several features, the Euclidean norm of the ridge coefficient vector never increases as grows, but single coefficients need not move steadily toward zero, and with correlated features one of them can grow or change sign along the way.
In plain words Ridge divides the least-squares coefficient by , so it never reaches zero unless . The lasso subtracts from its size and stops at zero. The elastic net subtracts and then divides. These formulas are exact for a single standardized feature, and with correlated features each coefficient also depends on the others.
Example With and , ridge gives , the lasso gives because , and the elastic net with gives . For two standardized features with correlation and average products and with the centered target, least squares gives about and , while ridge with gives about and . The second coefficient changes sign as passes .
Regularization path
Definition A regularization path is the collection of fitted coefficient vectors over a range of regularization strengths, computed on fixed training data, in practice on a grid of values. The strength is chosen by comparing the fits on validation data, or by cross-validation within the training data, with one metric on the same cases for every candidate. Training error cannot make this choice, because the unpenalized training loss of the penalized solution never decreases as grows. Comparing penalized objective values across is no fairer, since the objective itself changes with . The test set is used once, for the procedure finally chosen.
In plain words The path shows how each coefficient moves as the penalty tightens, which coefficients reach zero and in which order, and where the fit starts to underfit. Validation error often falls at first and rises later along the path, but its shape depends on the data.
Example For the identical features, the ridge path gives each coefficient , which is , , , and at , , , and , where the value at is the limit from positive , the least-squares solution of smallest norm. On the lasso path the coefficient sum is , which reaches zero at , while its split between the features is not unique. Two constructed validation rows and , standardized with the training statistics, have targets and . Along the ridge path their mean squared errors are , , about , and about , so is best among these candidates. Two validation rows are far too few for a real choice, and this ranking says nothing about other data.
Check yourself
- A single standardized feature has average product with the centered target. With , find the ridge, lasso and elastic-net coefficients, using for the elastic net. For which values of is each coefficient exactly zero?
Show the answer
Ridge gives . The lasso reduces the size by and keeps the sign, giving . The elastic net thresholds at , giving , and divides by , giving . The lasso coefficient is zero exactly when , and the elastic-net coefficient with exactly when , that is . The ridge coefficient is never zero for a finite , because .
- A lasso fit on the two identical standardized features with returns . A colleague concludes that the second sensor has no relationship with the target. What is wrong with this conclusion, and what would you report instead?
Show the answer
Every pair of nonnegative coefficients with sum minimizes the same objective, with value , and coordinate descent returns or depending only on which coefficient it updates first. The second feature is identical to the first, so its relationship with the target is exactly as strong. The zero says only that this penalized fit, with this scaling, data and , did not need the second feature once the first was in the model. Report that the data cannot distinguish the two features, so the split between them is arbitrary. With real sensors that are only nearly identical, refit on resampled training data and report how often each one is selected. Also consider the elastic net, which with and gives the unique answer . No penalized fit turns either coefficient into a causal effect.
- For the identical-feature training data, one analyst picks the ridge strength among , and by the smallest training mean squared error, and another by the smallest test error. Compute the training and validation mean squared errors of the three fits, using the validation rows of the regularization path card, and explain which way of choosing is sound.
Show the answer
The coefficient sums are , and . The training mean squared error is , which gives , and . The validation predictions are , so the validation mean squared error is , which gives , and . Training error can never favor a larger over a smaller one, because the training loss of the penalized solution never decreases as grows, so it cannot detect overfitting. Validation error picks . Choosing by test error turns the test set into development data, and the test error of the chosen is then optimistically biased. Choose on validation data or by cross-validation within the training data, and evaluate the chosen procedure once on the test set.