Course contents 08 / 27

08 / 27 · Linear Regression

Regularization

Adding a penalty on the size of the weights to least squares: ridge regression, its closed form and its shrinkage of the unstable directions, the prior behind each penalty, the constrained form and why an L1 ball produces exact zeros, the lasso with soft thresholding, coordinate descent and its path, the elastic net for correlated features, and choosing the penalty's strength by cross-validation.

On this page 7

The optimizers note finished the machinery: least squares can be fitted for any number of examples and features. Fitting it is not the problem any more; trusting the fit is. The least-squares note showed two nearly identical features whose coefficients swung between −9.90-9.90 and 11.8811.88 from one draw of the noise to the next, and a training R2R^2 that climbed to 0.920.92 with 40 useless features while the validation R2R^2 fell below zero. This note changes the objective rather than the optimizer: it adds a penalty that makes large weights expensive. The penalty trades a little bias for a large reduction of variance, has a Bayesian reading as a prior, and in its L1 form selects features by setting weights exactly to zero.

Notation. As in the least-squares note, X∈Rn×dX \in \mathbb R^{n \times d} is the design matrix and y∈Rny \in \mathbb R^n the targets. Throughout this note the features are standardized (mean 0, variance 1) and the target is centred, so no intercept is needed: it is yˉ\bar y and is never penalized. λ≥0\lambda \ge 0 is the strength of the penalty, ∥w∥22=∑jwj2\lVert w\rVert_2^2 = \sum_j w_j^2 and ∥w∥1=∑j∣wj∣\lVert w\rVert_1 = \sum_j \lvert w_j\rvert. p=rank⁡X≤min⁡(n,d)p = \operatorname{rank} X \le \min(n, d) is the rank of the design matrix. Its singular value decomposition is X=UΣV⊤X = U\Sigma V^\top, where U∈Rn×pU \in \mathbb R^{n \times p} and V∈Rd×pV \in \mathbb R^{d \times p} have orthonormal columns uju_j and vjv_j and Σ\Sigma is diagonal with the singular values σ1≥⋯≥σp>0\sigma_1 \ge \dots \ge \sigma_p > 0 (always with an index, unlike the noise level σ\sigma), so the nonzero eigenvalues of X⊤XX^\top X are σj2\sigma_j^2. S(z,a)=sign⁡(z)max⁡(∣z∣−a,0)S(z, a) = \operatorname{sign}(z)\max(\lvert z\rvert - a, 0) is the soft-thresholding function.

Ridge: a penalty on the size of the weights#

Ridge regression (Hoerl and Kennard, 1970) minimizes the squared error plus the squared length of the weight vector:

w^λ=arg⁡min⁡w  ∥y−Xw∥2+λ∥w∥22.\hat w_\lambda = \arg\min_w \; \lVert y - Xw\rVert^2 + \lambda\lVert w\rVert_2^2 .

The gradient is 2X⊤(Xw−y)+2λw2X^\top(Xw - y) + 2\lambda w, and setting it to zero gives

w^λ=(X⊤X+λI)−1X⊤y.\hat w_\lambda = (X^\top X + \lambda I)^{-1}X^\top y .

The matrix X⊤X+λIX^\top X + \lambda I has eigenvalues σj2+λ\sigma_j^2 + \lambda (and λ\lambda along the directions with no data), all at least λ\lambda. For any λ>0\lambda > 0 it is positive definite and invertible: the solution exists and is unique even with collinear features or with more features than examples, the cases where least squares broke down. Scaling matters: the penalty treats a weight of 1 on a feature measured in metres the same as on one measured in millimetres, which is why the features are standardized first, with statistics computed on the training data only.

Worked example. One feature x=(−1,0,1)x = (-1, 0, 1) and centred target y=(−2,0,2)y = (-2, 0, 2): x⊤x=2x^\top x = 2 and x⊤y=4x^\top y = 4. Least squares gives w=4/2=2w = 4/2 = 2; ridge with λ=2\lambda = 2 gives 4/(2+2)=14/(2 + 2) = 1, half the size. Now duplicate the feature, X=[x,x]X = [x, x]. Least squares has infinitely many solutions, every (w1,w2)(w_1, w_2) with w1+w2=2w_1 + w_2 = 2. Ridge has one: by symmetry w1=w2=aw_1 = w_2 = a, and the first equation of (X⊤X+2I)w=X⊤y(X^\top X + 2I)w = X^\top y reads 4a+2a=44a + 2a = 4, so a=2/3a = 2/3. The penalty splits the weight equally between identical features, the split with the smallest norm.

