Every model makes an assumption about uncertainty, often without saying so. Where data is rare, that assumption decides the answer. This introduction explains where uncertainty comes from and how to judge when it needs to be faithful to be useful.
Essentially, all models are wrong, but some are useful.
George E. P. Box
Most of machine learning is based on probabilistic modelling, even if it does not always seem like it. So most models carry some uncertainty, whether we chose it or not, and in many decisions it pays to take that uncertainty into account, as the examples in this post show.
In my experience, uncertainty in machine learning is a confusing topic to get into, so I wrote this introduction to give an intuitive overview. It is not comprehensive, but it should give you a good enough mental model to choose assumptions that make your uncertainty estimates useful for decision making, even though, as Box put it, the model itself will always be wrong.
Generally speaking, uncertainty is incomplete knowledge, and probability is how we describe it: a distribution over the possible outcomes says how plausible each one is, given what we know.
It helps to separate two kinds of uncertainty:
The line between the two depends on what we measure. A coin flip follows classical mechanics. If we do not measure how the coin is thrown, the best we can say is 50/50, and the uncertainty is aleatoric. If we do measure it, physics tells us how it will land, and the uncertainty becomes epistemic: now we have to learn the physics, which takes data. Richer inputs can reduce aleatoric uncertainty, but they give the model more to learn, and whether that pays off depends on how much data and prior knowledge we have.
A prediction is not a decision. When we decide, we often weigh the possible outcomes by how likely they are and what they would cost us, including rare outcomes that could hurt us badly. A single number hides this picture. It also cannot tell us when to hand a case to a human or to collect more data before we decide. The examples below show where modelling uncertainty matters.
All of these need the uncertainty to be useful. To see when it is, it makes sense to look at where it comes from.
As a running example, we look at steering angle prediction. The input is the image from a car’s front camera, and the output $y$ is the steering angle. Such datasets can be recorded cheaply from human drivers. Take a car approaching a point where the road splits. In the left lane, drivers mostly go left. In the right lane, they mostly go right. In the middle lane, which points straight at the divider, about half go left and half go right.
The car’s position across the lanes is implicit in the camera image. To keep the figures simple, we use it directly as a one-dimensional input $x$ in the figures below.
What we want to learn is the conditional distribution $p^*(y \mid x)$: for each position, how likely each steering angle is. As is common in statistical learning theory, we assume that the data come from a fixed joint distribution $p^*(x, y)$, and the recordings from human drivers are samples from it.
The ground truth $p^*(y \mid x)$ is just one of many possible conditional distributions.
That subset is the model, a family of hypotheses indexed by parameters $\theta$. A common choice for regression is a Gaussian whose mean is a neural network $f_\theta(x)$ and whose variance $\sigma^2$ is fixed:
\[p_\theta(y \mid x) = \mathcal{N}\big(y;\, f_\theta(x),\, \sigma^2\big).\]The truth need not lie inside the model. For the lane split, it does not: a single Gaussian has one peak, but in the middle lane the truth has two.
Training picks the hypothesis that best explains the data. It minimizes the negative log-likelihood (NLL), the average of $-\log p_\theta(y_i \mid x_i)$ over the training examples. For the Gaussian with fixed variance, this is the familiar mean squared error
So a network trained with mean squared error already has an uncertainty model, even if nobody chose it on purpose.
How far is the trained model from the truth? The expected NLL splits into two terms:
\[\mathbb{E}_{x,y}[\text{NLL}] = \mathbb{E}_x\Big[\mathrm{KL}\big(p^* \,\|\, p_\theta\big)\Big] + \mathbb{E}_x\big[H(p^*)\big],\]where both expectations are over the true data distribution. The first term is the KL divergence, which measures how far the model is from the truth and is zero only if they match. The second is the entropy of the truth, its aleatoric uncertainty, which does not depend on the model. Minimizing the NLL therefore moves the model toward the truth. But we never know the entropy of the truth, so the NLL alone cannot tell us how far away we still are.
Back in the middle lane, the single peak has a dangerous consequence. The mean squared error is minimized by the average steering angle, and the average of left and right is straight ahead, into the divider. No choice of parameters avoids this, because every hypothesis in the model has a single peak. In an earlier post, I looked at this example in more detail, and we studied it in a paper on misspecification, the case where the truth lies outside the model
The fix is a model that contains the truth. For this example, a mixture of two Gaussians, whose weights, means and variances are all outputs of the network, is enough to contain the truth.
Understanding the problem well enough to make this choice is the modeller’s job. But a richer model is not free: it contains more hypotheses, so the data has to do more work to single out the right one.
With enough data, the mixture matches the truth, including in the middle lane. Now its spread is the aleatoric uncertainty: with the given information, even a perfect model cannot say which lane a particular driver will take.
But in practice we do not know the truth, so we cannot check the fit like this. How do we know how much we still do not know?
So far, training gave us a single hypothesis. But with only 20 data points, many hypotheses explain the data about equally well, and by picking one of them, we ignore that the data cannot tell them apart. Bayesian inference instead keeps all of them.
Before seeing any data, we decide how plausible each hypothesis in the model is. This is the prior $p(\theta)$, a soft inductive bias. It captures what we know, or assume, before training, and everything the model does not know is measured relative to it.
where $\mathcal{D} = \{(x_i, y_i)\}_{i=1}^n$ is the data. The likelihood, written $p(\mathcal{D} \mid \theta)$ for short, measures how well hypothesis $\theta$ agrees with the data.
To predict, we average all hypotheses, each weighted by its posterior probability. This is the posterior predictive:
\[p(y \mid x, \mathcal{D}) = \int p_\theta(y \mid x) \, p(\theta \mid \mathcal{D}) \, \mathrm{d}\theta.\]With more and more data, the posterior concentrates on a single hypothesis: the one in the model that is closest to the truth.
This picture splits uncertainty into three sources, using the terms of Hüllermeier and Waegeman
The last point matters in practice. A misspecified model can still have a confident posterior: it is confident about the least wrong hypothesis it contains.
Both models above show more uncertainty where data is missing. That is approximation uncertainty, and both capture it. To also question the model itself, a full probabilistic treatment would weigh models too, but that is hard in practice. What we can do is compare a few candidate models. For this, Bayesian inference offers the evidence, $p(\mathcal{D} \mid \mathcal{M}) = \int p(\mathcal{D} \mid \theta) \, p(\theta \mid \mathcal{M}) \, \mathrm{d}\theta$, the probability of the data under model $\mathcal{M}$, averaged over its prior.
Like the KL divergence earlier, the evidence is an average over all data. In real driving data, lane splits are rare, so their effect can vanish in the evidence, and the simpler model may even win. Domain knowledge would tell us otherwise. This is why choosing the model remains the modeller’s job, and comparing models is a tool for it, not a replacement.
So far, we have taken the prior as given. But the prior is part of the model. In theory, with enough data, its choice stops mattering, as long as it does not rule out the right hypotheses. In practice, we rarely have that much data, and a poor prior, like any poor inductive bias, can hurt a lot. I discussed this in more detail in earlier posts on uncertainty and on continual learning.
In short, the posterior is uncertain where the data and our assumptions leave room for disagreement. Low uncertainty therefore does not mean we have seen data there. Generalization works exactly this way: far from the data, the inductive bias makes the hypotheses agree. If the bias is good, the model is confident and right. If not, it is confident and wrong.
Sometimes we want a statistical guarantee, ideally one that does not depend on our modelling choices, since those can be arbitrarily wrong. Conformal prediction gives such a guarantee. Instead of a single prediction, it outputs a prediction set that contains the true output, say, 90% of the time. This is called 90% coverage. It wraps any trained model, and as long as the data are exchangeable, the guarantee holds no matter how wrong the model is
Conformal prediction comes in many flavors. We look at the simplest and most common one, split conformal prediction. Its recipe has four steps:
If the calibration and test data are exchangeable, the set has at least 90% coverage. This holds on average over inputs, not for any particular input, such as one at the lane split.
But the guarantee does not say whether the sets are useful. For that, we want low scores in the histogram above, since a small $\hat q$ gives tight sets. A poor model has higher scores and gets wider sets, with the same coverage.
The guarantee only needs a calibration set. Useful sets also need the same modelling work as before. I wrote more about this in a recent post on conformal prediction.
So far, the goal was a faithful uncertainty: a predictive distribution close to the truth. But uncertainty is meant to be used, and decision theory says how. The model provides a belief $p(y \mid x)$. Whoever acts provides a loss $L(a, y)$, the cost of action $a$ if the outcome turns out to be $y$. We then pick the action with the lowest expected loss:
\[a^* = \arg\min_a \; \mathbb{E}_{y \sim p(y \mid x)}\big[L(a, y)\big].\]In the lane split, the action is a steering angle. The belief says which angles drivers take: left or right, never straight. The loss says what we want. If our destination is to the left, steering left has a low loss, steering right a higher one, and steering into the divider is worst of all. Under the true belief, the decision follows the left lane. A belief with a single peak in the middle rules out neither lane and picks straight.
How faithful does the belief need to be? It depends on the decision.
Many factories use cameras to check every part that comes off the line. Mistakes are costly here, since a missed defect may reach a customer, lead to a recall or, in the worst case, put people at risk. To keep things simple, we consider a binary classifier in this section.
To answer this, we look at how the probability turns into a decision. We reject a part if $p = p(\text{defect} \mid x)$ is above a threshold. If we reject, we pay a cost $c_{FP}$ when the part is good, which happens with probability $1 - p$. If we ship, we pay a cost $c_{FN}$ when it is defective, which happens with probability $p$. Rejecting is cheaper on average when $c_{FP}\,(1 - p) < c_{FN}\,p$, that is, when
\[p(\text{defect} \mid x) > \frac{c_{FP}}{c_{FP} + c_{FN}}.\]What this derivation hides is that it takes $p$ at face value, so it assumes the model is calibrated: of all parts scored 0.1, about 10% should be defective. In this example, though, calibration matters less than it seems, for two reasons. First, calibrated against whom? Inspectors disagree on borderline parts, and since labels are expensive, each part is usually labeled by only one of them. So the labels cannot tell us whether the model is calibrated where it matters most, on borderline parts. Second, $c_{FN}$ includes recalls, lost customers and reputation, so it is rarely known. It is not even a single number, since a customer may never notice a faint scratch, while a deep crack can make the part fail. So the threshold is a guess anyway.
What about a guarantee, such as “at most 5% of defects escape”? The guarantee we got from conformal prediction earlier does not give this. It holds on average over all parts, and since defects are rare, it can be met while missing most of them. We need coverage for defects alone. Conformal prediction can give this too, if we set the threshold using defects only. Then at most a fraction $\alpha$ of defects, here 5%, score below it
This guarantee has a price. Labeled defects are rare and expensive to collect, and we need many of them for a stable threshold. Worse, a defect is anything that is not normal. The guarantee holds only for defects that are exchangeable with the ones we collected, and the next one may look like none of them.
If the model rejects too many good parts and we raise the threshold, we give up the guarantee and are back to costs we cannot measure.
So in this example, we need neither calibration nor a guarantee, but a score that grows with the severity of a flaw. Only the order of the scores matters, and the operator decides where to put the threshold. Small scratches are rarely called defects and deep cracks almost always, and in between, inspectors disagree. A model calibrated against a large pool of inspectors would give such a score, but these labels are too expensive. Getting the order right without them is hard modelling work: we have to build the right inductive biases into the model, for instance during pre-training.
Wind power depends on the weather, so a grid operator never knows exactly how much will arrive. To cover a shortfall, meaning less wind power than planned, the operator keeps backup power on standby, called the reserve. Unlike inspecting parts, no person could forecast the shortfall by hand, so the model learns it from past data alone. It takes inputs $x$, such as the weather forecast and recent measurements, and outputs a distribution $p(\text{shortfall} \mid x)$ over the shortfall. If the shortfall exceeds the reserve, we have a shortage, and each missing unit costs $c_{\text{short}}$, and each unit of reserve that goes unused costs $c_{\text{idle}}$.
The best reserve is the $\tau$-quantile of the predicted shortfall, with
\[\tau = \frac{c_{\text{short}}}{c_{\text{short}} + c_{\text{idle}}}.\]The costs pick the quantile.
To see what this asks of the model, we again take a Gaussian model for simplicity. On most days, the forecast is roughly right, and the shortfall is small. But on rare days, the wind dies down unexpectedly, and the shortfall is large. So in the tail, the truth is not Gaussian. Since shortages are costly, $\tau$ is high, and the reserve sits far out in the tail of the predicted distribution, so the model has to get these rare, large shortfalls right.
If we plan for a shortage 1 in 10 times ($\tau = 90\%$), the two dots almost coincide, and the Gaussian works well. Now say we plan for 1 in 100 ($\tau = 99\%$). The Gaussian sets the reserve at the blue dot. But how often this reserve falls short is decided by the truth, and at this reserve, the orange curve lies well above 1 in 100. Shortages come more than twice as often as planned. Unlike in visual inspection, the probabilities are used at face value here,
In a year of daily data, a 1-in-100 shortfall shows up only a few times, and anything rarer than once a year not at all. So where we need the uncertainty most, in the tail, we have the least data, and the assumptions built into the model decide the answer. We have to choose them from what we know about wind and weather.
Every model in this post made an assumption about uncertainty, whether we chose it or not. Making it faithful took real work: a likelihood that can represent the truth, enough data to find it, and inductive biases that hold up where the data runs out.
That work is not always needed. Our own uncertainty is not faithful either. We humans misjudge probabilities in well-known ways
Here are some more articles you might like to read next:
Subscribe to be notified of future articles: