# ─── Impulse Response Functions ──────────────────────────────────────────────
irf = var_result.irf(periods=12)
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# Identify column indices
col_names = list(data_diff.columns)
usd_idx = col_names.index('S_USD')
khr_idx = col_names.index('S_KHR')
ffr_idx = col_names.index('FFR')
# FFR shock → USD Spread
ax = axes[0, 0]
ax.plot(irf.irfs[:, usd_idx, ffr_idx], color='#1565C0', linewidth=2)
ax.fill_between(range(13), irf.irfs[:, usd_idx, ffr_idx] - 1.96*irf.stderr()[:, usd_idx, ffr_idx],
irf.irfs[:, usd_idx, ffr_idx] + 1.96*irf.stderr()[:, usd_idx, ffr_idx],
alpha=0.15, color='#1565C0')
ax.axhline(y=0, color='grey', linestyle='--', alpha=0.5)
ax.set_title('FFR Shock → USD Spread', fontweight='bold')
ax.set_ylabel('Response')
ax.grid(True, alpha=0.3)
# FFR shock → KHR Spread
ax = axes[0, 1]
ax.plot(irf.irfs[:, khr_idx, ffr_idx], color='#C62828', linewidth=2)
ax.fill_between(range(13), irf.irfs[:, khr_idx, ffr_idx] - 1.96*irf.stderr()[:, khr_idx, ffr_idx],
irf.irfs[:, khr_idx, ffr_idx] + 1.96*irf.stderr()[:, khr_idx, ffr_idx],
alpha=0.15, color='#C62828')
ax.axhline(y=0, color='grey', linestyle='--', alpha=0.5)
ax.set_title('FFR Shock → KHR Spread', fontweight='bold')
ax.grid(True, alpha=0.3)
# USD Spread shock → KHR Spread (cross-currency spillover)
ax = axes[1, 0]
ax.plot(irf.irfs[:, khr_idx, usd_idx], color='#4A148C', linewidth=2)
ax.fill_between(range(13), irf.irfs[:, khr_idx, usd_idx] - 1.96*irf.stderr()[:, khr_idx, usd_idx],
irf.irfs[:, khr_idx, usd_idx] + 1.96*irf.stderr()[:, khr_idx, usd_idx],
alpha=0.15, color='#4A148C')
ax.axhline(y=0, color='grey', linestyle='--', alpha=0.5)
ax.set_title('USD Spread Shock → KHR Spread', fontweight='bold')
ax.set_ylabel('Response')
ax.set_xlabel('Months After Shock')
ax.grid(True, alpha=0.3)
# KHR Spread shock → USD Spread (reverse spillover)
ax = axes[1, 1]
ax.plot(irf.irfs[:, usd_idx, khr_idx], color='#E65100', linewidth=2)
ax.fill_between(range(13), irf.irfs[:, usd_idx, khr_idx] - 1.96*irf.stderr()[:, usd_idx, khr_idx],
irf.irfs[:, usd_idx, khr_idx] + 1.96*irf.stderr()[:, usd_idx, khr_idx],
alpha=0.15, color='#E65100')
ax.axhline(y=0, color='grey', linestyle='--', alpha=0.5)
ax.set_title('KHR Spread Shock → USD Spread', fontweight='bold')
ax.set_xlabel('Months After Shock')
ax.grid(True, alpha=0.3)
fig.suptitle('Figure 19: Impulse Response Functions — VAR Model', fontweight='bold', fontsize=14, y=1.01)
plt.tight_layout()
plt.savefig('../figures/fig19_irf.png', dpi=300, bbox_inches='tight')
plt.show()
print('Saved: fig19_irf.png')