On the nearly identical features of the least-squares note (correlation 0.99990.9999, true weights (1,1)(1, 1)), three fresh draws of the noise give least-squares weights (4.81,−2.86)(4.81, -2.86), (−15.84,17.95)(-15.84, 17.95) and (−0.11,2.09)(-0.11, 2.09), and ridge with λ=1\lambda = 1 gives (0.99,0.95)(0.99, 0.95), (0.97,1.13)(0.97, 1.13) and (0.98,0.99)(0.98, 0.99) (the Code section).

Which directions it shrinks#

Substitute the SVD. Since X⊤X=VΣ2V⊤X^\top X = V\Sigma^2V^\top and X⊤y=VΣU⊤yX^\top y = V\Sigma U^\top y, the fitted values are

Xw^λ=∑j=1puj σj2σj2+λ uj⊤y.X\hat w_\lambda = \sum_{j=1}^p u_j\, \frac{\sigma_j^2}{\sigma_j^2 + \lambda}\, u_j^\top y .

Least squares projects yy onto each principal direction uju_j of the data and keeps the projection in full. Ridge multiplies it by σj2/(σj2+λ)\sigma_j^2/(\sigma_j^2 + \lambda): directions in which the features vary a lot (σj2≫λ\sigma_j^2 \gg \lambda) are kept almost intact, directions of small variance (σj2≪λ\sigma_j^2 \ll \lambda) are shrunk almost to zero (Hastie, Tibshirani and Friedman, 2009, section 3.4.1). These are exactly the directions that made least squares unstable, because the variance of the least-squares coefficients along vjv_j is σ2/σj2\sigma^2/\sigma_j^2. The sum of the factors, df⁡(λ)=∑jσj2/(σj2+λ)\operatorname{df}(\lambda) = \sum_j \sigma_j^2/(\sigma_j^2 + \lambda), is the trace of the ridge hat matrix and is called the effective degrees of freedom: pp at λ=0\lambda = 0, falling to 0 as λ→∞\lambda \to \infty.

We use a newly specified synthetic sensor fixture, not observations from people or equipment. Generate 240 independent rows from eight standard-normal draws z1,…,z8z_1,\ldots,z_8. Set x1=z1x_1=z_1, x2=z1+0.08z2x_2=z_1+0.08z_2 and xj=zjx_j=z_j for j=3,…,8j=3,\ldots,8. The target is y=20+4x1+4x2−3x3+2x4+4εy=20+4x_1+4x_2-3x_3+2x_4+4\varepsilon, with independent ε∼N(0,1)\varepsilon\sim N(0,1). The columns are sensor_a, sensor_b, control, ambient and four auxiliary signals. The redundant pair tests coefficient stability; the last four columns carry no true signal. Seed 20261113 fixes this teaching experiment. The data generator and new calculations were composed for this course with Codex assistance; no external dataset or benchmark scores are inputs.

Standardizing this fixture gives correlation 0.99640.9964 between the sensor pair. The extreme eigenvalues of X⊤XX^\top X are 484.75484.75 and 0.85210.8521: the almost duplicated measurement creates a poorly observed direction. At λ=10\lambda=10, ridge keeps the largest direction with factor 0.9800.980 and the smallest with factor 0.0790.079; effective degrees of freedom fall from 8 to 6.816.81. At λ=100\lambda=100 and 10001000 they are 5.045.04 and 1.481.48. These are calculated properties of this fixture, not estimates for a real sensor system.

The shrinkage introduces bias: E[w^λ]=(X⊤X+λI)−1X⊤X w≠wE[\hat w_\lambda] = (X^\top X + \lambda I)^{-1}X^\top X\, w \ne w. It can remove enough variance to make the mean squared error fall, the trade-off of the estimators note that the bias–variance note applies to every model of the course.

