Skip to content
AI360Xpert
Core ML

Conformal Prediction

Instead of forcing a model to make a single, overconfident guess, conformal prediction wraps around the model and forces it to output a set of possibilities with a mathematical guarantee.

Conformal prediction wraps around any model, forcing it to output a set of predictions that is mathematically guaranteed to contain the true answer.
Conformal prediction wraps around any model, forcing it to output a set of predictions that is mathematically guaranteed to contain the true answer.

Why Does This Exist?

Machine learning models are notoriously overconfident. If you show a standard image classifier a blurry picture of a small, furry animal, it will likely output: "Dog (99% Confidence)."

In reality, the model doesn't know what it is. The image is too blurry. But because the model's Softmax output function is mathematically forced to sum to 100%, it dumps all of its probability into its best guess. In a medical setting, a model outputting "Benign (99%)" when it should be unsure is incredibly dangerous.

We learned in aleatoric-vs-epistemic-uncertainty that we need a way to quantify when a model is guessing. Conformal Prediction is the ultimate statistical tool for this. Instead of a single guess, it outputs a Prediction Set.

For the blurry image, it will output: {Dog, Fox, Coyote}. And it comes with a massive, mathematically proven guarantee: The true answer is in this set exactly 95% of the time.

Think of It Like This

Think of It Like This

Think of a standard model like a student taking a multiple-choice test where they are forced to circle exactly one answer. Even if they have no idea, they guess "B" and hope for the best.

Conformal prediction is like changing the rules of the test. You tell the student: "Your goal is to circle enough answers so that the correct answer is definitely inside the circle."

If the student knows the answer, they circle just one. If the student is completely confused, they circle three or four. If the question is completely unreadable, they circle all of them. The size of the circle perfectly represents their uncertainty.

How It Actually Works

The beauty of Conformal Prediction is that it is model-agnostic. It doesn't care if you are using a Random Forest, a Neural Network, or a 10-year-old linear regression. It acts as a statistical wrapper around the model.

1. The Calibration Set

You need a small set of hold-out data (data the model was not trained on) called the Calibration Set. You run the model on this data.

2. The Non-Conformity Score

For every image in the calibration set, you look at what the model predicted for the true label. If the true label was "Fox", but the model only gave "Fox" a 10% probability, it gets a very high non-conformity score (it was very wrong). If it gave "Fox" a 95% probability, it gets a low score.

3. Finding the Threshold (q-hat)

You sort all these non-conformity scores from smallest to largest. If you want a 95% guarantee (called the coverage level), you find the score that sits at exactly the 95th percentile of this sorted list. This threshold is called q^\hat{q}.

4. Making a Prediction

Now, a new, unseen, blurry image arrives. You run the model, and it assigns probabilities to every possible animal.

Instead of picking the top animal, the Conformal Wrapper gathers every animal whose probability meets the q^\hat{q} threshold, and throws them all into a Prediction Set.

  • If the image is crystal clear, only one animal makes the cut. Set size = 1.
  • If the image is blurry, three animals make the cut. Set size = 3.

Because q^\hat{q} was mathematically derived from the 95th percentile of the holdout data, statistics guarantees that the true animal will be in the final set 95% of the time.

Show Me the Code

Implementing basic Conformal Prediction (using the MAPIE library in Python) is surprisingly simple.

from mapie.classification import MapieClassifierfrom sklearn.ensemble import RandomForestClassifier
# 1. Train your standard, overconfident black-box modelbase_model = RandomForestClassifier()base_model.fit(X_train, y_train)
# 2. Wrap it in a Conformal Predictor# 'cv="prefit"' means we already trained it, just use X_calib to calibrateconformal_model = MapieClassifier(estimator=base_model, cv="prefit")conformal_model.fit(X_calib, y_calib)
# 3. Predict on new data with a 95% guarantee (alpha = 0.05)# 'y_pred' is the standard point guess# 'y_pis' is a boolean matrix indicating which classes are in the sety_pred, y_pis = conformal_model.predict(X_new, alpha=0.05)
# For a highly uncertain row, y_pis might look like:# [True, False, True, True, False] # (The model included 3 different classes in the set to maintain 95% safety)

Watch Out For

Useless Sets (The Size Problem)

Conformal prediction guarantees that the true answer is in the set 95% of the time. However, it does not guarantee the set will be small. If you feed a Conformal Predictor a completely blank image, it will maintain its 95% guarantee by simply outputting a set containing every single class in the dictionary. Prediction Set: {Dog, Cat, Car, Boat, Airplane...} This is mathematically correct, but practically useless. A good underlying model results in small set sizes; a bad underlying model results in massive set sizes.

The Quick Version

  • Standard models output a single point-guess and are often dangerously overconfident on out-of-distribution data.
  • Conformal Prediction is a statistical wrapper that forces any model to output a Prediction Set instead of a single guess.
  • It provides a mathematical guarantee (e.g., 95%) that the true label is contained within the prediction set.
  • If the model is confident, the set will contain 1 item. If the model is unsure, the set will expand to contain 3, 4, or 5 items to maintain the 95% guarantee.
  • The size of the set is the perfect, human-readable indicator of the model's uncertainty.
  • aleatoric-vs-epistemic-uncertainty — The fundamental theory of why models are uncertain in the first place.
  • model-explainability — How to peek inside the model to see why it included a specific item in the conformal prediction set.

Related concepts