-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
included compute_impulse_responses #25
+ updated cpp function and... + moved it from bsvarTOOLs + R method + docu + tests
- Loading branch information
1 parent
b4faf0e
commit 5bb6248
Showing
11 changed files
with
378 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,4 +118,76 @@ compute_fitted_values.PosteriorBSVARSIGN <- function(posterior) { | |
class(fv) = "PosteriorFitted" | ||
|
||
return(fv) | ||
} | ||
} # END compute_fitted_values.PosteriorBSVARSIGN | ||
|
||
|
||
|
||
|
||
|
||
#' @title Computes posterior draws of impulse responses | ||
#' | ||
#' @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 impulse responses. | ||
#' | ||
#' @method compute_impulse_responses PosteriorBSVARSIGN | ||
#' | ||
#' @param posterior posterior estimation outcome - an object of class | ||
#' \code{PosteriorBSVARSIGN} obtained by running the \code{estimate} function. | ||
#' @param horizon a positive integer number denoting the forecast horizon for the impulse responses computations. | ||
#' @param standardise a logical value. If \code{TRUE}, the impulse responses are standardised | ||
#' so that the variables' own shocks at horizon 0 are equal to 1. Otherwise, the parameter estimates | ||
#' determine this magnitude. | ||
#' | ||
#' @return An object of class PosteriorIR, that is, an \code{NxNx(horizon+1)xS} array with attribute PosteriorIR | ||
#' containing \code{S} draws of the impulse responses. | ||
#' | ||
#' @seealso \code{\link{estimate}}, \code{\link{summary}}, \code{\link{plot}} | ||
#' | ||
#' @author Xiaolei Wang \email{[email protected]} and Tomasz Woźniak \email{[email protected]} | ||
#' | ||
#' @references | ||
#' Kilian, L., & Lütkepohl, H. (2017). Structural VAR Tools, Chapter 4, In: Structural vector autoregressive analysis. Cambridge University Press. | ||
#' | ||
#' @examples | ||
#' # upload data | ||
#' data(oil) | ||
#' | ||
#' # specify the model and set seed | ||
#' set.seed(123) | ||
#' sign_irf = array(matrix(c(-1, -1, 1, rep(0, 6)), nrow = 3), dim = c(3, 3, 1)) | ||
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf) | ||
#' | ||
#' # run the burn-in | ||
#' posterior = estimate(specification, 10) | ||
#' | ||
#' # compute impulse responses 2 years ahead | ||
#' irf = compute_impulse_responses(posterior, horizon = 8) | ||
#' | ||
#' # workflow with the pipe |> | ||
#' ############################################################ | ||
#' set.seed(123) | ||
#' oil |> | ||
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |> | ||
#' estimate(S = 10) |> | ||
#' compute_impulse_responses(horizon = 8) -> ir | ||
#' | ||
#' | ||
#' @export | ||
compute_impulse_responses.PosteriorBSVARSIGN <- function(posterior, horizon, standardise = FALSE) { | ||
|
||
posterior_Theta0 = posterior$posterior$Theta0 | ||
posterior_A = posterior$posterior$A | ||
posterior_A = aperm(posterior_A, c(2, 1, 3)) | ||
N = dim(posterior_A)[2] | ||
p = posterior$last_draw$p | ||
S = dim(posterior_A)[3] | ||
|
||
qqq = .Call(`_bsvarSIGNs_bsvarSIGNs_ir`, posterior_A, posterior_Theta0, horizon, p, standardise) | ||
|
||
irfs = array(NA, c(N, N, horizon + 1, S)) | ||
for (s in 1:S) irfs[,,,s] = qqq[s][[1]] | ||
class(irfs) = "PosteriorIR" | ||
|
||
return(irfs) | ||
} # END compute_impulse_responses.PosteriorBSVARSIGN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
data(oil) | ||
|
||
set.seed(1) | ||
suppressMessages( | ||
specification_no1 <- specify_bsvarSIGN$new(oil) | ||
) | ||
run_no1 <- estimate(specification_no1, 3, 1, show_progress = FALSE) | ||
irf <- compute_impulse_responses(run_no1, horizon = 2) | ||
|
||
set.seed(1) | ||
suppressMessages( | ||
irf2 <- oil |> | ||
specify_bsvarSIGN$new() |> | ||
estimate(S = 3, thin = 1, show_progress = FALSE) |> | ||
compute_impulse_responses(horizon = 2) | ||
) | ||
|
||
|
||
expect_error( | ||
compute_impulse_responses(run_no1), | ||
info = "compute_impulse_responses: specify horizon." | ||
) | ||
|
||
expect_identical( | ||
irf[3,3,3,3], irf2[3,3,3,3], | ||
info = "compute_impulse_responses: identical for normal and pipe workflow." | ||
) | ||
|
||
|
||
set.seed(1) | ||
suppressMessages( | ||
specification_no1 <- specify_bsvarSIGN$new(oil) | ||
) | ||
run_no1 <- estimate(specification_no1, 3, 1, show_progress = FALSE) | ||
irf <- compute_impulse_responses(run_no1, horizon = 2, standardise = TRUE) | ||
|
||
set.seed(1) | ||
suppressMessages( | ||
irf2 <- oil |> | ||
specify_bsvarSIGN$new() |> | ||
estimate(S = 3, thin = 1, show_progress = FALSE) |> | ||
compute_impulse_responses(horizon = 2, standardise = TRUE) | ||
) | ||
|
||
|
||
|
||
expect_equal( | ||
irf[1,1,1,1], 1, | ||
info = "compute_impulse_responses: unit own shock at 0 horizon." | ||
) | ||
|
||
expect_error( | ||
compute_impulse_responses(run_no1), | ||
info = "compute_impulse_responses: specify horizon." | ||
) | ||
|
||
expect_identical( | ||
irf[3,3,3,3], irf2[3,3,3,3], | ||
info = "compute_impulse_responses: identical for normal and pipe workflow." | ||
) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.