The Bayesian reading#

The penalty is a prior. With Gaussian noise of variance σ2\sigma^2 and independent priors wj∼N(0,τ2)w_j \sim N(0, \tau^2), the negative log-posterior is the ridge objective divided by 2σ22\sigma^2, with λ=σ2/τ2\lambda = \sigma^2/\tau^2; a Laplace prior p(wj)∝e−∣wj∣/bp(w_j) \propto e^{-\lvert w_j\rvert/b} gives the L1 penalty of the lasso below, with λ=2σ2/b\lambda = 2\sigma^2/b. The derivation is in the MAP section of the Bayesian inference note. Two consequences are worth stating here:

  • A strong penalty is a confident prior that the weights are small; λ→0\lambda \to 0 is a flat prior and gives back least squares.
  • For the Gaussian prior the posterior is Gaussian, so w^λ\hat w_\lambda is both its mode and its mean, and the posterior covariance σ2(X⊤X+λI)−1\sigma^2(X^\top X + \lambda I)^{-1} gives uncertainties; the Bayesian linear regression note derives this posterior and the predictive distribution. For the Laplace prior the lasso estimate is only the mode; the posterior puts zero probability on any weight being exactly 0, so the sparsity below is a property of the optimization, not of the posterior.

The constrained form: why L1 gives zeros#

For each λ\lambda there is a radius tt such that the penalized problem has the same solution as the constrained one,

min⁡w∥y−Xw∥2subject to∥w∥2≤t(ridge)or∥w∥1≤t(lasso),\begin{gathered} \min_w \lVert y - Xw\rVert^2 \quad \text{subject to} \quad \lVert w\rVert_2 \le t \quad (\text{ridge}) \\ \text{or} \quad \lVert w\rVert_1 \le t \quad (\text{lasso}) , \end{gathered}

by the Lagrangian duality of convex problems (least squares under a norm constraint derives it for ridge): λ\lambda is the multiplier of the constraint, and a smaller tt corresponds to a larger λ\lambda. The constrained form explains the different behaviour of the two penalties.

Figure

Why the lasso zeroes weights and ridge does not

Loss contours and the L2 and L1 constraint setsTwo panels. Each shows tilted ellipses around the point ŵ in the first quadrant. On the left a shaded disc, which the bold ellipse touches at a point with two positive coordinates. On the right a shaded diamond, which the bold ellipse touches at its top corner on the w₂ axis.ridge: ‖w‖₂ ≤ 1w₁w₂ŵ (least squares)w = (0.54, 0.85)lasso: ‖w‖₁ ≤ 1w₁w₂ŵ (least squares)w = (0, 1)Loss contours and the L2 and L1 constraint setsTwo panels. Each shows tilted ellipses around the point ŵ in the first quadrant. On the left a shaded disc, which the bold ellipse touches at a point with two positive coordinates. On the right a shaded diamond, which the bold ellipse touches at its top corner on the w₂ axis.ridge: ‖w‖₂ ≤ 1w₁w₂ŵ (least squares)w = (0.54, 0.85)lasso: ‖w‖₁ ≤ 1w₁w₂ŵ (least squares)w = (0, 1)
The constrained problem in two dimensions. The ellipses are contours of the quadratic loss around the least-squares solution ŵ = (0.8, 2.2); the shaded set is the constraint: the disc ‖w‖₂ ≤ 1 for ridge and the diamond ‖w‖₁ ≤ 1 for the lasso. The solution is where the smallest ellipse touches the set (bold contour). The disc has a smooth boundary, the ellipse touches it at a generic point, (0.54, 0.85), and both weights are nonzero. The diamond has corners on the axes; an ellipse approaching from a wide range of directions hits a corner first, here (0, 1), and the weight w₁ becomes exactly zero. The losses at the solutions are 1.133 and 1.520.

