R/compute_historical_decompositions.R

Defines functions print.PosteriorHD compute_historical_decompositions.PosteriorBSVART compute_historical_decompositions.PosteriorBSVARSV compute_historical_decompositions.PosteriorBSVARMIX compute_historical_decompositions.PosteriorBSVARHMSH compute_historical_decompositions.PosteriorBSVARMSH compute_historical_decompositions.PosteriorBSVAREXH compute_historical_decompositions.PosteriorBSVAR compute_historical_decompositions

Documented in compute_historical_decompositions compute_historical_decompositions.PosteriorBSVAR compute_historical_decompositions.PosteriorBSVAREXH compute_historical_decompositions.PosteriorBSVARHMSH compute_historical_decompositions.PosteriorBSVARMIX compute_historical_decompositions.PosteriorBSVARMSH compute_historical_decompositions.PosteriorBSVARSV compute_historical_decompositions.PosteriorBSVART

#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome obtained by running the \code{estimate} 
#' function. The interpretation depends on the normalisation of the shocks
#' using function \code{normalise()}. Verify if the default settings 
#' are appropriate.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me} and Xiaolei Wang \email{adamwang15@gmail.com}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar$new(diff(us_fiscal_lsuw))
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd            = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' diff(us_fiscal_lsuw) |>
#'   specify_bsvar$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hd
#' 
#' @export
compute_historical_decompositions <- function(posterior, show_progress = TRUE) {
  stopifnot("Argument show_progress must be a logical value." = is.logical(show_progress))
  UseMethod("compute_historical_decompositions", posterior)
}







#' @method compute_historical_decompositions PosteriorBSVAR
#' 
#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVAR} obtained by running the \code{estimate} function.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar$new(diff(us_fiscal_lsuw))
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd            = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' diff(us_fiscal_lsuw) |>
#'   specify_bsvar$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hd
#'   
#' @export
compute_historical_decompositions.PosteriorBSVAR <- function(posterior, show_progress = TRUE) {
  
  posterior_B     = posterior$posterior$B
  posterior_A     = posterior$posterior$A
  
  Y               = posterior$last_draw$data_matrices$Y
  X               = posterior$last_draw$data_matrices$X
  
  N               = nrow(Y)
  T               = ncol(Y)
  p               = posterior$last_draw$p
  S               = dim(posterior_A)[3]
  
  ss              = .Call(`_bsvars_bsvars_structural_shocks`, posterior_B, posterior_A, Y, X)
  ir              = .Call(`_bsvars_bsvars_ir`, posterior_B, posterior_A, T, p, TRUE)
  qqq             = .Call(`_bsvars_bsvars_hd`, ir, ss, show_progress)
  
  hd              = array(NA, c(N, N, T, S), dimnames = list(rownames(Y), rownames(Y), colnames(Y), 1:S))
  for (s in 1:S) hd[,,,s] = qqq[s][[1]]
  class(hd)       = "PosteriorHD"
  
  return(hd)
}






#' @method compute_historical_decompositions PosteriorBSVAREXH
#' 
#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVAREXH} obtained by running the \code{estimate} function.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar_exh$new(us_fiscal_lsuw)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd             = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_exh$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hds
#'   
#' @export
compute_historical_decompositions.PosteriorBSVAREXH <- function(posterior, show_progress = TRUE) {
  
  posterior_B     = posterior$posterior$B
  posterior_A     = posterior$posterior$A
  
  Y               = posterior$last_draw$data_matrices$Y
  X               = posterior$last_draw$data_matrices$X
  
  N               = nrow(Y)
  T               = ncol(Y)
  p               = posterior$last_draw$p
  S               = dim(posterior_A)[3]
  
  ss              = .Call(`_bsvars_bsvars_structural_shocks`, posterior_B, posterior_A, Y, X)
  ir              = .Call(`_bsvars_bsvars_ir`, posterior_B, posterior_A, T, p, TRUE)
  qqq             = .Call(`_bsvars_bsvars_hd`, ir, ss, show_progress)
  
  hd              = array(NA, c(N, N, T, S), dimnames = list(rownames(Y), rownames(Y), colnames(Y), 1:S))
  for (s in 1:S) hd[,,,s] = qqq[s][[1]]
  class(hd)       = "PosteriorHD"
  
  return(hd)
}






