This example demonstrates how to create Feature-Conditioned Outcome Distributions (FCODs) showing how outcome proportions vary across feature values.
Unlike traditional Partial Dependence Plots (Friedman 2001) that show expected predictions, FCODs show the conditional empirical distribution of each conformal outcome (SC, SI, TS0, TS1) as a function of feature values, with bootstrap confidence intervals.
The smoothed FCOD computation ensures that outcome proportions sum to 1.0 at every grid point, making stacked area plots interpretable.
Setup
Create synthetic data with a clear feature-outcome relationship.
Compute and plot FCODs for multiple features at once.
Code
# Compute FCODs for all featuresfcods = compute_multi_feature_fcod( X_test, feature_names, prediction_sets, y_test, n_grid=25, method="smoothed",)# Plot gridfig = plot_multi_feature_fcod( fcods, outcomes=["SC", "SI", "TS0", "TS1"], n_cols=3, show_ci=False, # No CI for quick visualization figsize_per_plot=(4, 3),)plt.suptitle("Outcome FCODs for All Features", y=1.02)plt.show()
Merging Binary Uncertain Outcomes
In binary classification TS0 and TS1 split the two-set outcome by true label. At decision time the true label is unknown, so this split has no operational value. merge_uncertain_outcomes collapses them into a single TS key, simplifying downstream plots and analysis.
Code
from conformalpy.fcod import merge_uncertain_outcomesfcod_merged = merge_uncertain_outcomes(fcod_ci)print("Keys before merge:", [k for k in fcod_ci if k notin ("grid", "n_samples", "feature_name", "method")])print("Keys after merge: ", [k for k in fcod_merged if k notin ("grid", "n_samples", "feature_name", "method")])
Focus on SC (confident correct) vs TS (uncertain) to identify feature regions where the model is reliable vs uncertain.
Code
fig, axes = plt.subplots(1, 2, figsize=(14, 5))# Plot SC (confidence)ax1 = axes[0]for name, fcod in fcods.items(): ax1.plot(fcod["grid"], fcod["SC"], label=name, linewidth=2)ax1.set_xlabel("Feature Value (standardized)")ax1.set_ylabel("SC Proportion")ax1.set_title("Where is the model confident and correct?")ax1.legend()ax1.grid(True, alpha=0.3)# Plot TS (uncertainty) — use merge_uncertain_outcomes to avoid summing manuallyax2 = axes[1]for name, fcod in fcods.items(): merged = merge_uncertain_outcomes(fcod) ax2.plot(merged["grid"], merged["TS"], label=name, linewidth=2)ax2.set_xlabel("Feature Value (standardized)")ax2.set_ylabel("TS Proportion")ax2.set_title("Where is the model uncertain?")ax2.legend()ax2.grid(True, alpha=0.3)plt.tight_layout()plt.show()
Feature Importance via FCOD Variance
Features with high FCOD variance are more influential on outcomes.
plot_stacked_bars_by_class displays one full-width stacked-bar panel per class with a shared feature histogram at the bottom. This layout is cleaner than side-by-side columns when the feature grid has many points.
Code
from conformalpy.fcod import compute_fcod_smoothed, plot_stacked_bars_by_class# Compute one FCOD per class for Feature_Afeature_idx =0fcod_by_class = {}feature_values_by_class = {}for cls in [0, 1]: mask = y_test == cls fcod_by_class[cls] = merge_uncertain_outcomes( compute_fcod_smoothed( X_test[mask, feature_idx], [prediction_sets[i] for i in np.where(mask)[0]], y_test[mask], n_grid=40, ) ) feature_values_by_class[cls] = X_test[mask, feature_idx]fig = plot_stacked_bars_by_class( fcod_by_class=fcod_by_class, feature_values_by_class=feature_values_by_class, class_names={0: "Class 0", 1: "Class 1"}, xlabel=feature_names[feature_idx], title=f"Outcome distribution by class — {feature_names[feature_idx]}", alpha=0.10,)plt.show()
Interpretation
Key insights from outcome FCODs:
Flat SC curve: Model confidence is stable across feature range
Rising SC curve: Model becomes more reliable at higher values
High TS region (binary) / High MC region (multiclass): Feature range where the model is uncertain
SI spikes: Dangerous regions where model is confidently wrong
MU spikes (multiclass): Regions with multi-set coverage failures
Low density regions: FCOD estimates may be unreliable
Operational implications:
Identify feature ranges for automatic vs manual decisions
Detect regions needing more training data
Compare model behavior across features
Use density visualization to avoid over-interpreting sparse regions
References
Friedman, Jerome H. 2001. “Greedy Function Approximation: A Gradient Boosting Machine.”The Annals of Statistics 29 (5): 1189–232. https://doi.org/10.1214/aos/1013203451.