The contours of the squared error are ellipses around the least-squares solution w^\hat w. The constrained solution is the point of the feasible set on the lowest contour, the point where a growing ellipse first touches the set. The L2 ball is round, so the touching point is wherever the ellipse’s shape puts it, generically with all coordinates nonzero. The L1 ball is a diamond (a cross-polytope in dd dimensions) whose corners lie on the axes, and its corners stick out: an ellipse approaching from a wide range of directions hits a corner first, and at a corner some coordinates are exactly zero. In the figure the condition is visible as a computation: at the corner (0,1)(0, 1) the negative gradient of the loss is (1.4,1.6)(1.4, 1.6), which lies strictly between the normals (1,1)(1, 1) and (−1,1)(-1, 1) of the two edges that meet there, so no move along the diamond’s boundary lowers the loss.

The lasso#

The lasso (Tibshirani, 1996) is the L1-penalized problem,

w^=arg⁡min⁡w  ∥y−Xw∥2+λ∥w∥1.\hat w = \arg\min_w \; \lVert y - Xw\rVert^2 + \lambda\lVert w\rVert_1 .

It is convex but not differentiable where a weight is zero, and it has no closed form in general. With orthonormal features, X⊤X=IX^\top X = I, it does. Write z=X⊤yz = X^\top y, the least-squares solution in that case; then ∥y−Xw∥2=∥y∥2−2w⊤z+∥w∥2\lVert y - Xw\rVert^2 = \lVert y\rVert^2 - 2w^\top z + \lVert w\rVert^2, and the objective splits into one problem per coordinate, min⁡wjwj2−2zjwj+λ∣wj∣\min_{w_j} w_j^2 - 2z_jw_j + \lambda\lvert w_j\rvert. For wj>0w_j > 0 the derivative 2wj−2zj+λ2w_j - 2z_j + \lambda vanishes at wj=zj−λ/2w_j = z_j - \lambda/2, which is positive only if zj>λ/2z_j > \lambda/2; symmetrically for wj<0w_j < 0; otherwise the minimum is at 0. Together:

w^jlasso=S(zj,λ/2),w^jridge=zj1+λ.\begin{aligned} \hat w_j^{\text{lasso}} &= S(z_j, \lambda/2), \\ \hat w_j^{\text{ridge}} &= \frac{z_j}{1 + \lambda} . \end{aligned}

Ridge shrinks every coefficient by the same factor; the lasso subtracts the same amount from every coefficient’s absolute value and sets those smaller than λ/2\lambda/2 to zero.

Worked example. Orthonormal features with z=(3,1,−0.5)z = (3, 1, -0.5) and λ=2\lambda = 2. The lasso gives (S(3,1),S(1,1),S(−0.5,1))=(2,0,0)(S(3, 1), S(1, 1), S(-0.5, 1)) = (2, 0, 0): two features are dropped. Ridge gives (3,1,−0.5)/3=(1,0.333,−0.167)(3, 1, -0.5)/3 = (1, 0.333, -0.167): all three are kept, smaller. scikit-learn’s Lasso minimizes 12n∥y−Xw∥2+α∥w∥1\frac{1}{2n}\lVert y - Xw\rVert^2 + \alpha\lVert w\rVert_1, so its α\alpha is λ/(2n)\lambda/(2n) in this note’s units; with that conversion it returns (2,0,0)(2, 0, 0) (the Code section).

Coordinate descent. For general XX, minimize over one weight at a time with the others fixed. The one-dimensional problem is the orthonormal one with zz replaced by the correlation of feature jj with the partial residual r(j)=y−∑k≠jxkwkr^{(j)} = y - \sum_{k \ne j}x_k w_k:

wj←S(xj⊤r(j), λ/2)xj⊤xj.w_j \leftarrow \frac{S\big(x_j^\top r^{(j)},\ \lambda/2\big)}{x_j^\top x_j} .

Cycling over the coordinates converges for the lasso (Friedman et al., 2007), and computing the solutions for a decreasing sequence of λ\lambda, each started from the previous one, gives the whole regularization path cheaply; this is what scikit-learn does. A related method takes a gradient step on the squared error and then soft-thresholds all coordinates (the proximal gradient method); the least angle regression algorithm computes the path exactly, because it is piecewise linear in λ\lambda (Efron et al., 2004).