#' @method compute_historical_decompositions PosteriorBSVARMSH
#' 
#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVARMSH} obtained by running the \code{estimate} function.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar_msh$new(us_fiscal_lsuw, M = 2)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd             = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_msh$new(M = 2) |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hds
#'   
#' @export
compute_historical_decompositions.PosteriorBSVARMSH <- function(posterior, show_progress = TRUE) {

  posterior_B     = posterior$posterior$B
  posterior_A     = posterior$posterior$A

  Y               = posterior$last_draw$data_matrices$Y
  X               = posterior$last_draw$data_matrices$X

  N               = nrow(Y)
  T               = ncol(Y)
  p               = posterior$last_draw$p
  S               = dim(posterior_A)[3]

  ss              = .Call(`_bsvars_bsvars_structural_shocks`, posterior_B, posterior_A, Y, X)
  ir              = .Call(`_bsvars_bsvars_ir`, posterior_B, posterior_A, T, p, TRUE)
  qqq             = .Call(`_bsvars_bsvars_hd`, ir, ss, show_progress)

  hd              = array(NA, c(N, N, T, S), dimnames = list(rownames(Y), rownames(Y), colnames(Y), 1:S))
  for (s in 1:S) hd[,,,s] = qqq[s][[1]]
  class(hd)       = "PosteriorHD"

  return(hd)
}





#' @method compute_historical_decompositions PosteriorBSVARHMSH
#' 
#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVARHMSH} obtained by running the \code{estimate} function.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar_hmsh$new(us_fiscal_lsuw)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd             = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_hmsh$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hds
#'   
#' @export
compute_historical_decompositions.PosteriorBSVARHMSH <- function(posterior, show_progress = TRUE) {
  
  posterior_B     = posterior$posterior$B
  posterior_A     = posterior$posterior$A
  
  Y               = posterior$last_draw$data_matrices$Y
  X               = posterior$last_draw$data_matrices$X
  
  N               = nrow(Y)
  T               = ncol(Y)
  p               = posterior$last_draw$p
  S               = dim(posterior_A)[3]
  
  ss              = .Call(`_bsvars_bsvars_structural_shocks`, posterior_B, posterior_A, Y, X)
  ir              = .Call(`_bsvars_bsvars_ir`, posterior_B, posterior_A, T, p, TRUE)
  qqq             = .Call(`_bsvars_bsvars_hd`, ir, ss, show_progress)
  
  hd              = array(NA, c(N, N, T, S), dimnames = list(rownames(Y), rownames(Y), colnames(Y), 1:S))
  for (s in 1:S) hd[,,,s] = qqq[s][[1]]
  class(hd)       = "PosteriorHD"
  
  return(hd)
}





#' @method compute_historical_decompositions PosteriorBSVARMIX
#' 
#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVARMIX} obtained by running the \code{estimate} function.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar_mix$new(us_fiscal_lsuw, M = 2)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd             = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_mix$new(M = 2) |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hds
#'   
#' @export
compute_historical_decompositions.PosteriorBSVARMIX <- function(posterior, show_progress = TRUE) {
  
  posterior_B     = posterior$posterior$B
  posterior_A     = posterior$posterior$A
  
  Y               = posterior$last_draw$data_matrices$Y
  X               = posterior$last_draw$data_matrices$X
  
  N               = nrow(Y)
  T               = ncol(Y)
  p               = posterior$last_draw$p
  S               = dim(posterior_A)[3]
  
  ss              = .Call(`_bsvars_bsvars_structural_shocks`, posterior_B, posterior_A, Y, X)
  ir              = .Call(`_bsvars_bsvars_ir`, posterior_B, posterior_A, T, p, TRUE)
  qqq             = .Call(`_bsvars_bsvars_hd`, ir, ss, show_progress)
  
  hd              = array(NA, c(N, N, T, S), dimnames = list(rownames(Y), rownames(Y), colnames(Y), 1:S))
  for (s in 1:S) hd[,,,s] = qqq[s][[1]]
  class(hd)       = "PosteriorHD"
  
  return(hd)
}





