1 import numpy as np, matplotlib.pyplot as plt 2 from matplotlib.gridspec import GridSpec 3 from scipy.stats import gaussian_kde 4 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 5 setup_style() 6 7 np.random.seed(42) 8 t = np.arange(100) 9 actual = 50 + 10*np.sin(t/10) + np.cumsum(np.random.randn(100)*0.5) 10 pred = actual + np.random.randn(100)*2 11 ci_upper = pred + 3 + np.abs(np.random.randn(100))*1.5 12 ci_lower = pred - 3 - np.abs(np.random.randn(100))*1.5 13 errors = actual - pred; split_point = 70 14 15 fig = plt.figure(figsize=(11, 5)) 16 gs = GridSpec(1, 2, width_ratios=[5, 1], wspace=0.05) 17 ax = fig.add_subplot(gs[0]) 18 19 ax.axvspan(0, split_point, color=_lighten(PALETTE[0], 0.8), alpha=0.3, zorder=0) 20 ax.axvspan(split_point, 100, color=_lighten(COLORS['highlight'], 0.8), alpha=0.3, zorder=0) 21 ax.axvline(x=split_point, color=COLORS['ref_line'], linestyle=':', linewidth=1.2, alpha=0.6) 22 ax.text(split_point/2, actual.max()+2, 'Training', ha='center', fontsize=9, color=COLORS['text'], 23 bbox=dict(boxstyle='round,pad=0.25', facecolor='white', edgecolor=COLORS['grid'], alpha=0.9)) 24 ax.text((split_point+100)/2, actual.max()+2, 'Testing', ha='center', fontsize=9, color=COLORS['text'], 25 bbox=dict(boxstyle='round,pad=0.25', facecolor='white', edgecolor=COLORS['grid'], alpha=0.9)) 26 27 n_layers = 12 28 for k in range(n_layers, 0, -1): 29 frac = k / n_layers 30 ax.fill_between(t, pred-frac*(pred-ci_lower), pred+frac*(ci_upper-pred), 31 alpha=0.025, color=PALETTE[1], linewidth=0) 32 33 ax.plot(t, actual, color=PALETTE[0], linewidth=1.8, label='Actual', zorder=4) 34 ax.plot(t, pred, color=PALETTE[1], linewidth=1.5, linestyle='--', label='Predicted', zorder=3) 35 36 te = actual[split_point:] - pred[split_point:] 37 rmse = np.sqrt(np.mean(te**2)); mae = np.mean(np.abs(te)) 38 r2 = 1 - np.sum(te**2)/np.sum((actual[split_point:]-actual[split_point:].mean())**2) 39 ax.text(0.02, 0.03, f'Test Metrics:\nRMSE = {rmse:.3f}\nMAE = {mae:.3f}\nR² = {r2:.3f}', 40 transform=ax.transAxes, fontsize=8.5, va='bottom', ha='left', color=COLORS['text'], family='monospace', 41 bbox=dict(boxstyle='round,pad=0.4', facecolor=COLORS['bg_box'], edgecolor=COLORS['grid'], alpha=0.95)) 42 43 ax.set_xlabel('Time Step'); ax.set_ylabel('Value (a.u.)') 44 ax.spines['top'].set_visible(False) 45 ax.spines['right'].set_visible(False) 46 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9); ax.grid(alpha=0.15, linestyle='--') 47 48 ax_hist = fig.add_subplot(gs[1]) 49 ax_hist.hist(errors, bins=25, orientation='horizontal', color=_lighten(PALETTE[1], 0.4), alpha=0.5, edgecolor=PALETTE[1], linewidth=0.8, density=True) 50 kde = gaussian_kde(errors); y_kde = np.linspace(errors.min(), errors.max(), 100) 51 ax_hist.plot(kde(y_kde), y_kde, color=PALETTE[1], linewidth=1.5) 52 ax_hist.axhline(0, color=COLORS['ref_line'], linestyle='--', linewidth=0.6, alpha=0.5) 53 ax_hist.set_xlabel('Density (1/a.u.)', fontsize=9) 54 ax_hist.set_ylabel('Actual − predicted (a.u.)', fontsize=9) 55 ax_hist.yaxis.set_label_position('right') 56 ax_hist.yaxis.tick_right() 57 ax_hist.tick_params(axis='y', labelsize=8, labelleft=False, labelright=True) 58 ax_hist.spines['top'].set_visible(False); ax_hist.spines['right'].set_visible(False) 59 60 fig.tight_layout() 61 save_fig(fig, 'figures/fig_prediction_ci.pdf')