Where the path starts. At w=0w = 0 the subgradient condition for optimality is ∣2xj⊤y∣≤λ\lvert 2x_j^\top y\rvert \le \lambda for every jj. So all weights are zero for λ≥λmax⁡=2max⁡j∣xj⊤y∣\lambda \ge \lambda_{\max} = 2\max_j\lvert x_j^\top y\rvert, and the first feature to enter is the one most correlated with the target. On the synthetic fixture λmax⁡=3890.52\lambda_{\max} = 3890.52, attained by sensor_a.

Figure

Coefficient paths of ridge and the lasso

Regularization paths on synthetic signalsTwo panels with eight coefficient curves each. On the left the ridge curves converge smoothly to zero as λ grows. On the right the lasso curves hit zero one after another and stay there; the sensor_a curve is the last to reach zero.ridge: ‖y − Xw‖² + λ‖w‖²−404810⁻¹10¹10³10⁵λsensor acontrolambientlasso: ‖y − Xw‖² + λ‖w‖₁−404810¹10²10³λsensor acontrolambientRegularization paths on synthetic signalsTwo panels with eight coefficient curves each. On the left the ridge curves converge smoothly to zero as λ grows. On the right the lasso curves hit zero one after another and stay there; the sensor_a curve is the last to reach zero.ridge: ‖y − Xw‖² + λ‖w‖²−404810⁻¹10¹10³10⁵λsensor acontrolambientlasso: ‖y − Xw‖² + λ‖w‖₁−404810¹10²10³λsensor acontrolambient
Newly specified synthetic signals: 240 rows and eight standardized features. Every path is calculated from the new G = XᵀX and c = Xᵀy; no external data is used. The horizontal axis increases λ. Nearly duplicated sensor_a and sensor_b have least-squares weights 7.31 and 0.83; ridge reduces the imbalance. Lasso reaches the all-zero solution at λ = 3890.52. Dashed lines mark settings selected on five folds: λ = 1 for ridge and λ = 48 for lasso (α = 0.1); after fitting all rows lasso retains seven weights. The left edge has a small positive penalty, not exactly zero.

The paths are freshly computed from the synthetic fixture. Least squares assigns standardized weights 7.317.31 and 0.830.83 to sensor_a and sensor_b, although their generating coefficients were equal before scaling. Ridge reduces that imbalance. Along the lasso grid the entry order is sensor_a, control, ambient, aux_1, sensor_b, aux_3, aux_2, aux_4. The model selected below retains seven coefficients, including three auxiliary signals with no true effect. Sparsity does not certify which variables caused the target.

Correlated features: the elastic net#

The lasso’s selection is unstable within a group of correlated features. When two features carry the same signal, the L1 penalty is the same for any split of the weight between them, ∣a∣+∣b∣=∣a+b∣\lvert a\rvert + \lvert b\rvert = \lvert a + b\rvert for weights of one sign, and the squared error barely distinguishes the splits, so the lasso tends to keep one feature and drop the other, and which one depends on the noise. On two copies of one signal with correlation 0.9990.999 plus a useless feature, three draws of the data give lasso weights (1.46,0,0)(1.46, 0, 0), (0,1.40,0)(0, 1.40, 0) and (0.60,0.76,0)(0.60, 0.76, 0) (scikit-learn’s α=0.5\alpha = 0.5).

The elastic net (Zou and Hastie, 2005) adds a ridge term: λ1∥w∥1+λ2∥w∥22\lambda_1\lVert w\rVert_1 + \lambda_2\lVert w\rVert_2^2. The squared term is strictly convex, so it prefers the equal split, and Theorem 1 of the paper bounds the difference between the weights of two features by a multiple of 1−ρ\sqrt{1 - \rho}, where ρ\rho is their correlation: the grouping effect. The same three draws give (0.76,0.76,0)(0.76, 0.76, 0), (0.72,0.74,0)(0.72, 0.74, 0) and (0.71,0.72,0)(0.71, 0.72, 0) (scikit-learn’s α=0.5\alpha = 0.5 and l1_ratio=0.5), and the useless feature is still removed.

Choosing the penalty by validation#

