---
title: "Introduction to bvartools"
author: "Franz X. Mohr"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to bvartools}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## Introduction

The package `bvartools` implements functions for Bayesian inference of linear vector autoregressive (VAR) models. It separates a typical BVAR analysis workflow into multiple steps:

* *Model set-up*: Produces data matrices for given lag orders and model types, which can be used for posterior simulation.
* *Prior specification*: Generates prior matrices for a given model.
* *Estimation*: Researchers can choose to use the posterior simulation algorithms of the package or use their own algorithms.
* *Standardising model output*: Combines the output of the estimation step into standardised objects for subsequent steps of the analyis.
* *Evaluation*: Produces summary statistics, forecasts, impulse responses and forecast error variance decompositions.

In each step researchers can fine-tune a model according to their specific requirements or they can simply use the default framework for commonly used models and priors. Since version 0.1.0 the package comes with posterior simulation functions that do not require to implement any further simulation algorithms. For Bayesian inference of *stationary VAR models* the package covers

* Standard BVAR models with independent normal-Wishart priors
* BVAR models employing stochastic search variable selection à la Gerorge, Sun and Ni (2008)
* BVAR models employing Bayesian variable selection à la Korobilis (2013)
* Structural BVAR models, where the structural coefficients are estimated from contemporary endogenous variables (A-model)
* Stochastic volatility (SV) of the errors à la Kim, Shephard and Chip (1998)
* Time varying parameter models (TVP-VAR)

For Bayesian inference of *cointegrated VAR models* the package implements the algorithm of Koop, León-González and Strachan (2010) [KLS] -- which places identification restrictions on the cointegration space -- in the following variants

* The BVEC model as presented in Koop, León-González and Strachan (2010)
* The KLS model employing stochastic search variable selection à la Gerorge, Sun and Ni (2008)
* The KLS modol employing Bayesian variable selection à la Korobilis (2013)
* Structural BVEC models, where the structural coefficients are estimated from contemporaneous endogenous variables (A-model). However, no further restrictions are made regarding the cointegration term.
* Stochastic volatility (SV) of the errors à la Kim, Shephard and Chip (1998)
* Time varying parameter models (TVP-VEC) à la Koop, León-González and Strachan (2011)

For Bayesian inference of *dynamic factor models* the package implements the althorithm used in the textbook of Chan, Koop, Poirer and Tobias (2019).

This introduction to `bvartools` provides the code to set up and estimate a basic Bayesian VAR (BVAR) model.[^further] The first part covers a basic workflow, where the standard posterior simulation algorithm of the package is employed for Bayesian inference. The second part presents a workflow for a posterior algorithm as it could be implemented by a researcher.

For both illustrations the data set E1 from Lütkepohl (2006) is used. It contains data on West German fixed investment, disposable income and consumption expenditures in billions of DM from 1960Q1 to 1982Q4. Like in the textbook only the first 73 observations of the log-differenced series are used.

```{r data, fig.align='center', fig.height=5, fig.width=4.5}
library(bvartools)

# Load data
data("e1")
e1 <- diff(log(e1)) * 100

# Reduce number of oberservations
e1 <- window(e1, end = c(1978, 4))

# Plot the series
plot(e1)
```

## Using `bvartools` with built-in algorithms

### Setting up a model

The `gen_var` function produces an object, which contains information on the specification of the VAR model that should be estimated. The following code specifies a VAR(2) model with an intercept term. The number of iterations and burn-in draws is already specified at this stage.

```{r}
model <- gen_var(e1, p = 2, deterministic = "const",
                 iterations = 5000, burnin = 1000)
```

Note that the function is also capable of generating more than one model. For example, specifying `p = 0:2` would result in three models.

### Adding model priors

Function `add_priors` produces priors for the specified model(s) in object `model` and augments the object accordingly.
  
```{r}
model_with_priors <- add_priors(model,
                                coef = list(v_i = 0, v_i_det = 0),
                                sigma = list(df = 1, scale = .0001))
```

If researchers want to fine-tune individual prior specifications, this can be done by directly accessing the respective elements in object `model_with_priors`.

### Obtaining posterior draws

Function `draw_posterior` can be used to produce posterior draws for a model.

```{r, message=FALSE, warning=FALSE}
bvar_est <- draw_posterior(model_with_priors)
```

