{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"maximum-a-posteriori-and-regularisation","title":"Regularisation is a prior: L2, L1 and the diamond","url":"https://addaly.com/learn/maths-you-need/maximum-a-posteriori-and-regularisation","position":56,"of":76,"minutes":10,"answer":"Adding a penalty to a loss is maximising a posterior rather than a likelihood, a Gaussian prior on the weights gives the L2 penalty and a Laplace prior gives L1.","keyIdea":"Adding a penalty to a loss is maximising a posterior rather than a likelihood, a Gaussian prior on the weights gives the L2 penalty and a Laplace prior gives L1, and L1 produces exact zeros because its pull stays constant as a weight shrinks while L2's pull fades with the weight."},"body":{"format":"markdown","text":"## Where maximum likelihood goes wrong, and the repair\n\nModule 5 derived every standard loss from maximum likelihood: choose the parameters that make the data most probable. It also noted the flaw. Given enough parameters and too little data, the parameters that make the training data most probable are ones that have memorised it. The likelihood is happy to set a weight to 40 if that fits one row.\n\nThe repair is to say what you believed about the parameters before seeing data. Bayes' rule, from module 4, gives\n\n```\nposterior ∝ likelihood × prior\n```\n\nand maximising the posterior instead of the likelihood is **maximum a posteriori** estimation, MAP. Take logs:\n\n```\nlog posterior = log likelihood + log prior + const\n```\n\nMinimising the negative of this gives\n\n```\nloss = data loss + (−log prior)\n```\n\nThe second term is the regulariser. Every penalty term you have seen added to a loss is a negative log prior, whether or not the person who added it thought of it that way.\n\n## A Gaussian prior is weight decay\n\nSuppose you believe each weight is probably small: normally distributed around zero with spread `τ`. The log of that density is `−w²/(2τ²)` plus a constant, so the penalty is\n\n```\nΣ w² / (2τ²)\n```\n\nThat is the L2 penalty, with strength `λ = 1/(2τ²)`. A large `λ` is a narrow prior, a strong belief that weights are near zero. Weight decay in an optimiser is this penalty's gradient, `λ w`, applied each step. The hyperparameter you tune on a validation set is, in this reading, the width of your prior on the weights, chosen empirically. That is a legitimate way to choose a prior, and it is what nearly everyone does.\n\n## A Laplace prior is L1\n\nBelieve instead that weights follow a Laplace distribution, whose density is `exp(−|w|/b)`. The log is `−|w|/b`, and the penalty is\n\n```\nΣ |w| / b\n```\n\nThe L1 penalty. Same recipe, different prior, and a qualitatively different result: L1 sets some weights to exactly zero, L2 never does. The course `machine-learning-foundations` uses both; here is the mechanism.\n\n## Why L1 reaches zero and L2 does not\n\nTake one weight whose data loss is `(w − 0.3)²/2`, pulling it toward 0.3 with unit curvature.\n\nWith an L2 penalty `λw²/2` at `λ = 1`, the total is `(w − 0.3)²/2 + w²/2`. Its derivative, `(w − 0.3) + w`, is zero at `w = 0.15`. The weight is halved. Make `λ` larger and it shrinks further, but the derivative of the penalty is `λw`, which vanishes as `w` approaches zero. The penalty's pull fades exactly when it would need to finish the job. `w` is never exactly zero.\n\nWith an L1 penalty `λ|w|` at `λ = 0.5`, the pull is `0.5` toward zero regardless of how small `w` is. The data pull at `w` is `0.3 − w`. If the data pull at `w = 0` is weaker than 0.5, and it is, since `0.3 < 0.5`, the weight cannot leave zero. So `w = 0` exactly. At `λ = 0.1` the data win and `w = 0.3 − 0.1 = 0.2`. The general solution is\n\n```\nw = sign(w₀) × max(0, |w₀| − λ)\n```\n\ncalled soft thresholding: shrink by `λ`, and if that crosses zero, stop there. Weights whose data pull is weaker than `λ` are removed. That is a constant force against a fading one, and it is the whole reason L1 produces sparse models.\n\nThe usual picture says the same thing geometrically. The L2 penalty's level sets are circles, the L1 penalty's are diamonds, and the minimum of loss plus penalty tends to touch a diamond at a corner, where a coordinate is zero. The picture and the derivative are one fact.\n\n## Early stopping is a prior too\n\nStart gradient descent from zero weights and stop after `t` steps. Directions in which the loss has high curvature converge quickly; directions with low curvature have barely moved from zero. Stopping early therefore holds the low-curvature directions near zero, which is what an L2 penalty of strength about `1/(learning rate × t)` would have done. The number of epochs is a regularisation strength, which is why it gets tuned on the validation set like one.\n\n## Two honest limits\n\n**MAP is the peak of the posterior, not its centre.** Under a Laplace prior the peak is sparse, with weights at exactly zero; the posterior mean is not sparse at all. Sparsity is a property of the point estimate you chose to report, not of the belief. Averaging models trained with L1 loses the zeros.\n\n**The prior is a modelling choice with consequences.** A Gaussian prior on weights says a weight of 3τ is rare and 6τ nearly impossible. If the true function needs one large weight, L2 will fight it in every step and settle for many small ones instead. Regularisation encodes what you expect the solution to look like, and when the expectation is wrong the fit is worse, not merely slower.\n\n## One line to try\n\n```python\nimport numpy as np\nw0, lam = 0.3, 0.5\nl2 = w0 / (1 + lam)                                  # 0.2 for lam=0.5\nl1 = np.sign(w0) * max(0.0, abs(w0) - lam)            # 0.0\n```\n\nChange `lam` and watch L1 snap to zero while L2 only ever halves the distance."},"check":{"question":"Why does an L1 penalty set some weights to exactly zero while an L2 penalty only shrinks them?","options":[{"text":"The L1 pull stays at λ however small the weight; the L2 pull fades with it.","correct":true},{"text":"L1 is simply a stronger penalty than L2 for the same λ, so it pushes harder.","correct":false,"tempting":"Strength is not the mechanism; a large L2 λ shrinks weights aggressively and still never reaches zero, because its gradient is proportional to the weight."},{"text":"L2 squares the weights, so small weights are penalised more heavily than under L1.","correct":false,"tempting":"Backwards: squaring makes small weights cheaper, not dearer, which is exactly why L2 stops caring about them near zero."},{"text":"L1 rounds any weight below a threshold to zero after training finishes.","correct":false,"tempting":"Nothing is rounded afterwards; the zeros arise during optimisation because a constant pull toward zero beats a data pull weaker than λ."}]},"prev":{"slug":"beta-distributions-and-updating-a-rate","title":"Updating a rate as evidence arrives: the Beta distribution","url":"https://addaly.com/learn/maths-you-need/beta-distributions-and-updating-a-rate"},"next":{"slug":"regression-to-the-mean","title":"Regression to the mean, and the winner's curse in model selection","url":"https://addaly.com/learn/maths-you-need/regression-to-the-mean"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Regularisation is a prior: L2, L1 and the diamond', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/maximum-a-posteriori-and-regularisation (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Regularisation is a prior: L2, L1 and the diamond","courseTitle":"The Maths You Actually Need","module":{"index":6,"slug":"estimation-from-samples","title":"Estimation: from a sample to a number you can trust"},"answer":null,"keyIdea":"Adding a penalty to a loss is maximising a posterior rather than a likelihood, a Gaussian prior on the weights gives the L2 penalty and a Laplace prior gives L1, and L1 produces exact zeros because its pull stays constant as a weight shrinks while L2’s pull fades with the weight.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"where-maximum-likelihood-goes-wrong-and-the-repair","runs":[{"text":"Where maximum likelihood goes wrong, and the repair"}]},{"t":"p","runs":[{"text":"Module 5 derived every standard loss from maximum likelihood: choose the parameters that make the data most probable. It also noted the flaw. Given enough parameters and too little data, the parameters that make the training data most probable are ones that have memorised it. The likelihood is happy to set a weight to 40 if that fits one row."}]},{"t":"p","runs":[{"text":"The repair is to say what you believed about the parameters before seeing data. Bayes’ rule, from module 4, gives"}]},{"t":"code","lang":"","label":"Text","text":"posterior ∝ likelihood × prior"},{"t":"p","runs":[{"text":"and maximising the posterior instead of the likelihood is "},{"text":"maximum a posteriori","b":true},{"text":" estimation, MAP. Take logs:"}]},{"t":"code","lang":"","label":"Text","text":"log posterior = log likelihood + log prior + const"},{"t":"p","runs":[{"text":"Minimising the negative of this gives"}]},{"t":"code","lang":"","label":"Text","text":"loss = data loss + (−log prior)"},{"t":"p","runs":[{"text":"The second term is the regulariser. Every penalty term you have seen added to a loss is a negative log prior, whether or not the person who added it thought of it that way."}]},{"t":"h2","id":"a-gaussian-prior-is-weight-decay","runs":[{"text":"A Gaussian prior is weight decay"}]},{"t":"p","runs":[{"text":"Suppose you believe each weight is probably small: normally distributed around zero with spread "},{"text":"τ","code":true},{"text":". The log of that density is "},{"text":"−w²/(2τ²)","code":true},{"text":" plus a constant, so the penalty is"}]},{"t":"code","lang":"","label":"Text","text":"Σ w² / (2τ²)"},{"t":"p","runs":[{"text":"That is the L2 penalty, with strength "},{"text":"λ = 1/(2τ²)","code":true},{"text":". A large "},{"text":"λ","code":true},{"text":" is a narrow prior, a strong belief that weights are near zero. Weight decay in an optimiser is this penalty’s gradient, "},{"text":"λ w","code":true},{"text":", applied each step. The hyperparameter you tune on a validation set is, in this reading, the width of your prior on the weights, chosen empirically. That is a legitimate way to choose a prior, and it is what nearly everyone does."}]},{"t":"h2","id":"a-laplace-prior-is-l1","runs":[{"text":"A Laplace prior is L1"}]},{"t":"p","runs":[{"text":"Believe instead that weights follow a Laplace distribution, whose density is "},{"text":"exp(−|w|/b)","code":true},{"text":". The log is "},{"text":"−|w|/b","code":true},{"text":", and the penalty is"}]},{"t":"code","lang":"","label":"Text","text":"Σ |w| / b"},{"t":"p","runs":[{"text":"The L1 penalty. Same recipe, different prior, and a qualitatively different result: L1 sets some weights to exactly zero, L2 never does. The course "},{"text":"machine-learning-foundations","code":true},{"text":" uses both; here is the mechanism."}]},{"t":"h2","id":"why-l1-reaches-zero-and-l2-does-not","runs":[{"text":"Why L1 reaches zero and L2 does not"}]},{"t":"p","runs":[{"text":"Take one weight whose data loss is "},{"text":"(w − 0.3)²/2","code":true},{"text":", pulling it toward 0.3 with unit curvature."}]},{"t":"p","runs":[{"text":"With an L2 penalty "},{"text":"λw²/2","code":true},{"text":" at "},{"text":"λ = 1","code":true},{"text":", the total is "},{"text":"(w − 0.3)²/2 + w²/2","code":true},{"text":". Its derivative, "},{"text":"(w − 0.3) + w","code":true},{"text":", is zero at "},{"text":"w = 0.15","code":true},{"text":". The weight is halved. Make "},{"text":"λ","code":true},{"text":" larger and it shrinks further, but the derivative of the penalty is "},{"text":"λw","code":true},{"text":", which vanishes as "},{"text":"w","code":true},{"text":" approaches zero. The penalty’s pull fades exactly when it would need to finish the job. "},{"text":"w","code":true},{"text":" is never exactly zero."}]},{"t":"p","runs":[{"text":"With an L1 penalty "},{"text":"λ|w|","code":true},{"text":" at "},{"text":"λ = 0.5","code":true},{"text":", the pull is "},{"text":"0.5","code":true},{"text":" toward zero regardless of how small "},{"text":"w","code":true},{"text":" is. The data pull at "},{"text":"w","code":true},{"text":" is "},{"text":"0.3 − w","code":true},{"text":". If the data pull at "},{"text":"w = 0","code":true},{"text":" is weaker than 0.5, and it is, since "},{"text":"0.3 < 0.5","code":true},{"text":", the weight cannot leave zero. So "},{"text":"w = 0","code":true},{"text":" exactly. At "},{"text":"λ = 0.1","code":true},{"text":" the data win and "},{"text":"w = 0.3 − 0.1 = 0.2","code":true},{"text":". The general solution is"}]},{"t":"code","lang":"","label":"Text","text":"w = sign(w₀) × max(0, |w₀| − λ)"},{"t":"p","runs":[{"text":"called soft thresholding: shrink by "},{"text":"λ","code":true},{"text":", and if that crosses zero, stop there. Weights whose data pull is weaker than "},{"text":"λ","code":true},{"text":" are removed. That is a constant force against a fading one, and it is the whole reason L1 produces sparse models."}]},{"t":"p","runs":[{"text":"The usual picture says the same thing geometrically. The L2 penalty’s level sets are circles, the L1 penalty’s are diamonds, and the minimum of loss plus penalty tends to touch a diamond at a corner, where a coordinate is zero. The picture and the derivative are one fact."}]},{"t":"h2","id":"early-stopping-is-a-prior-too","runs":[{"text":"Early stopping is a prior too"}]},{"t":"p","runs":[{"text":"Start gradient descent from zero weights and stop after "},{"text":"t","code":true},{"text":" steps. Directions in which the loss has high curvature converge quickly; directions with low curvature have barely moved from zero. Stopping early therefore holds the low-curvature directions near zero, which is what an L2 penalty of strength about "},{"text":"1/(learning rate × t)","code":true},{"text":" would have done. The number of epochs is a regularisation strength, which is why it gets tuned on the validation set like one."}]},{"t":"h2","id":"two-honest-limits","runs":[{"text":"Two honest limits"}]},{"t":"p","runs":[{"text":"MAP is the peak of the posterior, not its centre.","b":true},{"text":" Under a Laplace prior the peak is sparse, with weights at exactly zero; the posterior mean is not sparse at all. Sparsity is a property of the point estimate you chose to report, not of the belief. Averaging models trained with L1 loses the zeros."}]},{"t":"p","runs":[{"text":"The prior is a modelling choice with consequences.","b":true},{"text":" A Gaussian prior on weights says a weight of 3τ is rare and 6τ nearly impossible. If the true function needs one large weight, L2 will fight it in every step and settle for many small ones instead. Regularisation encodes what you expect the solution to look like, and when the expectation is wrong the fit is worse, not merely slower."}]},{"t":"h2","id":"one-line-to-try","runs":[{"text":"One line to try"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\nw0, lam = 0.3, 0.5\nl2 = w0 / (1 + lam)                                  # 0.2 for lam=0.5\nl1 = np.sign(w0) * max(0.0, abs(w0) - lam)            # 0.0"},{"t":"p","runs":[{"text":"Change "},{"text":"lam","code":true},{"text":" and watch L1 snap to zero while L2 only ever halves the distance."}]}],"check":{"question":[{"text":"Why does an L1 penalty set some weights to exactly zero while an L2 penalty only shrinks them?"}],"options":[{"text":[{"text":"The L1 pull stays at λ however small the weight; the L2 pull fades with it."}],"correct":true,"tempting":null},{"text":[{"text":"L1 is simply a stronger penalty than L2 for the same λ, so it pushes harder."}],"correct":false,"tempting":[{"text":"Strength is not the mechanism; a large L2 λ shrinks weights aggressively and still never reaches zero, because its gradient is proportional to the weight."}]},{"text":[{"text":"L2 squares the weights, so small weights are penalised more heavily than under L1."}],"correct":false,"tempting":[{"text":"Backwards: squaring makes small weights cheaper, not dearer, which is exactly why L2 stops caring about them near zero."}]},{"text":[{"text":"L1 rounds any weight below a threshold to zero after training finishes."}],"correct":false,"tempting":[{"text":"Nothing is rounded afterwards; the zeros arise during optimisation because a constant pull toward zero beats a data pull weaker than λ."}]}],"order":[3,2,1,0],"why":null},"prev":{"slug":"beta-distributions-and-updating-a-rate","title":"Updating a rate as evidence arrives: the Beta distribution","minutes":10},"next":{"slug":"regression-to-the-mean","title":"Regression to the mean, and the winner’s curse in model selection","minutes":9}}}