Skip to content
BIASED

Regularization

A penalty on the size of the weights in least squares — ridge and its shrinkage of the unstable directions, the prior behind each penalty, the constrained form and the exact zeros of the lasso, soft thresholding, coordinate descent, the elastic net and choosing the penalty's strength by cross-validation.

Fitting is easy; trusting is not

Stochastic and adaptive optimizers explained how to approach a training minimum. Least squares showed why that minimum can still be unstable. Nearly duplicate features allow large cancelling weights, and a flexible model can fit sampling noise. Regularization changes the objective itself.

We use the unaveraged squared error ∥y−Xw∥2\lVert y-Xw\rVert^2. Features are centred and scaled using training data; the target is centred. The intercept is restored afterward and is not penalized. The numerical value of a penalty parameter depends on this normalization.

A weight changes when its feature's units change. Penalizing weights without controlling units therefore changes the model's preference. Fit the scaler inside each training fold, then apply that fitted transform to the validation fold. A penalty can reduce variance at the cost of bias; it does not guarantee lower prediction error for every dataset.

Ridge regression

Ridge adds a squared norm:

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\}.

Its normal equations are (X⊤X+λI)w^=X⊤y(X^\top X+\lambda I)\hat w=X^\top y. For λ>0\lambda>0, every eigenvalue is positive, so the penalized weights are unique even when columns are dependent.

For x=(−1,0,1)x=(-1,0,1) and y=(−2,0,2)y=(-2,0,2), least squares gives weight 22. At λ=2\lambda=2, ridge gives 4/(2+2)=14/(2+2)=1. If the feature is duplicated, symmetry gives both ridge weights 2/32/3: substituting equal weights into either equation gives 6a=46a=4. The total weight differs from the single-feature result because duplicating a feature changes the penalty for representing the same prediction.

Ridge prefers a small norm rather than discovering which copy is the real cause. Its coefficients need not all be nonzero: exact zeros are possible by symmetry or zero correlation, but are not systematically selected by a threshold.

Which directions shrink

Recall the thin SVD X=UΣV⊤X=U\Sigma V^\top from Mathematics for ML. Ridge multiplies the fitted component along uju_j by

aj=σj2σj2+λ,df⁡(λ)=∑jaj.a_j=\frac{\sigma_j^2}{\sigma_j^2+\lambda},\qquad \operatorname{df}(\lambda)=\sum_j a_j.

The sum is the effective degrees of freedom for the centred fit. Directions with small eigenvalues σj2\sigma_j^2 shrink most. Standardizing individual columns does not make all principal directions equally variable.

Use the original 240-row, eight-feature sensor fixture from Least squares. Its generating rule is additive, with two nearly duplicate signals and four irrelevant auxiliaries. After standardization, the extreme eigenvalues of X⊤XX^\top X are 484.75484.75 and 0.85210.8521. At λ=10\lambda=10, their factors are approximately 0.9800.980 and 0.0790.079. Degrees of freedom fall from 88 to 6.816.81; at λ=100\lambda=100 and 10001000, they are 5.045.04 and 1.481.48.

The shrinkage is determined by XX and λ\lambda, not by a causal assessment of each feature. It damps precisely the directions where least-squares coefficient variance is large under the equal-variance noise model.

The penalty as a prior

The topic Bayesian inference explains MAP estimation. With Gaussian noise variance σ2\sigma^2 and independent Gaussian priors of variance τ2\tau^2 on weights, the negative log-posterior contains

∥y−Xw∥22σ2+∥w∥22τ2.\frac{\lVert y-Xw\rVert^2}{2\sigma^2}+\frac{\lVert w\rVert^2}{2\tau^2}.

Multiplying by 2σ22\sigma^2 gives our ridge convention with λ=σ2/τ2\lambda=\sigma^2/\tau^2. A smaller prior variance means a stronger penalty.

A Laplace prior proportional to exp⁡(−∣wj∣/b)\exp(-|w_j|/b) instead gives an L1 penalty with λ=2σ2/b\lambda=2\sigma^2/b. That is the lasso MAP estimate. The continuous posterior under a Laplace prior does not put positive probability mass exactly at zero; a zero MAP coefficient is an optimization result. A prior is an explicit modelling choice, not proof that a selected feature is important.

The constrained form: corners of the diamond

