Statistical Modeling and Generalization

Regularization

Regularization as adding a preference for simpler parameter choices so models generalize instead of chasing noise.

Lesson
23

Goal

Understand regularization as adding a second goal to learning: fit the data, but prefer parameter values that stay simple enough to generalize.

Motivation

Lesson 22 introduced the problem: high-capacity models can memorize noise. Regularization is one of the cleanest responses to that problem.

Instead of asking only

How well does the model fit the observed data?

we also ask

Among models with similar fit, which ones are less brittle?

That extra preference changes the optimization problem in a useful way.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
wweight vectorThe model parameters we are learning.
λlambdaHow strongly we penalize complex parameter choices.
`w
`w
Loss_data(w)data-fit termThe part of the loss that measures prediction error on the training data.
Loss_data(w) + λR(w)regularized objectiveThe total objective, where R(w) is a penalty term.

Core Ideas

Fitting Error Alone Is Too Permissive

If the loss function only measures training error, it has no opinion about the size of the parameters.

That means it may choose:

as long as the training fit looks good.

This is one reason overfit polynomial models often come with large coefficients. The objective function never told them not to.

Regularization Adds Another Preference

The regularized pattern is:

Loss(w)=Lossdata(w)+λR(w),\text{Loss}(w) = \text{Loss}_{\text{data}}(w) + \lambda R(w),

where:

When λ = 0, we recover the unregularized objective.

As λ increases, the optimizer cares more about keeping the parameters small or sparse.

Why Large Weights Often Signal Brittleness

Large coefficients can make a model extremely sensitive to tiny changes in the input.

That is the geometric picture to keep:

Big weights can make nearby inputs produce very different outputs.

If the input contains noise, that sensitivity can amplify the noise into unstable predictions.

L2 Regularization Shrinks

L2 regularization uses

R(w)=w22=iwi2.R(w) = ||w||_2^2 = \sum_i w_i^2.

Because the weights are squared, very large weights are punished especially strongly.

The effect is usually:

This is why L2 often feels like:

Spread the work across many smaller coefficients.

L1 Regularization Encourages Sparsity

L1 regularization uses

R(w)=w1=iwi.R(w) = ||w||_1 = \sum_i |w_i|.

This creates a different preference. L1 is more willing to drive some weights exactly to zero.

That is why L1 often feels like:

Use as few coefficients as possible.

This becomes valuable when feature selection matters.

Lambda Controls The Tradeoff

As λ increases:

That last point matters. The common pattern is not “more regularization always helps.” It is:

Too little regularization can overfit, and too much regularization can underfit.

So the validation curve for test-like performance is often U-shaped with respect to λ.

Regularization Has A Bayesian Interpretation

Regularization is not only a numerical trick. It is also a way of encoding prior beliefs.

MAP estimation maximizes

P(Dθ)P(θ).P(D \mid \theta)P(\theta).

If the prior says “weights are probably near zero,” the corresponding optimization problem looks like L2 regularization.

If the prior says “many weights should actually be zero,” the corresponding preference lines up with L1 regularization.

This is the key bridge:

Optimization language says “add a penalty.” Bayesian language says “add a prior.” In this lesson, those are often two views of the same structure.

Worked Example

Example 1: Two Polynomial Models

Suppose we compare:

Even if both models fit the training data similarly, Model B is usually more fragile. Its huge coefficients suggest a function that bends sharply and reacts strongly to small input changes.

Regularization encodes a preference for Model A-like behavior unless the data gives a compelling reason to do something more extreme.

Example 2: L1 Versus L2 On Two Weight Vectors

Compare the vectors:

(2,2,2,2)(2, 2, 2, 2)

and

(8,0,0,0).(8, 0, 0, 0).

For L1:

2+2+2+2=82 + 2 + 2 + 2 = 8

and

8+0+0+0=8.8 + 0 + 0 + 0 = 8.

So L1 treats them equally.

For L2:

22+22+22+22=162^2 + 2^2 + 2^2 + 2^2 = 16

while

82=64.8^2 = 64.

So L2 punishes the single huge weight much more heavily.

That is a compact way to remember the difference:

Why This Shows Up In ML

Regularization appears everywhere in modern ML:

It also provides a durable mental separation:

That distinction becomes increasingly important as models get more expressive.

Exercises

  1. Why can a model with very large coefficients be more sensitive to noise than a model with smaller coefficients?
  2. What happens qualitatively to training error, test error, and weight magnitudes as λ increases from zero to a very large value?
  3. Why does L1 tend to produce sparse solutions more naturally than L2?
  4. In what sense is regularization similar to choosing a prior before seeing the data?

Implementation Exercise

Take a simple TypeScript regression experiment and extend its objective from

Lossdata(w)\text{Loss}_{\text{data}}(w)

to

Lossdata(w)+λw22.\text{Loss}_{\text{data}}(w) + \lambda ||w||_2^2.

Then run the same experiment for several values of λ and record:

If you want a second pass, swap in an L1 penalty and compare how the learned weight vectors differ.

Reflection Questions