If researchers prefer to use their own posterior algorithms, this can be done by specifying argument `FUN` with a function that uses obejct `model_with_priors` as its input. Its output is an object of class `bvar` (see below).

If multiple models should be estimated, the function allows to make use of parallel computing by specifying argument `mc.cores`.

### Inspect posterior draws

Posterior draws can be visually inspected by using the `plot` function. By default, it produces a series of histograms of all estimated coefficients.

```{r, message=FALSE, warning=FALSE, fig.align='center', fig.height=5, fig.width=10}
plot(bvar_est)
```

Alternatively, the trace plot of the post-burnin draws can be draws by adding the argument `type = "trace"`:

```{r, message=FALSE, warning=FALSE, fig.align='center', fig.height=5, fig.width=10}
plot(bvar_est, type = "trace")
```

### Summary statistics

Summary statistics can be obtained in the usual way using the `summary` method.

```{r}
summary(bvar_est)
```

As expected for an algrotihm with uninformative priors the posterior means are fairly close to the results of the frequentist estimator, which can be obtaind in the following way:

```{r}
# Obtain data for LS estimator
y <- t(model$data$Y)
z <- t(model$data$Z)

# Calculate LS estimates
A_freq <- tcrossprod(y, z) %*% solve(tcrossprod(z))

# Round estimates and print
round(A_freq, 3)
```

### Thin results

The MCMC series in object `est_bvar` can be thinned using

```{r}
bvar_est <- thin(bvar_est, thin = 10)
```

### Forecasts

Forecasts with credible bands can be obtained with the function `predict`. If the model contains deterministic terms, new values can be provided in the argument `new_d`. If no values are provided, the function sets them to zero. The number of rows of `new_d` must be the same as the argument `n.ahead`.

```{r forecasts, fig.width=5.5, fig.height=5.5}
bvar_pred <- predict(bvar_est, n.ahead = 10, new_d = rep(1, 10))

plot(bvar_pred)
```

### Impulse response analysis

