Quant Memo
Core

Regularization as a Prior

Adding a penalty to a loss function is not a hack. It is exactly the same arithmetic as writing down what you believed about the coefficients before you saw any data, and letting the evidence argue you out of it.

Prerequisites: Ridge and LASSO Regularization, Bayesian Inference, Maximum Likelihood Estimation (MLE)

Fit a regression with 40 candidate features and 60 rows of data and least squares will hand you a beautiful answer: coefficients that reproduce your training set almost perfectly and that mean nothing. Refit next month on new data and every coefficient moves, several flip sign, and the ones that looked strongest are the ones that moved most.

The data alone cannot pin the answer down, so something else has to. In practice everyone reaches for the same fix — add a penalty term that punishes big coefficients and rerun. It works, and it is usually taught as a trick: large coefficients are suspicious, so tax them. That framing leaves the important questions unanswerable. Why square the coefficients rather than cube them? Why does the absolute-value version set things to exactly zero when the squared version never does? How big should the penalty be?

All three have crisp answers once you see what the penalty actually is: a statement of what you believed about the coefficients before the data arrived.

The analogy: a new trader's first month

A trader joins your desk and makes 4% in his first month. Annualised, that is roughly 60%. Do you write that into next year's revenue plan?

Obviously not. You blend it with what you knew beforehand: most traders on this desk make something close to zero, and one month is a tiny sample. So you shade the estimate down hard — perhaps to 5% or 8% annualised. If he keeps it up for two years you shade much less, because now the evidence is loud enough to overrule your prior expectation.

Notice you did three things. You started from a belief centred on zero. You measured something. You produced an answer between them, closer to the data as the data got stronger and closer to zero as your prior belief got firmer. That is the entire content of ridge regression, and the "penalty" is just the algebraic residue of the prior.

From prior to penalty

Bayes' rule says the belief you should hold after seeing data DD is proportional to how well each coefficient vector ww explains that data, times how plausible you thought ww was beforehand:

p(wD)    p(Dw)p(w)p(w \mid D) \;\propto\; p(D \mid w)\,p(w)

In words: posterior belief = evidence × prior belief. The symbol p(Dw)p(D \mid w) is the likelihood — the probability of the data you actually saw, if ww were the truth. The symbol p(w)p(w) is the prior — how plausible ww looked before any data.

Maximising a product is awkward, so take negative logarithms, which turns products into sums and turns "find the biggest" into "find the smallest":

w^=argminw  [logp(Dw)a loss function  +  logp(w)a penalty]\hat{w} = \arg\min_{w} \; \Big[\underbrace{-\log p(D \mid w)}_{\text{a loss function}} \;+\; \underbrace{-\log p(w)}_{\text{a penalty}}\Big]

That is the whole identity. The loss you minimise is the negative log-likelihood, and the penalty you add is the negative log of your prior. Every penalty in machine learning is some prior read backwards.

Two priors dominate. A Gaussian prior wN(0,τ2)w \sim N(0, \tau^2) has logp(w)=w2/(2τ2)-\log p(w) = w^2/(2\tau^2) plus a constant — a squared penalty, so ridge. A Laplace prior with scale bb, whose density falls off like ew/be^{-|w|/b}, has logp(w)=w/b-\log p(w) = |w|/b — an absolute-value penalty, so lasso. The penalty strength is not arbitrary either: with Gaussian noise of variance σ2\sigma^2 in the data, the ridge parameter is λ=σ2/τ2\lambda = \sigma^2/\tau^2, the ratio of how noisy your measurements are to how uncertain you were beforehand.

w = 0 Gaussian smooth at 0 → ridge Laplace kink at 0 → lasso a corner, not a dome
Both priors say "coefficients are probably near zero". The Laplace says it with a sharp corner, and that corner is the only reason lasso produces exact zeros while ridge never does.

Worked example 1: shrinkage, computed

