fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# Per-class coverage bar chart
ax = axes[0, 0]
x = np.arange(n_classes)
width = 0.25
ax.bar(x - width, cov_standard, width, label="Standard", alpha=0.8)
ax.bar(x, cov_mondrian, width, label="Mondrian", alpha=0.8)
ax.bar(x + width, cov_clustered, width, label="Clustered", alpha=0.8)
ax.axhline(y=0.90, color="red", linestyle="--", linewidth=2, label="Target (90%)")
ax.set_xlabel("Class")
ax.set_ylabel("Coverage")
ax.set_title("Per-Class Coverage Comparison")
ax.legend(loc="lower right")
ax.set_ylim(0.5, 1.05)
ax.set_xticks(x[::5]) # Show every 5th class
# Coverage distribution histogram
ax = axes[0, 1]
bins = np.linspace(0.5, 1.0, 11)
ax.hist(cov_standard, bins=bins, alpha=0.5, label="Standard", edgecolor="black")
ax.hist(cov_mondrian, bins=bins, alpha=0.5, label="Mondrian", edgecolor="black")
ax.hist(cov_clustered, bins=bins, alpha=0.5, label="Clustered", edgecolor="black")
ax.axvline(x=0.90, color="red", linestyle="--", linewidth=2, label="Target")
ax.set_xlabel("Coverage")
ax.set_ylabel("Number of Classes")
ax.set_title("Distribution of Per-Class Coverage")
ax.legend()
# Coverage by cluster (for Clustered CP)
ax = axes[1, 0]
cluster_coverages = []
cluster_labels = []
for cluster_id, classes in sorted(cluster_info["classes_per_cluster"].items()):
if cluster_id == -1:
continue
mask = np.isin(y_test, classes)
if np.sum(mask) > 0:
covered = [y_test[i] in pred_sets_clustered[i] for i in np.where(mask)[0]]
cluster_coverages.append(np.mean(covered))
cluster_labels.append(f"C{cluster_id}\n({len(classes)})")
bars = ax.bar(range(len(cluster_coverages)), cluster_coverages, color="coral", edgecolor="black")
ax.axhline(y=0.90, color="red", linestyle="--", linewidth=2, label="Target (90%)")
ax.set_xlabel("Cluster (# classes)")
ax.set_ylabel("Coverage")
ax.set_title("Clustered CP: Coverage by Cluster")
ax.set_xticks(range(len(cluster_labels)))
ax.set_xticklabels(cluster_labels)
ax.set_ylim(0.7, 1.0)
ax.legend()
# Summary comparison
ax = axes[1, 1]
methods = ["Standard", "Mondrian", "Clustered"]
overall_coverage = [
coverage_score(pred_sets_standard, y_test),
coverage_score(pred_sets_mondrian, y_test),
coverage_score(pred_sets_clustered, y_test),
]
avg_set_sizes = [
average_set_size(pred_sets_standard),
average_set_size(pred_sets_mondrian),
average_set_size(pred_sets_clustered),
]
worst_class_coverage = [
np.nanmin(cov_standard),
np.nanmin(cov_mondrian),
np.nanmin(cov_clustered),
]
x = np.arange(len(methods))
width = 0.25
bars1 = ax.bar(x - width, overall_coverage, width, label="Overall Coverage", color="steelblue")
bars2 = ax.bar(x, worst_class_coverage, width, label="Worst-Class Coverage", color="coral")
ax.axhline(y=0.90, color="red", linestyle="--", linewidth=2, alpha=0.7)
ax.set_ylabel("Coverage")
ax.set_xlabel("Method")
ax.set_title("Coverage Summary")
ax.set_xticks(x)
ax.set_xticklabels(methods)
ax.legend(loc="lower right")
ax.set_ylim(0.6, 1.0)
# Add value labels
for bar in bars1:
ax.annotate(
f"{bar.get_height():.1%}",
xy=(bar.get_x() + bar.get_width() / 2, bar.get_height()),
xytext=(0, 3),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=9,
)
for bar in bars2:
ax.annotate(
f"{bar.get_height():.1%}",
xy=(bar.get_x() + bar.get_width() / 2, bar.get_height()),
xytext=(0, 3),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=9,
)
plt.tight_layout()
plt.suptitle(f"Clustered Conformal Prediction ({n_classes} Classes)", fontsize=14, y=1.02)
plt.show()