fig, axes = plt.subplots(1, 2, figsize=(12, 5))
regions = ["North", "South", "West"]
coverages_standard = []
coverages_mondrian = []
counts = []
for region in regions:
mask = g_test == region
counts.append(np.sum(mask))
# Standard CP coverage
covered_std = [y_test[i] in pred_sets_standard[i] for i in np.where(mask)[0]]
coverages_standard.append(np.mean(covered_std))
# Mondrian coverage
covered_mon = [y_test[i] in pred_sets_mondrian[i] for i in np.where(mask)[0]]
coverages_mondrian.append(np.mean(covered_mon))
x = np.arange(len(regions))
width = 0.35
# Coverage comparison
ax = axes[0]
bars1 = ax.bar(x - width / 2, coverages_standard, width, label="Standard CP", color="steelblue")
bars2 = ax.bar(x + width / 2, coverages_mondrian, width, label="Flexible Mondrian", color="coral")
ax.axhline(y=0.90, color="red", linestyle="--", linewidth=2, label="Target (90%)")
ax.set_ylabel("Coverage")
ax.set_xlabel("Region")
ax.set_title("Coverage by Region")
ax.set_xticks(x)
ax.set_xticklabels(regions)
ax.legend()
ax.set_ylim(0.8, 1.0)
# Add value labels
for bar, cov in zip(bars1, coverages_standard):
ax.annotate(
f"{cov:.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, cov in zip(bars2, coverages_mondrian):
ax.annotate(
f"{cov:.1%}",
xy=(bar.get_x() + bar.get_width() / 2, bar.get_height()),
xytext=(0, 3),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=9,
)
# Per-group qhat
ax2 = axes[1]
qhats = [cc_mondrian.qhat_per_group_[r] for r in regions]
bars = ax2.bar(regions, qhats, color=["#2ca02c", "#ff7f0e", "#d62728"])
ax2.axhline(
y=cc_standard.qhat_, color="steelblue", linestyle="--", linewidth=2, label=f"Global qhat ({cc_standard.qhat_:.3f})"
)
ax2.set_ylabel("qhat (threshold)")
ax2.set_xlabel("Region")
ax2.set_title("Calibrated Thresholds by Region")
ax2.legend()
# Add value labels
for bar, qhat in zip(bars, qhats):
ax2.annotate(
f"{qhat:.3f}",
xy=(bar.get_x() + bar.get_width() / 2, bar.get_height()),
xytext=(0, 3),
textcoords="offset points",
ha="center",
va="bottom",
fontsize=10,
)
plt.tight_layout()
plt.suptitle("Flexible Mondrian vs Standard Conformal Prediction", fontsize=14, y=1.02)
plt.show()