An Introduction to Bayesian VAR (BVAR) Models
with tags r bvar var bayes bayesian-var bvartools gibbs-samplerBayesian methods have significantly gained in popularity during the last decades as computers have become more powerful and new software has been developed. Their flexibility and other advantageous features have made these methods also more popular in econometrics. This post gives a brief introduction to Bayesian VAR (BVAR) models and provides the code to set up and estimate a basic model with the bvartools package.
BVAR models
Bayesian VAR (BVAR) models have the same mathematical form as any other VAR model, i.e.
\[ y_t = c + \sum_{l=i}^{p} A_i y_{t-i} + \epsilon_t,\] where \(y_t\) is a \(K \times 1\) vector of endogenous variables in period \(t\), \(A_i\) is the cofficient matrix corresponding to the \(i\)th lag of \(y_t\), \(c\) is a constant deterministic term and \(\epsilon_t\) is an error term with zero mean and variance-covariance \(\Sigma\).
The only difference between usual VAR models and BVAR models is the way parameter estimates are obtained and interpreted. VAR models are usually estimated by OLS, which is a simple and computationally fast estimator. By contrast, Bayesian estimators are slightly more complicated and more burdensome in terms of algebra and calculation power. The coefficients obtained by so-called frequentist estimators like OLS are interpreted based on the concept of the sampling distribution. In Bayesian inference, the coefficients are assumed to have their own distribution. A more detailed treatment of the difference between frequentist and Bayesian inference can be found in Kennedy (2008, ch. 14), which provides a short introduction to the Bayesian approach and a series of references for interested readers. Koop and Korobilis (2010) provide a very good introduction to Bayesian VAR estimators.
As already mentioned, Bayesian inference can be algebraically demanding. However, Bayesian estimators for linear VAR models can be implemented in a straightforward manner. A standard implementation is a so-called Gibbs sampler, which belongs to the family of Markov-Chain-Monte-Carlo (MCMC) methods. A detailed treatment of this method is beyond the scope of this post, but Wikipedia might be a good start to become familiar with it. Personally, I like to think of the Gibbs sampler as throwing a bunch of random numbers at a model and see what sticks. The remainer of this text provides the code to set up and estimate a basic BVAR model with the bvartools package.
Model and data
For this illustration the dataset E1 from Lütkepohl (2007) is used. It contains data on West German fixed investment, disposable income and consumption expenditures in billions of DM from 1960Q1 to 1982Q4. Following Lütkepohl (2007) the VAR model has two lags, i.e. \(p = 2\) and only the first 73 observations are used for inference.
library("bvartools")
data("e1") # Load the data
e1 <- diff(log(e1)) # Calculate first-log-differences
# Reduce number of oberservations
e1 <- window(e1, end = c(1978, 4))
Basically, it would be possible to proceed without any further transformation of the data. However, it is good practise to multiply log-differenced data by 100 so that, for example, the value 1% is indicated by 1 and not by 0.01.
e1 <- e1 * 100 # Rescale data
plot(e1) # Plot the series

