plots.plot_outcome_distribution_by_bins

plots.plot_outcome_distribution_by_bins(
    feature_values,
    prediction_sets,
    y_true,
    *,
    n_bins=10,
    bin_method='quantile',
    bin_edges=None,
    show_counts=True,
    normalize=True,
    title=None,
    feature_name='Feature',
    figsize=(12, 6),
    colors=None,
    ax=None,
)

Plot stacked bar chart showing outcome distribution by feature bins.

This visualization shows how conformal prediction outcomes (SC, SI, TS0, TS1) are distributed across different values of a numeric feature. Each bar represents a bin of feature values, with colors indicating the proportion of each outcome.

Parameters

feature_values : array-like of shape (n_samples,)

Numeric feature values used to create bins.

prediction_sets : list of sets or lists

Conformal prediction sets, one per sample.

y_true : array-like of shape (n_samples,)

True binary labels (0 or 1).

n_bins : int = 10

Number of bins to create. Ignored if bin_edges is provided.

bin_method : (quantile, uniform) = "quantile"

Method for creating bins: - “quantile”: Create bins with approximately equal sample sizes - “uniform”: Create bins with equal width

bin_edges : array - like = None

Custom bin edges. If provided, n_bins and bin_method are ignored. Must have length n_bins + 1.

show_counts : bool = True

If True, annotate bars with sample count per bin.

normalize : bool = True

If True, bars sum to 1 within each bin (proportions). If False, show raw counts.

title : str = None

Plot title. If None, uses a default based on feature_name.

feature_name : str = "Feature"

Name of the feature for axis labels.

figsize : tuple = (12, 6)

Figure size.

colors : dict = None

Custom colors for outcomes. Keys are outcome names (“SC”, “SI”, “TS0”, “TS1”, “Empty”), values are color strings.

ax : matplotlib.axes.Axes = None

Axes to plot on. If None, creates a new figure.

Returns

fig : matplotlib.figure.Figure

The figure object.

ax : matplotlib.axes.Axes

The axes object.

bin_stats : pandas.DataFrame

DataFrame with outcome counts/proportions per bin.

See Also

plot_outcome_distribution_by_category : For categorical features. conformalpy.outcomes.categorize_outcomes : Compute outcomes from prediction sets.

Examples

>>> from conformalpy.plots import plot_outcome_distribution_by_bins
>>> # Using quantile-based bins (default)
>>> fig, ax, stats = plot_outcome_distribution_by_bins(
...     X_test["fico_score"].values,
...     prediction_sets,
...     y_test,
...     feature_name="FICO Score",
...     n_bins=8
... )
>>> # Using custom bin edges (e.g., industry-standard credit tiers)
>>> credit_tiers = [300, 580, 670, 740, 800, 850]
>>> fig, ax, stats = plot_outcome_distribution_by_bins(
...     X_test["fico_score"].values,
...     prediction_sets,
...     y_test,
...     bin_edges=credit_tiers,
...     feature_name="FICO Score"
... )

Notes

Interpreting the plot:

  • High SC (green) regions indicate where the model is both confident and correct - good candidates for automation.
  • High SI (red) regions indicate problematic areas where the model is overconfident - may need recalibration or additional features.
  • High TS (blue/purple) regions indicate uncertainty - good candidates for manual review or additional data collection.
  • The balance between TS0 and TS1 shows class distribution within uncertain predictions.

For FICO scores in credit scoring: - Expect higher SC at score extremes (very high/low scores are predictable) - Expect higher TS in mid-range scores (borderline cases) - SI should ideally be low across all bins