# Bias and Variance — Cut Through the Noise

## A mathematical look into where Bias and Variance come from

Bias and Variance are amongst the more misunderstood concepts in ML, as they’re usually described using superficial explanations of under-fitting and over-fitting. In this post, we lay down the statistical groundwork to understand where they come from. The maths is thoroughly explained, so you won’t need to be an expert in Statistics to understand it.

That said, you should be familiar with the basic concepts of a probability distribution and its Expected Value (average). We’ll also assume you’re familiar with the ML ideas of regression and supervised learning. So let’s start with the problem setup:

# Supervised Learning Setup

## Input and Output Vectors

** x** is a random input vector of any dimension — e.g. the features of a house (no. of bedrooms, size of living room, etc).

** y** is a random output vector of any dimension — e.g. the house price. In ML, your aim is to predict

**given an**

*y***.**

*x*## Joint Distribution

** P(x, y)** is the joint probability distribution from which

**pairs are drawn. This is the key assumption, that there is some process that generates all your data, represented by a probability distribution. Note that there is a different probability distribution over**

*x, y***for every given**

*y***. i.e. for a given**

*x***there are many possible outputs,**

*x***.**

*y*In other words, for a given set of house features (same size, same no. of bedrooms, etc), there are many possible prices, each with a probability. This is the conditional distribution *P(y|x).*

Below is an example probability density function for ** P(x, y)**:

**is just 1 feature, the house size, and**

*x***is the house price. For each feature you add to**

*y***, you need an extra dimension to visualise the graph, which is why we’re sticking to just 1 feature for now. In reality you’ll have a lot more information about a house than just its size.**

*x*## Dataset

Random variable ** D** representing the dataset, consisting of N

**pairs.**

*x, y***is obtained by sampling**

*D***N times.**

*P(x, y)*## Model

** h** is a function/model which outputs a prediction

**upon taking an input**

*ŷ***.**

*x*## Trained Model and Learner

** A** is a learner function, which takes an input dataset

**and outputs a trained model**

*D***. Think of**

*h_D***as some Sci-Kit Learn code that describes the parameters of a model you’d like to build.**

*A*** h_D** is then the the binary model you get by calling

`.fit()`

on your code, which, like any **, outputs a prediction**

*h***when given an input**

*ŷ***.**

*x*It is important to make this distinction between the learner and the model, as the only thing you really control when building ML models is the learner function ** A** (i.e. your sklearn code). So it’s in your interests that

**has good properties such that it outputs models that work well.**

*A*## Test Error

This is the typical least squares error metric you’ll have seen. It’s telling you to take the square of the difference between the prediction of your model, ** h_D(x)**, and the actual label

*y**for that*

**. The aim of your model**

*x***is to minimise this error across all possible**

*h_D***pairs that you could sample from**

*x, y***.**

*P(x, y)*Let’s quickly revise this, to see that these quantities are in fact random variables themselves. We started with random variables ** x** and

*y**which are distributed according to*

**. As such,**

*P***, the result of drawing N times from**

*D***is also a random variable. Then, you apply a learner function**

*P***to this random variable**

*A***to get another random variable: the model,**

*D***. Therefore,**

*h_D***must also have properties such as mean and variance.**

*h_D*# Expectations of Random Variables

Note on Expected Values: Inside the expectation function (i.e. inside the square brackets), we have some function of random variables. We indicate next to the **E**, what distributions those random variables have.

## Expected Label

Function that gives out the expectation/mean of ** y** for an input

**. This is the**

*x***you’d get**

*y***on average**, for that input

**. It is the expected value of the green conditional distribution curves you saw in the graph earlier.**

*x*In regression, where you predict some ** ŷ** for an input

**, your model cannot do any better than simply predicting the output,**

*x***. Therefore, your aim when developing a model**

*ȳ(x)***is to get it to mimic the outputs of**

*h***as closely as possible. Of course, in ML you don’t have actually have access to this function**

*ȳ***, so you try to estimate it.**

*ȳ*Another way to think of ** ȳ(x)** is to consider it as a collapsed version of

**which instead of giving a distribution over**

*P(x, y)***for any given**

*y***, it just gives the expected value of**

*x***for any given**

*y***.**

*x*Think of ** ȳ** as a ‘

*true model*’ that your ML model tries to mimic.

## Expected Test Error of a Trained Model

This tells us the Test Error you can expect from a regressor ** h_D** on a new sample

**drawn from**

*(x, y)***. Theoretically, this is the sum of errors of every possible**

*P***sample that can be drawn from**

*(x, y)***, weighted by the probability of that sample being drawn.**

*P*Practically, you cannot draw infinitely many samples from ** P**, so instead we approximate this ETE as the Mean Squared Error of the model over a finite number of test samples. Be aware that you could have a model with high ETE (i.e. a bad model), but a low MSE, if you get lucky with the samples you drew from

