Using our wildest imagination, we can picture a dataset consisting of features X and
labels Y, as on the left. Also imagine that we’d like to generalize this relationship
to additional values of X - that we’d like to predict future values based on what
we’ve already seen before.
With our imagination now undoubtedly spent, we can take a very simple approach to
modeling the relationship between X and Y by just drawing a line to the general trend
of the data.
A Simple Model
Our simple model isn’t the best at modeling the relationship - clearly there's
information in the data that it's failing to capture.
We'll measure the performance of our model by looking at the
mean-squared error
of its output and the true values (displayed in the bottom barchart). Our model is
close to some of the training points, but overall there's definitely room for
improvement.
The error on the training data is important for model tuning, but what we really care
about is how it performs on data we haven't seen before, called test data. So let's
check that out as well.
Low Complexity & Underfitting
Uh-oh, it looks like our earlier suspicions were correct - our model is garbage. The
test error is even higher than the train error!
In this case, we say that our model is
underfitting the data: our model is so simple
that it fails to adequately capture the relationships in the data. The high test error
is a direct result of the lack of complexity of our model.
An underfit model is one that is too simple to accurately capture the relationships
between its features X and label Y.
A Complex Model
Our previous model performed poorly because it was too simple. Let's try our luck with
something more complex. In fact, let's get as complex as we can - let's train a model
that predicts every point in our training data perfectly.
Great! Now our training error is zero.
High Complexity & Overfitting
Wait a second... Even though our training error from our model was effectively zero,
the error on our test data is high. What gives?
Unsurprisingly, our model is too complicated. We say that it
overfits the data. Instead of learning the true
trends underlying our dataset, it memorized noise and, as a result, the model is not
generalizable to datasets beyond its training data.
Overfitting refers to the case when a model is so specific to the data on which it
was trained that it is no longer applicable to different datasets.
In situations where your training error is low but your test error is high, you've
likely overfit your model.
Test Error Decomposition
Our test error can come as a result of both under- and over-fitting our data, but how
do the two relate to each other?
Under the fixed-input squared-error assumptions above,
expected prediction error is squared bias plus prediction variance plus noise variance.
The expectation averages over training samples and fresh target noise, not just the
residuals from one fitted model.
Or, mathematically:
We can’t do much about the irreducible term, but we
can make use of the relationship between both bias and variance to obtain better
predictions.
Bias
Bias is the average fitted prediction minus the conditional mean f(x), at the same fixed input
:
The term is a tricky one. It refers to the
average prediction after the model has been trained over several independent datasets.
We can think of the bias as measuring a systematic error in prediction.
These different model realizations are shown in the top chart, while the error
decomposition (for each point of data) is shown in the bottom chart.
In this demonstration's low-complexity fit, squared bias is the larger contribution.
Variance
As with bias, the notion of variance also relates to different realizations of our
model. Specifically,
prediction variance is the average squared deviation from the mean prediction at a fixed input, across training samples
:
In the displayed high-complexity example, variance contributes more than squared bias.
Compare the spread of fitted lines at one input with the corresponding decomposition;
this example is not a guarantee about every high-complexity model.
Finding A Balance
Compare the three displayed fits: the middle one captures the trend without following
every training point. Its advantage belongs to this example, not to a universal rule
that the best model has intermediate complexity.
Choose a fitting rule using validation performance, then evaluate it on held-out test
data. Complexity alone does not determine bias, variance, or generalization.
Across Complexities
We just showed, at different levels of complexity, a sample of model realizations
alongside their corresponding prediction error decompositions.
Let’s direct our focus to the error decompositions across model complexities.
For each level of complexity, we’ll aggregate the error decomposition across all
data-points, and plot the aggregate errors at their level of complexity.
This aggregation applied to our balanced model (i.e. the middle level of complexity)
is shown to the left.
The Bias Variance Trade-off
Aggregating over the displayed inputs gives this example's U-shaped error curve.
That is an additional average across inputs, distinct from the fixed-input expectation
used in the decomposition.
Here, squared bias dominates the simpler fits and variance dominates the more flexible
fits. Other data distributions and fitting procedures can produce different curves.
The decomposition explains contributions to expected squared error; it does not prove
that increasing complexity always lowers bias, raises variance, or yields a U-shaped curve.