MAPIE Documentation

repository·master·Indexed 23 days ago

https://github.com/scikit-learn-contrib/mapie

A scikit-learn-compatible module for uncertainty quantification and risk control in machine learning. MAPIE uses conformal prediction to provide theoretical guarantees for prediction intervals and sets across regression, classification, and time series tasks. It is model-agnostic and compatible with scikit-learn, TensorFlow, and PyTorch via scikit-learn-compatible wrappers.

Tokens
10.8K
Snippets
17
Records
54
Agent score
81%

What's inside MAPIE

  1. Overview of MAPIE capabilities

    master

    MAPIE (Model Agnostic Prediction Interval Estimator) is an open-source Python library designed for quantifying uncertainties and controlling risks in machine learning models. It is part of the scikit-learn-contrib ecosystem and is compatible with models from scikit-learn, TensorFlow, and PyTorch via scikit-learn-compatible wrappers.

    Key Features:

    • Uncertainty Quantification: Compute prediction intervals (regression) or prediction sets (classification and time series) by estimating model uncertainty on a conformalization dataset.
    • Risk Control: Control risks for complex tasks like multi-label classification and semantic segmentation in computer vision, providing probabilistic guarantees on metrics such as recall and precision.
    • Theoretical Guarantees: Implements peer-reviewed algorithms from Conformal Prediction and Distribution-Free Inference that work under minimal assumptions.
  2. Overview of Risk Control methods in MAPIE

    master

    MAPIE implements three primary methods for risk control, which allow you to adjust model parameters to ensure a specific risk level (like precision or recall) is maintained on unseen data.

    Terminology Note

    • alpha ($\alpha$) is the target risk level (equivalent to 1 - confidence_level).
    • calibrate and calibration are used interchangeably with conformalize and conformalization.

    Comparison of Methods

    MethodType of ControlAssumptionNon-monotonic RisksBinary ClassificationMulti-label Classification
    RCPSProbabilityi.i.d.NoNoYes
    CRCExpectationExchangeableNoNoYes
    LTTProbabilityi.i.d.YesYesYes

    Application in Multi-label Classification

    • For recall control: Use CRC or RCPS.
    • For precision control: Use LTT.
  3. Risk-Controlling Prediction Sets (RCPS)

    master

    RCPS is a method for computing prediction sets that control risk with high probability. It requires a set-valued function $\mathcal{T}_{\hat{\lambda}}$ that is nested (larger $\lambda$ results in larger sets) and a loss function $L$ that is monotonic (larger sets result in lower loss).

    Guarantee

    $\mathbb{P}(R(\mathcal{T}_{\hat{\lambda}}) \leq \alpha) \geq 1 - \delta$

    Available Bounds for Upper Confidence Bound $\hat{R}^+(\lambda)$

    • Hoeffding Bound: $\hat{R}_{\text{Hoeffding}}^+(\lambda) = \hat{R}(\lambda) + \sqrt{\frac{1}{2n}\log\frac{1}{\delta}}$
    • Bernstein Bound: $\hat{R}_{\text{Bernstein}}^+(\lambda) = \hat{R}(\lambda) + \hat{\sigma}(\lambda)\sqrt{\frac{2\log(2/\delta)}{n}} + \frac{7\log(2/\delta)}{3(n-1)}$
    • Waudby-Smith–Ramdas: Recommended for bounded losses.
  4. Conformal Risk Control (CRC)

    master

    CRC controls any monotone and bounded loss function. It provides a guarantee on the expected risk:

    $$\mathbb{E}\left[L_{n+1}(\hat{\lambda})\right] \leq \alpha$$

    It finds the optimal parameter $\hat{\lambda}$ by solving for the smallest $\lambda$ such that the empirical risk (adjusted for the sample size) stays below the target $\alpha$.

  5. Understand statistical tests for calibration curves

    master

    Several statistical metrics can be used to evaluate how well a calibration curve follows the ideal diagonal line (perfect calibration):

    • Kolmogorov-Smirnov (KS) Statistics: Tests if the calibration curve deviates significantly from the ideal diagonal.
    • Kuiper Statistics: Similar to KS, but specifically designed to capture both positive and negative deviations from perfect calibration.
    • Spiegelhalter Statistics: Used to test the overall calibration of predicted probabilities.
    • Cumulative Differences: Calculates the cumulative differences between sorted true values and prediction scores.
  6. Learn Then Test (LTT)

    master

    LTT is a versatile method that can control any loss, including non-monotonic ones, by using multiple hypothesis testing.

    Process

    1. For each $\lambda_j$ in a discrete set $\Lambda$, estimate the risk on calibration data.
    2. Associate a hypothesis $\mathcal{H}_j: R(\lambda_j) > \alpha$.
    3. Compute p-values (e.g., using Hoeffding-Bentkus).
    4. Apply Family-Wise Error Rate (FWER) control, such as the Bonferroni correction.

    Guarantee

    $\mathbb{P}(R(\mathcal{T}_{\lambda}) \leq \alpha) \geq 1 - \delta$ for all $\lambda \in \hat{\Lambda}$.

  7. Use the new MAPIE v1 Conformal Prediction Workflow

    master

    The workflow has been restructured to separate model training from the conformalization (calibration) step.

    Split Conformal Workflow

    1. Data splitting: Use train_conformalize_test_split() to prepare your data.
    2. Model training: Call .fit(X_train, y_train) on your chosen regressor or classifier.
    3. Conformalization: Call .conformalize(X_conf, y_conf) to calibrate the model.
    4. Prediction: Use .predict_interval() (for regression) or .predict_set() (for classification).

    Cross Conformal Workflow

    For cross-conformal techniques, steps 2 and 3 are combined into a single call:

    1. Data splitting: Use train_conformalize_test_split().
    2. Training & Conformalization: Call .fit_conformalize(X_train, y_train, X_conf, y_conf).
    3. Prediction: Use .predict_interval() or .predict_set().
  8. How Jackknife+ works for regression

    master

    The Jackknife+ method is a robust way to estimate prediction intervals that accounts for model variability by using leave-one-out predictions on the new test point.

    Key Properties:

    • Guarantee: It guarantees a coverage level of $1-2\alpha$ for a target of $1-\alpha$ without a priori assumptions on the data distribution or the predictive model.
    • Mechanism: It uses each leave-one-out prediction on the new test point to account for variability.

    Warning: Jackknife methods (including Jackknife+) require running as many simulations as training points, which can be computationally prohibitive for large datasets.

  9. How Conditional Conformal Prediction works

    master

    The method replaces the fixed quantile $S^*$ used in standard split conformal prediction with a function $\hat{g}{S(X{n+1}, y)}(X_{n+1})$ that estimates conditional quantiles of the conformity scores.

    In the finite-dimensional setting, the process follows three steps:

    1. Choose a class of functions $\mathcal{F}$: A common approach is to use linear functions defined by a user-provided feature map $\Phi: \mathcal{X} \to \mathbb{R}^d$.
    2. Optimize the function: Find the best function in the class by solving an optimization problem (in practice, a dual formulation is used) that minimizes the pinball loss over the calibration set.
    3. Compute intervals: Use the optimized function $\hat{g}S$ to define the prediction set: $$\hat{C}(X{n+1}) = { y : S(X_{n+1}, : y) \leq \hat{g}{S(X{n+1}, y)}(X_{n+1}) }$$
  10. How Risk Control works

    master

    Standard thresholding (e.g., choosing a probability threshold for binary classification) based on validation data offers no statistical guarantees on new, unseen data.

    Risk control solves this by adjusting a model parameter $\lambda$ so that a given risk stays below a desired level $\alpha$ with high probability (or in expectation) on unseen data.

    Mathematical Guarantees

    • CRC (Conformal Risk Control): Requires exchangeable data and provides an expectation guarantee: $\mathbb{E}(R) \leq \alpha$.
    • RCPS (Risk-Controlling Prediction Sets) and LTT (Learn Then Test): Require i.i.d. data and provide a probability guarantee: $\mathbb{P}(R \leq \alpha) \geq 1 - \delta$, where $\delta$ is the confidence level.
  11. How Conformalized Quantile Regression (CQR) works

    master

    Conformalized Quantile Regression (CQR) is designed to provide better interval widths when dealing with heteroscedastic data (where error variance changes across the input space).

    Instead of using a single point estimate, CQR uses quantile regressors to predict the lower and upper bounds of the interval.

    Formulation: The prediction interval for a new sample $X_{n+1}$ is calculated as: $$\hat{C}{n, \alpha}^{\text{CQR}}(X{n+1}) = \left[\hat{q}{\alpha{\text{lo}}}(X_{n+1}) - Q_{1-\alpha}(E_{\text{low}}, \mathcal{I}2), ; \hat{q}{\alpha_{\text{hi}}}(X_{n+1}) + Q_{1-\alpha}(E_{\text{high}}, \mathcal{I}_2)\right]$$

    Where:

    • $\hat{q}{\alpha{\text{lo}}}$ and $\hat{q}{\alpha{\text{hi}}}$ are the predicted lower and upper quantiles.
    • $Q_{1-\alpha}$ is the empirical quantile of residuals from the calibration set.

    Symmetric Variant: In the symmetric method, $E_{\text{low}}$ and $E_{\text{high}}$ are merged into $E_{\text{all}}$, and the quantile is calculated on all absolute residuals.