1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 setup_style()
   5 
   6 # === Example data (replace with actual IRF results) ===
   7 periods = np.arange(0, 16)
   8 responses = {
   9     '数字经济 → 数字经济': {'irf': [1.0, 0.8, 0.5, 0.3, 0.15, 0.08, 0.04, 0.02, 0.01, 0.005, 0, 0, 0, 0, 0, 0],
  10                           'lower': None, 'upper': None},
  11     '算力基础设施 → 数字经济': {'irf': [0, 0.15, 0.25, 0.30, 0.28, 0.22, 0.16, 0.11, 0.07, 0.04, 0.02, 0.01, 0, 0, 0, 0],
  12                               'lower': None, 'upper': None},
  13     '数字经济 → 算力基础设施': {'irf': [0, 0.05, 0.12, 0.18, 0.20, 0.18, 0.14, 0.10, 0.07, 0.04, 0.02, 0.01, 0, 0, 0, 0],
  14                               'lower': None, 'upper': None},
  15     '算力基础设施 → 算力基础设施': {'irf': [1.0, 0.7, 0.4, 0.2, 0.1, 0.05, 0.02, 0.01, 0, 0, 0, 0, 0, 0, 0, 0],
  16                                   'lower': None, 'upper': None},
  17 }
  18 
  19 # Generate CI bands
  20 for key in responses:
  21     irf = np.array(responses[key]['irf'])
  22     noise = np.abs(irf) * 0.3 + 0.02
  23     responses[key]['lower'] = irf - noise
  24     responses[key]['upper'] = irf + noise
  25 
  26 n_panels = len(responses)
  27 ncols = 2
  28 nrows = (n_panels + 1) // 2
  29 fig, axes = plt.subplots(nrows, ncols, figsize=(10, 4 * nrows), sharex=True)
  30 axes = axes.flatten()
  31 
  32 for idx, (title, data) in enumerate(responses.items()):
  33     ax = axes[idx]
  34     irf = np.array(data['irf'])
  35     lower = np.array(data['lower'])
  36     upper = np.array(data['upper'])
  37 
  38     # Gradient CI bands (3 layers)
  39     for layer, alpha in enumerate([0.08, 0.15, 0.25]):
  40         shrink = (1 - layer * 0.3)
  41         mid = irf
  42         ax.fill_between(periods, mid - (mid - lower) * shrink, mid + (upper - mid) * shrink,
  43                         alpha=alpha, color=PALETTE[idx % len(PALETTE)])
  44 
  45     # IRF line
  46     ax.plot(periods, irf, '-o', color=PALETTE[idx % len(PALETTE)], linewidth=2,
  47             markersize=4, markeredgecolor='white', markeredgewidth=0.8, zorder=5)
  48 
  49     # Zero reference
  50     ax.axhline(0, color=COLORS['ref_line'], linewidth=0.8, linestyle='--', alpha=0.5)
  51 
  52     # Significance shading (where CI doesn't cross zero)
  53     for t in range(len(periods)):
  54         if lower[t] > 0 or upper[t] < 0:
  55             ax.axvspan(t - 0.4, t + 0.4, alpha=0.04, color=PALETTE[idx % len(PALETTE)])
  56 
  57     ax.set_title(title, fontsize=9, fontweight='bold', loc='center', pad=3)
  58     ax.spines['top'].set_visible(False)
  59     ax.spines['right'].set_visible(False)
  60     ax.set_ylabel('响应', fontsize=9)
  61 
  62 for idx in range(n_panels, len(axes)):
  63     axes[idx].set_visible(False)
  64 
  65 axes[-2].set_xlabel('期数', fontsize=10)
  66 axes[-1].set_xlabel('期数', fontsize=10)
  67 fig.tight_layout()
  68 save_fig(fig, 'figures/fig_irf.pdf')