A Primer
Welcome to another post on basic (but important!) concepts in statistics and machine learning. This post is on model assessment and follows Chapter 7 in The Elements of Statistical Learning
We will, in general, assume to have some training dataset, $\mathcal{T} = { (x_i, y_i) }_{i = 1}^N $, which consists of points $(x,y) \overset{iid}{\sim} \mathcal{F}$ where $\mathcal{F}$ is some joint distribution. Each $x$ is a vector or scalar containing input information, and $y$ is the response variable that we are interested in learning about using the information in $x$. We will fit some prediction model, $\hat{f}$, to $\mathcal{T}$ and compare its predictions to the true $Y$ values using a loss function, which we denote with $\mathcal{L}(y, \hat{f}(x))$.
In what follows, we will assume that $\mathcal{T}$ consists of $N$ training points. There is a slight abuse of notation where we use lowercase $x$ and $y$ to denote both arbitrary random variables from the joint distribution in question as well as realizations of them.
We’ll start with several types of errors that quantify how well a model performs. First is the training error, which is easy to compute but doesn’t tell us more about how the model will work in general.
To get an idea of how the model performs for data we haven’t seen before, we define the test error.
This quantifies how this particular fit of our model is expected to perform on new data. Letting the training set vary yields the expected test error.
By the law of total expectation, $\text{Err} = \mathbb{E}[\text{Err}_{\text{test}}]$. In words, this means that the expected test error is the average test error across all possible training sets and testing points.
This leads us to the in-sample error.
In words, the in-sample error gives us a measure of how well this kind of model can be expected to perform given it was fit to $\mathcal{T}$, where the expectation is with respect to the possible response values associated with each training point.
The optimism of a model is the difference between the in-sample error and the training error. It basically quantifies how much worse (or better) our particular model fit is performing compared to what we expected for this type of model and this training set. The average optimism is simply the expectation of the optimism taken with respect to the responses, $\mathbf{y}$, in $\mathcal{T}$.
How do we judge whether a model is “good”? A reasonable choice is to consider how well we would expect it to perform on data like the ones we used to train it. Due to the relationship between the quantities we defined in the previous section, we can estimate the in-sample error by computing the training error and estimating the expected optimism:
\[\hat{\text{Err}}_{\text{in-sample}} = \text{Err}_{\text{train}} + \hat{\omega}\]We can do this estimation by saving some of the training data to use for this estimation, effectively making it “unseen”.
For certain situations, we can select the “best” model based upon a choice of statistic that is related to the expected optimism is some way. For now, we will consider the standard choices of loss function(0-1 loss, squared error, entropy loss) For these, the following identity holds:
\[\begin{aligned} \label{eq:exp-opt} \omega = \frac{2}{N} \sum_{i = 1}^N \text{Cov}(\hat{y}_i, y_i) \end{aligned}\]The interpretation here is that the higher the covariance between the fitted and true values are, the higher the expected optimism. In other words, the more $y_i$ affects its fitted value, $\hat{y}_i$, the greater the difference between the training and in-sample errors.
For models linear in the predictors (with, say, $d$ parameters) and fit with squared loss, the expected optimism can be expressed as:
\[\begin{aligned} \label{eq:lin-sqr-loss} \omega = \frac{2}{N} \sum_{i = 1}^N \text{Cov}(\hat{y}_i, y_i) = \frac{2}{N} d \sigma_\epsilon^2 \end{aligned}\]This shows that the expected optimism increases with the number of parameters (with $d$) and decreases with the training set size (with $N$). Intuitively, this makes sense. As we increase the number of parameters, our model becomes more and more flexible, which sends the training error to zero.
Our first estimate of the in-sample error is the $C_p$ statistic, which holds when we have a model that is linear in the predictors and fit using squared loss.
If we instead fit the model by maximizing the log-likelihood, then we obtain the Akaike information criterion.
Similar to the AIC is the Bayesian information criterion (BIC). It differs slightly in its form but largely in its motivation/derivation.
A nice property of the BIC is that it is asymptotically consistent, meaning as $N \rightarrow \infty$, it will select the correct model from a family of models with probability going to $1$. AIC does not exhibit this property and tends to select models with higher complexity. However, this property doesn’t mean either one is better than the other. For finite samples, BIC can choose a model that is too simple.
How do we measure the complexity of a model? For models that are linear in the predictors, one way is with the effective number of parameters.
For the $C_p$ statistic, this is what we use for $d$!
Let’s say that we assumed that we are using the squared-error loss, $\mathcal{L}(Y, \hat{f}(X)) = (Y - \hat{f}(X))^2$. We’ll also assume the following data-generating mechanism:
\[Y = f(X) + \epsilon; \hspace{5mm} \mathbb{E}[\epsilon] = 0, \hspace{2mm} \text{Var}(\epsilon) = \sigma^2_{\epsilon}\]The expected testing error of some regression model $\hat{f}(X)$ at a given point $X = x_0$ can be decomposed as:
Adding and subtracting $\mathbb{E}[\hat{f}(x_0)]$ within the expectation in the last line yields:
\[\begin{aligned} \text{Err}(x_0) &= \sigma^2_\epsilon + \mathbb{E}\left[ (f(x_0) - \mathbb{E}[\hat{f}(x_0)] + \mathbb{E}[\hat{f}(x_0)] - \hat{f}(x_0))^2 \right] \\ &= \sigma^2_\epsilon + \underbrace{\mathbb{E}\left[ (f(x_0) - \mathbb{E}[\hat{f}(x_0)])^2 \right]}_{(a)} + 2 \underbrace{\mathbb{E}\left[ (f(x_0) - \mathbb{E}[\hat{f}(x_0)])(\mathbb{E}[\hat{f}(x_0)] - \hat{f}(x_0)) \right]}_{(b)} + \underbrace{\mathbb{E}\left[ (\mathbb{E}[\hat{f}(x_0)] - \hat{f}(x_0))^2 \right]}_{(c)} \end{aligned}\]Because $f(x_0)$ is a fixed quantity, we have:
\[\begin{aligned} (a) &= \mathbb{E}\left[ (f(x_0) - \mathbb{E}[\hat{f}(x_0)])^2 \right] \\ &= \mathbb{E}\left[ f^2(x_0) - f(x_0) \mathbb{E}[\hat{f}(x_0)] + \left( \mathbb{E}[\hat{f}(x_0)] \right)^2 \right] \\ &= f^2(x_0) - f(x_0)\mathbb{E}[\hat{f}(x_0)] + \left( \mathbb{E}[\hat{f}(x_0)] \right)^2 \\ &= \left(f(x_0) - \mathbb{E}[\hat{f}(x_0)] \right)^2\]Furthermore, $\mathbb{E}[f(x_0)]$ is also fixed, so:
\[\begin{align} (b) &= \mathbb{E}\left[ (f(x_0) - \mathbb{E}[\hat{f}(x_0)])(\mathbb{E}[\hat{f}(x_0)] - \hat{f}(x_0)) \right] \\ &= \mathbb{E}\left[ f(x_0)\mathbb{E}[\hat{f}(x_0)] - f(x_0) \hat{f}(x_0) - \left(\mathbb{E}[\hat{f}(x_0)]\right)^2 + \hat{f}(x_0) \mathbb{E}[\hat{f}(x_0)] \right] \\ &= f(x_0) \mathbb{E}[\hat{f}(x_0)] - f(x_0) \mathbb{E}[\hat{f}(x_0)] - \left(\mathbb{E}[\hat{f}(x_0)]\right)^2 + \left( \mathbb{E}[\hat{f}(x_0)] \right)^2 \\ &= 0 \end{align}\]Finally:
\[\begin{align} (c) &= \mathbb{E}\left[ (\mathbb{E}[\hat{f}(x_0)] - \hat{f}(x_0))^2 \right] \\ &= \mathbb{E}\left[ (\hat{f}(x_0) - \mathbb{E}[\hat{f}(x_0)] )^2 \right] \\ &= \text{Var}(\hat{f}(x_0)) \end{align}\]Putting everything together:
\[\begin{aligned} \text{Err}(x_0) &= \sigma^2_\epsilon + \left(f(x_0) - \mathbb{E}[\hat{f}(x_0)] \right)^2 + \text{Var}(\hat{f}(x_0)) \end{aligned}\]In the above decomposition, $\sigma^2_\epsilon$ is the irreducible error and represents the variation of $Y$ about its mean, $f(X)$. It is irreducible because we cannot get rid of this term no matter how good our model is. The second term, $\left(f(x_0) - \mathbb{E}[\hat{f}(x_0)]\right)^2$, is called the squared bias. It represents the difference between the average of our estimate, $\hat{f}(x_0)$, and the true mean, $f(x_0)$. The last term, $\text{Var}(\hat{f}(x_0))$, is the variance of our model.