Estimate one number ww from n=10n = 10 noisy readings whose average came out at yˉ=4.0\bar{y} = 4.0. The noise variance is σ2=4\sigma^2 = 4. Your prior is wN(0,τ2)w \sim N(0, \tau^2) with τ2=1\tau^2 = 1 — you genuinely expected something within about ±1\pm 1 of zero.

Step 1 — the penalty strength. λ=σ2/τ2=4/1=4\lambda = \sigma^2/\tau^2 = 4/1 = 4.

Step 2 — minimise. The objective is i(yiw)2+λw2\sum_i (y_i - w)^2 + \lambda w^2. Differentiate and set to zero: 2i(yiw)+2λw=0-2\sum_i (y_i - w) + 2\lambda w = 0, so n(yˉw)=λwn(\bar{y} - w) = \lambda w, giving

w^=nyˉn+λ=10×4.010+4=4014=2.857\hat{w} = \frac{n\bar{y}}{n + \lambda} = \frac{10 \times 4.0}{10 + 4} = \frac{40}{14} = 2.857

Step 3 — check it against Bayes directly. Posterior precision (the inverse of variance) adds: n/σ2+1/τ2=2.5+1=3.5n/\sigma^2 + 1/\tau^2 = 2.5 + 1 = 3.5. Posterior mean =(n/σ2)yˉ/3.5=2.5×4.0/3.5=2.857= (n/\sigma^2)\bar{y} / 3.5 = 2.5 \times 4.0 / 3.5 = 2.857. Identical. The penalised estimate is the peak of the posterior — nothing was approximated.

Step 4 — see the dials move. The shrinkage factor is n/(n+λ)=10/14=0.714n/(n+\lambda) = 10/14 = 0.714; the estimate keeps 71.4% of what the data said. With n=100n = 100 readings instead, w^=400/104=3.846\hat{w} = 400/104 = 3.846 and the factor rises to 0.962 — loud evidence, prior barely heard. Tighten the prior instead to τ2=0.25\tau^2 = 0.25, so λ=16\lambda = 16, and w^=40/26=1.538\hat{w} = 40/26 = 1.538. Same data, firmer belief, harder shrink.

This is the trader story with numbers attached: 4.0 was the measurement, 0 was the prior, 2.857 is where the argument settled.

The explorer below is the same mechanism with a coin instead of a coefficient. Drag the prior to be tight or diffuse, then add observations, and watch the posterior slide from the prior towards the data as evidence accumulates.

Bayes updater
00.51dashed = prior · solid = posterior
data 7/10posterior mean 0.643prior mean 0.500

Worked example 2: why lasso hits exactly zero and ridge does not

Take the simplest possible case — one coefficient, unit noise variance, and zz standing for what plain least squares would have chosen.

Ridge. Minimise 12(zw)2+λ2w2\tfrac{1}{2}(z - w)^2 + \tfrac{\lambda}{2}w^2. The derivative is (wz)+λw(w - z) + \lambda w, zero at

w^ridge=z1+λ\hat{w}_{\text{ridge}} = \frac{z}{1+\lambda}

With λ=0.20\lambda = 0.20: an OLS estimate of z=0.30z = 0.30 becomes 0.30/1.2=0.2500.30/1.2 = 0.250, and z=0.12z = 0.12 becomes 0.1000.100. Everything is scaled by the same factor and nothing reaches zero.

Lasso. Minimise 12(zw)2+λw\tfrac{1}{2}(z-w)^2 + \lambda|w|. For w>0w > 0 the derivative is (wz)+λ(w - z) + \lambda, zero at w=zλw = z - \lambda; if that is negative the minimum sits at the corner instead. So

w^lasso=sign(z)max(zλ,  0)\hat{w}_{\text{lasso}} = \text{sign}(z)\,\max(|z| - \lambda,\; 0)

With λ=0.20\lambda = 0.20: z=0.30z = 0.30 becomes 0.300.20=0.1000.30 - 0.20 = 0.100, and z=0.12z = 0.12 becomes exactly 00, because 0.12<0.200.12 < 0.20.

