bsvars.org design concept

R packages for Predictive Analyses using Bayesian Structural Vector Autoregressions

\[ \]

by Adam Wang and Tomasz Woźniak

Student’s experience

  • make something useful to the world
  • collaborate and learn from others
  • gain programming experience (especially in C++)
  • adds competency to the CV
  • being a package developer is fun!

the packages and their working

Structural VARs.

  • go-to models for the analysis of policy effects
  • facilitate the analysis of dynamic causal effects of a well-isolated cause
  • perfect for predictive analyses
  • extensively used for: monetary and fiscal policy, financial markets, …
  • extendible:
    • non-normality
    • heteroskedasticity
    • time-varying parameters
    • Bayesian hierarchical modelling

the packages and their working

Structural VARs.

\[ \begin{align} \text{VAR equation: } & & y_t & = \mathbf{A} x_{t} + \epsilon_t \\[1ex] \text{structural equation: } & & \mathbf{B}\epsilon_t & = u_t \\[1ex] \text{structural shocks: } & & u_t |Y_{t-1} & \sim N_N\left(\mathbf{0}_N,\text{diag}\left(\boldsymbol\sigma_t^2\right)\right) \end{align} \]

Features.

  • system dynamic modelling
  • structure of the economy modelled by \(\mathbf{B}\)
  • identified structural shocks \(u_t\)
  • time-varying variances

the packages and their working

Model specification.

sign_irf[, , 1]
     [,1] [,2] [,3] [,4]
[1,]    1   NA   NA   NA
[2,]   NA   NA   NA   NA
[3,]   -1   NA   NA   NA
[4,]    1   NA   NA   NA
sign_structural
     [,1] [,2] [,3] [,4]
[1,]    1   -1   -1    1
[2,]   NA   NA   NA   NA
[3,]   NA   NA   NA   NA
[4,]   NA   NA   NA   NA
# specify the model
spec <- specify_bsvarSIGN$new(
  Y,
  p = 4,
  sign_irf = sign_irf,
  sign_structural = sign_structural
)

the packages and their working

Estimation.

# sample posterior draws
post <- estimate(spec, S = 5000)
**************************************************|
 bsvarSIGNs: Bayesian Structural VAR with sign,   |
             zero and narrative restrictions      |
**************************************************|
 Progress of simulation for 5000 independent draws
 Press Esc to interrupt the computations
**************************************************|

the packages and their working

Forecasting.

post |>
  forecast(h = 4) |>
  plot(data_in_plot = 0.1)

the packages and their working

Forecast error variance decomposition.

post |>
  compute_variance_decompositions(h = 20) |>
  plot()

the packages and their working

Impulse responses.

post |>
  compute_impulse_responses(h = 20) |>
  plot(probability = 0.68)

the packages and their working

Historical Decompositions.

post |>
  compute_historical_decompositions() |>
  plot()
**************************************************|
bsvars: Bayesian Structural Vector Autoregressions|
**************************************************|
 Computing historical decomposition               |
**************************************************|
 This might take a little while :)                
**************************************************|

target audience

  • academic researchers

    • reproducibility, transparency, literature context
  • PhD students

    • communication, extendibility, up-to date with the newest developments
  • master students

    • simple workflows, documentation and examples, video presentations
  • applied economists

    • reliability, speed, incorporating feedback on new features
  • economic governance institutions

    • workflow integration, reporting practices, off-the-shelf applications

bsvars.org design concept

\[ \]

1. combine the best of two worlds:

-speed of algorithms written using compiled code, C++

-convenience of data analysis using interpreted code, R

bsvars.org design concept

\[ \]

2. C++ implementation using Rcpp

  • provide essential functionality for package development
  • manageable dependencies combined with good communication by developers
  • simplicity in package setup/compilation/linking/assuring object compatibility lowering the (perceived) entry requirements
  • great community support
  • super fast on loops (Gibbs sampler in bsvars is a serial job)
  • parallel computations using openMP (independent sampler in bsvarSIGNs)

bsvars.org design concept

\[ \]

3. Essential algorithms rely on Armadillo through RcppArmadillo

  • frontier package for linear algebra (speed, speed, speed)
  • fast and reproducible random number generators
  • excellent documentation and support in package development
  • makes C++ code as expressive as R’s

bsvars.org design concept

\[ \]

4. Delegate computational tasks to C++ code

  • all time intensive tasks are implemented in C++
    • parameter estimation
    • forecasts
    • impulse response functions
    • etc.
  • only user interface and data management is done in R
    • R6 objects for specification
    • R wrapper functions for estimation, forecasting, etc.
    • plotting and summary methods

bsvars.org design concept

