1 import numpy as np, matplotlib.pyplot as plt
   2 from scipy import stats
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 setup_style()
   5 
   6 np.random.seed(42)
   7 n = 200; fitted = np.random.uniform(2, 8, n); resid = np.random.normal(0, 0.5, n)
   8 
   9 fig, axes = plt.subplots(2, 2, figsize=(5.0, 4.9))   # 示例尺寸;保留模板比例,按实际显示尺寸检查字号与布局
  10 
  11 ax = axes[0, 0]
  12 ax.scatter(fitted, resid, alpha=0.45, s=18, color=PALETTE[0], edgecolor='white', linewidth=0.3, zorder=3)
  13 ax.axhline(0, color=COLORS['down'], linestyle='--', linewidth=0.8, alpha=0.7)
  14 z = np.polyfit(fitted, resid, 3); p = np.poly1d(z)
  15 x_smooth = np.linspace(fitted.min(), fitted.max(), 100)
  16 ax.plot(x_smooth, p(x_smooth), color=PALETTE[1], linewidth=2, zorder=4)
  17 ax.text(0.97, 0.97, 'Breusch-Pagan\nχ²=2.14, p=0.143', transform=ax.transAxes, fontsize=7.5,
  18         va='top', ha='right', color=COLORS['text'],
  19         bbox=dict(boxstyle='round,pad=0.3', facecolor=_lighten(COLORS['highlight'], 0.85), edgecolor=_lighten(COLORS['highlight'], 0.4), alpha=0.95))
  20 ax.set_xlabel('Fitted Values'); ax.set_ylabel('Residuals')
  21 ax.set_title('(a)', fontsize=12, fontweight='bold', loc='left', pad=3)
  22 ax.grid(alpha=0.15, linestyle='--')
  23 
  24 ax = axes[0, 1]
  25 osm, osr = stats.probplot(resid, dist='norm')[:2]
  26 ax.scatter(osm[0], osm[1], alpha=0.5, s=15, color=PALETTE[0], edgecolor='white', linewidth=0.3)
  27 slope, intercept = osr[:2]
  28 x_line = np.array([osm[0].min(), osm[0].max()])
  29 ax.plot(x_line, slope*x_line+intercept, color=COLORS['down'], linewidth=1.2, linestyle='--')
  30 sw_stat, sw_p = stats.shapiro(resid[:100])
  31 ax.text(0.03, 0.97, f'Shapiro-Wilk\nW={sw_stat:.3f}\np={sw_p:.3f}', transform=ax.transAxes,
  32         fontsize=7.5, va='top', ha='left', color=COLORS['text'],
  33         bbox=dict(boxstyle='round,pad=0.3', facecolor=_lighten(PALETTE[0], 0.8), edgecolor=_lighten(PALETTE[0], 0.6), alpha=0.95))
  34 ax.set_xlabel('Theoretical Quantiles'); ax.set_ylabel('Sample Quantiles')
  35 ax.set_title('(b)', fontsize=12, fontweight='bold', loc='left', pad=3)
  36 ax.grid(alpha=0.15, linestyle='--')
  37 
  38 ax = axes[1, 0]
  39 ax.hist(resid, bins=30, density=True, color=_lighten(PALETTE[0], 0.4), alpha=0.5, edgecolor=PALETTE[0], linewidth=0.8)
  40 x_norm = np.linspace(resid.min()-0.3, resid.max()+0.3, 200)
  41 y_norm = stats.norm.pdf(x_norm, resid.mean(), resid.std())
  42 ax.plot(x_norm, y_norm, color=COLORS['down'], linewidth=2, zorder=4)
  43 for k in range(10, 0, -1):
  44     ax.fill_between(x_norm, 0, y_norm*(k/10), alpha=0.015, color=COLORS['down'], linewidth=0)
  45 ax.set_xlabel('Residuals'); ax.set_ylabel('Density')
  46 ax.set_title('(c)', fontsize=12, fontweight='bold', loc='left', pad=3)
  47 ax.grid(alpha=0.15, linestyle='--')
  48 
  49 ax = axes[1, 1]
  50 cooks = np.random.exponential(0.005, n); cooks[15] = 0.035; cooks[87] = 0.028
  51 threshold = 4/n
  52 ml, sl, bl = ax.stem(range(n), cooks, linefmt='-', markerfmt=',', basefmt='')
  53 plt.setp(sl, linewidth=0.6, color=PALETTE[2], alpha=0.7); plt.setp(bl, linewidth=0)
  54 influential = cooks > threshold
  55 ax.scatter(np.where(influential)[0], cooks[influential], color=COLORS['down'], s=25, zorder=5, edgecolor='white')
  56 for idx in np.where(influential)[0]:
  57     ax.text(idx, cooks[idx]+0.001, f'#{idx}', fontsize=7, ha='center', color=COLORS['down'], fontweight='bold')
  58 ax.axhline(threshold, color=COLORS['down'], linestyle='--', linewidth=1.0, alpha=0.7)
  59 ax.axhspan(threshold, cooks.max()*1.2, color=COLORS['bg_box'], alpha=0.3, zorder=0)
  60 ax.text(n*0.97, threshold+0.001, f'4/n={threshold:.3f}', ha='right', va='bottom', fontsize=8, color=COLORS['down'],
  61         bbox=dict(boxstyle='round,pad=0.2', facecolor='white', edgecolor=COLORS['down'], alpha=0.9))
  62 ax.set_xlabel('Observation Index'); ax.set_ylabel("Cook's Distance")
  63 ax.set_title('(d)', fontsize=12, fontweight='bold', loc='left', pad=3)
  64 ax.grid(alpha=0.15, linestyle='--')
  65 
  66 for ax in axes.flat:
  67     ax.spines['top'].set_visible(False)
  68     ax.spines['right'].set_visible(False)
  69 fig.tight_layout()
  70 save_fig(fig, 'figures/fig_diagnostics.pdf')