BVAR with conditional Normal-inverse-Wishart Prior.


Fields and Methods


Instantiation:

# create a new object
bvar_obj <- new(bvarcnw)

Fields:

  • cons_term a logical value (TRUE/FALSE) indicating the presence of a constant term (intercept) in the model.
  • p lag order (integer).
  • c_int integer version of cons_term.

  • n sample length.
  • M number of variables.
  • n_ext_vars number of exogenous variables.
  • K number of right-hand-side terms in each equation.

  • Y a $(n-p) \times M$ data matrix.
  • X a $(n-p) \times K$ data matrix.

  • alpha_hat OLS estimate of $\alpha = \text{vec}(\beta)$.
  • Sigma_hat OLS estimate of $\Sigma$.

  • alpha_pr_mean prior mean of $\alpha = \text{vec}(\beta)$.
  • alpha_pr_var prior covariance matrix of $\alpha = \text{vec}(\beta)$.
  • Sigma_pr_scale prior scale matrix of $\Sigma$.
  • Sigma_pr_dof prior degrees of freedom of $\Sigma$.

  • alpha_pt_mean posterior mean of $\alpha = \text{vec}(\beta)$.
  • alpha_pt_var posterior covariance matrix of $\alpha = \text{vec}(\beta)$.
  • Sigma_pt_mean posterior mean of $\Sigma$.
  • Sigma_pt_dof posterior degrees of freedom of $\Sigma$.

  • beta_draws an array of posterior draws for $\beta$.
  • Sigma_draws an array of posterior draws for $\Sigma$.

Build: two methods,

bvar_obj$build(data_endog,cons_term,p)
bvar_obj$build(data_endog,data_exog,cons_term,p)
  • data_endog a $n \times M$ matrix of endogenous varibles.
  • data_exog a $n \times q$ matrix of exogenous varibles.
  • cons_term a logical value (TRUE/FALSE) indicating the presence of a constant term (intercept) in the model.
  • p lag order (integer).

Prior: two methods,

bvar_obj$prior(coef_prior,HP_1,HP_3,gamma)
bvar_obj$prior(coef_prior,HP_1,HP_3,gamma,full_cov_prior)
  • coef_prior a $M \times 1$ vector containing the prior mean for each first own-lag coefficient.
  • gamma prior degrees of freedom for $\Sigma$.
  • HP_1,HP_3 hyperparameters; see below for more details.
  • full_cov_prior a logical value (TRUE/FALSE) to switch between diagonal or full prior error covariance matrix construction.

Gibbs sampling:

bvar_obj$gibbs(n_draws)
  • n_draws number of posterior draws.

Details


The model:

$$ y_t = \beta_0 + y_{t-1} \beta_1 + \cdots y_{t-p} \beta_p + e_t, \ \ e_t \stackrel{\text{iid}}{\sim} \mathcal{N} \left( \mathbf{0}, \Sigma \right) $$

where $y_t$ is a $1 \times M$ vector. We write this in stacked form as:

$$ Y = X \beta + e $$

Priors take the following form:

\begin{align} p(\Sigma^{-1}) &= \mathcal{W} \left( \frac{1}{\gamma}\Xi_{\Sigma}^{-1} , \gamma \right) \\ p(\alpha | \Sigma) &= \mathcal{N} \left( \text{vec}(\bar{\beta}), \Sigma \otimes \Xi_{\beta} \right) \end{align}

Notes on priors and hyperparameters:

  • $\gamma \geq M + 1$ is set by the gamma input in the prior function;
  • the prior mean of the coefficients, $\bar{\beta}$, is a $K \times M$ matrix of zeros, except for the first-order own-lag terms which are set by coef_prior the prior function.
  • The location matrix $\Xi_{\Sigma}$ is formed in an identical way to a Minnesota prior: as the residual variance from equation-by-equation estimation of AR($p$) models,
  • $$ \Xi_{\Sigma} = \begin{bmatrix} \sigma_{1}^2 &0 & 0 & \cdots & 0 \\ 0& \sigma_2^2 & 0 & \cdots & 0 \\ 0& 0 & \sigma_3^2 & \ddots & 0 \\ \vdots & \vdots & \ddots & \ddots & \vdots \\ 0& 0 & 0 & 0 & \sigma_M^2 \end{bmatrix} $$
  • $\Xi_\beta$ is a diagonal matrix, with elements given in the following order: the first element relates to a constant term (intercept); then $p$-number of $M \times M$ diagonal blocks, one for each lag; and finally a block for any additional exogenous variables (other than a constant term).
    The scaling of these variances is controlled by the hyperparameters HP_1,HP_3, with elements of each block given by:
  • \begin{equation*} \Xi_{\beta_{i,i}} (\ell) = \begin{cases} & H_1 / \left( \ell^2 \cdot \sigma_{i}^2 \right) \\ & H_1 \cdot H_3 \end{cases} \end{equation*} for lags and exogenous variables, respectively.

The conditional posterior kernels are given by

\begin{align} p(\Sigma^{-1} | X,Y) &= \mathcal{W} \left( \gamma \Xi_{\Sigma} + \bar{\beta}^\top \Xi_{\beta}^{-1} \bar{\beta} + Y^\top Y - \widetilde{\beta}^\top \widetilde{\Sigma}_{\beta}^{-1} \widetilde{\beta}, n - p + \gamma \right) \\ p(\alpha | \Sigma,X,Y) &= \mathcal{N} \left( \text{vec}(\widetilde{\beta}), \Sigma \otimes \widetilde{\Sigma}_{\beta} \right) \end{align}

where

\begin{align*} \widetilde{\Sigma}_{\beta}^{-1} &= \Xi_{\beta}^{-1} + X^{\top} X \\ \widetilde{\beta} &= \widetilde{\Sigma}_{\beta} \left( \Xi_{\beta}^{-1} \bar{\beta} + X^\top Y \right) \end{align*}

Computational notes: $\widetilde{\beta}$ and $\widetilde{\Sigma}_{\beta}$ are fixed over posterior draws, and the posterior distribution for $\Sigma$ does not depend on a $\beta$ draw. Thus the sampling steps can be performed in parallel:

  • first, draw many values from the distribution $p(\Sigma^{-1} | X,Y)$; then
  • for each $\Sigma^{(i)}$ draw, sample from $p(\alpha | \Sigma^{(i)},X,Y)$.

Examples


rm(list=ls())
library(BMR)

#

data(BMRVARData)
bvar_data <- data.matrix(USMacroData[,2:4])

#

coef_prior <- c(0.9,0.9,0.9)
HP_1 <- 1/2
HP_3 <- 1
gamma <- 4

bvar_obj <- new(bvarcnw)

#

bvar_obj$build(bvar_data,TRUE,4)
bvar_obj$prior(coef_prior,HP_1,HP_3,gamma)
bvar_obj$gibbs(10000)

IRF(bvar_obj,20,var_names=colnames(bvar_data),save=FALSE)
plot(bvar_obj,var_names=colnames(bvar_data),save=FALSE)
forecast(bvar_obj,shocks=TRUE,var_names=colnames(bvar_data),back_data=10,save=FALSE)