Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

⚖️ Bias-Variance Tradeoff

To begin with, let’s briefly recap some theoretical concepts you learned about in the lecture. When we talk about fitting machine learning models, we are referring to the process of estimating a function ff that best represents the relationship between a set of predictors XX and an outcome YY (in supervised learning) or to uncover structural patterns in unlabelled data (in unsupervised learning). While the estimated function f^\hat{f} conveys important information about the data from which it was derived (the training data), our primary interest is in using this function to make accurate predictions for future cases in new, unseen data sets.

The fundamental question in statistical learning is how well f^\hat{f} will perform on these future data sets, which brings us to the concept of the bias-variance tradeoff. Bias occurs when a model is too simple to capture the underlying complexities of the data, leading to systematic inaccuracies in its predictions. Variance measures how much the model’s predictions fluctuate when it is trained on different samples from the same population.

Before we continue, check what you remember about the different types of errors from the lecture:

Loading...
Loading...
Loading...
Loading...
Reminder: Types of errors (klick to expand)
  • The irreducible error is inherent in the data due to noise and factors beyond our control (unmeasured variables).

  • The reducible error arises from shortcomings in the model and can be further broken down into:

    • Bias: introduced when a model makes too simple assumptions about the data (underfitting)

    • Variance: the sensitivity of the model to small changes in the training data (overfitting)

This closely relates to the example introduced in 🔁 Recap: Regression Models. Let’s have another look and simulate some data with an underlying relationship in line with a cubic polynomial function. We can see that a linear regression does not capture the nuance of the cubic relationship in the data, while a 10th order model already overfits quite a lot:

import numpy as np

x = np.linspace(-3, 3, 30)
y = (x**3 + np.random.normal(0, 15, size=x.shape)) / 10
<Figure size 1000x400 with 3 Axes>

If we look at the mean squared error (MSE) on the training data, we can see that it decreases with increasing model flexibility:

Reminder: MSE
MSE=1n∑i=1n(yi−y^i)2\text{MSE} = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2
  • yiy_i is the actual value for the ii-th observation

  • y^i\hat{y}_i is the predicted value for the ii-th observation

  • nn is the total number of observations

The term (yi−y^i)2(y_i - \hat{y}_i)^2 represents the squared error for each observation. By averaging these squared errors, the MSE provides a single metric that quantifies how far off the predictions are from the true values.

Degree  Train MSE
1       1.809
3       1.417
10      1.094

However, things look different if we evaluate the same models on new, unseen data. Here, we draw a new sample with fresh noise at the same xx values and compute the MSE of the already fitted models:

<Figure size 1000x400 with 3 Axes>
Degree  Test MSE
1       2.203
3       2.046
10      2.478

Please compare the previous plots and outputs. What do you notice?

Show answer

Two things should become apparent:

  1. In contrast to the training MSE, which decreases with the order of the model, the test MSE is lowest for the 3rd order model.

  2. The test MSEs are generally higher than the training MSEs. This is to be expected, as the initial models did all, to some degree, fit to the noise in the training data.

As an experiment, we can further fit a 10th order model to multiple draws of samples from the same data population. You can see that the fitted curve looks very different every time. The model has high variance:

<Figure size 1000x400 with 3 Axes>

When we increase the flexibility of the model by adding more parameters, we are effectively trading between bias and variance. Initially, as the model becomes more flexible, its bias decreases quickly because it can capture more complex patterns in the data. However, this increased flexibility also makes the model more sensitive to the noise in the training data, which leads to a rise in variance.

Eventually, the reduction in bias is no longer sufficient to counterbalance the increase in variance. This is why a model with a very low training MSE may still suffer from a high test MSE: the low training error is primarily a result of fitting the noise (i.e. high variance), rather than capturing a true underlying pattern.

Bias-variance tradeoff

Figure 1:The bias-variance tradeoff.

Decomposing the error

The figure above is usually drawn on a whiteboard and taken on faith. Because we simulated the data, we actually know the true function, so we can measure bias and variance instead of asserting them.

For this we can draw many training sets from the same data population, fit a model of a given degree to each of them, and compare their predictions at a new test point x0x_0 with the truth:

Their sum is the expected test MSE at x0x_0:

E[(y0−f^(x0))2]=Bias2[f^(x0)]⏟too simple+Var[f^(x0)]⏟too flexible+σ2⏟noise\mathbb{E}\left[(y_0 - \hat{f}(x_0))^2\right] = \underbrace{\mathrm{Bias}^2[\hat{f}(x_0)]}_{\text{too simple}} + \underbrace{\mathrm{Var}[\hat{f}(x_0)]}_{\text{too flexible}} + \underbrace{\sigma^2}_{\text{noise}}

We do this for many test points and average the results:

Source
degree  1   bias² =  0.185   variance =   0.016   expected test MSE =   0.451
degree  2   bias² =  0.184   variance =   0.024   expected test MSE =   0.459
degree  3   bias² =  0.000   variance =   0.032   expected test MSE =   0.282
degree  4   bias² =  0.000   variance =   0.039   expected test MSE =   0.289
degree  5   bias² =  0.000   variance =   0.047   expected test MSE =   0.297
degree  6   bias² =  0.000   variance =   0.054   expected test MSE =   0.304
degree  7   bias² =  0.000   variance =   0.063   expected test MSE =   0.313
degree  8   bias² =  0.000   variance =   0.070   expected test MSE =   0.320
degree  9   bias² =  0.000   variance =   0.079   expected test MSE =   0.329
degree 10   bias² =  0.000   variance =   0.087   expected test MSE =   0.337
degree 11   bias² =  0.000   variance =   0.097   expected test MSE =   0.347
degree 12   bias² =  0.000   variance =   0.107   expected test MSE =   0.357
<Figure size 800x500 with 1 Axes>

Notice that the measured curves (very roughly) follow the schematic figure: bias² collapses as soon as the model is flexible enough to represent a cubic and the variance grows steadily.

Loading...
Loading...
Loading...
Loading...