\[ \]

5. R6 management of the input object

  • minimal scripting specify_bsvar$new() provides basic setup of:
    • starting values
    • identification
    • data matrices
    • prior hyper-parameters (each of them are lists with R6 structure)
  • ample modelling choices managed by arguments of specify_bsvar$new()
  • possibility of coherent customisation using in functions R6 public elements
  • essential specification variables managed by R6 private elements

bsvars.org design concept

\[ \]

5. R6 management of the input object

  • R6 is R’s object-oriented programming system
  • both spec and spec$prior are R6 objects
  • modify a public field
spec$prior$A[1, 1] <- 0
spec$prior$A
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0
[2,]    0    1    0    0    0    0    0    0    0     0     0     0     0     0
[3,]    0    0    1    0    0    0    0    0    0     0     0     0     0     0
[4,]    0    0    0    1    0    0    0    0    0     0     0     0     0     0
     [,15] [,16] [,17]
[1,]     0     0     0
[2,]     0     0     0
[3,]     0     0     0
[4,]     0     0     0
  • call a public method
spec$prior$estimate_hyper()
**************************************************|
 Adaptive Metropolis MCMC: hyper parameters       |
**************************************************|

bsvars.org design concept

\[ \]

6. Establish a set of generics in bsvars

  • provide model-specific methods in other packages
  • assure similar workflows
  • implement through Depends: and Imports:

bsvars.org design concept

\[ \]

7. S3 methods

  • R’s native object-oriented programming system
  • simple workflows
... |>
  plot()
  • same method for different objects
  • applies to objects generated from both packages bsvars and bsvarSIGNs
bsvars::plot.Forecasts
bsvars::plot.PosteriorFEVD
bsvars::plot.PosteriorIR
bsvars::plot.PosteriorHD

bsvars.org design concept

\[ \]

7. S3 generics and methods

  • simple workflows
us_fiscal_lsuw |>
  specify_bsvar$new(p = 4) |>
  estimate(S = 5000) -> post
  • full transparency on what model is used
bsvars::estimate

bsvars::estimate.BSVAR              bsvars::estimate.PosteriorBSVAR
bsvars::estimate.BSVART             bsvars::estimate.PosteriorBSVART
bsvars::estimate.BSVARMIX           bsvars::estimate.PosteriorBSVARMIX
bsvars::estimate.BSVARMSH           bsvars::estimate.PosteriorBSVARMSH
bsvars::estimate.BSVARSV            bsvars::estimate.PosteriorBSVARSV

bsvarSIGNs::estimate.BSVARSIGN

bsvars.org design concept

\[ \]

8. export all C++ functions in a library

  • for developers to access all code for their R package development using C++ code
  • implement through LinkingTo: and Depends:
  • pointer management
  • assure consistency in object declarations
  • assignment by reference not working across packages

bsvars.org design concept

\[ \]

9. Outputs and workflows to target various users

  • provide all the outputs, posterior draws
    • insightful users can can compute demanded quantities not provided in the packages
  • provide all the basic functionality with well-designed workflows
    • plot and summary methods for users valuing simplicity in scripting
    • It’s amazing how much you do with a few lines of code!

bsvars.org design concept

\[ \]

10. Extensive resources

  • documentation pitched to users of various level of advancement
  • pkgdown website with package features, social media chanels, and updates on the most recent presentations!
  • a vignette to be sent to the Journal of Statistical Software or the R Journal

future developments

\[ \]

bsvars and bsvarSIGNs

  • bsvars include more models non-normal and heteroskedastic
  • bsvars provide computations for more outputs (cummulative IRF and HD, connectedness measures)
  • bsvars include more model verification techniques
  • bsvarSIGNs include COVID modelling
  • bsvarSIGNs implement parallel computations for independence sampler
  • bsvarSIGNs improve hyperparameter estimation
  • bsvarSIGNs implement some user-demanded features
  • bsvars and bsvarSIGNs JSS submissions
  • bsvars and bsvarSIGNs forecasting performance evaluation ??

future developments

\[ \]

upcoming packages

  • bvarPANELs - Bayesian Panel VAR forecasting of labour market outcomes for the International Labour Organization
  • bsvarTVPs with Annika Camehl - Time-varying identification of Structural VARs as an extension to bsvars
  • bsvarCFs with Dan Zhu - Bayesian forecasting with VARs subject to soft and hard restrictions as an extension to bsvars and bsvarSIGNs
  • bvarGIGs with Rui Liu and Andres Ramirez Hassan - Bayesian forecasting with VARs with flexible shrinkage
  • bsmars with Longcan Li - Bayesian forecasting with Matrix Autoregression