A norm constraint offers another view: minimize squared error inside an L2 ball or an L1 ball. For the appropriate radius, it recovers a penalized solution. The numerical multiplier depends on whether the constraint uses a norm or a squared norm; do not equate these multipliers without checking the convention.

In two dimensions, the L1 ball is a diamond with corners on the axes. A range of loss-gradient directions can make a corner optimal. At a corner, one weight is exactly zero. The L2 boundary is smooth, so it does not give the same region of gradient directions to an axis point.

This explains why L1 can select zeros, not why it must do so for every dataset. An optimum can lie along an edge with two nonzero weights, or inside the ball when the constraint is inactive. Geometry also does not identify causal variables.

The lasso and soft thresholding

The lasso minimizes ∥y−Xw∥2+λ∥w∥1\lVert y-Xw\rVert^2+\lambda\lVert w\rVert_1. With orthonormal columns, X⊤X=IX^\top X=I, each coordinate separates. Set z=X⊤yz=X^\top y and define

S(z,a)=sign⁡(z)max⁡(∣z∣−a,0).S(z,a)=\operatorname{sign}(z)\max(|z|-a,0).

Then w^j=S(zj,λ/2)\hat w_j=S(z_j,\lambda/2). For z=(3,1,−0.5)z=(3,1,-0.5) and λ=2\lambda=2, the answer is (2,0,0)(2,0,0). Ridge on the same orthonormal problem divides zz by 1+λ1+\lambda instead. Subtraction with a threshold and multiplication by a factor are different operations.

For general columns, coordinate descent holds the other weights fixed and uses the partial residual r(j)=y−∑k≠jxkwkr^{(j)}=y-\sum_{k\ne j}x_kw_k:

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

Library conventions matter. scikit-learn's lasso uses RSS/(2n)+α∥w∥1\mathrm{RSS}/(2n)+\alpha\lVert w\rVert_1, so α=λ/(2n)\alpha=\lambda/(2n). A parameter copied between these formulas without conversion solves a different problem.

The path and the elastic net

At zero weights, the lasso optimality condition is ∣2xj⊤y∣≤λ|2x_j^\top y|\le\lambda for every column. Hence all weights can stay zero when

λ≥λmax⁡=2max⁡j∣xj⊤y∣.\lambda\ge\lambda_{\max}=2\max_j|x_j^\top y|.

For our synthetic fixture, λmax⁡=3890.52\lambda_{\max}=3890.52, first attained by sensor_a. A decreasing parameter grid gives a regularization path. The chosen model retains seven coefficients, including three auxiliaries with no effect in the generator. Being selected is not evidence of causation.

Exact duplicate features expose another limit. For same-sign weights with fixed sum, ∣a∣+∣b∣|a|+|b| stays constant, so L1 cannot choose the split. Adding λ2(a2+b2)\lambda_2(a^2+b^2) with λ2>0\lambda_2>0 prefers equal weights because the squared sum is smallest there. This is the elastic-net idea: combine L1 selection with L2 stability. Nearly correlated features behave less exactly; grouping remains a preference, not a guarantee of recovering true coefficients.

Choosing the strength

Choose penalty strength on validation data. Each fold fits its own scaler and model. After choosing the parameter, refit on all training data and evaluate once on an untouched test set, or use nested cross-validation. The best score from a parameter search is a selection score.

In the original fixture, five folds with seed 17 compare eight original features with polynomial expansions. With 164 features up to degree three, least squares has mean validation R2=−7.090R^2=-7.090, ridge 0.6520.652 at λ=100\lambda=100, and lasso 0.8030.803 at α=0.3\alpha=0.3. The original eight-feature least-squares fit gets 0.8120.812. The additive generator contains no interactions to uncover. A penalty stabilizes the expanded fit but does not make those features useful.

The optional notebook reproduces the data and selection calculations. Early stopping can also delay fitting weak directions, but is not exactly one ridge penalty in every direction. Next, Linear classifiers and surrogate losses changes the target from a number to a class.

The reviewed source note retains primary ridge, lasso and elastic-net references. This synthetic case and the new assessment were prepared for the Fedor Pakhurov project with Codex assistance.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Regularization: shrinkage and penalty selectionoptional

    Check ridge and soft thresholding; reproduce penalty selection on synthetic data with fold-local preprocessing.

    ~25 min · easier on a computer · The matching course topic has been read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.