Regularization

An interactive companion to the Konstanz 2026 lecture. Each section is paired with a live demonstration; adjust the controls to explore.

1. The data-generating process

We fix a true regression function and observe noisy samples

with inputs drawn uniformly. Throughout this page we take , the canonical example from the first chapter of Bishop, chosen because it is non-polynomial, has nontrivial curvature, and cannot be fit exactly by the polynomial function class introduced below.

Our task is to learn an estimator from a training sample that approximates well on new .

Demo 1 · The data-generating process

00.20.40.60.81-1.5-1-0.500.511.5xy

true f(x) = sin(2π x) noisy training points

The two parameters that control the difficulty of the problem are and . With and large, any reasonable estimator recovers essentially perfectly. With large and small, even predictions made with the true leave residuals of variance : the irreducible error that no estimator can avoid.

2. Polynomial regression by ordinary least squares

Choose a degree and use the polynomial function class

Stack the training inputs into a design matrix with . The OLS estimator minimizes

with the well-known closed form

(In the demos we work in the rescaled coordinate so that the columns of remain bounded; this is a numerical convenience and does not affect the analysis.)

Demo 2 · Polynomial regression by OLS

00.20.40.60.81-2-1012xy
train MSE0.0345
test MSE0.8130
d12

As increases at fixed , two patterns are visible:

This is the symptom of the bias–variance tradeoff. To analyze it properly we require the decomposition.

3. The bias–variance decomposition

The quantity of interest is the total error, the expected squared error of the learned averaged over a fresh test input , the training set , and the test noise :

A short calculation shows that, at a fixed , this expected error splits into three nonnegative pieces:

Averaging over extends the same identity to the total error:

Interpretation. Bias measures how far the average learner is from the truth and is a property of the function class. Variance measures how much the learner fluctuates across draws of and reflects how strongly it adapts to the noise. The noise term is the irreducible floor that no estimator can fall below.

Estimating the components. Bias and variance are expectations over training sets, so we cannot compute them in closed form. The demo approximates them by simulation: it draws many random training sets, fits a degree- polynomial to each, and looks at the spread of the resulting predictions. At each , the spread across training sets gives , and the gap between the average prediction and gives . Averaging these along yields the scalar values shown in the legend. The MC resamples slider controls the number of training sets — more samples give smoother estimates.

Demo 3 · Live Monte-Carlo bias–variance decomposition

00.20.40.60.81-2-1012xŷ over many training sets

true f individual fits mean fit E[ŷ]

Pointwise decomposition · 𝔼[(y − ŷ)² | x] = Bias²(x) + Var(x) + σ²

00.20.40.60.8100.20.40.60.811.2xexpected error at x
𝔼ₓ[Bias²] 0.2034 𝔼ₓ[Var] 0.0278 σ² 0.0625

𝔼ₓ[Bias²] + 𝔼ₓ[Var] + σ² = 0.2937  ·  empirical test MSE = 0.2903 (should match within MC error)

The top plot shows the ensemble of fits; the second plot is the integrand of as a function of , with the three components stacked. The top edge of the stack is the total expected error at each , and the area of each band integrates to its scalar in the legend below.

Suggested exercises:

  1. Set (the constant fit). The orange Bias² band dominates and tracks , visibly larger near the peaks where is maximal. Variance is uniformly small; every training set produces nearly the same flat line.
  2. Set at small . Variance grows substantially, particularly near and , where high-degree polynomials oscillate most strongly given the limited support at the boundaries. Bias² remains small throughout.
  3. Increase . The gray noise band rises uniformly across , and the integrated identity continues to track the empirical test MSE up to Monte Carlo error.

This is the structure we now seek to control. Regularization is the principal tool.

4. Ridge regression: shrinking variance with

A standard remedy in high-variance regimes is to add an penalty to the weights:

Setting the gradient to zero gives the closed form

Two limiting cases are useful to keep in mind: recovers OLS, while shrinks every coefficient to zero.

Demo 4 · Ridge regression (L₂ penalty)

00.20.40.60.81-2-1012xy

Coefficients ŵj

