Skip to content

Commit

Permalink
combine sign_irf and zero_irf
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwang15 committed Jul 16, 2024
1 parent b380b91 commit f199ce8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 45 deletions.
8 changes: 5 additions & 3 deletions R/estimate.BSVARSIGN.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ estimate.BSVARSIGN = function(specification, S, thin = 1, show_progress = TRUE)
prior$Xsur = t(prior$Xsur)
Y = t(data_matrices$Y)
X = t(data_matrices$X)

Z = get_Z(identification$sign_irf)
sign = identification$sign_irf
sign[is.na(sign)] = 0

# estimation
qqq = .Call(`_bsvarSIGNs_bsvar_sign_cpp`, S, p, Y, X, identification$VB,
identification$sign_irf, identification$sign_narrative,
identification$sign_B, identification$zero_irf,
sign, identification$sign_narrative, identification$sign_B, Z,
prior, starting_values, show_progress, thin, max_tries)


specification$starting_values$set_starting_values(qqq$last_draw)
output = specify_posterior_bsvarSIGN$new(specification, qqq$posterior)
Expand Down
43 changes: 16 additions & 27 deletions R/specify_bsvarSIGN.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# construct Z_j matrices
get_Z = function(zero_irf) {
get_Z = function(sign_irf) {
zero_irf = sign_irf[, , 1] == 0
zero_irf[is.na(zero_irf)] = 0

if (sum(zero_irf) == 0) {
return(NULL)
}
Expand Down Expand Up @@ -36,7 +39,7 @@ verify_traditional = function(N, A) {
if (!(is.matrix(A) && all(dim(A) == c(N, N)))) {
stop("Sign restriction matrix is not NxN.")
}
if (!(all(A %in% c(-1, 0, 1)))) {
if (!(all(A %in% c(-1, 0, 1, NA)))) {
stop("Sign restriction matrix has entries that are not in {-1, 0, 1}.")
}
}
Expand Down Expand Up @@ -74,10 +77,6 @@ verify_all = function(N, sign_irf, sign_narrative, sign_B) {

verify_narrative(N, sign_narrative)

dim_irf = dim(sign_irf)
if (length(dim_irf) != 3) {
stop("Sign restriction array is not 3-dimensional.")
}
for (h in 1:dim(sign_irf)[3]) {
verify_traditional(N, sign_irf[,,h])
}
Expand Down Expand Up @@ -426,8 +425,6 @@ specify_identification_bsvarSIGN = R6::R6Class(
sign_narrative = matrix(),
#' @field sign_B a \code{NxN} matrix of sign restrictions on contemporaneous relations.
sign_B = matrix(),
#' @field zero_irf a \code{NxNxH} array of zero restrictions on the impulse response functions.
zero_irf = array(),
#' @field max_tries a positive integer with the maximum number of iterations
#' for finding a rotation matrix \eqn{Q} that would satisfy sign restrictions.
max_tries = 1,
Expand Down Expand Up @@ -460,12 +457,10 @@ specify_identification_bsvarSIGN = R6::R6Class(
#' contemporaneous relations \code{B} between reduced-form errors \code{E} and
#' structural shocks \code{U}. Recall the structural equation \code{BE=U}, the inverse
#' of \code{B} is the contemporaneous impulse response function.
#' @param zero_irf a \code{NxN} matrix with entries in (0, 1), zero restrictions on the
#' contemporaneous impulse response functions.
#' @param max_tries a positive integer with the maximum number of iterations
#' for finding a rotation matrix \eqn{Q} that would satisfy sign restrictions.
#' @return Identifying restrictions IdentificationBSVARSIGN.
initialize = function(N, sign_irf, sign_narrative, sign_B, zero_irf, max_tries = 1) {
initialize = function(N, sign_irf, sign_narrative, sign_B, max_tries = 1) {

missing_all = TRUE
if (missing(sign_irf)) {
Expand All @@ -485,8 +480,9 @@ specify_identification_bsvarSIGN = R6::R6Class(
sign_B = matrix(rep(0, N^2), ncol = N, nrow = N)
}
}
if (missing(zero_irf)) {
zero_irf = matrix(rep(0, N^2), ncol = N, nrow = N)

if (is.matrix(sign_irf)) {
sign_irf = array(sign_irf, dim = c(dim(sign_irf), 1))
}
verify_all(N, sign_irf, sign_narrative, sign_B)

Expand All @@ -501,7 +497,6 @@ specify_identification_bsvarSIGN = R6::R6Class(
self$sign_irf = sign_irf
self$sign_narrative = sign_narrative
self$sign_B = sign_B
self$zero_irf = get_Z(zero_irf)
self$max_tries = max_tries
}, # END initialize

Expand All @@ -514,7 +509,6 @@ specify_identification_bsvarSIGN = R6::R6Class(
sign_irf = as.array(self$sign_irf),
sign_narrative = as.matrix(self$sign_narrative),
sign_B = as.matrix(self$sign_B),
zero_irf = as.list(self$zero_irf),
max_tries = self$max_tries
)
}, # END get_identification
Expand Down Expand Up @@ -547,11 +541,9 @@ specify_identification_bsvarSIGN = R6::R6Class(
#' contemporaneous relations \code{B} between reduced-form errors \code{E} and
#' structural shocks \code{U}. Recall the structural equation \code{BE=U}, the inverse
#' of \code{B} is the contemporaneous impulse response function.
#' @param zero_irf a \code{NxN} matrix with entries in (0, 1), zero restrictions on the
#' contemporaneous impulse response functions.
#' @param max_tries a positive integer with the maximum number of iterations
#' for finding a rotation matrix \eqn{Q} that would satisfy sign restrictions.
set_identification = function(N, sign_irf, sign_narrative, sign_B, zero_irf) {
set_identification = function(N, sign_irf, sign_narrative, sign_B) {
B = matrix(FALSE, N, N)
B[lower.tri(B, diag = TRUE)] = TRUE

Expand All @@ -578,15 +570,15 @@ specify_identification_bsvarSIGN = R6::R6Class(
sign_B = matrix(rep(0, N^2), ncol = N, nrow = N)
}
}
if (missing(zero_irf)) {
zero_irf = matrix(rep(0, N^2), ncol = N, nrow = N)

if (is.matrix(sign_irf)) {
sign_irf = array(sign_irf, dim = c(dim(sign_irf), 1))
}
verify_all(N, sign_irf, sign_narrative, sign_B)

self$sign_irf = sign_irf
self$sign_narrative = sign_narrative
self$sign_B = sign_B
self$zero_irf = get_Z(zero_irf)
} # END set_identification
) # END public
) # END specify_identification_bsvarSIGN
Expand Down Expand Up @@ -657,8 +649,6 @@ specify_bsvarSIGN = R6::R6Class(
#' contemporaneous relations \code{B} between reduced-form errors \code{E} and
#' structural shocks \code{U}. Recall the structural equation \code{BE=U}, the inverse
#' of \code{B} is the contemporaneous impulse response function.
#' @param zero_irf a \code{NxN} matrix with entries in (0, 1), zero restrictions on the
#' contemporaneous impulse response functions.
#' @param max_tries a positive integer with the maximum number of iterations
#' for finding a rotation matrix \eqn{Q} that would satisfy sign restrictions.
#' @param exogenous a \code{(T+p)xd} matrix of exogenous variables.
Expand All @@ -672,7 +662,6 @@ specify_bsvarSIGN = R6::R6Class(
sign_irf,
sign_narrative,
sign_B,
zero_irf,
max_tries = 1,
exogenous = NULL,
stationary = rep(FALSE, ncol(data))
Expand Down Expand Up @@ -706,8 +695,9 @@ specify_bsvarSIGN = R6::R6Class(
sign_B = matrix(rep(0, N^2), ncol = N, nrow = N)
}
}
if (missing(zero_irf)) {
zero_irf = matrix(rep(0, N^2), ncol = N, nrow = N)

if (is.matrix(sign_irf)) {
sign_irf = array(sign_irf, dim = c(dim(sign_irf), 1))
}
verify_all(N, sign_irf, sign_narrative, sign_B)

Expand All @@ -719,7 +709,6 @@ specify_bsvarSIGN = R6::R6Class(
sign_irf,
sign_narrative,
sign_B,
zero_irf,
max_tries)
self$prior = specify_prior_bsvarSIGN$new(data, p, exogenous,
stationary)
Expand Down
4 changes: 0 additions & 4 deletions man/specify_bsvarSIGN.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions man/specify_identification_bsvarSIGN.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f199ce8

Please sign in to comment.