Introduction

conformalpy is a Python library for conformal prediction with support for classification (binary and multiclass), regression, and a complete operational framework for real-world decision systems.

Tip

New to conformalpy? See Why conformalpy? for what makes this library different from alternatives like MAPIE and Crepes.

What is Conformal Prediction?

Conformal prediction creates prediction sets (classification) or intervals (regression) with guaranteed coverage. Instead of a single prediction, conformal methods output a set of classes or an interval guaranteed to contain the true value with probability at least \(1 - \alpha\).

  • Classification (\(\alpha = 0.10\)): A set \(\{0\}\) → confident; a set \(\{0, 1\}\) → uncertain. The true label is in the set ≥90% of the time.
  • Regression (\(\alpha = 0.10\)): An interval \([2.3, 4.7]\) containing the true value ≥90% of the time.

Theoretical Foundation

The finite-sample coverage guarantee for split (inductive) conformal prediction is stated as follows (Vovk et al. 2005):

Theorem (Coverage guarantee). Let \((X_1, Y_1), \dots, (X_n, Y_n), (X_{n+1}, Y_{n+1})\) be exchangeable random variables. For any nonconformity function \(s\) and any miscoverage level \(\alpha \in (0, 1)\), the prediction set

\[ C(X_{n+1}) = \{y : s(X_{n+1}, y) \leq \hat{q}\} \]

where

\[ \hat{q} = \text{Quantile}\left(s_1, \dots, s_n;\; \frac{\lceil (n+1)(1-\alpha) \rceil}{n}\right) \]

satisfies

\[ P(Y_{n+1} \in C(X_{n+1})) \geq 1 - \alpha \]

This guarantee is distribution-free (no distributional assumptions), model-agnostic (works with any fitted predictor), and holds in finite samples (not just asymptotically).

For a comprehensive treatment, see (Shafer and Vovk 2008) and (Angelopoulos and Bates 2023).

Installation

pip install conformalpy

Quick Example

Classification:

from sklearn.ensemble import RandomForestClassifier
from conformalpy.classifier import ConformalClassifier
from conformalpy.nonconformity.classification import lac_nonconformity

model = RandomForestClassifier()
model.fit(X_train, y_train)

conf_clf = ConformalClassifier(
    model=model,
    alpha=0.10,
    nonconformity_function=lac_nonconformity,
)
conf_clf.calibrate(X_cal, y_cal)

prediction_sets = conf_clf.predict(X_test)
# Returns: [{0}, {1}, {0, 1}, {0}, ...]

Regression:

from sklearn.ensemble import GradientBoostingRegressor
from conformalpy.regressor import ConformalRegressor

model = GradientBoostingRegressor()
model.fit(X_train, y_train)

conf_reg = ConformalRegressor(model=model, alpha=0.10)
conf_reg.calibrate(X_cal, y_cal)

intervals = conf_reg.predict_interval(X_test)
# Returns: array of shape (n_samples, 2) with [lower, upper]

Next Steps

Angelopoulos, Anastasios N., and Stephen Bates. 2023. “Conformal Prediction: A Gentle Introduction.” Foundations and Trends in Machine Learning 16 (4): 494–591. https://doi.org/10.1561/2200000101.
Shafer, Glenn, and Vladimir Vovk. 2008. “A Tutorial on Conformal Prediction.” Journal of Machine Learning Research 9 (12): 371–421. http://jmlr.org/papers/v9/shafer08a.html.
Vovk, Vladimir, Alexander Gammerman, and Glenn Shafer. 2005. Algorithmic Learning in a Random World. Springer. https://doi.org/10.1007/b106715.