library(fpp3)
library(astsa)
library(vars)
select <- dplyr::selectNote 24: Vector Autoregression (VAR): Formulation, Stability & Impulse Responses
Module 6: Multivariate & Breaks
Learning Objectives
- Write a \(K\)-variable \(\text{VAR}(p)\) system in matrix form and state the companion-matrix eigenvalue condition for covariance stationarity.
- Select lag order \(p\) via information criteria (
VARselect()), verify stability (vars::roots()), and test multivariate residual autocorrelation (serial.test()). - Derive Cholesky-orthogonalized Impulse Response Functions (IRFs) and explain why a 1-SD orthogonalized shock depends critically on variable ordering.
- Estimate and visualize crude-oil-to-gasoline pass-through dynamics using
ggplot2with bootstrapped 95% confidence bands.
1. The \(\text{VAR}(p)\) System and Stability Condition
Univariate models treat a series in isolation, ignoring feedback loops across coupled systems (such as oil prices and retail gasoline, or consumption, income, and production). A Vector Autoregression of order \(p\), denoted \(\text{VAR}(p)\), models a \(K \times 1\) vector of stationary series \(\mathbf{y}_t = (y_{1,t}, \dots, y_{K,t})^\top\) symmetrically as linear functions of \(p\) lags of every variable in the system:
\[ \mathbf{y}_t = \mathbf{c} + \mathbf{A}_1 \mathbf{y}_{t-1} + \mathbf{A}_2 \mathbf{y}_{t-2} + \dots + \mathbf{A}_p \mathbf{y}_{t-p} + \mathbf{u}_t, \qquad \mathbf{u}_t \stackrel{\text{iid}}{\sim} (\mathbf{0}, \boldsymbol{\Sigma}_u), \]
where \(\mathbf{c}\) is a \(K \times 1\) intercept vector, each \(\mathbf{A}_j\) (\(j = 1, \dots, p\)) is a \(K \times K\) coefficient matrix, and \(\boldsymbol{\Sigma}_u\) is a symmetric positive-definite \(K \times K\) error covariance matrix whose off-diagonal elements allow contemporaneous correlation among shocks.
Because every equation shares the identical set of regressors, equation-by-equation OLS is both consistent and efficient (identical to generalized least squares). However, each equation estimates \(1 + Kp\) parameters (\(K(1 + Kp)\) mean parameters in total), so VARs grow overparameterized rapidly unless \(K\) and \(p\) remain modest.
Companion-Matrix Stability Condition
Stacking \(\mathbf{Y}_t = (\mathbf{y}_t^\top, \mathbf{y}_{t-1}^\top, \dots, \mathbf{y}_{t-p+1}^\top)^\top\) rewrites any \(\text{VAR}(p)\) as a \(Kp\)-dimensional \(\text{VAR}(1)\):
\[ \mathbf{Y}_t = \mathbf{C} + \mathbf{F} \mathbf{Y}_{t-1} + \mathbf{U}_t, \qquad \mathbf{F} = \begin{pmatrix} \mathbf{A}_1 & \mathbf{A}_2 & \dots & \mathbf{A}_{p-1} & \mathbf{A}_p \\ \mathbf{I}_K & \mathbf{0} & \dots & \mathbf{0} & \mathbf{0} \\ \vdots & \ddots & & \vdots & \vdots \\ \mathbf{0} & \mathbf{0} & \dots & \mathbf{I}_K & \mathbf{0} \end{pmatrix}. \]
The \(\text{VAR}(p)\) process is stable (and therefore covariance-stationary) if and only if every eigenvalue \(\lambda_i\) of the companion matrix \(\mathbf{F}\) lies strictly inside the unit circle:
\[ |\lambda_i| < 1 \quad \text{for all } i = 1, \dots, Kp. \]
In R, vars::roots(fit) returns the eigenvalue moduli \(|\lambda_i|\) directly, so stability requires max(vars::roots(fit)) < 1. Unstable VARs generate explosive forecasts and diverging impulse responses.
2. Estimation, Lag Selection & Stability: Crude Oil and Gasoline
Weekly WTI crude oil prices (astsa::oil, USD per barrel) and U.S. regular gasoline prices (astsa::gas, cents per gallon) from 2000 to mid-2010 are non-stationary price levels. To satisfy stationarity before fitting a VAR, we transform both series to weekly log percentage returns \(r_t = 100 \Delta \log(P_t)\) (\(n = 544\) weeks).
og <- tibble(
week = seq_len(length(astsa::oil) - 1),
oil_ret = 100 * diff(log(as.numeric(astsa::oil))),
gas_ret = 100 * diff(log(as.numeric(astsa::gas)))
)
og |>
pivot_longer(-week, names_to = "series", values_to = "ret") |>
mutate(series = recode(series, oil_ret = "WTI Crude Oil (%)", gas_ret = "U.S. Gasoline (%)")) |>
ggplot(aes(x = week, y = ret, colour = series)) +
geom_line(linewidth = 0.45, alpha = 0.85) +
facet_wrap(vars(series), ncol = 1, scales = "free_y") +
guides(colour = "none") +
labs(
title = "Stationary Weekly Log Returns: Crude Oil vs. Gasoline (2000-2010)",
subtitle = TeX(r"(Weekly log returns $100\,\Delta \log(P_t)$ remove trends and show co-movement)"),
x = "Week Index",
y = "Weekly Log Return (%)"
)
We select lag order \(p\) via VARselect(), fit a parsimonious VAR(3) (the Hannan-Quinn choice), verify companion-matrix stability, and inspect the multivariate Portmanteau test for residual autocorrelation.
og_mat <- og |> select(oil_ret, gas_ret)
lag_sel <- VARselect(og_mat, lag.max = 8, type = "const")
tibble(Criterion = names(lag_sel$selection), Selected_p = as.integer(lag_sel$selection)) |>
fmt_tbl(digits = 0, caption = "VAR lag-order selection criteria for weekly oil and gasoline returns")| Criterion | Selected_p |
|---|---|
| AIC(n) | 4 |
| HQ(n) | 3 |
| SC(n) | 1 |
| FPE(n) | 4 |
var_og <- vars::VAR(og_mat, p = 3, type = "const")
pt_test <- serial.test(var_og, lags.pt = 16, type = "PT.asymptotic")
tibble(
Metric = c("Max companion eigenvalue |lambda|", "Min companion eigenvalue |lambda|", "Portmanteau Q (16 lags)", "Portmanteau p-value"),
Value = c(max(vars::roots(var_og)), min(vars::roots(var_og)), unname(pt_test$serial$statistic), unname(pt_test$serial$p.value))
) |>
fmt_tbl(digits = 4, caption = "Stability and multivariate residual diagnostics for VAR(3)")| Metric | Value |
|---|---|
| Max companion eigenvalue |lambda| | 0.5822 |
| Min companion eigenvalue |lambda| | 0.1926 |
| Portmanteau Q (16 lags) | 83.6320 |
| Portmanteau p-value | 0.0035 |
All six companion eigenvalues lie well inside the unit circle (max |lambda| = 0.582 < 1), strictly guaranteeing stability. The Portmanteau test (\(p = 0.0035\)) signals mild residual correlation beyond lag 3 (common in weekly energy markets where refining bottlenecks and volatility clustering linger), which we probe further in Exercise 1.
3. Impulse Response Functions & Cholesky Ordering
A stable \(\text{VAR}(p)\) admits a vector moving-average \(\text{VMA}(\infty)\) representation:
\[ \mathbf{y}_t = \boldsymbol{\mu} + \sum_{s=0}^\infty \boldsymbol{\Phi}_s \mathbf{u}_{t-s}, \qquad \boldsymbol{\Phi}_0 = \mathbf{I}_K. \]
Because reduced-form residuals \(\mathbf{u}_t\) have correlated components (\(\text{Cor}(u_{\text{oil},t}, u_{\text{gas},t}) > 0\)), shocking \(u_{\text{oil},t}\) in isolation while holding \(u_{\text{gas},t} = 0\) is unrealistic. Instead, vars::irf(..., ortho = TRUE) performs a Cholesky decomposition \(\boldsymbol{\Sigma}_u = \mathbf{P}\mathbf{P}^\top\) where \(\mathbf{P}\) is lower triangular, defining orthogonal unit-variance shocks \(\boldsymbol{\epsilon}_t = \mathbf{P}^{-1}\mathbf{u}_t \sim (\mathbf{0}, \mathbf{I}_K)\):
\[ \mathbf{y}_t = \boldsymbol{\mu} + \sum_{s=0}^\infty (\boldsymbol{\Phi}_s \mathbf{P}) \boldsymbol{\epsilon}_{t-s} = \boldsymbol{\mu} + \sum_{s=0}^\infty \boldsymbol{\Theta}_s \boldsymbol{\epsilon}_{t-s}. \]
Entry \((i, j)\) of \(\boldsymbol{\Theta}_h\) traces the dynamic response of variable \(i\) at horizon \(h\) to a one-standard-deviation (1-SD) orthogonalized shock in variable \(j\).
Why Variable Ordering Matters Critically
Because \(\mathbf{P}\) is lower triangular, the first variable can affect the second contemporaneously (\(h = 0\)), whereas the second variable can affect the first only with a one-period lag (\(h \ge 1\)):
- Ordering
(oil_ret, gas_ret)matches refinery economics: upstream crude oil shocks pass through to retail gasoline immediately in week \(0\). - Reversing the order to
(gas_ret, oil_ret)forces crude oil’s week-\(0\) impact on gasoline to be identically zero!
We demonstrate this ordering sensitivity directly using boot = TRUE, runs = 200, and set.seed(493), extracting both IRFs into a tidy tibble for ggplot2.
extract_irf <- function(irf_obj, imp, label) {
tibble(
horizon = 0:(nrow(irf_obj$irf[[imp]]) - 1),
irf = as.numeric(irf_obj$irf[[imp]]),
lower = as.numeric(irf_obj$Lower[[imp]]),
upper = as.numeric(irf_obj$Upper[[imp]]),
ordering = label
)
}
set.seed(493)
irf_oil_first <- vars::irf(
var_og, impulse = "oil_ret", response = "gas_ret",
n.ahead = 12, ortho = TRUE, boot = TRUE, runs = 200
)
var_og_rev <- vars::VAR(og_mat |> select(gas_ret, oil_ret), p = 3, type = "const")
set.seed(493)
irf_gas_first <- vars::irf(
var_og_rev, impulse = "oil_ret", response = "gas_ret",
n.ahead = 12, ortho = TRUE, boot = TRUE, runs = 200
)
irf_df <- bind_rows(
extract_irf(irf_oil_first, "oil_ret", "Order 1: (oil_ret, gas_ret)"),
extract_irf(irf_gas_first, "oil_ret", "Order 2: (gas_ret, oil_ret)")
)ggplot(irf_df, aes(x = horizon, y = irf, colour = ordering, fill = ordering)) +
geom_hline(yintercept = 0, linetype = "dashed", colour = ts_pal["slate"]) +
geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.22, colour = NA) +
geom_line(linewidth = 0.9) +
geom_point(size = 1.8) +
facet_wrap(vars(ordering), ncol = 2) +
guides(colour = "none", fill = "none") +
scale_x_continuous(breaks = seq(0, 12, 2)) +
labs(
title = "Gasoline Return Response to a 1-SD Crude Oil Shock",
subtitle = TeX(r"(Cholesky ordering governs contemporaneous pass-through at $h = 0$)"),
x = "Horizon (Weeks After Shock)",
y = "Gasoline Return Response (Percentage Points)"
)
Under the economically natural ordering (oil_ret, gas_ret), a 1-SD crude oil shock immediately raises gasoline returns by +3.60 percentage points in week 0 and another +1.00 percentage point in week 1 before dying out after week 4. Under the reversed ordering (gas_ret, oil_ret), the contemporaneous week-0 response is artificially constrained to 0.00, leaving only the lagged week-1 bounce (+0.80 pp). Always justify Cholesky ordering from domain theory and test sensitivity to alternative orderings.
Practice Exercises
Exercise 1: Companion Stability & Residual Check for VAR(4)
AIC and FPE selected \(p = 4\) on (oil_ret, gas_ret). Fit vars::VAR(og_mat, p = 4, type = "const"), print all eight companion eigenvalue moduli via vars::roots(), and test whether increasing to \(p = 4\) resolves the Portmanteau serial correlation test (lags.pt = 16).
Solution
All eight eigenvalues remain comfortably below 1 (max |lambda| = 0.625), confirming stability. Raising \(p\) from 3 to 4 improves the Portmanteau p-value from 0.0035 to 0.0121, though weak higher-order dependence remains typical of financial and commodity return series.
var_og4 <- vars::VAR(og_mat, p = 4, type = "const")
pt4 <- serial.test(var_og4, lags.pt = 16, type = "PT.asymptotic")
tibble(
Root_Index = seq_along(vars::roots(var_og4)),
Modulus = vars::roots(var_og4)
) |>
fmt_tbl(digits = 4, caption = "Companion eigenvalue moduli for VAR(4)")| Root_Index | Modulus |
|---|---|
| 1 | 0.6254 |
| 2 | 0.6254 |
| 3 | 0.5908 |
| 4 | 0.5908 |
| 5 | 0.5129 |
| 6 | 0.5129 |
| 7 | 0.5000 |
| 8 | 0.3886 |
tibble(
Model = c("VAR(3)", "VAR(4)"),
Max_Root = c(max(vars::roots(var_og)), max(vars::roots(var_og4))),
PT_Stat = c(unname(pt_test$serial$statistic), unname(pt4$serial$statistic)),
PT_pvalue = c(unname(pt_test$serial$p.value), unname(pt4$serial$p.value))
) |>
fmt_tbl(digits = 4, caption = "Stability and Portmanteau comparison: VAR(3) vs. VAR(4)")| Model | Max_Root | PT_Stat | PT_pvalue |
|---|---|---|---|
| VAR(3) | 0.5822 | 83.63 | 0.0035 |
| VAR(4) | 0.6254 | 72.74 | 0.0121 |
Exercise 2: Reverse Impulse (gas_ret \(\to\) oil_ret) Under Both Orderings
Compute the orthogonalized IRF for impulse = "gas_ret" and response = "oil_ret" (n.ahead = 12, boot = TRUE, runs = 200, set.seed(493)) under both (oil_ret, gas_ret) and (gas_ret, oil_ret), and compare the week-0 and week-1 responses in a ggplot2 figure.
Solution
When oil_ret is ordered first, a gasoline shock cannot affect crude oil contemporaneously (h = 0 equals 0), and only a modest positive feedback emerges at week 1 (h = 1). When gas_ret is ordered first, all contemporaneous oil-gasoline correlation is attributed to the gasoline shock, shifting the h = 0 crude oil response sharply upward.
set.seed(493)
irf_g2o_1 <- vars::irf(var_og, impulse = "gas_ret", response = "oil_ret",
n.ahead = 12, ortho = TRUE, boot = TRUE, runs = 200)
set.seed(493)
irf_g2o_2 <- vars::irf(var_og_rev, impulse = "gas_ret", response = "oil_ret",
n.ahead = 12, ortho = TRUE, boot = TRUE, runs = 200)
bind_rows(
extract_irf(irf_g2o_1, "gas_ret", "Order 1: (oil_ret, gas_ret)"),
extract_irf(irf_g2o_2, "gas_ret", "Order 2: (gas_ret, oil_ret)")
) |>
ggplot(aes(x = horizon, y = irf, colour = ordering, fill = ordering)) +
geom_hline(yintercept = 0, linetype = "dashed", colour = ts_pal["slate"]) +
geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.22, colour = NA) +
geom_line(linewidth = 0.9) +
geom_point(size = 1.8) +
facet_wrap(vars(ordering), ncol = 2) +
guides(colour = "none", fill = "none") +
scale_x_continuous(breaks = seq(0, 12, 2)) +
labs(
title = "Crude Oil Return Response to a 1-SD Gasoline Shock",
subtitle = TeX(r"(Reversing order shifts contemporaneous covariance to $h = 0$)"),
x = "Horizon (Weeks After Shock)",
y = "Oil Return Response (Percentage Points)"
)
Key Takeaways
- Fit VAR models only on stationary series (difference or take log returns first) and verify stability via
max(vars::roots(fit)) < 1so all companion eigenvalues lie inside the unit circle. - When attaching
library(vars), immediately restoreselect <- dplyr::select(masked byMASS) and qualifyvars::VAR()whenfpp3is also loaded. vars::irf(..., ortho = TRUE)shocks the system by 1 SD using a lower-triangular Cholesky factor \(\mathbf{P}\), making the contemporaneous response (\(h = 0\)) strongly dependent on variable ordering.- Always set
set.seed()before bootstrapping IRF confidence bands and test robustness across plausible economic orderings.