BVAR with conditional Normal-inverse-Wishart Prior.
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.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
input in the prior function;coef_prior
the prior function.HP_1,HP_3
, with elements of each block given by: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:
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)