Why the difference, in one sentence. The pull a penalty exerts is its slope. The Gaussian penalty w2/(2τ2)w^2/(2\tau^2) has slope w/τ2w/\tau^2, which goes to zero as ww approaches zero — the prior stops pushing just when it would need to push hardest. The Laplace penalty w/b|w|/b has slope 1/b1/b right up to the origin — a constant force that never lets go, so any coefficient whose evidence is weaker than that force gets pinned at zero. The corner in the picture above is that constant force.

unpenalised estimate z fitted w no penalty ridge lasso dead zone: |z| < λ
Ridge tilts the line — every coefficient shrinks by the same proportion and none arrives at zero. Lasso shifts it and flattens a dead zone near the origin, so weak coefficients are deleted outright.

A penalty is a prior written as arithmetic: penalty=logp(w)\text{penalty} = -\log p(w). Squared penalty means Gaussian prior; absolute-value penalty means Laplace prior; and the strength is λ=σ2/τ2\lambda = \sigma^2/\tau^2 — measurement noise divided by prior confidence.

What this means in practice

You can reason about λ\lambda instead of grid-searching it blindly. "How large did I honestly expect these coefficients to be?" gives you τ\tau, and the residual scale gives you σ\sigma. That will not beat cross-validation, but it tells you the right order of magnitude and catches the case where the best cross-validated λ\lambda implies a prior nobody would defend.

Standardise your features first, or your prior is nonsense. One penalty applied to coefficients measured in basis points and in dollars asserts that both should be about the same size. They should not.

Do not penalise the intercept. A prior centred on zero is a claim that the effect is probably absent. You rarely believe that about the overall mean.

Choosing a penalty is choosing a belief, so choose deliberately. Expect a handful of real drivers among many candidates? That is a spike near zero with fat tails, and lasso approximates it. Expect many small correlated effects, as with most return factors? That is Gaussian, so ridge. Want both, use elastic net.

The equivalence is between the penalised estimate and the mode of the posterior — its single highest point — not the posterior itself. Two consequences bite. First, the mode is not the mean, and for skewed posteriors they differ; lasso's zeros are a property of the mode alone, and the posterior mean under a Laplace prior is essentially never exactly zero. Second, a MAP estimate is a single number, so it carries none of the uncertainty the prior was there to represent. Reporting a ridge coefficient without an interval is not Bayesian inference — it is one point plucked from a distribution you did not look at. The related mix-up: λ\lambda is not "how much regularization is good", it is a ratio of two variances, which is why the best λ\lambda shrinks as your sample grows even though your beliefs never changed.

Practise it

  1. With n=25n = 25, σ2=9\sigma^2 = 9, τ2=1\tau^2 = 1 and yˉ=2.0\bar{y} = 2.0, compute λ\lambda and w^\hat{w}. (Answer: λ=9\lambda = 9, w^=50/34=1.471\hat{w} = 50/34 = 1.471.)
  2. How large must nn be before the shrinkage factor n/(n+λ)n/(n+\lambda) exceeds 0.95 when λ=4\lambda = 4? (Answer: n>76n > 76.)
  3. Under lasso with λ=0.15\lambda = 0.15, which of the OLS estimates 0.42-0.42, 0.090.09, 0.150.15, 0.31-0.31 survive, and at what values? (Answer: 0.27-0.27, 00, 00, 0.16-0.16.)
  4. A colleague sets λ=100\lambda = 100 on standardised features and gets good cross-validated error. What prior standard deviation τ\tau does that imply if the residual variance is 4, and would you defend that belief out loud?

Related concepts

Practice in interviews

Further reading

  • Murphy, Probabilistic Machine Learning: An Introduction, ch. 4.5 and 11.3
  • Bishop, Pattern Recognition and Machine Learning, ch. 3.3
ShareTwitterLinkedIn