w0
0.02
w1
-3.16
w2
-1.44
w3
3.62
w4
3.24
w5
2.18
w6
-0.42
w7
0.34
w8
-1.06
w9
-1.29
w10
-0.43
w11
-2.43
w12
0.40

Fix the polynomial degree at the deliberately large value and vary :

As grows, the quadratic penalty pulls each coefficient continuously toward zero. The shrinkage is smooth: ridge rarely sets any coefficient exactly to zero.

5. Lasso: sparsity from bonus

Optional material — ℓ₁ regularization was not covered in the lecture. Click to expand, or proceed to §6.

Switching the penalty to ,

changes the geometry. The constraint set is a polytope with vertices on the coordinate axes, so the optimum tends to lie at a vertex, i.e., with several coefficients exactly zero.

Unlike ridge, the lasso objective has no closed-form solution. It can instead be solved iteratively by gradient descent, a method that will be introduced in a later lecture; the demo below uses such an iterative scheme.

Demo 5 · Lasso (L₁ penalty) · sparsity

00.20.40.60.81-2-1012xy

Coefficients · 24 / 25 non-zero

w0
0.18
w1
-2.27
w2
-0.79
w3
1.78
w4
·
w5
1.11
w6
0.22
w7
0.26
w8
0.42
w9
-0.01
w10
0.49
w11
-0.21
w12
0.48
w13
-0.31
w14
0.44
w15
-0.33
w16
0.39
w17
-0.31
w18
0.32
w19
-0.27
w20
0.26
w21
-0.21
w22
0.20
w23
-0.16
w24
0.14
w25
-0.11

Increase and count the coefficients that are exactly zero. Lasso is not rounding small coefficients to zero post hoc; those weights are the actual optimum of the penalized objective. This is the property that gives lasso its feature-selection interpretation.

6. Bias–variance with regularization

We now repeat the bias–variance Monte Carlo with a regularization penalty in place. At fixed degree , varying traces a smooth path through model space that exchanges bias for variance.

Demo 3 · Live Monte-Carlo bias–variance decomposition

penalty:
00.20.40.60.81-2-1012xŷ over many training sets

true f individual fits mean fit E[ŷ]

Pointwise decomposition · 𝔼[(y − ŷ)² | x] = Bias²(x) + Var(x) + σ²

00.20.40.60.810246xexpected error at x
𝔼ₓ[Bias²] 0.0098 𝔼ₓ[Var] 0.1918 σ² 0.0625

𝔼ₓ[Bias²] + 𝔼ₓ[Var] + σ² = 0.2641  ·  empirical test MSE = 0.2608 (should match within MC error)

Suggested exercises:

  1. With and ridge selected, locate the that minimizes the total error.
  2. Switch to lasso and locate its optimum; compare with the ridge result.
  3. Disable the penalty (select OLS) and observe the estimator at the same . The gap quantifies the gain from regularization.

7. The bias–variance tradeoff curve

The previous demos have shown single-shot views: one fit at a chosen , one decomposition at a chosen . We now hold fixed and sweep , plotting each component of the total error

as a curve in . The result is the canonical bias–variance tradeoff plot:

Demo 7 · The regularization path

penalty:
1e-51e-41e-31e-21e-11e01e100.20.40.60.81λ (log scale)expected error λ* = 1e-1
𝔼ₓ[Bias²] 𝔼ₓ[Var] σ² total R(f̂; λ)

min total = 0.1116  at  λ* = 1e-1 (this is what cross-validation is approximately searching for)

Reading from left to right:

The minimizer is the population-optimal hyperparameter: the value one would select with full knowledge of and . In practice neither is available, so must be estimated from data — typically by a train–validation split or by cross-validation. Both procedures pick a point on this same curve.

Suggested exercises:

  1. Increase to 15. The variance curve at small rises sharply, since high-degree polynomials carry high capacity, and shifts to the right.
  2. Decrease to 15. The variance curve rises across the entire range and shifts further right: less data calls for stronger regularization.
  3. Switch to lasso. The bias curve rises in characteristic stair-steps as crosses the thresholds at which individual coefficients become exactly zero.