#' @method compute_historical_decompositions PosteriorBSVARSV
#' 
#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVARSV} obtained by running the \code{estimate} function.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar_sv$new(us_fiscal_lsuw, p = 1)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd             = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_sv$new(p = 1) |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hds
#'   
#' @export
compute_historical_decompositions.PosteriorBSVARSV <- function(posterior, show_progress = TRUE) {
  
  posterior_B     = posterior$posterior$B
  posterior_A     = posterior$posterior$A
  
  Y               = posterior$last_draw$data_matrices$Y
  X               = posterior$last_draw$data_matrices$X
  
  N               = nrow(Y)
  T               = ncol(Y)
  p               = posterior$last_draw$p
  S               = dim(posterior_A)[3]
  
  ss              = .Call(`_bsvars_bsvars_structural_shocks`, posterior_B, posterior_A, Y, X)
  ir              = .Call(`_bsvars_bsvars_ir`, posterior_B, posterior_A, T, p, TRUE)
  qqq             = .Call(`_bsvars_bsvars_hd`, ir, ss, show_progress)
  
  hd              = array(NA, c(N, N, T, S), dimnames = list(rownames(Y), rownames(Y), colnames(Y), 1:S))
  for (s in 1:S) hd[,,,s] = qqq[s][[1]]
  class(hd)       = "PosteriorHD"
  
  return(hd)
}







#' @method compute_historical_decompositions PosteriorBSVART
#' 
#' @title Computes posterior draws of historical decompositions
#'
#' @description Each of the draws from the posterior estimation of models from
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into
#' a draw from the posterior distribution of the historical decompositions. 
#' IMPORTANT! The historical decompositions are interpreted correctly for 
#' covariance stationary data. Application to unit-root non-stationary data might
#' result in non-interpretable outcomes.
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVART} obtained by running the \code{estimate} function.
#' @param show_progress a logical value, if \code{TRUE} the estimation progress bar is visible
#' 
#' @return An object of class \code{PosteriorHD}, that is, an \code{NxNxTxS} array 
#' with attribute \code{PosteriorHD} containing \code{S} draws of the historical 
#' decompositions.
#'
#' @seealso \code{\link{estimate}}, \code{\link{normalise}}, \code{\link{summary}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @references 
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press.
#' 
#' @examples
#' specification  = specify_bsvar_t$new(diff(us_fiscal_lsuw))
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute historical decompositions
#' hd            = compute_historical_decompositions(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' diff(us_fiscal_lsuw) |>
#'   specify_bsvar_t$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_historical_decompositions() -> hd
#'   
#' @export
compute_historical_decompositions.PosteriorBSVART <- function(posterior, show_progress = TRUE) {
  
  posterior_B     = posterior$posterior$B
  posterior_A     = posterior$posterior$A
  
  Y               = posterior$last_draw$data_matrices$Y
  X               = posterior$last_draw$data_matrices$X
  
  N               = nrow(Y)
  T               = ncol(Y)
  p               = posterior$last_draw$p
  S               = dim(posterior_A)[3]
  
  ss              = .Call(`_bsvars_bsvars_structural_shocks`, posterior_B, posterior_A, Y, X)
  ir              = .Call(`_bsvars_bsvars_ir`, posterior_B, posterior_A, T, p, TRUE)
  qqq             = .Call(`_bsvars_bsvars_hd`, ir, ss, show_progress)
  
  hd              = array(NA, c(N, N, T, S), dimnames = list(rownames(Y), rownames(Y), colnames(Y), 1:S))
  for (s in 1:S) hd[,,,s] = qqq[s][[1]]
  class(hd)       = "PosteriorHD"
  
  return(hd)
}


#' @export
print.PosteriorHD <- function(x, ...) {
  cat("Posterior draws of historical decompositions\n")
  cat("An array of dimensions:", paste(dim(x), collapse = " x "), "\n")
  invisible(x)
}

Try the bsvars package in your browser

Any scripts or data that you put into this service are public.

bsvars documentation built on Aug. 22, 2026, 5:09 p.m.