The penalty strength is a hyperparameter, and the validation note gives the procedure: fit for a grid of λ\lambda on a log scale, estimate the error of each by cross-validation, keep the best, and report the final performance with a nested scheme or a test set, since the best of many cross-validated scores is optimistic. Ridge has a shortcut: its fitted values are linear in yy, y^=Hλy\hat y = H_\lambda y, and the leave-one-out residual is (yi−y^i)/(1−hii)(y_i - \hat y_i)/(1 - h_{ii}), so leave-one-out cross-validation costs one fit per λ\lambda (Hastie, Tibshirani and Friedman, 2009, section 7.10.1, eq. 7.51); scikit-learn’s RidgeCV uses it.

On the synthetic fixture, use five shuffled folds (seed 17), with polynomial expansion and standardization fitted inside each training fold. Choose ridge λ∈{0.1,1,10,100,1000}\lambda\in\{0.1,1,10,100,1000\} or lasso α∈{0.03,0.1,0.3,1,3}\alpha\in\{0.03,0.1,0.3,1,3\}. The table reports the mean validation R2R^2 and the best setting on these same folds, so these are selection scores:

FeaturesCountLeast squaresRidge, best λ\lambdaLasso, best α\alpha
original80.8120.8120.8120.812 (λ=1\lambda = 1)0.8140.814 (α=0.1\alpha = 0.1)
up to degree 2440.7700.7700.7870.787 (λ=10\lambda = 10)0.8090.809 (α=0.3\alpha = 0.3)
up to degree 3164−7.090-7.0900.6520.652 (λ=100\lambda = 100)0.8030.803 (α=0.3\alpha = 0.3)

Each training fold has 192 rows. Expanding eight inputs to 164 cubic terms leaves little independent information per coefficient, and unpenalized fitting is unstable. The penalty makes the cubic fit usable, but its best score still does not beat the original linear model: the generator contains no interactions to discover. This is a controlled illustration of variance, not a recommendation to add all polynomials. A separate test set or nested validation is still required to estimate performance after selection.

Other ways of limiting a model act like penalties and appear later in the course: stopping gradient descent early (a spectral filter that, like ridge, delays fitting small-curvature directions; it is not exactly one ridge penalty for every direction), dropout and data augmentation, all in the training note, and pruning in the decision-tree note. Weight decay, shrinking every weight by a fixed fraction at each step, is the same as an L2 penalty for plain SGD; with adaptive optimizers such as Adam the two differ, and the decoupled form, AdamW, is the one used in practice (weight decay under Adam). Weight decay regularizes the fine-tuning of language models, and an L1 penalty on activations makes the sparse autoencoder sparse.

What changed#

Least squaresRidgeLasso
Objective∥y−Xw∥2\lVert y - Xw\rVert^2+ λ∥w∥22+\,\lambda\lVert w\rVert_2^2+ λ∥w∥1+\,\lambda\lVert w\rVert_1
Solution(X⊤X)−1X⊤y(X^\top X)^{-1}X^\top y, if it exists(X⊤X+λI)−1X⊤y(X^\top X + \lambda I)^{-1}X^\top y, for λ>0\lambda>0no closed form; coordinate descent
Collinear features or d>nd > nnot unique, unstableunique, weight sharedunique fit, selection unstable within groups
Zerosnot enforcednot enforced (only by coincidence)exact zeros: feature selection
PriorflatGaussianLaplace (as MAP)
Hyperparameternoneλ\lambda, by cross-validationλ\lambda, by cross-validation
Bias and varianceunbiased, high variancebiased, lower variancebiased, lower variance

Limitations#

  • The penalty depends on the units. Features must be standardized, and the result changes with the scaling chosen.
  • Bias. Both penalties shrink large true coefficients too; the lasso’s estimates of the features it keeps are biased towards zero, and refitting least squares on the selected features is a common remedy.
  • The lasso keeps at most nn features when d>nd > n and the features are in general position, for example drawn from a continuous distribution: the solution is then unique with probability one and has at most nn nonzero weights (Tibshirani, 2013). With exactly duplicated columns the fit stays unique but the weights do not: any split of a weight between two copies, with the same sign, has the same fit and penalty. Within strongly correlated groups the choice is arbitrary; the elastic net softens both limits.
  • Selection is not inference. A feature dropped by the lasso may still matter, and standard errors computed after selection are too small.
  • One more hyperparameter. λ\lambda costs a cross-validation loop and makes an honest error estimate need a nested one.

