Impulse Response Function (IRF) utilities for VAR models with plotting helpers. Extracted from the workflow in Tutorial.ipynb.
pip install var-irfRepository: https://github.com/TianzhuQin/Vector-Autoregression-Impulse-Response-Function
Paper: Hug fans or follow celebrities? How nationalism is reinforced on Chinese social media
- Fit a VAR(p) with
statsmodels.tsa.api.VAR(df).fit(lags). - Get IRF object via
fit(...).irf(h):- IRF values: cumulative
cum_effects(or non-cumulativeirfs). - Error bands:
- Asymptotic (
asym): build ±z·stderr from covariance. - Monte Carlo (
mc): direct lower/upper intervals.
- Asymptotic (
- IRF values: cumulative
- Plotting:
- Central IRF curve, gradient shaded bands, style options.
import pandas as pd
import matplotlib.pyplot as plt
from var_irf import compute_irf, plot_irf
# df is a DataFrame with multiple series columns and a time index
result = compute_irf(df, lags=2, horizon=7, stderr_type="asym", orth=False)
fig, ax = plt.subplots(figsize=(10, 8))
plot_irf(result, "Series1", "Series2", ax=ax, direction="single", band="shaded", layout="overlay", shaded=True)
ax.axhline(0, color='r', linestyle='dashdot', linewidth=0.5)
plt.show()compute_irf(df, lags=2, horizon=7, stderr_type='asym'|'mc', orth=False, cumulative=True, ...): Fit a VAR and return IRF arrays and error information.plot_irf(result, impulse, response, ax=None, direction='single'|'bi', band='shaded'|'interval', layout='overlay'|'side-by-side', shaded=True, ...): Plot using the result fromcompute_irfwith three orthogonal switches:- direction: single A→B or bidirectional.
- band: shaded (gradient) or interval (errorbar-like); set
shaded=Falsefor line-only. - layout: overlay in one axes or side-by-side two subplots.
MIT
If you use this package in your research, please cite:
@article{
HongKimZhangQin2025,
author = {Hong, Ji Yeon and Kim, Yong H. and Zhang, Han and Qin, Tianzhu},
title = {Hug fans or follow celebrities? How nationalism is reinforced on Chinese social media},
journal = {Science Advances},
year = {2025},
volume = {11},
number = {45},
pages = {eadu8241},
doi = {10.1126/sciadv.adu8241},
url = {https://www.science.org/doi/10.1126/sciadv.adu8241}
}