nk027 / bvar

Toolkit for the estimation of hierarchical Bayesian vector autoregressions. Implements hierarchical prior selection for conjugate priors in the fashion of Giannone, Lenza & Primiceri (2015). Allows for the computation of impulse responses and forecasts and provides functionality for assessing results.
https://cran.r-project.org/package=BVAR
Other
48 stars 21 forks source link

Historical decomposition with varying identification #92

Open nk027 opened 3 months ago

nk027 commented 3 months ago

The recently introduced function to compute historical decompositions is a bit lazy – it always defaults to identification via Cholesky (thanks to Rapolas for pointing this out). This should be an easy extension – for sign-restrictions something like this should work:

hist_decomp_sr <- function(x, type = c("mean", "quantile"), sign_restr, ...) {

  type <- match.arg(type)

  # Compare this to the IRF function:
  shock <- matrix(0, x[["meta"]][["K"]] - 1, x[["meta"]][["M"]])
  shock[seq(x[["meta"]][["M"]]), ] <- t(chol(vcov(x, type = type)))

  # Now we need an extra step for the sign restriction:
  shock[seq(x[["meta"]][["M"]]), ] <- BVAR:::sign_restr(
    sigma_chol = shock, sign_restr = sign_restr, M = x[["meta"]][["M"]])
  # (BVAR::: is needed to access the internal function)

  # And proceed as before:
  comp <- companion(x, type = type)
  eps <- solve(shock[seq(x[["meta"]][["M"]]), ], t(residuals(x, type = type)))

  # This will also be an internal function
  out <- BVAR:::compute_hd(x, shock, comp, eps)

  return(out)
}

Of course, the actual API should include this as an option instead of exposing another function.

https://github.com/nk027/bvar/blob/6919c2fbea6625c8c870a71a35f7b376a0a95474/R/70_hist_decomp.R#L30