To assist with the set-up of the model the create_bvarmodel function produces an object of class bvarmodel, which contains the specification of the model and its data matrices. Element data$train contains the inputs for the estimator, where y is a matrix of dependent variables and x is the matrix of regressors for the model
\[y_t = A x_t + u_t,\] with \(u_t \sim N(0, \Sigma)\). This is a more compact form of the model above, where the lags of the endogenous variables and the constant are already included in \(x_t\). Both matrices contain one row per period. Since the formulas below are written for matrices with one column per period, they are transposed.
model <- create_bvarmodel(e1, p = 2, deterministic = "const")
y <- t(model[["data"]][["train"]][["y"]])
x <- t(model[["data"]][["train"]][["x"]])
Estimation
Frequentist estimator
We calculate frequentist VAR estimates using the standard formula \(y x' (x x')^{-1}\) to obtain a benchmark for the Bayesian estimator. The parameters are obtained by OLS:
a_freq <- tcrossprod(y, x) %*% solve(tcrossprod(x)) # Calculate estimates
round(a_freq, 3) # Round estimates and print
## invest.l1 income.l1 cons.l1 invest.l2 income.l2 cons.l2 const
## invest -0.320 0.146 0.961 -0.161 0.115 0.934 -1.672
## income 0.044 -0.153 0.289 0.050 0.019 -0.010 1.577
## cons -0.002 0.225 -0.264 0.034 0.355 -0.022 1.293
And \(\Sigma\) is calculated by
u_freq <- y - a_freq %*% x
u_sigma_freq <- tcrossprod(u_freq) / (ncol(y) - nrow(x))
round(u_sigma_freq, 2)
## invest income cons
## invest 21.30 0.72 1.23
## income 0.72 1.37 0.61
## cons 1.23 0.61 0.89
These are virtually the same values as in Lütkepohl (2007, Ch. 3). The only differences stem from the re-scaling of the data above, which results in proportionally higher values for the const variable and does not require us to multiply the covariance matrix by \(10^4\).
Bayesian estimator
The following code is a Gibbs sampler for a simple VAR model with non-informative priors. First, we define some variables, which help in the set-up of the sampler:
# Reset random number generator for reproducibility
set.seed(1234567)
iter <- 30000 # Number of iterations of the Gibbs sampler
burnin <- 15000 # Number of burn-in draws
store <- iter - burnin
tt <- ncol(y) # Number of observations
k <- nrow(y) # Number of endogenous variables
m <- k * nrow(x) # Number of estimated coefficients
Next, we set the priors. The prior means of the coefficient vector \(a = vec(A)\) are set to \(0\) and the diagonal elements of the corresponding covariance matrix \(V\) are set to \(1\), except for coefficients corresponding to intercept terms, which are set to \(10\). The prior degrees of freedom of the error term are set to \(6\) and the diagonal elements of the scale matrix to \(1\).
Note that the choice of the prior variances for coefficients, which correspond to intercept terms, can be motivated by the values of the analysed time series. For example, if a stationary series seems to move around the value \(5\), this could be taken into account by setting the prior mean of the respective coefficients to \(5\) with a small prior variance around it. If the researcher still prefers to set the mean to \(0\), it should be ensured that the prior variance is large enough so that the value \(5\) is captured by the prior distribution.
Note that the choice of the prior variances of the errors can be highly influential if the scale of the errors is not taken into account appropriately. For example, if the dependent variable does exceed values between \(-0.1\) and \(0.1\), a prior of \(1\) would be too high and could lead to useless results.
Function add_priors adds these specifications to the model object, from where they are taken for the sampler. Argument v_i sets the prior precision, i.e. the inverse of the prior variance, of the coefficients and v_i_det that of the coefficients of deterministic terms. Argument df sets the prior degrees of freedom of the error covariance matrix and scale the diagonal elements of its scale matrix.
# Set priors
model <- add_priors(model,
coef = list(v_i = 1, v_i_det = 1 / 10),
sigma = list(df = 6, scale = 1))
# Vector of prior parameter means
a_mu_prior <- model[["priors"]][["a"]][["mu"]]
# Inverse of the prior covariance matrix
a_v_i_prior <- model[["priors"]][["a"]][["v_inv"]]
# Prior degrees of freedom
u_sigma_df_prior <- model[["priors"]][["u_sigma"]][["df"]]
# Prior covariance matrix
u_sigma_scale_prior <- model[["priors"]][["u_sigma"]][["scale"]]
# Posterior degrees of freedom
u_sigma_df_post <- tt + u_sigma_df_prior
Note that we could also use uninformative priors, where all elements of the inverse prior covariance matrix \(V^{-1}\) are set to zero and the prior degrees of freedom and the elements of the scale matrix are set to 0 as well. Such a specification should lead to posterior draws, which mimic the results of a frequentist OLS estimator.
Then we obtain some starting values. It might be a good idea to start with the OLS estimates of the model.
# Initial values
u_sigma_i <- solve(u_sigma_freq)
Next, create object, in which the posterior draws are saved for later.
# Data containers for posterior draws
draws_a <- matrix(NA, m, store)
draws_sigma <- matrix(NA, k * k, store)
Finally run the Gibbs sampler.
# Start Gibbs sampler
for (draw in 1:iter) {
# Draw conditional mean parameters
a <- post_normal(y, x, u_sigma_i, a_mu_prior, a_v_i_prior)
# Draw variance-covariance matrix
u <- y - matrix(a, k) %*% x # Obtain residuals
u_sigma_scale_post <- solve(u_sigma_scale_prior + tcrossprod(u))
u_sigma_i <- rWishart(1, u_sigma_df_post, u_sigma_scale_post)[, , 1]
u_sigma_i <- matrix(u_sigma_i, k)
u_sigma <- solve(u_sigma_i) # Invert Sigma_i to obtain Sigma
# Store draws
if (draw > burnin) {
draws_a[, draw - burnin] <- a
draws_sigma[, draw - burnin] <- u_sigma
}
}
After the Gibbs sampler has finished, point estimates for the coefficient matrix can be obtained as, e.g., the mean of the posterior draws:
a_mean <- rowMeans(draws_a) # Obtain means for every row
a_mean <- matrix(a_mean, k) # Transform mean vector into a matrix
a_mean <- round(a_mean, 3) # Round values
# Rename matrix dimensions
dimnames(a_mean) <- list(dimnames(y)[[1]], dimnames(x)[[1]])
a_mean # Print
## invest.l1 income.l1 cons.l1 invest.l2 income.l2 cons.l2 const
## invest -0.288 0.232 0.637 -0.144 0.201 0.604 -0.780
## income 0.045 -0.146 0.279 0.050 0.023 -0.014 1.577
## cons -0.001 0.228 -0.276 0.035 0.357 -0.032 1.320
Point estimates for the covariance matrix can also be obtained by calculating the means of the posterior draws.
sigma_mean <- rowMeans(draws_sigma) # Obtain means for every row
sigma_mean <- matrix(sigma_mean, k) # Transform mean vector into a matrix
sigma_mean <- round(sigma_mean, 2) # Round values
# Rename matrix dimensions
dimnames(sigma_mean) <- list(dimnames(y)[[1]], dimnames(y)[[1]])
sigma_mean # Print
## invest income cons
## invest 20.49 0.68 1.18
## income 0.68 1.34 0.59
## cons 1.18 0.59 0.88
The means of the coefficient draws are relatively close to the results of the frequentist estimatior. However, note that the frequentist estimator suggests coefficients, which are close to \(1\) for the relationship between investment and consumption. By contrast, the Bayesian estimator implies different point estimates, which might be a result of the rather close prior variance of \(1\) for those parameters. As mentioned above, using a non-informative prior will lead to results that are closer to the OLS estimates.
bvar objects
The bvar function can be used to collect relevant output of the Gibbs sampler into a standardised object, which can be used by further functions such as predict to obtain forecasts or irf for impulse respons analysis.
bvar_est <- bvar(y = model[["data"]][["train"]][["y"]],
x = model[["data"]][["train"]][["x"]],
A = draws_a[1:18, ],
C = draws_a[19:21, ],
Sigma = draws_sigma)
There is also a summary function, which calculates some summary statistics.
summary(bvar_est)
##
## Bayesian VAR model with p = 2
##
## Endogenous variables: invest, income, cons
##
## Variable: invest
##
## Mean SD Naive SD Time-series SD 2.5% 50% 97.5%
## invest.l1 -0.2882 0.1207 0.0009857 0.0009857 -0.5260 -0.2880 -0.04813 *
## income.l1 0.2315 0.4490 0.0036664 0.0036664 -0.6472 0.2326 1.10483
## cons.l1 0.6366 0.5178 0.0042280 0.0042280 -0.3717 0.6386 1.64417
## invest.l2 -0.1435 0.1216 0.0009927 0.0009766 -0.3835 -0.1445 0.09648
## income.l2 0.2013 0.4414 0.0036039 0.0036039 -0.6607 0.1976 1.07119
## cons.l2 0.6045 0.5164 0.0042162 0.0042162 -0.4180 0.6028 1.61097
## const -0.7796 1.3981 0.0114153 0.0114153 -3.4729 -0.7865 1.97095
##
## Variable: income
##
## Mean SD Naive SD Time-series SD 2.5% 50% 97.5%
## invest.l1 0.04465 0.03154 0.0002576 0.0002450 -0.01705 0.04462 0.1060
## income.l1 -0.14554 0.13449 0.0010981 0.0011132 -0.41227 -0.14566 0.1188
## cons.l1 0.27906 0.16143 0.0013181 0.0013181 -0.03807 0.27888 0.5958
## invest.l2 0.05020 0.03156 0.0002577 0.0002577 -0.01252 0.05016 0.1126
## income.l2 0.02274 0.13067 0.0010669 0.0010706 -0.23450 0.02275 0.2799
## cons.l2 -0.01448 0.16243 0.0013262 0.0013162 -0.33315 -0.01417 0.3012
## const 1.57684 0.42171 0.0034432 0.0034432 0.74462 1.57289 2.3935 *
##
## Variable: cons
##
## Mean SD Naive SD Time-series SD 2.5% 50% 97.5%
## invest.l1 -0.001072 0.02552 0.0002084 0.0002084 -0.05142 -0.001047 0.04879
## income.l1 0.228043 0.10796 0.0008815 0.0009244 0.01625 0.227852 0.43859 *
## cons.l1 -0.275684 0.12989 0.0010605 0.0010749 -0.52967 -0.275852 -0.02394 *
## invest.l2 0.034606 0.02533 0.0002068 0.0002068 -0.01515 0.034777 0.08391
## income.l2 0.357030 0.10450 0.0008532 0.0008532 0.15215 0.357227 0.56453 *
## cons.l2 -0.032052 0.13024 0.0010634 0.0010634 -0.28863 -0.032660 0.22055
## const 1.319548 0.33675 0.0027496 0.0027496 0.64900 1.316347 1.98316 *
##
## Variance-covariance matrix:
##
## Mean SD Naive SD Time-series SD 2.5% 50% 97.5%
## invest_invest 20.4883 3.5212 0.028750 0.030671 14.7117 20.0677 28.4506 *
## invest_income 0.6766 0.6424 0.005245 0.005652 -0.5480 0.6579 2.0001
## invest_cons 1.1789 0.5321 0.004345 0.004674 0.2242 1.1501 2.3087 *
## income_income 1.3439 0.2342 0.001912 0.002105 0.9622 1.3177 1.8733 *
## income_cons 0.5923 0.1509 0.001232 0.001347 0.3364 0.5781 0.9256 *
## cons_cons 0.8765 0.1526 0.001246 0.001379 0.6252 0.8611 1.2210 *
Posterior draws can be thinned with the function thin:
bvar_est <- thin(bvar_est, thin = 15)
Forecasts
Forecasts are obtained in two steps. First, function add_forecast_input generates the data of the forecast periods. Values of deterministic terms can be provided in its argument deterministic. If they are not provided, the function tries to obtain them from the model. Second, function add_posterior_forecasts simulates the forecasts. Forecasts with credible bands can then be extracted with the function predict.
bvar_est <- add_forecast_input(bvar_est, n_ahead = 10)
bvar_est <- add_posterior_forecasts(bvar_est)
bvar_pred <- predict(bvar_est, n_ahead = 10)
plot(bvar_pred, n_pre = 20)



