Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some user-friendly things #35

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions R/estimate.BSVARSIGN.R → R/estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ estimate.BSVARSIGN = function(specification, S, thin = 1, show_progress = TRUE)
sign[is.na(sign)] = 0

# estimation
qqq = .Call(`_bsvarSIGNs_bsvar_sign_cpp`, S, p, Y, X, identification$VB,
sign, identification$sign_narrative, identification$sign_B, Z,
prior, starting_values, show_progress, thin, max_tries)
qqq = .Call(`_bsvarSIGNs_bsvar_sign_cpp`, S, p, Y, X,
identification$VB, sign, identification$sign_narrative,
identification$sign_relation, 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
52 changes: 26 additions & 26 deletions R/specify_bsvarSIGN.R → R/specify.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ verify_narrative = function(N, A) {
}

# verify all restrictions
verify_all = function(N, sign_irf, sign_narrative, sign_B) {
verify_traditional(N, sign_B)
verify_all = function(N, sign_irf, sign_narrative, sign_relation) {
verify_traditional(N, sign_relation)

verify_narrative(N, sign_narrative)

Expand Down Expand Up @@ -422,8 +422,8 @@ specify_identification_bsvarSIGN = R6::R6Class(
sign_irf = array(),
#' @field sign_narrative a \code{Mx6} matrix of narrative sign restrictions.
sign_narrative = matrix(),
#' @field sign_B a \code{NxN} matrix of sign restrictions on contemporaneous relations.
sign_B = matrix(),
#' @field sign_relation a \code{NxN} matrix of sign restrictions on contemporaneous relations.
sign_relation = matrix(),
#' @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 @@ -452,14 +452,14 @@ specify_identification_bsvarSIGN = R6::R6Class(
#' Column 6 (horizons_h): an integer in 1:(T-start_t), number horizons of the restriction,
#' if start=t and horizons=h the restriction in on periods t to t+h,
#' e.g. when h=0 the restriction in only placed on period t.
#' @param sign_B a \code{NxN} matrix with entries in (-1 ,0, 1), sign restrictions on the
#' @param sign_relation a \code{NxN} matrix with entries in (-1 ,0, 1), sign restrictions on the
#' 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 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, max_tries = 1) {
initialize = function(N, sign_irf, sign_narrative, sign_relation, max_tries = 1) {

missing_all = TRUE
if (missing(sign_irf)) {
Expand All @@ -472,18 +472,18 @@ specify_identification_bsvarSIGN = R6::R6Class(
} else {
missing_all = FALSE
}
if (missing(sign_B)) {
if (missing(sign_relation)) {
if (missing_all) {
sign_B = diag(N)
sign_relation = diag(N)
} else {
sign_B = matrix(rep(0, N^2), ncol = N, nrow = N)
sign_relation = 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)
verify_all(N, sign_irf, sign_narrative, sign_relation)

B = matrix(FALSE, N, N)
B[lower.tri(B, diag = TRUE)] = TRUE
Expand All @@ -495,7 +495,7 @@ specify_identification_bsvarSIGN = R6::R6Class(

self$sign_irf = sign_irf
self$sign_narrative = sign_narrative
self$sign_B = sign_B
self$sign_relation = sign_relation
self$max_tries = max_tries
}, # END initialize

Expand All @@ -507,7 +507,7 @@ specify_identification_bsvarSIGN = R6::R6Class(
VB = as.list(self$VB),
sign_irf = as.array(self$sign_irf),
sign_narrative = as.matrix(self$sign_narrative),
sign_B = as.matrix(self$sign_B),
sign_relation = as.matrix(self$sign_relation),
max_tries = self$max_tries
)
}, # END get_identification
Expand Down Expand Up @@ -536,13 +536,13 @@ specify_identification_bsvarSIGN = R6::R6Class(
#' Column 6 (horizons_h): an integer in 1:(T-start_t), number horizons of the restriction,
#' if start=t and horizons=h the restriction in on periods t to t+h,
#' e.g. when h=0 the restriction in only placed on period t.
#' @param sign_B a \code{NxN} matrix with entries in (-1 ,0, 1), sign restrictions on the
#' @param sign_relation a \code{NxN} matrix with entries in (-1 ,0, 1), sign restrictions on the
#' 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 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) {
set_identification = function(N, sign_irf, sign_narrative, sign_relation) {
B = matrix(FALSE, N, N)
B[lower.tri(B, diag = TRUE)] = TRUE

Expand All @@ -562,22 +562,22 @@ specify_identification_bsvarSIGN = R6::R6Class(
} else {
missing_all = FALSE
}
if (missing(sign_B)) {
if (missing(sign_relation)) {
if (missing_all) {
sign_B = diag(N)
sign_relation = diag(N)
} else {
sign_B = matrix(rep(0, N^2), ncol = N, nrow = N)
sign_relation = 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)
verify_all(N, sign_irf, sign_narrative, sign_relation)

self$sign_irf = sign_irf
self$sign_narrative = sign_narrative
self$sign_B = sign_B
self$sign_relation = sign_relation
} # END set_identification
) # END public
) # END specify_identification_bsvarSIGN
Expand Down Expand Up @@ -644,7 +644,7 @@ specify_bsvarSIGN = R6::R6Class(
#' Column 6 (horizons_h): an integer in 1:(T-start_t), number horizons of the restriction,
#' if start=t and horizons=h the restriction in on periods t to t+h,
#' e.g. when h=0 the restriction in only placed on period t.
#' @param sign_B a \code{NxN} matrix with entries in (-1 ,0, 1), sign restrictions on the
#' @param sign_relation a \code{NxN} matrix with entries in (-1 ,0, 1), sign restrictions on the
#' 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.
Expand All @@ -660,7 +660,7 @@ specify_bsvarSIGN = R6::R6Class(
p = 1L,
sign_irf,
sign_narrative,
sign_B,
sign_relation,
max_tries = 1,
exogenous = NULL,
stationary = rep(FALSE, ncol(data))
Expand All @@ -687,18 +687,18 @@ specify_bsvarSIGN = R6::R6Class(
} else {
missing_all = FALSE
}
if (missing(sign_B)) {
if (missing(sign_relation)) {
if (missing_all) {
sign_B = diag(N)
sign_relation = diag(N)
} else {
sign_B = matrix(rep(0, N^2), ncol = N, nrow = N)
sign_relation = 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)
verify_all(N, sign_irf, sign_narrative, sign_relation)

B = matrix(FALSE, N, N)
B[lower.tri(B, diag = TRUE)] = TRUE
Expand All @@ -707,7 +707,7 @@ specify_bsvarSIGN = R6::R6Class(
self$identification = specify_identification_bsvarSIGN$new(N,
sign_irf,
sign_narrative,
sign_B,
sign_relation,
max_tries)
self$prior = specify_prior_bsvarSIGN$new(data, p, exogenous,
stationary)
Expand Down
2 changes: 1 addition & 1 deletion man/estimate.BSVARSIGN.Rd

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

6 changes: 3 additions & 3 deletions man/specify_bsvarSIGN.Rd

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

12 changes: 6 additions & 6 deletions man/specify_identification_bsvarSIGN.Rd

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

2 changes: 1 addition & 1 deletion man/specify_posterior_bsvarSIGN.Rd

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

2 changes: 1 addition & 1 deletion man/specify_prior_bsvarSIGN.Rd

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

2 changes: 1 addition & 1 deletion src/bsvars_sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Rcpp::List bsvar_sign_cpp(
if (show_progress) {
Rcout << "**************************************************|" << endl;
Rcout << " bsvarSIGNs: Bayesian Structural VAR with zero, |" << endl;
Rcout << " sign and narrative restriction |" << endl;
Rcout << " sign and narrative restrictions |" << endl;
Rcout << "**************************************************|" << endl;
// Rcout << " Gibbs sampler for the SVAR model |" << endl;
// Rcout << "**************************************************|" << endl;
Expand Down
4 changes: 4 additions & 0 deletions src/sample_hyper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ arma::mat sample_hyper(
const Rcpp::List& prior
) {

Rcout << "**************************************************|" << endl;
Rcout << " Adaptive Metropolis MCMC: hyper parameters |" << endl;
Rcout << "**************************************************|" << endl;

mat hypers = metropolis(

S, start, narrow_hyper(model, init), W,
Expand Down
Loading