**to calculate the MSE.**

*P*## Expected Test Error of a Learner

Very similar to the previous ETE, except we also average over all possible datasets the model could have been trained on. Note that we fix the size of the datasets to N samples.

So this is the test error you get by averaging the errors of all possible ** x, y** samples drawn from

**, and all possible datasets**

*P***drawn from**

*D***.**

*Pᴺ*## Expected Regressor

Remember that ** h** is a random variable, so for a given learner

**, there is a distribution of models produced: where the probability associated with each trained model**

*A***is the probability of having picked its dataset**

*h_D***in the first place (which is the product of the probabilities of all the samples it contains). And as with any distribution, there is an expected value to it, which we denote as**

*D***. You can find this expected regressor by averaging all possible trained models for a learner.**

*ħ*Realising that there is a **distribution of trained models for a learner** is the key point to understanding Variance in ML. It’s simply the variance of that distribution.

We have so far seen an intuition for bias and variance, by treating the model we train as a random variable (because it’s a function of our dataset, which is a random variable).

Bias is how much our **expected model** differs from the true model ** ȳ(x)**. Variance is how much the various possible

**trained models**you train vary around the

**expected model**

*.*

Let’s first peek at the final equation and unpack the precise meaning of the terms Bias, Variance and Noise.

**Variance**: On average, how much does the output of each trained model vary from the output of the average of all models for that learner?

**Bias²**: On average, how much does the output of the average model for the learner vary from the expected true output for its input?

**Noise**: On average, how much does the expected output for an input vary from its true output?

There’s a lot of averages and expected values floating around here, so a good way to digest this is to carefully look at this decomposition equation and think about what each of the terms mean — and refer back to where the terms are introduced if needed. **Remember, Bias and Variance are always properties of a Learner, not the trained model itself.**

# Derivation

All we’ve done so far is take the ETE of a learner ** A** , then add and subtract

**inside the expectation. Of course, this is equivalent to just adding zero, but we need this for later steps. Also note that, we’ve shortened the notation next to the**

*ħ(x)***a little bit; but it should be**

*E***as before.**

*(x, y)~P, D~Pᴺ*Now we’ve used ** (a+b)² = a² + b² + 2ab** to expand the square inside the expectation. But it turns out the

**term is in fact zero. Quick proof for that:**

*2ab*Let’s now expand out the square in the ** b² **term using the same tricks. It once again turns out the last term is zero (Try proving this yourself!):

So we end up with this final decomposition of the Expected Test Error of learner ** A**:

## In Words

**Variance**: On average, how much does the output of each trained model vary from the output of the average of all models for that learner?

**Bias²**: On average, how much does the output of the average model for the learner vary from the expected true output for its input?

**Noise**: On average, how much does the expected output for an input vary from its true output?

Intuitively, if ** A** has high variance, it shows that the predictions of the trained

**vary quite a lot based on which dataset**

*h_D***it is trained on. And this is the exact thing we’d like to avoid with our models, since it shows that our learner**

*D***is too sensitive to its training data. This is**

*A***overfitting**.

And if ** A** has high bias, it shows that the predictions on average are just wrong. This usually indicates that the model is not expressive enough to model our data. This is

**underfitting**.

# Footnotes ††

## How we represent Noise in the system

You may have seen a Gaussian distributed Noise term in other derivations of Bias/Variance (such as wikipedia). The joint distribution ** P(x, y)** assumption that we use in fact encapsulates the noise.

*‘Noise’*tries to capture the uncertainty in our system: it tells us that given an input

**, you cannot be certain about the output. A joint distribution is an effective way of treating this uncertainty, since each**

*x***will have a distribution over**

*x***.**

*y*The other viewpoint uses a slightly different setup, where a deterministic function ** f(x)** outputs

**for an input**

*y***. But to indicate that each**

*x***does not always correspond to the same**

*x***, a Gaussian distributed noise term**

*y***is added, giving**

*ε***. This’ll lead to a very similar decomposition of bias and variance. FYI: the reason this error term**

*y = f(x) + ε***is assumed to be Gaussian distributed comes from the Central Limit Theorem.**

*ε*## How size of your dataset affects variance

If your dataset was really really big, then your learner is unlikely to have a large variance. Conversely, training on a tiny dataset will result in a high variance even if the model is pretty good. This is reflected in the expectation equation, where we explicitly state that ** D~Pᴺ**.

**Thanks to CS4780@Cornell**

The derivation and intuitions laid out in this post roughly follow this lecture taught by Kilian Weinberger. I’d also highly recommend checking out the rest of his course, it’s one of the best explained ML courses online.

Thanks to Sebastian Wagner.