1 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 2 setup_style() 3 import matplotlib.pyplot as plt 4 import matplotlib.gridspec as gridspec 5 import numpy as np 6 7 np.random.seed(42) 8 bins = np.linspace(0, 1, 11) 9 bin_centers = (bins[:-1] + bins[1:]) / 2 10 model_a = np.clip(bin_centers + np.random.uniform(-0.05, 0.05, 10), 0, 1) 11 model_b = np.clip(bin_centers ** 0.7 + np.random.uniform(-0.03, 0.03, 10), 0, 1) 12 13 # Simulated predicted probabilities for histogram 14 pred_probs_a = np.clip(np.random.beta(2, 2, 500), 0, 1) 15 pred_probs_b = np.clip(np.random.beta(1.5, 3, 500), 0, 1) 16 17 # Brier scores 18 brier_a = 0.023 19 brier_b = 0.089 20 21 fig = plt.figure(figsize=(6, 7)) 22 gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1], hspace=0.08) 23 24 # Top: Calibration curve 25 ax_cal = fig.add_subplot(gs[0]) 26 ax_cal.grid(alpha=0.15, linestyle='-', color=COLORS['grid']) 27 ax_cal.set_axisbelow(True) 28 29 # Perfect calibration line 30 ax_cal.plot([0, 1], [0, 1], 'k--', linewidth=0.8, alpha=0.5, label='Perfect') 31 32 # Gradient CI band for Model A 33 ci_width_a = np.random.uniform(0.03, 0.07, 10) 34 ax_cal.fill_between(bin_centers, model_a - ci_width_a, model_a + ci_width_a, 35 alpha=0.15, color=PALETTE[0]) 36 ax_cal.plot(bin_centers, model_a, 'o-', color=PALETTE[0], linewidth=2.2, 37 markersize=7, label=f'Ours (Brier={brier_a:.3f})', 38 markeredgecolor='white', markeredgewidth=1.0) 39 40 # Gradient CI band for Model B 41 ci_width_b = np.random.uniform(0.04, 0.09, 10) 42 ax_cal.fill_between(bin_centers, model_b - ci_width_b, model_b + ci_width_b, 43 alpha=0.12, color=PALETTE[3]) 44 ax_cal.plot(bin_centers, model_b, 's--', color=PALETTE[3], linewidth=2.0, 45 markersize=7, label=f'Baseline (Brier={brier_b:.3f})', 46 markeredgecolor='white', markeredgewidth=1.0) 47 48 # Brier score annotation box 49 ax_cal.annotate(f'Brier Score\nOurs: {brier_a:.3f}\nBaseline: {brier_b:.3f}', 50 xy=(0.05, 0.88), xycoords='axes fraction', 51 fontsize=8.5, va='top', 52 bbox=dict(boxstyle='round,pad=0.4', facecolor=_lighten(PALETTE[0], 0.7), 53 edgecolor=PALETTE[0], alpha=0.9)) 54 55 ax_cal.set_ylabel('Fraction of positives', fontsize=11) 56 ax_cal.set_xlim(0, 1) 57 ax_cal.set_ylim(0, 1) 58 ax_cal.set_aspect('equal') 59 ax_cal.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8.5, loc='lower right') 60 ax_cal.spines['top'].set_visible(False) 61 ax_cal.spines['right'].set_visible(False) 62 ax_cal.set_xticklabels([]) 63 64 # Bottom: Histogram of predicted probabilities 65 ax_hist = fig.add_subplot(gs[1]) 66 ax_hist.hist(pred_probs_a, bins=20, alpha=0.5, color=PALETTE[0], label='Ours', 67 edgecolor='white', linewidth=0.5) 68 ax_hist.hist(pred_probs_b, bins=20, alpha=0.4, color=PALETTE[3], label='Baseline', 69 edgecolor='white', linewidth=0.5) 70 ax_hist.set_xlabel('Mean predicted probability', fontsize=11) 71 ax_hist.set_ylabel('Count', fontsize=10) 72 ax_hist.set_xlim(0, 1) 73 ax_hist.legend(frameon=False, fontsize=8) 74 ax_hist.spines['top'].set_visible(False) 75 ax_hist.spines['right'].set_visible(False) 76 77 fig.tight_layout() 78 save_fig(fig, 'figures/fig_calibration.pdf')