What you should remember#

  • Ridge: w^λ=(X⊤X+λI)−1X⊤y\hat w_\lambda = (X^\top X + \lambda I)^{-1}X^\top y, unique for every λ>0\lambda > 0; it shrinks each principal direction by σj2/(σj2+λ)\sigma_j^2/(\sigma_j^2 + \lambda), the small-variance directions most; df⁡(λ)=∑jσj2/(σj2+λ)\operatorname{df}(\lambda) = \sum_j \sigma_j^2/(\sigma_j^2 + \lambda).
  • The penalties are priors: Gaussian gives ridge with λ=σ2/τ2\lambda = \sigma^2/\tau^2, Laplace gives the lasso as a MAP estimate.
  • The L1 ball has corners on the axes, so the lasso sets weights exactly to zero; with orthonormal features it soft-thresholds the least-squares coefficients at λ/2\lambda/2, while ridge divides them by 1+λ1 + \lambda.
  • All lasso weights are zero for λ≥2max⁡j∣xj⊤y∣\lambda \ge 2\max_j\lvert x_j^\top y\rvert; the path is computed by coordinate descent with warm starts.
  • The elastic net adds an L2 term so that correlated features share their weight.
  • Standardize features, do not penalize the intercept, choose λ\lambda by cross-validation and report with nested cross-validation.

Code#

The worked examples, the new fixture, spectral shrinkage, the lasso path and the polynomial comparison. Recomputed with NumPy 2.5.4 and scikit-learn 1.9.1; the recorded run raised no convergence warnings. No external data is downloaded:

import numpy as np, warnings
from sklearn.linear_model import Ridge, Lasso, ElasticNet, LinearRegression, lasso_path
from sklearn.model_selection import KFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
warnings.filterwarnings('ignore', category=FutureWarning)   # only scikit-learn 1.9's notice about n_alphas; convergence warnings stay on
rng = np.random.default_rng(0)
ridge = lambda X, y, lam: np.linalg.solve(X.T @ X + lam * np.eye(X.shape[1]), X.T @ y)

# worked example: x = (-1, 0, 1), centred y = (-2, 0, 2); then the same feature twice
x = np.array([[-1.], [0], [1]]); y = np.array([-2., 0, 2])
print(ridge(x, y, 1e-12), ridge(x, y, 2.0), ridge(np.hstack([x, x]), y, 2.0).round(3))   # [2.] [1.] [0.667 0.667]

# orthonormal features: the lasso soft-thresholds z = X^T y at lambda/2, ridge divides it by 1 + lambda
n = 50
Xo = np.linalg.qr(rng.normal(size=(n, 3)))[0]            # orthonormal columns
z = np.array([3.0, 1.0, -0.5]); yo = Xo @ z
lam = 2.0                                                # objective |y - Xw|^2 + lam |w|_1; scikit-learn's alpha = lam / (2n)
print(Lasso(alpha=lam / (2 * n), fit_intercept=False, tol=1e-12).fit(Xo, yo).coef_.round(3) + 0, ridge(Xo, yo, lam).round(3))
# [2. 0. 0.] [ 1.     0.333 -0.167]

# two nearly identical features (the least-squares note's example): ridge with lambda = 1 is stable
x1 = rng.normal(size=100); X2 = np.column_stack([x1, x1 + 0.01 * rng.normal(size=100)])
for _ in range(3):
    t = X2.sum(1) + rng.normal(size=100)
    print(np.linalg.lstsq(X2, t, rcond=None)[0].round(2), ridge(X2, t, 1.0).round(2))
# [ 4.81 -2.86] [0.99 0.95] | [-15.84  17.95] [0.97 1.13] | [-0.11  2.09] [0.98 0.99]

