Part 3 · Data scienceLesson 1114 minQuant Bible §4.4
Ridge, lasso and elastic net
Shrinking coefficients on purpose, and the geometry that makes lasso set some of them to exactly zero.
Least squares is unbiased, and with many correlated inputs that is a weakness: its variance is huge. Deliberately shrinking coefficients towards zero adds a little bias and removes a lot of variance. With hundreds of noisy signals, that is usually a winning trade.
Why shrink at all?
Test error. Total error is bias² + variance (lesson 9). Plain regression sits where a bit more bias buys a big cut in variance.
Interpretability. Knowing which 5 of 200 inputs matter is worth a lot.
Subset selection
The bluntest tool keeps a subset of k inputs and drops the rest.
Forward stepwise: start empty; repeatedly add the input that improves the fit most.
Backward stepwise: start full; repeatedly remove the input that contributes least.
Forward stagewise: start at zero; repeatedly find the input most correlated with the current residual and nudge its coefficient. It takes many more steps but behaves well in very high dimensions.
For up to about 30 inputs, the leaps-and-bounds algorithm finds the best subset of each size efficiently.
X⊤X+λI is always invertible, which was ridge’s original motivation. As λ grows the effective degrees of freedom fall smoothly from p towards 0. Ridge never sets a coefficient exactly to zero; it shrinks the least important directions (the last principal components, lesson 12) the most.
No closed form: it is a quadratic programme, solved in practice by coordinate descent or least-angle regression. The payoff is sparsity: as λ grows, coefficients hit exactly zero and stay there, so lasso does selection and shrinkage in one step.
Figure 11.1The RSS bowl and the penalty prism3D model
Penalised regression is the lowest point of the RSS bowl that stays inside the penalty region. For lasso the region is a diamond prism, and the bowl usually first touches it on an edge sitting on an axis: a coefficient of exactly zero. Switch to ridge and the round cylinder only shrinks.
Figure 11.2The same picture from aboveInteractive
Penalised regression is the same as minimising RSS inside a budget region. The orange ellipses are RSS contours around the least-squares answer; the solution is where the smallest one first touches the blue region. The L1 diamond has corners on the axes, so the touch point is often a corner: a coefficient of exactly zero. Drag the orange dot and change the penalty.
Figure 11.3Coefficient pathsInteractive
Six standardised inputs, three of which matter. From left (heavy penalty) to right (none), lasso switches coefficients on one at a time and keeps the useless ones at zero; ridge shrinks all of them smoothly. Elastic net is in between.
Three ways to shrink
With orthonormal inputs each method is a simple function of the least-squares estimate β^j:
Method
Estimate
Behaviour
Best subset of size M
β^j⋅1{∣β^j∣≥∣β^(M)∣}
hard threshold: keep or drop
Ridge
β^j/(1+λ)
proportional shrinkage
Lasso
sign(β^j)(∣β^j∣−λ)+
soft threshold: subtract, stop at 0
Figure 11.4Hard, proportional and soft thresholdsInteractive
The dashed diagonal is “leave the estimate alone”. Raise λ to see each rule bite.
Elastic net and friends
Ridge and lasso are the q=2 and q=1 cases of an Lq penalty λ∑∣βj∣q (q=0 is subset selection). The best q in practice is often between 1 and 2, which elastic net approximates with a blend:
λj=1∑p(αβj2+(1−α)∣βj∣).
Least-angle regression (LAR) grows coefficients continuously, always moving the inputs most correlated with the residual. With one modification (drop a variable when its coefficient crosses zero) it traces the entire lasso path for the cost of one least-squares fit, and after k steps the fit has exactly k degrees of freedom.