Impulse response analysis
Currently, bvartools supports forecast error, orthogonalised, and generalised impulse response functions.
Forecast error impulse response
feir <- irf(bvar_est, impulse = "income", response = "cons", n_ahead = 8)
plot(feir,
main = "Forecast Error Impulse Response",
xlab = "Period",
ylab = "Response")

Orthogonalised impulse response
oir <- irf(bvar_est,
impulse = "income",
response = "cons",
n_ahead = 8,
type = "oir")
plot(oir,
main = "Orthogonalised Impulse Response",
xlab = "Period",
ylab = "Response")

Generalised impulse response
gir <- irf(bvar_est,
impulse = "income",
response = "cons",
n_ahead = 8,
type = "gir")
plot(gir,
main = "Generalised Impulse Response",
xlab = "Period",
ylab = "Response")

Forecast error variance decomposition
Default forecast error variance decomposition (FEVD) is based on orthogonalised impulse responses (OIR).
bvar_fevd_oir <- fevd(bvar_est, response = "cons")
plot(bvar_fevd_oir, main = "OIR-based FEVD of consumption")

It is also possible to calculate FEVDs, which are based on generalised impulse responses (GIR). Note that these do not automatically add up to unity.
bvar_fevd_gir <- fevd(bvar_est, response = "cons", type = "gir")
plot(bvar_fevd_gir, main = "GIR-based FEVD of consumption")

References
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2019). Bayesian Econometric Methods (2nd ed.). Cambridge: University Press.
Koop, G., & Korobilis, D. (2010). Bayesian multivariate time series methods for empirical macroeconomics. Foundations and trends in econometrics, 3(4), 267-358. https://dx.doi.org/10.1561/0800000013
Koop, G., Pesaran, M. H., & Potter, S.M. (1996). Impulse response analysis in nonlinear multivariate models. Journal of Econometrics 74(1), 119-147. https://doi.org/10.1016/0304-4076(95)01753-4
Lütkepohl, H. (2007). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Kennedy, P. (2008). A guide to econometrics (6th ed.) Malden, MA: Blackwell.
Pesaran, H. H., & Shin, Y. (1998). Generalized impulse response analysis in linear multivariate models. Economics Letters, 58(1), 17-29. https://doi.org/10.1016/S0165-1765(97)00214-0