explainability
explainability
Explainability utilities for conformal prediction.
This module provides wrapper functions that expose the conformal prediction pipeline as callable functions suitable for use with SHAP’s KernelExplainer. Instead of training surrogate models and explaining the surrogates, these wrappers allow direct explanation of the actual conformal pipeline (Johansson et al., 2025).
The key idea: wrap x -> model.predict_proba -> nonconformity scores -> p-values as a single callable, then pass it to shap.KernelExplainer. Margin and confidence SHAP values are derived from per-class p-value SHAP values by linearity of Shapley values.
References
Caparrini, A., Ariza-Garzón, M.-J., & Arroyo, J. (2026). Explaining Conformal Prediction: Diagnosing Reliability through Feature-Conditioned Outcomes and p-Value Margins. COPA, PMLR 329.
Classes
| Name | Description |
|---|---|
| ConformalPValueFunction | Wrap a ConformalClassifier’s p-value computation as a callable. |
| NumericOnlyPValueFunction | Wrap p-value computation for a subset of numeric features. |
ConformalPValueFunction
explainability.ConformalPValueFunction(conf_clf, feature_names)Wrap a ConformalClassifier’s p-value computation as a callable.
This is intended for use with shap.KernelExplainer. The callable accepts a 2-D numpy array and returns p-values for all classes.
Parameters
conf_clf : ConformalClassifier-
A calibrated conformal classifier.
feature_names : list[str]-
Column names expected by the underlying sklearn pipeline.
Examples
>>> func = ConformalPValueFunction(conf_clf, feature_names=X_train.columns.tolist())
>>> explainer = shap.KernelExplainer(func, X_background)
>>> shap_values = explainer.shap_values(X_explain)NumericOnlyPValueFunction
explainability.NumericOnlyPValueFunction(
conf_clf,
numeric_cols,
all_feature_names,
reference_values,
)Wrap p-value computation for a subset of numeric features.
Categorical features are fixed at reference values (modes from the training data). Only numeric features vary, which makes the wrapper suitable for shap.KernelExplainer with a small number of features.
Parameters
conf_clf : ConformalClassifier-
A calibrated conformal classifier.
numeric_cols : list[str]-
Names of the numeric features that will be varied.
all_feature_names : list[str]-
All column names expected by the underlying pipeline.
reference_values : dict[str, object]-
Fixed values for non-numeric features (typically mode of training data).
Examples
>>> ref = {col: X_train[col].mode()[0] for col in categorical_cols}
>>> func = NumericOnlyPValueFunction(conf_clf, numeric_cols, all_cols, ref)
>>> explainer = shap.KernelExplainer(func, X_background_numeric)Functions
| Name | Description |
|---|---|
| derive_confidence_shap | Derive SHAP values for confidence from per-class p-value SHAP values. |
| derive_margin_shap | Derive SHAP values for margin of correctness from p-value SHAP values. |
| derive_margin_shap_multiclass | Derive SHAP values for margin in multiclass setting. |
| select_class_shap | Select per-sample SHAP values for given class indices. |
derive_confidence_shap
explainability.derive_confidence_shap(shap_p_values, p_values)Derive SHAP values for confidence from per-class p-value SHAP values.
Confidence = 1 - p_second_largest, where p_second_largest is the p-value of the strongest competing class (second-highest p-value across all classes). By linearity of Shapley values:
SHAP(confidence)_j = -SHAP(p_second_largest)_j
Works for both binary and multiclass settings:
- Binary (K=2): the second-largest p-value is the minimum, so this reduces to
confidence = 1 - min(p_0, p_1). - Multiclass (K>2): the second-largest p-value is the highest p-value among all non-predicted classes; the formula is the same.
Parameters
shap_p_values : list[np.ndarray]-
List of SHAP value arrays, one per class. Each has shape
(n_samples, n_features). p_values : np.ndarray-
Actual p-values of shape
(n_samples, n_classes).
Returns
: np.ndarray-
SHAP values for confidence, shape
(n_samples, n_features).
derive_margin_shap
explainability.derive_margin_shap(shap_p_true, shap_p_false)Derive SHAP values for margin of correctness from p-value SHAP values.
Margin = p_true - p_false. By linearity of Shapley values: SHAP(margin)_j = SHAP(p_true)_j - SHAP(p_false)_j
Works for both binary and multiclass settings:
- Binary:
shap_p_falseis the single competing class’s SHAP values (shape(n_samples, n_features)). - Multiclass:
shap_p_falseshould be the strongest competitor’s SHAP values, extracted per-sample from the 3-D array usingselect_class_shap.
Parameters
shap_p_true : np.ndarray-
SHAP values for p-value of the true class, shape
(n_samples, n_features). shap_p_false : np.ndarray-
SHAP values for p-value of the competing class, shape
(n_samples, n_features). For multiclass, useselect_class_shapto build this array by selecting the strongest competitor per sample from the 3-D SHAP array.
Returns
: np.ndarray-
SHAP values for margin, shape
(n_samples, n_features).
derive_margin_shap_multiclass
explainability.derive_margin_shap_multiclass(shap_per_class, p_values, y_true)Derive SHAP values for margin in multiclass setting.
Margin = p_true - p_best_false, where p_best_false is the highest p-value among non-true classes. By linearity of Shapley values: SHAP(margin)_j = SHAP(p_true)_j - SHAP(p_best_false)_j
Parameters
shap_per_class : list[np.ndarray]-
SHAP values per class, each of shape (n_samples, n_features).
p_values : np.ndarray-
Actual p-values, shape (n_samples, n_classes).
y_true : np.ndarray-
True class indices, shape (n_samples,).
Returns
: np.ndarray-
SHAP values for margin, shape (n_samples, n_features).
select_class_shap
explainability.select_class_shap(shap_values, class_indices)Select per-sample SHAP values for given class indices.
Converts a 3-D SHAP array to 2-D by picking, for each sample i, the slice shap_values[i, :, class_indices[i]].
Parameters
shap_values : np.ndarray-
3-D array of shape
(n_samples, n_features, n_classes). class_indices : np.ndarray-
Class index for each sample, shape
(n_samples,).
Returns
: np.ndarray-
2-D array of shape
(n_samples, n_features).