# New synthetic fixture; standardize and center for the full-data path only.
def make_data():
    # Independent dimensionless simulated inputs, with one deliberately redundant pair.
    rng = np.random.default_rng(20261113)
    base = rng.normal(size=(240, 8))
    X = base.copy()
    X[:, 1] = base[:, 0] + 0.08 * base[:, 1]
    y = 20 + X @ np.array([4., 4., -3., 2., 0., 0., 0., 0.])
    y += 4 * rng.normal(size=len(X))
    return X, y

X, yd = make_data()
names = ['sensor_a', 'sensor_b', 'control', 'ambient', 'aux_1', 'aux_2', 'aux_3', 'aux_4']
Xs = (X - X.mean(0)) / X.std(0); yc = yd - yd.mean()
assert np.max(abs(ridge(Xs, yc, 10) - Ridge(alpha=10).fit(Xs, yc).coef_)) < 1e-9
d2 = np.linalg.eigvalsh(Xs.T @ Xs)[::-1]
print(d2.round(3))
for lam in (1, 10, 100, 1000):
    factors = d2 / (d2 + lam)
    print(lam, factors[[0, -1]].round(3), round(factors.sum(), 2))
# lambda=10: [0.980, 0.079], df=6.81; lambda=100: df=5.04; lambda=1000: df=1.48
print(round(2 * abs(Xs.T @ yc).max(), 2), names[abs(Xs.T @ yc).argmax()])  # 3890.52 sensor_a
alphas, coefs, _ = lasso_path(Xs, yc, n_alphas=2000, eps=1e-5, max_iter=200000, tol=1e-9)
order = []
for column in coefs.T:
    order += [names[j] for j in np.flatnonzero(abs(column) > 1e-9) if names[j] not in order]
print(order)
# ['sensor_a', 'control', 'ambient', 'aux_1', 'sensor_b', 'aux_3', 'aux_2', 'aux_4']

# Selection scores, with every transformation inside each training fold.
cv = KFold(5, shuffle=True, random_state=17)
for deg in (1, 2, 3):
    pipe = lambda model: make_pipeline(PolynomialFeatures(deg, include_bias=False), StandardScaler(), model)
    score = lambda model: float(cross_val_score(pipe(model), X, yd, cv=cv, scoring='r2').mean())
    p = PolynomialFeatures(deg, include_bias=False).fit(X).n_output_features_
    r = {a: score(Ridge(alpha=a)) for a in (0.1, 1, 10, 100, 1000)}
    l = {a: score(Lasso(alpha=a, max_iter=200000, tol=1e-6)) for a in (0.03, 0.1, 0.3, 1, 3)}
    print(deg, p, round(score(LinearRegression()), 3), max(r, key=r.get), round(max(r.values()), 3), max(l, key=l.get), round(max(l.values()), 3))
# 1 8 0.812 1 0.812 0.1 0.814 | 2 44 0.770 10 0.787 0.3 0.809 | 3 164 -7.090 100 0.652 0.3 0.803

# two copies of one signal (correlation 0.999) and a useless feature, three draws: the lasso picks a copy, the elastic net shares
for seed in (0, 1, 2):
    r = np.random.default_rng(seed); zz = r.normal(size=200)
    Xe = np.column_stack([zz + 0.03 * r.normal(size=200), zz + 0.03 * r.normal(size=200), r.normal(size=200)])
    Xe = (Xe - Xe.mean(0)) / Xe.std(0); ye = 2 * zz + r.normal(size=200); ye -= ye.mean()
    print(Lasso(alpha=0.5).fit(Xe, ye).coef_.round(2) + 0, ElasticNet(alpha=0.5, l1_ratio=0.5).fit(Xe, ye).coef_.round(2) + 0)
# [1.46 0.   0.  ] [0.76 0.76 0.  ] | [0.  1.4 0. ] [0.72 0.74 0.  ] | [0.6  0.76 0.  ] [0.71 0.72 0.  ]

Next#

The linear regression section ends with a model that predicts a number, is fitted by formula or by descent, and does not chase the noise. Many targets are not numbers but classes: spam or not, default or repayment. The next note asks what a linear model should minimize when the answer is a side of a hyperplane, why squared error is the wrong loss for that, and which losses replace it. The ridge penalty comes along: it is the second term of the support vector machine objective and the heart of kernel ridge regression.

References#