`bvartools` supports commonly used impulse response functions. See [https://www.r-econometrics.com/timeseries/irf/](https://www.r-econometrics.com/timeseries/irf/) for an introduction.

#### Forecast error impulse response

```{r feir, fig.width=5.5, fig.height=4.5}
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

```{r oir, fig.width=5.5, fig.height=4.5}
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

```{r gir, fig.width=5.5, fig.height=4.5}
GIR <- irf(bvar_est, impulse = "income", response = "cons", n.ahead = 8, type = "gir")

plot(GIR, main = "Generalised Impulse Response", xlab = "Period", ylab = "Response")
```

### Variance decomposition

`bvartools` also supports forecast error variance decomposition (FEVD) and generalised forecast error variance decomposition.

#### Forecast error variance decomposition 

```{r fevd-oir, fig.width=5.5, fig.height=4.5}
bvar_fevd_oir <- fevd(bvar_est, response = "cons")

plot(bvar_fevd_oir, main = "OIR-based FEVD of consumption")
```

#### Generalised forecast error variance decomposition 

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. However, this could be changed by adding `normalise_gir = TRUE` to the function's arguments.

```{r fevd-gir, fig.width=5.5, fig.height=4.5}
bvar_fevd_gir <- fevd(bvar_est, response = "cons", type = "gir")

plot(bvar_fevd_gir, main = "GIR-based FEVD of consumption")
```

## Using `bvartools` with user-written algorithms

`bvartools` was created to assist researchers in building and evaluating their own posterior simulation algorithms for linear BVAR models. Functions `gen_var` and `add_priors` simply help to quickly obtain the relevant data matrices for posterior simulation. Estimation can be done using algortihms, which are usually implemented by the researchers themselves. But once posterior draws are obtained `bvartools` can assist throughout in the following steps of the analysis. In this context the main contributions of the package are:

- Functions `bvar` and `bvec` collect the output of a Gibbs sampler in standardised objects, which can be used for subsequent steps in an analysis.
- Functions such as `predict`, `irf`, `fevd` for forecasting, impulse response analysis and forecast error variance decomposition, respectively, use the output of `bvar` and, hence, researchers do not have to implement them themselves and can save time.
- Computationally intensive functions - such as for posterior simulation - are written in C++ using the `RcppArmadillo` package of Eddelbuettel and Sanderson (2014).[^cpp] This decreases calculation time and makes the code less complex and, thus, less prone to mistakes.

If researchers are willing to rely on the model generation and evaluation functions of `bvartools`, the only remaing step is to illustrate how user specific algorithms can be combined with the functional framework of the package. This is shown in the remainder of this introduction.

### Model set-up and prior specifications

These steps are exactly the same as described above. Thus, the following Gibbs sampler departs from object `model_with_priors` from above.

### A Gibbs sampler algorithm

```{r flat prior}
# Reset random number generator for reproducibility
set.seed(1234567)

# Get data matrices
y <- t(model_with_priors$data$Y)
x <- t(model_with_priors$data$Z)

tt <- ncol(y) # Number of observations
k <- nrow(y) # Number of endogenous variables
m <- k * nrow(x) # Number of estimated coefficients

# Priors for coefficients
a_mu_prior <- model_with_priors$priors$coefficients$mu # Prior means
a_v_i_prior <- model_with_priors$priors$coefficients$v_i # Prior precisions

# Priors for error variance-covariance matrix
u_sigma_df_prior <- model_with_priors$priors$sigma$df # Prior degrees of freedom
u_sigma_scale_prior <- model_with_priors$priors$sigma$scale # Prior covariance matrix
u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom

# Initial values for variance-covariance matrix
u_sigma <- diag(.00001, k)
u_sigma_i <- solve(u_sigma)

# Number of iterations of the Gibbs sampler
iterations <- model_with_priors$model$iterations 
# Number of burn-in draws
burnin <- model_with_priors$model$burnin
# Total number of draws
draws <- iterations + burnin

# Storate for posterior draws
draws_a <- matrix(NA, m, iterations)
draws_sigma <- matrix(NA, k^2, iterations)

# Start Gibbs sampler
for (draw in 1:draws) {
  
  # 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 <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k)
  
  # Store draws
  if (draw > burnin) {
    draws_a[, draw - burnin] <- a
    draws_sigma[, draw - burnin] <- solve(u_sigma_i) # Invert Sigma_i to obtain Sigma
  }
}
```

### `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 functions such as `predict` to obtain forecasts or `irf` for impulse respons analysis.

```{r bvar-object}
bvar_est_two <- bvar(y = model_with_priors$data$Y,
                     x = model_with_priors$data$Z,
                     A = draws_a[1:18,],
                     C = draws_a[19:21, ],
                     Sigma = draws_sigma)
```

Since the output of function `draw_posterior` is an object of class `bvar`, the calculation of summary statistics, forecasts, impulse responses and forecast error variance decompositions is performed as described above.

```{r}
summary(bvar_est_two)
```

## References

Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2019). *Bayesian Econometric Methods* (2nd ed.). Cambridge: University Press.

Eddelbuettel, D., & Sanderson C. (2014). RcppArmadillo: Accelerating R with high-performance C++ linear algebra. *Computational Statistics and Data Analysis, 71*, 1054-1063. <https://doi.org/10.1016/j.csda.2013.02.005>

George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. *Journal of Econometrics, 142*(1), 553-580. <https://doi.org/10.1016/j.jeconom.2007.08.017>

Kim, S., Shephard, N., & Chib, S. (1998). Stochastic volatility: Likelihood inference and comparison with ARCH models. *Review of Economic Studies 65*(3), 361-396.

Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. *Econometric Reviews, 29*(2), 224-242. <https://doi.org/10.1080/07474930903382208>

Koop, G., León-González, R., & Strachan R. W. (2011). Bayesian inference in a time varying cointegration model. *Journal of Econometrics, 165*(2), 210-220. <https://doi.org/10.1016/j.jeconom.2011.07.007>

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>

Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. *Journal of Applied Econometrics, 28*(2), 204-230. <https://doi.org/10.1002/jae.1271>

Lütkepohl, H. (2006). *New introduction to multiple time series analysis* (2nd ed.). Berlin: Springer.

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>

Sanderson, C., & Curtin, R. (2016). Armadillo: a template-based C++ library for linear algebra. *Journal of Open Source Software, 1*(2), 26. <https://doi.org/10.21105/joss.00026>

[^cpp]: `RcppArmadillo` is the `Rcpp` bridge to the open source 'Armadillo' library of Sanderson and Curtin (2016).

[^further]: Further examples about the use of the `bvartools` package are available at <https://www.r-econometrics.com/timeseriesintro/>.