Explaining Conformal Predictions with SHAP

conformalpy provides wrappers that make the conformal prediction pipeline directly explainable with SHAP, following the approach of (Johansson et al. 2025). Instead of explaining surrogate models, you explain the actual x → predict_proba → nonconformity scores → p-values pipeline.

Why Explain P-Values, Not Probabilities?

Model probabilities are constrained to sum to 1 across classes. This means that in binary classification, SHAP values for class 0 are exactly the negation of SHAP values for class 1 — they carry no independent information.

Conformal p-values are fundamentally different:

  • Each class p-value is computed independently by ranking the instance’s nonconformity score against the calibration set (or against a per-class partition in Mondrian mode).
  • P-values do not sum to 1. Both can be large (the instance looks conforming to multiple classes) or both can be small (globally strange instance).
  • A feature can push all p-values in the same direction — something impossible with probabilities.

This independence means that SHAP values for different class p-values are not negations of each other, making per-class explanations genuinely informative.

Basic Usage

ConformalPValueFunction wraps the full conformal pipeline as a callable suitable for shap.KernelExplainer:

import shap
from conformalpy.explainability import ConformalPValueFunction

# Wrap the calibrated conformal classifier
pv_func = ConformalPValueFunction(conf_clf, feature_names=feature_names)

# Create SHAP explainer with a background summary
background = shap.kmeans(X_train, 50)
explainer = shap.KernelExplainer(pv_func, background)

# Explain test instances
shap_values = explainer.shap_values(X_test[:10])
# shap_values is a list of arrays, one per class

Mixed Feature Types

When the underlying pipeline includes categorical features (e.g., via a ColumnTransformer), KernelExplainer would need to vary those categoricals — which can produce nonsensical feature combinations.

NumericOnlyPValueFunction fixes categorical features at reference values (typically the mode from training data) and only varies numeric features:

from conformalpy.explainability import NumericOnlyPValueFunction

reference = {col: X_train[col].mode()[0] for col in categorical_cols}
pv_func = NumericOnlyPValueFunction(
    conf_clf, numeric_cols, all_feature_names, reference
)

background = shap.kmeans(X_train[numeric_cols].values, 50)
explainer = shap.KernelExplainer(pv_func, background)

Margin and Confidence SHAP

By the linearity of Shapley values, SHAP values for derived quantities can be obtained directly from per-class p-value SHAP values without re-running the explainer:

Margin SHAP — which features drive the gap between true-class and competing-class evidence:

\[\text{SHAP}(\text{margin})_j = \text{SHAP}(p_{\text{true}})_j - \text{SHAP}(p_{\text{false}})_j\]

Confidence SHAP — which features affect prediction set size:

\[\text{SHAP}(\text{confidence})_j = -\text{SHAP}(p_{\text{second}})_j\]

from conformalpy.explainability import derive_margin_shap, derive_confidence_shap

import numpy as np
# shap_values[0] = SHAP for class 0 p-value, shap_values[1] = SHAP for class 1
shap_p_true = np.where(
    y_true[:, None] == 1, shap_values[1], shap_values[0]
)
shap_p_false = np.where(
    y_true[:, None] == 1, shap_values[0], shap_values[1]
)
margin_shap = derive_margin_shap(shap_p_true, shap_p_false)
confidence_shap = derive_confidence_shap(shap_values, p_values)
Important

Because conformal p-values are independent, margin SHAP ≠ 2 × p_true SHAP. In probability space, SHAP(margin) = SHAP(p_true) - SHAP(p_false) = 2 × SHAP(p_true) since SHAP(p_false) = -SHAP(p_true). With conformal p-values this cancellation does not occur — the margin captures genuinely different information from individual p-value explanations.

Practical Interpretation

Margin SHAP values have a direct operational interpretation:

  • Positive margin SHAP: The feature widens the gap between true-class and false-class evidence. This is a reliability driver — it makes the prediction more trustworthy.
  • Negative margin SHAP: The feature pushes toward an overconfident error (high false-class p-value relative to true-class). This is a risk driver.
  • Near-zero margin SHAP despite large individual p-value effects: The feature affects calibration globally but does not discriminate between classes — a “globally confusing” feature. It shifts both p-values in the same direction, which is invisible in probability-based explanations.

See Also

  • outcomes — Outcome categories that margin/confidence affect
  • fcod — Feature-conditioned outcome distributions
  • operational-framework — Connecting explanations to decisions
Johansson, Ulf, Cecilia Sönströd, and Aicha Maalej. 2025. “Explaining Set-Valued Predictions: SHAP Analysis for Conformal Classification.” Proceedings of the 14th Symposium on Conformal and Probabilistic Prediction with Applications, 359–78. https://proceedings.mlr.press/v266/johansson25a.html.