Quantify The Uncertainty Of Predictive Models With Conformal Prediction
Machine learning models can be like bad TV pundits: They make statements boasting with confidence and without nuance.
It’s a cat with a 94% probability!
The house has an estimated value of €413,412.98!
But machine learning models are usually not trained to correctly state their uncertainty, they are trained to predict well. Uncertainty quantification requires additional effort or it means restricting model selection to only approaches that have it built in.
Not ideal.
But there is a solution: Conformal prediction.
Conformal prediction is a powerful approach to quantifying the uncertainty of any predictive model. The benefits:
Fast
Model-agnostic
Few lines of code
Statistical guarantees
No retraining required
Here is how it works.
Intuition Behind Conformal Prediction
Conformal prediction takes a heuristic uncertainty score and turns it into a rigorous one.
The approach achieves rigorous uncertainty quantification by calibrating the heuristic on a calibration data set. Before we have a look at this recipe, here are some examples where we already have access to a heuristic score, but want a better one:
Multi-class models output classification scores. What if, instead of the scores, you want to provide a set of classes that contains the true class with a (guaranteed) probability? The scores alone don’t provide that information.
If a classification model says “94% cat”, you can’t automatically interpret it as a probability. If you take all images with a +/- 94% score and compare it with the ground truth, it’s likely that the true probability (no. correct class divided by no. of images with 94% probs) will be different.
Quantile regression: The quantiles may be wrongly calibrated. For example, the presumed 10% quantile might be biased toward the median.
In all these cases, conformal prediction can be used to calibrate the scores and provide rigorous uncertainty quantification.
One Recipe, Many Implementations
I mentioned that there are different types of calibrations: for multi-class probabilities, for regression, … What confused me first about conformal prediction: It's not just one algorithm, but many.
Conformal prediction is a general calibration recipe with the following steps:
Split data into training and calibration subsets
Train model on training data
Pick a certainty level (e.g. 90%)
Calculate the heuristic scores on calibration data
Calibrate the heuristic scores to make them rigorous with guaranteed coverage
Use calibrated thresholds on new data
This recipe enables many use cases
Adaptive prediction sets in multi-class problems (paper)
Fixed coverage of quantile regression (code)
...
A big difference between these use cases: the underlying heuristic.
The Starting Point: Heuristic Uncertainty
Can you use conformal prediction for your model? It depends. A requirement for conformal prediction is that you have a heuristic score of uncertainty. This heuristic score needs to be good enough: It must rank the examples correctly. If the heuristic is mostly random, conformal prediction can’t magically calibrate it to produce a better score. It’s the classic garbage-in-garbage-out rule.
Class probabilities or classification scores in multi-class classification are examples of good heuristics. A higher score means more certainty in the outcome, a lower score means that the model is less certain. If “cat” is the top class with a 90% probability for one image, and 30% for another, the former image classification is more certain, and the latter is less certain.
If the model doesn’t provide a heuristic out-of-the-box, you can also construct a simple heuristic. For a regression model, you might use the absolute error of the k-nearest neighbors as a heuristic for the uncertainty of a prediction.
For a deep dive into conformal prediction, I recommend the following free resources:
Tutorial paper: https://arxiv.org/abs/2107.07511
Awesome list of conformal prediction approaches: https://github.com/valeman/awesome-conformal-prediction
scikit-ready Python package: https://github.com/scikit-learn-contrib/MAPIE
R package: https://github.com/ryantibs/conformal