# A Very Detailed Bias-Variance Breakdown

Although the concept of bias-variance trade-off is often discussed in machine learning textbooks, e.g., Bishop (2006), Hastie, Tibshirani, and Friedman (2009), James et al. (2013), I also find it important in almost any occasions in which we need to fit a statistical model on a data set with a limited number of observations. To better understand the trade-off, we should be clear about the bias-variance decomposition. I prefer to call it bias-variance breakdown cause there are fewer syllables. This post is an attempt to go through the breakdown in a very detailed manner, mainly for my future reference. It may not be 100% correct because I'm very new to this topic. I will make changes if there is anything wrong.

## What is being broken down?

First off, the bias-variance concept lives in theory. In practice, we have no way to separate bias and variance. What we could observe is just the sum of them, as well as something called “irreducible error”. We will cover that later in this post.

Since we are talking about theory, let's make up the thing that needs to be broken down. Let's assume that we are interested in studying the relationship between $X$ and $Y$. Suppose the relationship is a regression model $Y = f(X) + \epsilon$, $\epsilon$ is i.i.d. $E(\epsilon) = 0$ and $\text{Var}(\epsilon) = \sigma^2$.

Then we obtain a data set, $D$, that has $x_i$ and $y_i$ ($D = {x_i, y_i}$). Note that in the data set, $X$ and $Y$ are lowercase and have subscripts. This is because we are referring to observed data of $X$ and $Y$.

As usual, we fit a model to $D$ and obtain the model $\widehat{f}(x_i)$. There is a “hat” on $f(x_i)$ because we are estimating the true model, $f()$. After getting $\widehat{f}()$, we are interested in how this model would perform in the future. So we have to come up with a way to measure the performance of the model when applied to new data. The most common way to measure the predictive performance of a model is mean squared error (MSE) on a new data set ${x^*, y^*}$, or theoretically, the expected squared error:

$$E[(y^* - \widehat{f}(x^*))^2]$$

This is the thing to be broken down.

## Breaking it down

Before we go further, we need to make sure we are clear about which is which. Since this is an expected value, there must be random variables in this equation. What is random here? First, let's take a look at $\widehat{f}(x^*)$. We know that $\widehat{f}()$ comes from $D$, and $D$ contains $\epsilon$, because the true model we assumed is $Y = f(X) + \epsilon$. So $\widehat{f}()$ also contains $\epsilon$ and hence it is a random variable. Second, what about $y^*$? Since ${x^*, y^*}$ is a sample from the true model, it again contains $\epsilon$, therefore $y^*$ is also a random variable.

Let's now play a mathematical trick:

$$E[(y^* - \widehat{f}(x^*))^2] = E[(y^* - f(x^*) + f(x^*) - \widehat{f}(x^*))^2]$$

Here we just add and subtract $f(x^*)$, nothing is changed. Let $A = y^* - f(x^*)$ and $B = f(x^*) - \widehat{f}(x^*)$. Then the above equation becomes:

$$
\begin{aligned}
& E[(A + B)^2]\

&= E[A^2 + B^2 + 2AB]\

&= E[A^2] + E[B^2] + 2E[AB]
\end{aligned}
$$

Let's put $A$ and $B$ back

$$E[(y^* - f(x^*))^2] + E[(f(x^*) - \widehat{f}(x^*))^2] + 2E{[y^* - f(x^*)][f(x^*) - \widehat{f}(x^*)]}$$

This is very complicated, especially the long thing on the right. Let's first expand it: $$2{E[y^* f(x^*)] - E[y^* \widehat{f}(x^*)] - E[f(x^*) f(x^*)] + E[f(x^*) \widehat{f}(x^*)]}$$

According to our theoretical model, we know that $y^* = f(x^*) + \epsilon$, so

$$2{E[(f(x^*) + \epsilon) f(x^*)] - E[(f(x^*) + \epsilon) \widehat{f}(x^*)] - E[f(x^*) f(x^*)] + E[f(x^*) \widehat{f}(x^*)]}$$

$$2{[f(x^*)]^2 - E[f(x^*) \widehat{f}(x^*) + \epsilon \widehat{f}(x^*)] - [f(x^*)]^2 + E[f(x^*) \widehat{f}(x^*)]}$$

$$2{[f(x^*)]^2 - E[f(x^*) \widehat{f}(x^*)] + E[\epsilon \widehat{f}(x^*)] - [f(x^*)]^2 + E[f(x^*) \widehat{f}(x^*)]}$$

Four terms cancel out, the term $E[\epsilon \widehat{f}(x^*)] = 0$, because $\epsilon$ and $\widehat{f}(x^*)$ are independent. Therefore $E[\epsilon \widehat{f}(x^*)] = E[\epsilon] \times E[\widehat{f}(x^*)] = 0$.

OK. That long thing becomes zero and we are left with

$$E[(y^* - f(x^*))^2] + E[(f(x^*) - \widehat{f}(x^*))^2]$$

Now let's play a similar trick on the second term, the first term remains unchanged.

$$E[(y^* - f(x^*))^2] + E{[f(x^*) - E[\widehat{f}(x^*)] + E[\widehat{f}(x^*)] - \widehat{f}(x^*)]^2}$$

Again let $A = f(x^*) - E[\widehat{f}(x^*)]$ and $B = E[\widehat{f}(x^*)] - \widehat{f}(x^*)$.

$$E[(y^* - f(x^*))^2] + E[(A + B)^2]$$

$$E[(y^* - f(x^*))^2] + E[A^2] + E[B^2] + 2E[AB]$$

Plug in $A$ and $B$.

$$E[(y^* - f(x^*))^2] + E{[f(x^*) - E[\widehat{f}(x^*)]]^2} + E{[E[\widehat{f}(x^*)] - \widehat{f}(x^*)]^2}$$ $$ + 2E{[f(x^*) - E[\widehat{f}(x^*)]][E[\widehat{f}(x^*)] - \widehat{f}(x^*)]}$$

Let's again look at the most annoying thing on the second row. We notice that (1) $f(x^*) - E[\widehat{f}(x^*)]$ is a constant, and (2) the expected value of $E[\widehat{f}(x^*)] - \widehat{f}(x^*)]$ is just $E{E[\widehat{f}(x^*)]} - E[\widehat{f}(x^*)]$. This equals $E[\widehat{f}(x^*)] - E[\widehat{f}(x^*)] = 0$.

So we are left with

$$E[(y^* - f(x^*))^2] + E{[f(x^*) - E[\widehat{f}(x^*)]]^2} + E{[E[\widehat{f}(x^*)] - \widehat{f}(x^*)]^2}$$

We also notice that, in the second term, both $f(x^*)$ and $E[\widehat{f}(x^*)]$ are constants, so we can drop the expectation operator.

$$E[(y^* - f(x^*))^2] + [f(x^*) - E[\widehat{f}(x^*)]]^2 + E{[E[\widehat{f}(x^*)] - \widehat{f}(x^*)]^2}$$

Cool! We have finished the breakdown.

## Naming things

As a final step, let's name the three terms in

$$E[(y^* - f(x^*))^2] + [f(x^*) - E[\widehat{f}(x^*)]]^2 + E{[E[\widehat{f}(x^*)] - \widehat{f}(x^*)]^2}$$

The first term, $E[(y^* - f(x^*))^2]$, is the variance of $\epsilon$, which is $\sigma^2$. We call this “irreducible error” or “irreducible noise”.

The second term, $[f(x^*) - E[\widehat{f}(x^*)]]^2$, is the “squared bias”, because the definition of the bias of an estimator is $\text{bias}(\widehat{\theta}) = E(\widehat{\theta}) - \theta$.

The third term, $E{[E[\widehat{f}(x^*)] - \widehat{f}(x^*)]^2}$is, by definition, the variance of $\widehat{f}(x^*)$.

So to put everything together,

$$E[(y^* - \widehat{f}(x^*))^2] = \text{Var}(\epsilon) + \text{bias}^2(\widehat{f}(x^*)) + \text{Var}(\widehat{f}(x^*))$$

## Reference

Bishop, Christopher M. (2006). *Pattern recognition and machine learning*. New York: Springer,

Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani. (2013). *An introduction to statistical learning: with applications in R*. New York :Springer,

Hastie, T., Tibshirani, R., & Friedman, J. H. (2009). *The elements of statistical learning: data mining, inference, and prediction*. 2nd ed. New York: Springer.