From a7223beb295e9aae6f739161ec06e35f8e3fd24d Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Fri, 9 Aug 2024 22:37:08 +1000 Subject: [PATCH 01/51] new object for vec(A0) #43 --- src/restrictions_zero.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 527a69d..f091cae 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -34,7 +34,9 @@ arma::colvec zero_restrictions( const arma::colvec vec_structural ) { int N = Z(0).n_cols; - mat A0 = reshape(vec_structural.rows(0, N*N-1), N, N); + + mat A0 = vec_structural.rows(0, N * N - 1); + A0 = reshape(A0, N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); From 50d95a2a1a0dca4e448787d4b5bcb4708985f7ba Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Fri, 9 Aug 2024 22:54:47 +1000 Subject: [PATCH 02/51] Revert "new object for vec(A0) #43" This reverts commit a7223beb295e9aae6f739161ec06e35f8e3fd24d. --- src/restrictions_zero.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index f091cae..527a69d 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -34,9 +34,7 @@ arma::colvec zero_restrictions( const arma::colvec vec_structural ) { int N = Z(0).n_cols; - - mat A0 = vec_structural.rows(0, N * N - 1); - A0 = reshape(A0, N, N); + mat A0 = reshape(vec_structural.rows(0, N*N-1), N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); From eb51de4ce705202f277ff84945ddc050879a0a79 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Fri, 9 Aug 2024 22:56:25 +1000 Subject: [PATCH 03/51] remove scaling #43 --- R/bsvarSIGNs-package.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index 0cc6442..cc9a3b4 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -136,7 +136,7 @@ #' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) #' #' # specify the model -#' specification = specify_bsvarSIGN$new(optimism * 100, +#' specification = specify_bsvarSIGN$new(optimism, #' p = 4, #' sign_irf = sign_irf) #' From ebd40f5c1c3fa79d2db02ba3cbfe4e60e29d305d Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Fri, 9 Aug 2024 23:06:59 +1000 Subject: [PATCH 04/51] remove scaling 2 #43 --- R/estimate.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/estimate.R b/R/estimate.R index 429f60d..e57c680 100644 --- a/R/estimate.R +++ b/R/estimate.R @@ -82,7 +82,7 @@ #' #' # specify the model and set seed #' set.seed(123) -#' specification = specify_bsvarSIGN$new(optimism * 100, +#' specification = specify_bsvarSIGN$new(optimism, #' p = 12, #' sign_irf = sign_irf) #' From c89c23bc7513f620c2c98ef8f154f9a7db34578b Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Fri, 9 Aug 2024 23:18:03 +1000 Subject: [PATCH 05/51] narrow catch condition #43 --- src/sample_hyper.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/sample_hyper.cpp b/src/sample_hyper.cpp index 4e5ec8a..3464a2c 100644 --- a/src/sample_hyper.cpp +++ b/src/sample_hyper.cpp @@ -1,4 +1,5 @@ +#define ARMA_WARN_LEVEL 1 #include #include "utils.h" @@ -111,7 +112,8 @@ double log_ml( mat inv_Omega = diagmat(1 / Omega.diag()); try { - mat Bhat = inv_sympd(X.t() * X + inv_Omega) * (X.t() * Y + inv_Omega * b); + mat XX = X.t() * X + inv_Omega; + mat Bhat = solve(XX, X.t() * Y + inv_Omega * b, solve_opts::likely_sympd); mat ehat = Y - X * Bhat; log_ml += - N * T / 2.0 * log(M_PI); @@ -119,11 +121,11 @@ double log_ml( log_ml += -log_mvgamma(N, d / 2.0); log_ml += - N / 2.0 * log_det_sympd(Omega); log_ml += d / 2.0 * log_det_sympd(Psi); - log_ml += - N / 2.0 * log_det_sympd(X.t() * X + inv_Omega); + log_ml += - N / 2.0 * log_det_sympd(XX); mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); log_ml += - (T + d) / 2.0 * log_det_sympd(A); - } catch(...) { + } catch(std::runtime_error) { log_ml = -1e+10; } From dc4daa2174eab4018fdbcbda75c465886b103c24 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Fri, 9 Aug 2024 23:30:34 +1000 Subject: [PATCH 06/51] remove scaling 3 #43 --- README.Rmd | 4 ++-- README.md | 4 ++-- man/bsvarSIGNs-package.Rd | 2 +- man/estimate.BSVARSIGN.Rd | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.Rmd b/README.Rmd index 2f4dd56..ff3c5a5 100644 --- a/README.Rmd +++ b/README.Rmd @@ -103,7 +103,7 @@ data(optimism) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model -specification = specify_bsvarSIGN$new(optimism * 100, +specification = specify_bsvarSIGN$new(optimism, p = 4, sign_irf = sign_irf) @@ -134,7 +134,7 @@ sign_narrative = list( ) # specify the model -specification = specify_bsvarSIGN$new(monetary * 100, +specification = specify_bsvarSIGN$new(monetary, p = 12, sign_irf = sign_irf, sign_narrative = sign_narrative) diff --git a/README.md b/README.md index 43548dc..2696d50 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ data(optimism) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model -specification = specify_bsvarSIGN$new(optimism * 100, +specification = specify_bsvarSIGN$new(optimism, p = 4, sign_irf = sign_irf) @@ -176,7 +176,7 @@ sign_narrative = list( ) # specify the model -specification = specify_bsvarSIGN$new(monetary * 100, +specification = specify_bsvarSIGN$new(monetary, p = 12, sign_irf = sign_irf, sign_narrative = sign_narrative) diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 145d120..5d8db3f 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -97,7 +97,7 @@ data(optimism) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model -specification = specify_bsvarSIGN$new(optimism * 100, +specification = specify_bsvarSIGN$new(optimism, p = 4, sign_irf = sign_irf) diff --git a/man/estimate.BSVARSIGN.Rd b/man/estimate.BSVARSIGN.Rd index a172042..635598f 100644 --- a/man/estimate.BSVARSIGN.Rd +++ b/man/estimate.BSVARSIGN.Rd @@ -79,7 +79,7 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) -specification = specify_bsvarSIGN$new(optimism * 100, +specification = specify_bsvarSIGN$new(optimism, p = 12, sign_irf = sign_irf) From 0e5ec9503bc3027abcbaf18a06414e6b9272b6a1 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Fri, 9 Aug 2024 23:47:05 +1000 Subject: [PATCH 07/51] const error #43 --- src/sample_hyper.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/sample_hyper.cpp b/src/sample_hyper.cpp index 3464a2c..b349147 100644 --- a/src/sample_hyper.cpp +++ b/src/sample_hyper.cpp @@ -110,25 +110,25 @@ double log_ml( double log_ml = 0; mat inv_Omega = diagmat(1 / Omega.diag()); - + try { mat XX = X.t() * X + inv_Omega; mat Bhat = solve(XX, X.t() * Y + inv_Omega * b, solve_opts::likely_sympd); mat ehat = Y - X * Bhat; - - log_ml += - N * T / 2.0 * log(M_PI); - log_ml += log_mvgamma(N, (T + d) / 2.0); - log_ml += -log_mvgamma(N, d / 2.0); - log_ml += - N / 2.0 * log_det_sympd(Omega); - log_ml += d / 2.0 * log_det_sympd(Psi); - log_ml += - N / 2.0 * log_det_sympd(XX); - mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); - log_ml += - (T + d) / 2.0 * log_det_sympd(A); - - } catch(std::runtime_error) { + + log_ml += - N * T / 2.0 * log(M_PI); + log_ml += log_mvgamma(N, (T + d) / 2.0); + log_ml += -log_mvgamma(N, d / 2.0); + log_ml += - N / 2.0 * log_det_sympd(Omega); + log_ml += d / 2.0 * log_det_sympd(Psi); + log_ml += - N / 2.0 * log_det_sympd(XX); + mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); + log_ml += - (T + d) / 2.0 * log_det_sympd(A); + + } catch(const std::runtime_error& e) { log_ml = -1e+10; } - + return log_ml; } From 5a0d30f2291481de910a0f6315ef9dd0f398ac48 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 00:07:44 +1000 Subject: [PATCH 08/51] remove const #43 --- inst/include/bsvarSIGNs_RcppExports.h | 4 ++-- src/RcppExports.cpp | 6 +++--- src/restrictions_zero.cpp | 10 ++++++---- src/utils.cpp | 6 ++++-- src/utils.h | 5 ++--- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index e99db65..82ca6be 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural) { + inline arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); + validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 12d3c87..9e9e2e2 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -463,12 +463,12 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural); +arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); - Rcpp::traits::input_parameter< const arma::colvec >::type vec_structural(vec_structuralSEXP); + Rcpp::traits::input_parameter< arma::vec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; END_RCPP_RETURN_ERROR @@ -1058,7 +1058,7 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); signatures.insert("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); + signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); signatures.insert("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 527a69d..f4ae4d0 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -18,7 +18,7 @@ arma::field ZIRF( arma::field ZIRF(Z.n_elem); - for (int j=0; j ZIRF( // [[Rcpp::export]] arma::colvec zero_restrictions( const arma::field& Z, - const arma::colvec vec_structural + arma::vec vec_structural ) { int N = Z(0).n_cols; - mat A0 = reshape(vec_structural.rows(0, N*N-1), N, N); + + mat A0 = vec_structural.rows(0, N * N - 1); + A0 = reshape(A0, N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); colvec z; - for (int j=0; j& f, - const arma::vec& x, - const double h = 1e-10 + const arma::vec& x ) { + + double h = 1e-10; + colvec f_x = f(x); int n = x.n_elem; diff --git a/src/utils.h b/src/utils.h index 1ef8311..f66e4ec 100644 --- a/src/utils.h +++ b/src/utils.h @@ -14,9 +14,8 @@ bool match_sign( ); arma::mat Df( - const std::function& f, - const arma::colvec& x, - double h = 1e-10 + const std::function& f, + const arma::vec& x ); arma::mat metropolis( From 53b963bd0ea6c7e2a5fa8e3bffbc400fa1a7d8e9 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 00:21:23 +1000 Subject: [PATCH 09/51] start with nonempty vec #43 --- src/restrictions_zero.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index f4ae4d0..ea37764 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -40,8 +40,8 @@ arma::colvec zero_restrictions( arma::field ZF = ZIRF(Z, inv(A0.t())); - colvec z; - for (int j = 0; j < ZF.n_elem; j++) { + vec z = ZF(0).col(0); + for (int j = 1; j < ZF.n_elem; j++) { z = join_vert(z, ZF(j).col(j)); } From 632e78133ddd7802764447c4212070137c8b4254 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 00:23:55 +1000 Subject: [PATCH 10/51] Update restrictions_zero.cpp --- src/restrictions_zero.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index ea37764..b0509a8 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -35,8 +35,7 @@ arma::colvec zero_restrictions( ) { int N = Z(0).n_cols; - mat A0 = vec_structural.rows(0, N * N - 1); - A0 = reshape(A0, N, N); + mat A0 = reshape(vec_structural.rows(0, N * N - 1), N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); From da269c625ec51d21d6b18f91e3fe86933c4760bc Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 00:40:59 +1000 Subject: [PATCH 11/51] new ZF ref #43 --- src/restrictions_zero.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index b0509a8..4eeabc6 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -41,7 +41,8 @@ arma::colvec zero_restrictions( vec z = ZF(0).col(0); for (int j = 1; j < ZF.n_elem; j++) { - z = join_vert(z, ZF(j).col(j)); + vec zj = ZF(j).col(j); + z = join_vert(z, zj); } return z; From 60d89c4e140567d99928d485c71867a05ed3655e Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 01:05:09 +1000 Subject: [PATCH 12/51] remove const for Z #43 --- inst/include/bsvarSIGNs_RcppExports.h | 36 +++++++++--------- src/RcppExports.cpp | 54 +++++++++++++-------------- src/bsvars_sign.cpp | 2 +- src/bsvars_sign.h | 2 +- src/restrictions_zero.cpp | 28 ++++++++------ src/restrictions_zero.h | 4 +- src/sample_Q.cpp | 2 +- src/sample_Q.h | 2 +- 8 files changed, 68 insertions(+), 62 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 82ca6be..3c80632 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -25,11 +25,11 @@ namespace bsvarSIGNs { } } - inline Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const Rcpp::List& prior, const bool show_progress = true, const int thin = 100, const int& max_tries = 10000) { + inline Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const Rcpp::List& prior, const bool show_progress = true, const int thin = 100, const int& max_tries = 10000) { typedef SEXP(*Ptr_bsvar_sign_cpp)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP); static Ptr_bsvar_sign_cpp p_bsvar_sign_cpp = NULL; if (p_bsvar_sign_cpp == NULL) { - validateSignature("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const Rcpp::List&,const bool,const int,const int&)"); + validateSignature("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const Rcpp::List&,const bool,const int,const int&)"); p_bsvar_sign_cpp = (Ptr_bsvar_sign_cpp)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_bsvar_sign_cpp"); } RObject rcpp_result_gen; @@ -256,11 +256,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::field ZIRF(const arma::field& Z, const arma::mat& irf_0) { + inline arma::field ZIRF(arma::field& Z, const arma::mat& irf_0) { typedef SEXP(*Ptr_ZIRF)(SEXP,SEXP); static Ptr_ZIRF p_ZIRF = NULL; if (p_ZIRF == NULL) { - validateSignature("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); + validateSignature("arma::field(*ZIRF)(arma::field&,const arma::mat&)"); p_ZIRF = (Ptr_ZIRF)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_ZIRF"); } RObject rcpp_result_gen; @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural) { + inline arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); + validateSignature("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; @@ -298,11 +298,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::colvec g_fh(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { + inline arma::colvec g_fh(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { typedef SEXP(*Ptr_g_fh)(SEXP,SEXP,SEXP); static Ptr_g_fh p_g_fh = NULL; if (p_g_fh == NULL) { - validateSignature("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); + validateSignature("arma::colvec(*g_fh)(arma::field&,const arma::mat&,const arma::mat&)"); p_g_fh = (Ptr_g_fh)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh"); } RObject rcpp_result_gen; @@ -319,11 +319,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::colvec g_fh_vec(const arma::field& Z, const arma::colvec vec_structural) { + inline arma::colvec g_fh_vec(arma::field& Z, const arma::colvec vec_structural) { typedef SEXP(*Ptr_g_fh_vec)(SEXP,SEXP); static Ptr_g_fh_vec p_g_fh_vec = NULL; if (p_g_fh_vec == NULL) { - validateSignature("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); + validateSignature("arma::colvec(*g_fh_vec)(arma::field&,const arma::colvec)"); p_g_fh_vec = (Ptr_g_fh_vec)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh_vec"); } RObject rcpp_result_gen; @@ -340,11 +340,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { + inline double log_volume_element(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { typedef SEXP(*Ptr_log_volume_element)(SEXP,SEXP,SEXP); static Ptr_log_volume_element p_log_volume_element = NULL; if (p_log_volume_element == NULL) { - validateSignature("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); + validateSignature("double(*log_volume_element)(arma::field&,const arma::mat&,const arma::mat&)"); p_log_volume_element = (Ptr_log_volume_element)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_log_volume_element"); } RObject rcpp_result_gen; @@ -361,11 +361,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline double weight_zero(const arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q) { + inline double weight_zero(arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q) { typedef SEXP(*Ptr_weight_zero)(SEXP,SEXP,SEXP,SEXP); static Ptr_weight_zero p_weight_zero = NULL; if (p_weight_zero == NULL) { - validateSignature("double(*weight_zero)(const arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); + validateSignature("double(*weight_zero)(arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); p_weight_zero = (Ptr_weight_zero)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_weight_zero"); } RObject rcpp_result_gen; @@ -382,11 +382,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::mat rzeroQ(const arma::field& Z, const arma::mat& irf_0) { + inline arma::mat rzeroQ(arma::field& Z, const arma::mat& irf_0) { typedef SEXP(*Ptr_rzeroQ)(SEXP,SEXP); static Ptr_rzeroQ p_rzeroQ = NULL; if (p_rzeroQ == NULL) { - validateSignature("arma::mat(*rzeroQ)(const arma::field&,const arma::mat&)"); + validateSignature("arma::mat(*rzeroQ)(arma::field&,const arma::mat&)"); p_rzeroQ = (Ptr_rzeroQ)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_rzeroQ"); } RObject rcpp_result_gen; @@ -424,11 +424,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const int& max_tries) { + inline arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const int& max_tries) { typedef SEXP(*Ptr_sample_Q)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP); static Ptr_sample_Q p_sample_Q = NULL; if (p_sample_Q == NULL) { - validateSignature("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const int&)"); + validateSignature("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const int&)"); p_sample_Q = (Ptr_sample_Q)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_sample_Q"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 9e9e2e2..2c3e309 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -15,7 +15,7 @@ Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); #endif // bsvar_sign_cpp -Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const Rcpp::List& prior, const bool show_progress, const int thin, const int& max_tries); +Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const Rcpp::List& prior, const bool show_progress, const int thin, const int& max_tries); static SEXP _bsvarSIGNs_bsvar_sign_cpp_try(SEXP SSEXP, SEXP pSEXP, SEXP YSEXP, SEXP XSEXP, SEXP sign_irfSEXP, SEXP sign_narrativeSEXP, SEXP sign_BSEXP, SEXP ZSEXP, SEXP priorSEXP, SEXP show_progressSEXP, SEXP thinSEXP, SEXP max_triesSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; @@ -26,7 +26,7 @@ BEGIN_RCPP Rcpp::traits::input_parameter< const arma::cube& >::type sign_irf(sign_irfSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_narrative(sign_narrativeSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_B(sign_BSEXP); - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const Rcpp::List& >::type prior(priorSEXP); Rcpp::traits::input_parameter< const bool >::type show_progress(show_progressSEXP); Rcpp::traits::input_parameter< const int >::type thin(thinSEXP); @@ -428,11 +428,11 @@ RcppExport SEXP _bsvarSIGNs_weight_narrative(SEXP TSEXP, SEXP sign_narrativeSEXP return rcpp_result_gen; } // ZIRF -arma::field ZIRF(const arma::field& Z, const arma::mat& irf_0); +arma::field ZIRF(arma::field& Z, const arma::mat& irf_0); static SEXP _bsvarSIGNs_ZIRF_try(SEXP ZSEXP, SEXP irf_0SEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type irf_0(irf_0SEXP); rcpp_result_gen = Rcpp::wrap(ZIRF(Z, irf_0)); return rcpp_result_gen; @@ -463,11 +463,11 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural); +arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< arma::vec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; @@ -498,11 +498,11 @@ RcppExport SEXP _bsvarSIGNs_zero_restrictions(SEXP ZSEXP, SEXP vec_structuralSEX return rcpp_result_gen; } // g_fh -arma::colvec g_fh(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); +arma::colvec g_fh(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); static SEXP _bsvarSIGNs_g_fh_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type A0(A0SEXP); Rcpp::traits::input_parameter< const arma::mat& >::type Aplus(AplusSEXP); rcpp_result_gen = Rcpp::wrap(g_fh(Z, A0, Aplus)); @@ -534,11 +534,11 @@ RcppExport SEXP _bsvarSIGNs_g_fh(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { return rcpp_result_gen; } // g_fh_vec -arma::colvec g_fh_vec(const arma::field& Z, const arma::colvec vec_structural); +arma::colvec g_fh_vec(arma::field& Z, const arma::colvec vec_structural); static SEXP _bsvarSIGNs_g_fh_vec_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::colvec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(g_fh_vec(Z, vec_structural)); return rcpp_result_gen; @@ -569,11 +569,11 @@ RcppExport SEXP _bsvarSIGNs_g_fh_vec(SEXP ZSEXP, SEXP vec_structuralSEXP) { return rcpp_result_gen; } // log_volume_element -double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); +double log_volume_element(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); static SEXP _bsvarSIGNs_log_volume_element_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type A0(A0SEXP); Rcpp::traits::input_parameter< const arma::mat& >::type Aplus(AplusSEXP); rcpp_result_gen = Rcpp::wrap(log_volume_element(Z, A0, Aplus)); @@ -605,11 +605,11 @@ RcppExport SEXP _bsvarSIGNs_log_volume_element(SEXP ZSEXP, SEXP A0SEXP, SEXP Apl return rcpp_result_gen; } // weight_zero -double weight_zero(const arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q); +double weight_zero(arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q); static SEXP _bsvarSIGNs_weight_zero_try(SEXP ZSEXP, SEXP BSEXP, SEXP h_invSEXP, SEXP QSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type B(BSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type h_inv(h_invSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type Q(QSEXP); @@ -642,11 +642,11 @@ RcppExport SEXP _bsvarSIGNs_weight_zero(SEXP ZSEXP, SEXP BSEXP, SEXP h_invSEXP, return rcpp_result_gen; } // rzeroQ -arma::mat rzeroQ(const arma::field& Z, const arma::mat& irf_0); +arma::mat rzeroQ(arma::field& Z, const arma::mat& irf_0); static SEXP _bsvarSIGNs_rzeroQ_try(SEXP ZSEXP, SEXP irf_0SEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type irf_0(irf_0SEXP); rcpp_result_gen = Rcpp::wrap(rzeroQ(Z, irf_0)); return rcpp_result_gen; @@ -754,7 +754,7 @@ RcppExport SEXP _bsvarSIGNs_match_sign_irf(SEXP QSEXP, SEXP sign_irfSEXP, SEXP i return rcpp_result_gen; } // sample_Q -arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const int& max_tries); +arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const int& max_tries); static SEXP _bsvarSIGNs_sample_Q_try(SEXP pSEXP, SEXP YSEXP, SEXP XSEXP, SEXP BSEXP, SEXP h_invpSEXP, SEXP chol_SigmaSEXP, SEXP priorSEXP, SEXP sign_irfSEXP, SEXP sign_narrativeSEXP, SEXP sign_BSEXP, SEXP ZSEXP, SEXP max_triesSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; @@ -768,7 +768,7 @@ BEGIN_RCPP Rcpp::traits::input_parameter< const arma::cube& >::type sign_irf(sign_irfSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_narrative(sign_narrativeSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_B(sign_BSEXP); - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const int& >::type max_tries(max_triesSEXP); rcpp_result_gen = Rcpp::wrap(sample_Q(p, Y, X, B, h_invp, chol_Sigma, prior, sign_irf, sign_narrative, sign_B, Z, max_tries)); return rcpp_result_gen; @@ -1046,7 +1046,7 @@ RcppExport SEXP _bsvarSIGNs_match_sign(SEXP ASEXP, SEXP signSEXP) { static int _bsvarSIGNs_RcppExport_validate(const char* sig) { static std::set signatures; if (signatures.empty()) { - signatures.insert("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const Rcpp::List&,const bool,const int,const int&)"); + signatures.insert("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const Rcpp::List&,const bool,const int,const int&)"); signatures.insert("arma::cube(*bsvarSIGNs_structural_shocks)(const arma::cube&,const arma::cube&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::cube(*bsvarSIGNs_fitted_values)(arma::cube&,arma::cube&,arma::cube&,arma::mat&)"); signatures.insert("arma::cube(*ir1_cpp)(const arma::mat&,const arma::mat&,int,const int&)"); @@ -1057,15 +1057,15 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("arma::cube(*forecast_bsvarSIGNs)(arma::cube&,arma::cube&,arma::vec&,arma::mat&,arma::mat&,const int&)"); signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); - signatures.insert("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); - signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); - signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); - signatures.insert("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); - signatures.insert("double(*weight_zero)(const arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); - signatures.insert("arma::mat(*rzeroQ)(const arma::field&,const arma::mat&)"); + signatures.insert("arma::field(*ZIRF)(arma::field&,const arma::mat&)"); + signatures.insert("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); + signatures.insert("arma::colvec(*g_fh)(arma::field&,const arma::mat&,const arma::mat&)"); + signatures.insert("arma::colvec(*g_fh_vec)(arma::field&,const arma::colvec)"); + signatures.insert("double(*log_volume_element)(arma::field&,const arma::mat&,const arma::mat&)"); + signatures.insert("double(*weight_zero)(arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); + signatures.insert("arma::mat(*rzeroQ)(arma::field&,const arma::mat&)"); signatures.insert("bool(*match_sign_irf)(const arma::mat&,const arma::cube&,const arma::cube&)"); - signatures.insert("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const int&)"); + signatures.insert("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const int&)"); signatures.insert("arma::mat(*qr_sign_cpp)(const arma::mat&)"); signatures.insert("arma::mat(*rortho_cpp)(const int&)"); signatures.insert("bool(*match_sign)(const arma::mat&,const arma::mat&)"); diff --git a/src/bsvars_sign.cpp b/src/bsvars_sign.cpp index 1d5285c..be35e42 100644 --- a/src/bsvars_sign.cpp +++ b/src/bsvars_sign.cpp @@ -23,7 +23,7 @@ Rcpp::List bsvar_sign_cpp( const arma::cube& sign_irf, // NxNxh cube of signs for impulse response function const arma::mat& sign_narrative, // ANYx6 matrix of signs for historical decomposition const arma::mat& sign_B, // NxN matrix of signs for B - const arma::field& Z, // a list of zero restrictions + arma::field& Z, // a list of zero restrictions const Rcpp::List& prior, // a list of priors const bool show_progress = true, const int thin = 100, // introduce thinning diff --git a/src/bsvars_sign.h b/src/bsvars_sign.h index 8ea3b66..abba312 100644 --- a/src/bsvars_sign.h +++ b/src/bsvars_sign.h @@ -11,7 +11,7 @@ Rcpp::List bsvar_sign_cpp( const arma::cube& sign_irf, // NxNxh cube of signs for impulse response function const arma::mat& sign_narrative, // Mx6 matrix of signs for historical decomposition const arma::mat& sign_B, // Mx6 matrix of signs for B - const arma::field& Z, // a list of zero restrictions + arma::field& Z, // a list of zero restrictions const Rcpp::List& prior, // a list of priors const bool show_progress = true, const int thin = 100, // introduce thinning diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 4eeabc6..7a07cd7 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -12,7 +12,7 @@ using namespace arma; // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::field ZIRF( - const arma::field& Z, + arma::field& Z, const arma::mat& irf_0 ) { @@ -30,7 +30,7 @@ arma::field ZIRF( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec zero_restrictions( - const arma::field& Z, + arma::field& Z, arma::vec vec_structural ) { int N = Z(0).n_cols; @@ -41,8 +41,7 @@ arma::colvec zero_restrictions( vec z = ZF(0).col(0); for (int j = 1; j < ZF.n_elem; j++) { - vec zj = ZF(j).col(j); - z = join_vert(z, zj); + z = join_vert(z, ZF(j).col(j)); } return z; @@ -53,7 +52,7 @@ arma::colvec zero_restrictions( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec g_fh( - const arma::field& Z, + arma::field& Z, const arma::mat& A0, const arma::mat& Aplus ) { @@ -88,7 +87,7 @@ arma::colvec g_fh( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec g_fh_vec( - const arma::field& Z, + arma::field& Z, const arma::colvec vec_structural ) { int N = Z(0).n_cols; @@ -106,14 +105,21 @@ arma::colvec g_fh_vec( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] double log_volume_element( - const arma::field& Z, + arma::field& Z, const arma::mat& A0, const arma::mat& Aplus ) { colvec vec_structural = join_vert(vectorise(A0), vectorise(Aplus)); - mat Dz = Df([Z](const colvec& x) { return zero_restrictions(Z, x); }, vec_structural); - mat Dgf = Df([Z](const colvec& x) { return g_fh_vec(Z, x); }, vec_structural); + mat Dz = Df([Z](const colvec& x) { + field ZZ = Z; + return zero_restrictions(ZZ, x); + }, vec_structural); + + mat Dgf = Df([Z](const colvec& x) { + field ZZ = Z; + return g_fh_vec(ZZ, x); + }, vec_structural); mat DN = Dgf * null(Dz); @@ -125,7 +131,7 @@ double log_volume_element( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] double weight_zero( - const arma::field& Z, + arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q @@ -147,7 +153,7 @@ double weight_zero( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::mat rzeroQ( - const arma::field& Z, + arma::field& Z, const arma::mat& irf_0 ) { // Algorithm 2 in ARRW (2018) diff --git a/src/restrictions_zero.h b/src/restrictions_zero.h index 9cd71e9..0f98ffd 100644 --- a/src/restrictions_zero.h +++ b/src/restrictions_zero.h @@ -8,14 +8,14 @@ using namespace arma; double weight_zero( - const arma::field& Z, + arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q ); arma::mat rzeroQ( - const arma::field& Z, + arma::field& Z, const arma::mat& irf_0 ); diff --git a/src/sample_Q.cpp b/src/sample_Q.cpp index 2dad44c..a1f9fb8 100644 --- a/src/sample_Q.cpp +++ b/src/sample_Q.cpp @@ -43,7 +43,7 @@ arma::field sample_Q( const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, - const arma::field& Z, + arma::field& Z, const int& max_tries ) { diff --git a/src/sample_Q.h b/src/sample_Q.h index 35f543c..acf33a6 100644 --- a/src/sample_Q.h +++ b/src/sample_Q.h @@ -14,7 +14,7 @@ arma::field sample_Q( const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, - const arma::field& Z, + arma::field& Z, const int& max_tries ); From 9aca271850149ebf23d38e0e8013bad2be50d891 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 01:37:44 +1000 Subject: [PATCH 13/51] separate ZF #43 --- src/restrictions_zero.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 7a07cd7..c75c02d 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -37,11 +37,12 @@ arma::colvec zero_restrictions( mat A0 = reshape(vec_structural.rows(0, N * N - 1), N, N); - arma::field ZF = ZIRF(Z, inv(A0.t())); + field ZF = ZIRF(Z, inv(A0.t())); vec z = ZF(0).col(0); for (int j = 1; j < ZF.n_elem; j++) { - z = join_vert(z, ZF(j).col(j)); + mat ZF_j = ZF(j); + z = join_vert(z, ZF_j.col(j)); } return z; From 1d560cb1735d12fb4e52c0334c3b119756157f37 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 01:49:53 +1000 Subject: [PATCH 14/51] Update restrictions_zero.cpp --- src/restrictions_zero.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index c75c02d..5397ba6 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -39,10 +39,10 @@ arma::colvec zero_restrictions( field ZF = ZIRF(Z, inv(A0.t())); - vec z = ZF(0).col(0); - for (int j = 1; j < ZF.n_elem; j++) { + vec z; + for (int j = 0; j < ZF.n_elem; j++) { mat ZF_j = ZF(j); - z = join_vert(z, ZF_j.col(j)); + z = join_vert(z, ZF_j.col(j)); } return z; From 8330418dbd857654372f28b81b474e7cd85830d9 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 02:04:12 +1000 Subject: [PATCH 15/51] Update restrictions_zero.cpp --- src/restrictions_zero.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 5397ba6..981c664 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -37,12 +37,12 @@ arma::colvec zero_restrictions( mat A0 = reshape(vec_structural.rows(0, N * N - 1), N, N); - field ZF = ZIRF(Z, inv(A0.t())); + mat irf_0 = inv(A0.t()); vec z; - for (int j = 0; j < ZF.n_elem; j++) { - mat ZF_j = ZF(j); - z = join_vert(z, ZF_j.col(j)); + for (int j = 0; j < Z.n_elem; j++) { + mat ZF = Z(j) * irf_0; + z = join_vert(z, ZF.col(j)); } return z; From 77b937d6910c3ef98182d7afc59380873768cd4d Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 02:25:57 +1000 Subject: [PATCH 16/51] remove pointer #43 --- inst/include/bsvarSIGNs_RcppExports.h | 4 ++-- src/RcppExports.cpp | 6 +++--- src/restrictions_zero.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 3c80632..1434293 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural) { + inline arma::colvec zero_restrictions(arma::field Z, arma::vec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); + validateSignature("arma::colvec(*zero_restrictions)(arma::field,arma::vec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 2c3e309..ec5f441 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -463,11 +463,11 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural); +arma::colvec zero_restrictions(arma::field Z, arma::vec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field >::type Z(ZSEXP); Rcpp::traits::input_parameter< arma::vec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; @@ -1058,7 +1058,7 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); signatures.insert("arma::field(*ZIRF)(arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); + signatures.insert("arma::colvec(*zero_restrictions)(arma::field,arma::vec)"); signatures.insert("arma::colvec(*g_fh)(arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(arma::field&,const arma::colvec)"); signatures.insert("double(*log_volume_element)(arma::field&,const arma::mat&,const arma::mat&)"); diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 981c664..75b3130 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -30,7 +30,7 @@ arma::field ZIRF( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec zero_restrictions( - arma::field& Z, + arma::field Z, arma::vec vec_structural ) { int N = Z(0).n_cols; From a0fc07b9f3f4ece02cd173b82281248f4dd35162 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 09:57:17 +1000 Subject: [PATCH 17/51] change lag #43 --- R/bsvarSIGNs-package.R | 2 +- man/bsvarSIGNs-package.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index cc9a3b4..f880825 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,7 +137,7 @@ #' #' # specify the model #' specification = specify_bsvarSIGN$new(optimism, -#' p = 4, +#' p = 12, #' sign_irf = sign_irf) #' #' # estimate the model diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 5d8db3f..660e195 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,7 +98,7 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model specification = specify_bsvarSIGN$new(optimism, - p = 4, + p = 12, sign_irf = sign_irf) # estimate the model From 41c9c3f90cb88f317ec5103686d6b96f626c112e Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 10:08:28 +1000 Subject: [PATCH 18/51] smaller sample #43 --- R/bsvarSIGNs-package.R | 4 ++-- R/estimate.R | 2 +- man/bsvarSIGNs-package.Rd | 4 ++-- man/estimate.BSVARSIGN.Rd | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index f880825..c409744 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,11 +137,11 @@ #' #' # specify the model #' specification = specify_bsvarSIGN$new(optimism, -#' p = 12, +#' p = 4, #' sign_irf = sign_irf) #' #' # estimate the model -#' posterior = estimate(specification, S = 100) +#' posterior = estimate(specification, S = 10) #' #' # compute and plot impulse responses #' irf = compute_impulse_responses(posterior, horizon = 40) diff --git a/R/estimate.R b/R/estimate.R index e57c680..4580ac7 100644 --- a/R/estimate.R +++ b/R/estimate.R @@ -83,7 +83,7 @@ #' # specify the model and set seed #' set.seed(123) #' specification = specify_bsvarSIGN$new(optimism, -#' p = 12, +#' p = 4, #' sign_irf = sign_irf) #' #' # estimate the model diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 660e195..caec6e2 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,11 +98,11 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model specification = specify_bsvarSIGN$new(optimism, - p = 12, + p = 4, sign_irf = sign_irf) # estimate the model -posterior = estimate(specification, S = 100) +posterior = estimate(specification, S = 10) # compute and plot impulse responses irf = compute_impulse_responses(posterior, horizon = 40) diff --git a/man/estimate.BSVARSIGN.Rd b/man/estimate.BSVARSIGN.Rd index 635598f..6a00376 100644 --- a/man/estimate.BSVARSIGN.Rd +++ b/man/estimate.BSVARSIGN.Rd @@ -80,7 +80,7 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) specification = specify_bsvarSIGN$new(optimism, - p = 12, + p = 4, sign_irf = sign_irf) # estimate the model From b8a11278056cfeeb72eedc8881df71e060c6c909 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 10:19:03 +1000 Subject: [PATCH 19/51] set seed #43 --- R/bsvarSIGNs-package.R | 7 ++++--- man/bsvarSIGNs-package.Rd | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index c409744..15fdc7a 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -129,13 +129,14 @@ #' @examples #' # investigate the effects of the optimism shock #' data(optimism) -#' +#' #' # specify identifying restrictions: #' # + no effect on productivity (zero restriction) -#' # + positive effect on stock prices (positive sign restriction) +#' # + positive effect on stock prices (positive sign restriction) #' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) #' -#' # specify the model +#' # specify the model and set seed +#' set.seed(123) #' specification = specify_bsvarSIGN$new(optimism, #' p = 4, #' sign_irf = sign_irf) diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index caec6e2..3e477c9 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -93,10 +93,11 @@ data(optimism) # specify identifying restrictions: # + no effect on productivity (zero restriction) -# + positive effect on stock prices (positive sign restriction) +# + positive effect on stock prices (positive sign restriction) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) -# specify the model +# specify the model and set seed +set.seed(123) specification = specify_bsvarSIGN$new(optimism, p = 4, sign_irf = sign_irf) From 46ec7745b0b3266f4d6715cb2cf54660a5aeffeb Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 10:31:08 +1000 Subject: [PATCH 20/51] remove lag #43 --- R/bsvarSIGNs-package.R | 4 +--- man/bsvarSIGNs-package.Rd | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index 15fdc7a..acdba59 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,9 +137,7 @@ #' #' # specify the model and set seed #' set.seed(123) -#' specification = specify_bsvarSIGN$new(optimism, -#' p = 4, -#' sign_irf = sign_irf) +#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) #' #' # estimate the model #' posterior = estimate(specification, S = 10) diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 3e477c9..4a3ed8d 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,9 +98,7 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) -specification = specify_bsvarSIGN$new(optimism, - p = 4, - sign_irf = sign_irf) +specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) # estimate the model posterior = estimate(specification, S = 10) From 9b8509e8ce5acdad1c877c123e7ca8c3d82b2618 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 10:41:01 +1000 Subject: [PATCH 21/51] copy same example #43 --- R/bsvarSIGNs-package.R | 8 +++----- man/bsvarSIGNs-package.Rd | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index acdba59..381fad7 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,13 +137,11 @@ #' #' # specify the model and set seed #' set.seed(123) -#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) +#' specification = specify_bsvarSIGN$new(optimism, +#' p = 4, +#' sign_irf = sign_irf) #' #' # estimate the model #' posterior = estimate(specification, S = 10) #' -#' # compute and plot impulse responses -#' irf = compute_impulse_responses(posterior, horizon = 40) -#' plot(irf, probability = 0.68) -#' NULL diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 4a3ed8d..af75dd4 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,15 +98,13 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) -specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) +specification = specify_bsvarSIGN$new(optimism, + p = 4, + sign_irf = sign_irf) # estimate the model posterior = estimate(specification, S = 10) -# compute and plot impulse responses -irf = compute_impulse_responses(posterior, horizon = 40) -plot(irf, probability = 0.68) - } \references{ Antolín-Díaz & Rubio-Ramírez (2018) Narrative Sign Restrictions for SVARs, American Economic Review, 108(10), 2802-29, . From 6fc7f09c94f18cd9005307d3b52c582e0fe6df5f Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 10:52:53 +1000 Subject: [PATCH 22/51] remove estimate #43 --- R/bsvarSIGNs-package.R | 5 +---- man/bsvarSIGNs-package.Rd | 3 --- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index 381fad7..8152156 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -140,8 +140,5 @@ #' specification = specify_bsvarSIGN$new(optimism, #' p = 4, #' sign_irf = sign_irf) -#' -#' # estimate the model -#' posterior = estimate(specification, S = 10) -#' +#' NULL diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index af75dd4..618586d 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -101,9 +101,6 @@ set.seed(123) specification = specify_bsvarSIGN$new(optimism, p = 4, sign_irf = sign_irf) - -# estimate the model -posterior = estimate(specification, S = 10) } \references{ From 3504e713012bbd84d19d2153fa8bc74656bdf31b Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:23:02 +1000 Subject: [PATCH 23/51] eevert "remove estimate #43" This reverts commit 6fc7f09c94f18cd9005307d3b52c582e0fe6df5f. revert commits do not fix issue --- R/bsvarSIGNs-package.R | 5 ++++- man/bsvarSIGNs-package.Rd | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index 8152156..381fad7 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -140,5 +140,8 @@ #' specification = specify_bsvarSIGN$new(optimism, #' p = 4, #' sign_irf = sign_irf) -#' +#' +#' # estimate the model +#' posterior = estimate(specification, S = 10) +#' NULL diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 618586d..af75dd4 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -101,6 +101,9 @@ set.seed(123) specification = specify_bsvarSIGN$new(optimism, p = 4, sign_irf = sign_irf) + +# estimate the model +posterior = estimate(specification, S = 10) } \references{ From 128df72cde5a5c5b042b4df95d1f452ab53455c9 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:24:19 +1000 Subject: [PATCH 24/51] Revert "copy same example #43" This reverts commit 9b8509e8ce5acdad1c877c123e7ca8c3d82b2618. reverts --- R/bsvarSIGNs-package.R | 8 +++++--- man/bsvarSIGNs-package.Rd | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index 381fad7..acdba59 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,11 +137,13 @@ #' #' # specify the model and set seed #' set.seed(123) -#' specification = specify_bsvarSIGN$new(optimism, -#' p = 4, -#' sign_irf = sign_irf) +#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) #' #' # estimate the model #' posterior = estimate(specification, S = 10) #' +#' # compute and plot impulse responses +#' irf = compute_impulse_responses(posterior, horizon = 40) +#' plot(irf, probability = 0.68) +#' NULL diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index af75dd4..4a3ed8d 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,13 +98,15 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) -specification = specify_bsvarSIGN$new(optimism, - p = 4, - sign_irf = sign_irf) +specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) # estimate the model posterior = estimate(specification, S = 10) +# compute and plot impulse responses +irf = compute_impulse_responses(posterior, horizon = 40) +plot(irf, probability = 0.68) + } \references{ Antolín-Díaz & Rubio-Ramírez (2018) Narrative Sign Restrictions for SVARs, American Economic Review, 108(10), 2802-29, . From 5e39691642fbbce2a549e7fa8a290cdc755322aa Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:11 +1000 Subject: [PATCH 25/51] Revert "remove lag #43" This reverts commit 46ec7745b0b3266f4d6715cb2cf54660a5aeffeb. --- R/bsvarSIGNs-package.R | 4 +++- man/bsvarSIGNs-package.Rd | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index acdba59..15fdc7a 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,7 +137,9 @@ #' #' # specify the model and set seed #' set.seed(123) -#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) +#' specification = specify_bsvarSIGN$new(optimism, +#' p = 4, +#' sign_irf = sign_irf) #' #' # estimate the model #' posterior = estimate(specification, S = 10) diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 4a3ed8d..3e477c9 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,7 +98,9 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) -specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf) +specification = specify_bsvarSIGN$new(optimism, + p = 4, + sign_irf = sign_irf) # estimate the model posterior = estimate(specification, S = 10) From ad8a4f26b6880e5a4e11c0013609bb47205096a3 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:17 +1000 Subject: [PATCH 26/51] Revert "set seed #43" This reverts commit b8a11278056cfeeb72eedc8881df71e060c6c909. --- R/bsvarSIGNs-package.R | 7 +++---- man/bsvarSIGNs-package.Rd | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index 15fdc7a..c409744 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -129,14 +129,13 @@ #' @examples #' # investigate the effects of the optimism shock #' data(optimism) -#' +#' #' # specify identifying restrictions: #' # + no effect on productivity (zero restriction) -#' # + positive effect on stock prices (positive sign restriction) +#' # + positive effect on stock prices (positive sign restriction) #' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) #' -#' # specify the model and set seed -#' set.seed(123) +#' # specify the model #' specification = specify_bsvarSIGN$new(optimism, #' p = 4, #' sign_irf = sign_irf) diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 3e477c9..caec6e2 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -93,11 +93,10 @@ data(optimism) # specify identifying restrictions: # + no effect on productivity (zero restriction) -# + positive effect on stock prices (positive sign restriction) +# + positive effect on stock prices (positive sign restriction) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) -# specify the model and set seed -set.seed(123) +# specify the model specification = specify_bsvarSIGN$new(optimism, p = 4, sign_irf = sign_irf) From 3c8a4884329b147b6d6f5548cb6c2946340cf1b6 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:18 +1000 Subject: [PATCH 27/51] Revert "smaller sample #43" This reverts commit 41c9c3f90cb88f317ec5103686d6b96f626c112e. --- R/bsvarSIGNs-package.R | 4 ++-- R/estimate.R | 2 +- man/bsvarSIGNs-package.Rd | 4 ++-- man/estimate.BSVARSIGN.Rd | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index c409744..f880825 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,11 +137,11 @@ #' #' # specify the model #' specification = specify_bsvarSIGN$new(optimism, -#' p = 4, +#' p = 12, #' sign_irf = sign_irf) #' #' # estimate the model -#' posterior = estimate(specification, S = 10) +#' posterior = estimate(specification, S = 100) #' #' # compute and plot impulse responses #' irf = compute_impulse_responses(posterior, horizon = 40) diff --git a/R/estimate.R b/R/estimate.R index 4580ac7..e57c680 100644 --- a/R/estimate.R +++ b/R/estimate.R @@ -83,7 +83,7 @@ #' # specify the model and set seed #' set.seed(123) #' specification = specify_bsvarSIGN$new(optimism, -#' p = 4, +#' p = 12, #' sign_irf = sign_irf) #' #' # estimate the model diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index caec6e2..660e195 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,11 +98,11 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model specification = specify_bsvarSIGN$new(optimism, - p = 4, + p = 12, sign_irf = sign_irf) # estimate the model -posterior = estimate(specification, S = 10) +posterior = estimate(specification, S = 100) # compute and plot impulse responses irf = compute_impulse_responses(posterior, horizon = 40) diff --git a/man/estimate.BSVARSIGN.Rd b/man/estimate.BSVARSIGN.Rd index 6a00376..635598f 100644 --- a/man/estimate.BSVARSIGN.Rd +++ b/man/estimate.BSVARSIGN.Rd @@ -80,7 +80,7 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) specification = specify_bsvarSIGN$new(optimism, - p = 4, + p = 12, sign_irf = sign_irf) # estimate the model From b0c4568a2337dbdeb1e70e7df3b71ebd36831543 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:19 +1000 Subject: [PATCH 28/51] Revert "change lag #43" This reverts commit a0fc07b9f3f4ece02cd173b82281248f4dd35162. --- R/bsvarSIGNs-package.R | 2 +- man/bsvarSIGNs-package.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index f880825..cc9a3b4 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -137,7 +137,7 @@ #' #' # specify the model #' specification = specify_bsvarSIGN$new(optimism, -#' p = 12, +#' p = 4, #' sign_irf = sign_irf) #' #' # estimate the model diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 660e195..5d8db3f 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -98,7 +98,7 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model specification = specify_bsvarSIGN$new(optimism, - p = 12, + p = 4, sign_irf = sign_irf) # estimate the model From 21159230ad03f593bba77b95fcd3c9d6dbb5fde7 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:20 +1000 Subject: [PATCH 29/51] Revert "remove pointer #43" This reverts commit 77b937d6910c3ef98182d7afc59380873768cd4d. --- inst/include/bsvarSIGNs_RcppExports.h | 4 ++-- src/RcppExports.cpp | 6 +++--- src/restrictions_zero.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 1434293..3c80632 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(arma::field Z, arma::vec vec_structural) { + inline arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(arma::field,arma::vec)"); + validateSignature("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index ec5f441..2c3e309 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -463,11 +463,11 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(arma::field Z, arma::vec vec_structural); +arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field >::type Z(ZSEXP); + Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< arma::vec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; @@ -1058,7 +1058,7 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); signatures.insert("arma::field(*ZIRF)(arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(arma::field,arma::vec)"); + signatures.insert("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); signatures.insert("arma::colvec(*g_fh)(arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(arma::field&,const arma::colvec)"); signatures.insert("double(*log_volume_element)(arma::field&,const arma::mat&,const arma::mat&)"); diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 75b3130..981c664 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -30,7 +30,7 @@ arma::field ZIRF( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec zero_restrictions( - arma::field Z, + arma::field& Z, arma::vec vec_structural ) { int N = Z(0).n_cols; From c5fd0844377c64e5ba0d760cd59770ba69fb69bb Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:21 +1000 Subject: [PATCH 30/51] Revert "Update restrictions_zero.cpp" This reverts commit 8330418dbd857654372f28b81b474e7cd85830d9. --- src/restrictions_zero.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 981c664..5397ba6 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -37,12 +37,12 @@ arma::colvec zero_restrictions( mat A0 = reshape(vec_structural.rows(0, N * N - 1), N, N); - mat irf_0 = inv(A0.t()); + field ZF = ZIRF(Z, inv(A0.t())); vec z; - for (int j = 0; j < Z.n_elem; j++) { - mat ZF = Z(j) * irf_0; - z = join_vert(z, ZF.col(j)); + for (int j = 0; j < ZF.n_elem; j++) { + mat ZF_j = ZF(j); + z = join_vert(z, ZF_j.col(j)); } return z; From 9d350e294fbe5f428a118dbd1b4b82fe9a19229f Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:21 +1000 Subject: [PATCH 31/51] Revert "Update restrictions_zero.cpp" This reverts commit 1d560cb1735d12fb4e52c0334c3b119756157f37. --- src/restrictions_zero.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 5397ba6..c75c02d 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -39,10 +39,10 @@ arma::colvec zero_restrictions( field ZF = ZIRF(Z, inv(A0.t())); - vec z; - for (int j = 0; j < ZF.n_elem; j++) { + vec z = ZF(0).col(0); + for (int j = 1; j < ZF.n_elem; j++) { mat ZF_j = ZF(j); - z = join_vert(z, ZF_j.col(j)); + z = join_vert(z, ZF_j.col(j)); } return z; From ac24a73dbdce2f1f33c99897e8cf8b7c98c481eb Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:22 +1000 Subject: [PATCH 32/51] Revert "separate ZF #43" This reverts commit 9aca271850149ebf23d38e0e8013bad2be50d891. --- src/restrictions_zero.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index c75c02d..7a07cd7 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -37,12 +37,11 @@ arma::colvec zero_restrictions( mat A0 = reshape(vec_structural.rows(0, N * N - 1), N, N); - field ZF = ZIRF(Z, inv(A0.t())); + arma::field ZF = ZIRF(Z, inv(A0.t())); vec z = ZF(0).col(0); for (int j = 1; j < ZF.n_elem; j++) { - mat ZF_j = ZF(j); - z = join_vert(z, ZF_j.col(j)); + z = join_vert(z, ZF(j).col(j)); } return z; From 054df85b08642e4540f06d20c7cb706a4685e397 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:23 +1000 Subject: [PATCH 33/51] Revert "remove const for Z #43" This reverts commit 60d89c4e140567d99928d485c71867a05ed3655e. --- inst/include/bsvarSIGNs_RcppExports.h | 36 +++++++++--------- src/RcppExports.cpp | 54 +++++++++++++-------------- src/bsvars_sign.cpp | 2 +- src/bsvars_sign.h | 2 +- src/restrictions_zero.cpp | 28 ++++++-------- src/restrictions_zero.h | 4 +- src/sample_Q.cpp | 2 +- src/sample_Q.h | 2 +- 8 files changed, 62 insertions(+), 68 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 3c80632..82ca6be 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -25,11 +25,11 @@ namespace bsvarSIGNs { } } - inline Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const Rcpp::List& prior, const bool show_progress = true, const int thin = 100, const int& max_tries = 10000) { + inline Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const Rcpp::List& prior, const bool show_progress = true, const int thin = 100, const int& max_tries = 10000) { typedef SEXP(*Ptr_bsvar_sign_cpp)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP); static Ptr_bsvar_sign_cpp p_bsvar_sign_cpp = NULL; if (p_bsvar_sign_cpp == NULL) { - validateSignature("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const Rcpp::List&,const bool,const int,const int&)"); + validateSignature("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const Rcpp::List&,const bool,const int,const int&)"); p_bsvar_sign_cpp = (Ptr_bsvar_sign_cpp)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_bsvar_sign_cpp"); } RObject rcpp_result_gen; @@ -256,11 +256,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::field ZIRF(arma::field& Z, const arma::mat& irf_0) { + inline arma::field ZIRF(const arma::field& Z, const arma::mat& irf_0) { typedef SEXP(*Ptr_ZIRF)(SEXP,SEXP); static Ptr_ZIRF p_ZIRF = NULL; if (p_ZIRF == NULL) { - validateSignature("arma::field(*ZIRF)(arma::field&,const arma::mat&)"); + validateSignature("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); p_ZIRF = (Ptr_ZIRF)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_ZIRF"); } RObject rcpp_result_gen; @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural) { + inline arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); + validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; @@ -298,11 +298,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::colvec g_fh(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { + inline arma::colvec g_fh(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { typedef SEXP(*Ptr_g_fh)(SEXP,SEXP,SEXP); static Ptr_g_fh p_g_fh = NULL; if (p_g_fh == NULL) { - validateSignature("arma::colvec(*g_fh)(arma::field&,const arma::mat&,const arma::mat&)"); + validateSignature("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); p_g_fh = (Ptr_g_fh)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh"); } RObject rcpp_result_gen; @@ -319,11 +319,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::colvec g_fh_vec(arma::field& Z, const arma::colvec vec_structural) { + inline arma::colvec g_fh_vec(const arma::field& Z, const arma::colvec vec_structural) { typedef SEXP(*Ptr_g_fh_vec)(SEXP,SEXP); static Ptr_g_fh_vec p_g_fh_vec = NULL; if (p_g_fh_vec == NULL) { - validateSignature("arma::colvec(*g_fh_vec)(arma::field&,const arma::colvec)"); + validateSignature("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); p_g_fh_vec = (Ptr_g_fh_vec)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh_vec"); } RObject rcpp_result_gen; @@ -340,11 +340,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline double log_volume_element(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { + inline double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { typedef SEXP(*Ptr_log_volume_element)(SEXP,SEXP,SEXP); static Ptr_log_volume_element p_log_volume_element = NULL; if (p_log_volume_element == NULL) { - validateSignature("double(*log_volume_element)(arma::field&,const arma::mat&,const arma::mat&)"); + validateSignature("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); p_log_volume_element = (Ptr_log_volume_element)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_log_volume_element"); } RObject rcpp_result_gen; @@ -361,11 +361,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline double weight_zero(arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q) { + inline double weight_zero(const arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q) { typedef SEXP(*Ptr_weight_zero)(SEXP,SEXP,SEXP,SEXP); static Ptr_weight_zero p_weight_zero = NULL; if (p_weight_zero == NULL) { - validateSignature("double(*weight_zero)(arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); + validateSignature("double(*weight_zero)(const arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); p_weight_zero = (Ptr_weight_zero)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_weight_zero"); } RObject rcpp_result_gen; @@ -382,11 +382,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::mat rzeroQ(arma::field& Z, const arma::mat& irf_0) { + inline arma::mat rzeroQ(const arma::field& Z, const arma::mat& irf_0) { typedef SEXP(*Ptr_rzeroQ)(SEXP,SEXP); static Ptr_rzeroQ p_rzeroQ = NULL; if (p_rzeroQ == NULL) { - validateSignature("arma::mat(*rzeroQ)(arma::field&,const arma::mat&)"); + validateSignature("arma::mat(*rzeroQ)(const arma::field&,const arma::mat&)"); p_rzeroQ = (Ptr_rzeroQ)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_rzeroQ"); } RObject rcpp_result_gen; @@ -424,11 +424,11 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const int& max_tries) { + inline arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const int& max_tries) { typedef SEXP(*Ptr_sample_Q)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP); static Ptr_sample_Q p_sample_Q = NULL; if (p_sample_Q == NULL) { - validateSignature("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const int&)"); + validateSignature("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const int&)"); p_sample_Q = (Ptr_sample_Q)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_sample_Q"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 2c3e309..9e9e2e2 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -15,7 +15,7 @@ Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); #endif // bsvar_sign_cpp -Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const Rcpp::List& prior, const bool show_progress, const int thin, const int& max_tries); +Rcpp::List bsvar_sign_cpp(const int& S, const int& p, const arma::mat& Y, const arma::mat& X, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const Rcpp::List& prior, const bool show_progress, const int thin, const int& max_tries); static SEXP _bsvarSIGNs_bsvar_sign_cpp_try(SEXP SSEXP, SEXP pSEXP, SEXP YSEXP, SEXP XSEXP, SEXP sign_irfSEXP, SEXP sign_narrativeSEXP, SEXP sign_BSEXP, SEXP ZSEXP, SEXP priorSEXP, SEXP show_progressSEXP, SEXP thinSEXP, SEXP max_triesSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; @@ -26,7 +26,7 @@ BEGIN_RCPP Rcpp::traits::input_parameter< const arma::cube& >::type sign_irf(sign_irfSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_narrative(sign_narrativeSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_B(sign_BSEXP); - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const Rcpp::List& >::type prior(priorSEXP); Rcpp::traits::input_parameter< const bool >::type show_progress(show_progressSEXP); Rcpp::traits::input_parameter< const int >::type thin(thinSEXP); @@ -428,11 +428,11 @@ RcppExport SEXP _bsvarSIGNs_weight_narrative(SEXP TSEXP, SEXP sign_narrativeSEXP return rcpp_result_gen; } // ZIRF -arma::field ZIRF(arma::field& Z, const arma::mat& irf_0); +arma::field ZIRF(const arma::field& Z, const arma::mat& irf_0); static SEXP _bsvarSIGNs_ZIRF_try(SEXP ZSEXP, SEXP irf_0SEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type irf_0(irf_0SEXP); rcpp_result_gen = Rcpp::wrap(ZIRF(Z, irf_0)); return rcpp_result_gen; @@ -463,11 +463,11 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(arma::field& Z, arma::vec vec_structural); +arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< arma::vec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; @@ -498,11 +498,11 @@ RcppExport SEXP _bsvarSIGNs_zero_restrictions(SEXP ZSEXP, SEXP vec_structuralSEX return rcpp_result_gen; } // g_fh -arma::colvec g_fh(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); +arma::colvec g_fh(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); static SEXP _bsvarSIGNs_g_fh_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type A0(A0SEXP); Rcpp::traits::input_parameter< const arma::mat& >::type Aplus(AplusSEXP); rcpp_result_gen = Rcpp::wrap(g_fh(Z, A0, Aplus)); @@ -534,11 +534,11 @@ RcppExport SEXP _bsvarSIGNs_g_fh(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { return rcpp_result_gen; } // g_fh_vec -arma::colvec g_fh_vec(arma::field& Z, const arma::colvec vec_structural); +arma::colvec g_fh_vec(const arma::field& Z, const arma::colvec vec_structural); static SEXP _bsvarSIGNs_g_fh_vec_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::colvec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(g_fh_vec(Z, vec_structural)); return rcpp_result_gen; @@ -569,11 +569,11 @@ RcppExport SEXP _bsvarSIGNs_g_fh_vec(SEXP ZSEXP, SEXP vec_structuralSEXP) { return rcpp_result_gen; } // log_volume_element -double log_volume_element(arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); +double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); static SEXP _bsvarSIGNs_log_volume_element_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type A0(A0SEXP); Rcpp::traits::input_parameter< const arma::mat& >::type Aplus(AplusSEXP); rcpp_result_gen = Rcpp::wrap(log_volume_element(Z, A0, Aplus)); @@ -605,11 +605,11 @@ RcppExport SEXP _bsvarSIGNs_log_volume_element(SEXP ZSEXP, SEXP A0SEXP, SEXP Apl return rcpp_result_gen; } // weight_zero -double weight_zero(arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q); +double weight_zero(const arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q); static SEXP _bsvarSIGNs_weight_zero_try(SEXP ZSEXP, SEXP BSEXP, SEXP h_invSEXP, SEXP QSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type B(BSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type h_inv(h_invSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type Q(QSEXP); @@ -642,11 +642,11 @@ RcppExport SEXP _bsvarSIGNs_weight_zero(SEXP ZSEXP, SEXP BSEXP, SEXP h_invSEXP, return rcpp_result_gen; } // rzeroQ -arma::mat rzeroQ(arma::field& Z, const arma::mat& irf_0); +arma::mat rzeroQ(const arma::field& Z, const arma::mat& irf_0); static SEXP _bsvarSIGNs_rzeroQ_try(SEXP ZSEXP, SEXP irf_0SEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type irf_0(irf_0SEXP); rcpp_result_gen = Rcpp::wrap(rzeroQ(Z, irf_0)); return rcpp_result_gen; @@ -754,7 +754,7 @@ RcppExport SEXP _bsvarSIGNs_match_sign_irf(SEXP QSEXP, SEXP sign_irfSEXP, SEXP i return rcpp_result_gen; } // sample_Q -arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, arma::field& Z, const int& max_tries); +arma::field sample_Q(const int& p, const arma::mat& Y, const arma::mat& X, arma::mat& B, arma::mat& h_invp, arma::mat& chol_Sigma, const Rcpp::List& prior, const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, const arma::field& Z, const int& max_tries); static SEXP _bsvarSIGNs_sample_Q_try(SEXP pSEXP, SEXP YSEXP, SEXP XSEXP, SEXP BSEXP, SEXP h_invpSEXP, SEXP chol_SigmaSEXP, SEXP priorSEXP, SEXP sign_irfSEXP, SEXP sign_narrativeSEXP, SEXP sign_BSEXP, SEXP ZSEXP, SEXP max_triesSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; @@ -768,7 +768,7 @@ BEGIN_RCPP Rcpp::traits::input_parameter< const arma::cube& >::type sign_irf(sign_irfSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_narrative(sign_narrativeSEXP); Rcpp::traits::input_parameter< const arma::mat& >::type sign_B(sign_BSEXP); - Rcpp::traits::input_parameter< arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); Rcpp::traits::input_parameter< const int& >::type max_tries(max_triesSEXP); rcpp_result_gen = Rcpp::wrap(sample_Q(p, Y, X, B, h_invp, chol_Sigma, prior, sign_irf, sign_narrative, sign_B, Z, max_tries)); return rcpp_result_gen; @@ -1046,7 +1046,7 @@ RcppExport SEXP _bsvarSIGNs_match_sign(SEXP ASEXP, SEXP signSEXP) { static int _bsvarSIGNs_RcppExport_validate(const char* sig) { static std::set signatures; if (signatures.empty()) { - signatures.insert("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const Rcpp::List&,const bool,const int,const int&)"); + signatures.insert("Rcpp::List(*bsvar_sign_cpp)(const int&,const int&,const arma::mat&,const arma::mat&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const Rcpp::List&,const bool,const int,const int&)"); signatures.insert("arma::cube(*bsvarSIGNs_structural_shocks)(const arma::cube&,const arma::cube&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::cube(*bsvarSIGNs_fitted_values)(arma::cube&,arma::cube&,arma::cube&,arma::mat&)"); signatures.insert("arma::cube(*ir1_cpp)(const arma::mat&,const arma::mat&,int,const int&)"); @@ -1057,15 +1057,15 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("arma::cube(*forecast_bsvarSIGNs)(arma::cube&,arma::cube&,arma::vec&,arma::mat&,arma::mat&,const int&)"); signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); - signatures.insert("arma::field(*ZIRF)(arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(arma::field&,arma::vec)"); - signatures.insert("arma::colvec(*g_fh)(arma::field&,const arma::mat&,const arma::mat&)"); - signatures.insert("arma::colvec(*g_fh_vec)(arma::field&,const arma::colvec)"); - signatures.insert("double(*log_volume_element)(arma::field&,const arma::mat&,const arma::mat&)"); - signatures.insert("double(*weight_zero)(arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); - signatures.insert("arma::mat(*rzeroQ)(arma::field&,const arma::mat&)"); + signatures.insert("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); + signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); + signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); + signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); + signatures.insert("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); + signatures.insert("double(*weight_zero)(const arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); + signatures.insert("arma::mat(*rzeroQ)(const arma::field&,const arma::mat&)"); signatures.insert("bool(*match_sign_irf)(const arma::mat&,const arma::cube&,const arma::cube&)"); - signatures.insert("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,arma::field&,const int&)"); + signatures.insert("arma::field(*sample_Q)(const int&,const arma::mat&,const arma::mat&,arma::mat&,arma::mat&,arma::mat&,const Rcpp::List&,const arma::cube&,const arma::mat&,const arma::mat&,const arma::field&,const int&)"); signatures.insert("arma::mat(*qr_sign_cpp)(const arma::mat&)"); signatures.insert("arma::mat(*rortho_cpp)(const int&)"); signatures.insert("bool(*match_sign)(const arma::mat&,const arma::mat&)"); diff --git a/src/bsvars_sign.cpp b/src/bsvars_sign.cpp index be35e42..1d5285c 100644 --- a/src/bsvars_sign.cpp +++ b/src/bsvars_sign.cpp @@ -23,7 +23,7 @@ Rcpp::List bsvar_sign_cpp( const arma::cube& sign_irf, // NxNxh cube of signs for impulse response function const arma::mat& sign_narrative, // ANYx6 matrix of signs for historical decomposition const arma::mat& sign_B, // NxN matrix of signs for B - arma::field& Z, // a list of zero restrictions + const arma::field& Z, // a list of zero restrictions const Rcpp::List& prior, // a list of priors const bool show_progress = true, const int thin = 100, // introduce thinning diff --git a/src/bsvars_sign.h b/src/bsvars_sign.h index abba312..8ea3b66 100644 --- a/src/bsvars_sign.h +++ b/src/bsvars_sign.h @@ -11,7 +11,7 @@ Rcpp::List bsvar_sign_cpp( const arma::cube& sign_irf, // NxNxh cube of signs for impulse response function const arma::mat& sign_narrative, // Mx6 matrix of signs for historical decomposition const arma::mat& sign_B, // Mx6 matrix of signs for B - arma::field& Z, // a list of zero restrictions + const arma::field& Z, // a list of zero restrictions const Rcpp::List& prior, // a list of priors const bool show_progress = true, const int thin = 100, // introduce thinning diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 7a07cd7..4eeabc6 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -12,7 +12,7 @@ using namespace arma; // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::field ZIRF( - arma::field& Z, + const arma::field& Z, const arma::mat& irf_0 ) { @@ -30,7 +30,7 @@ arma::field ZIRF( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec zero_restrictions( - arma::field& Z, + const arma::field& Z, arma::vec vec_structural ) { int N = Z(0).n_cols; @@ -41,7 +41,8 @@ arma::colvec zero_restrictions( vec z = ZF(0).col(0); for (int j = 1; j < ZF.n_elem; j++) { - z = join_vert(z, ZF(j).col(j)); + vec zj = ZF(j).col(j); + z = join_vert(z, zj); } return z; @@ -52,7 +53,7 @@ arma::colvec zero_restrictions( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec g_fh( - arma::field& Z, + const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus ) { @@ -87,7 +88,7 @@ arma::colvec g_fh( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::colvec g_fh_vec( - arma::field& Z, + const arma::field& Z, const arma::colvec vec_structural ) { int N = Z(0).n_cols; @@ -105,21 +106,14 @@ arma::colvec g_fh_vec( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] double log_volume_element( - arma::field& Z, + const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus ) { colvec vec_structural = join_vert(vectorise(A0), vectorise(Aplus)); - mat Dz = Df([Z](const colvec& x) { - field ZZ = Z; - return zero_restrictions(ZZ, x); - }, vec_structural); - - mat Dgf = Df([Z](const colvec& x) { - field ZZ = Z; - return g_fh_vec(ZZ, x); - }, vec_structural); + mat Dz = Df([Z](const colvec& x) { return zero_restrictions(Z, x); }, vec_structural); + mat Dgf = Df([Z](const colvec& x) { return g_fh_vec(Z, x); }, vec_structural); mat DN = Dgf * null(Dz); @@ -131,7 +125,7 @@ double log_volume_element( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] double weight_zero( - arma::field& Z, + const arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q @@ -153,7 +147,7 @@ double weight_zero( // [[Rcpp::interfaces(cpp)]] // [[Rcpp::export]] arma::mat rzeroQ( - arma::field& Z, + const arma::field& Z, const arma::mat& irf_0 ) { // Algorithm 2 in ARRW (2018) diff --git a/src/restrictions_zero.h b/src/restrictions_zero.h index 0f98ffd..9cd71e9 100644 --- a/src/restrictions_zero.h +++ b/src/restrictions_zero.h @@ -8,14 +8,14 @@ using namespace arma; double weight_zero( - arma::field& Z, + const arma::field& Z, const arma::mat& B, const arma::mat& h_inv, const arma::mat& Q ); arma::mat rzeroQ( - arma::field& Z, + const arma::field& Z, const arma::mat& irf_0 ); diff --git a/src/sample_Q.cpp b/src/sample_Q.cpp index a1f9fb8..2dad44c 100644 --- a/src/sample_Q.cpp +++ b/src/sample_Q.cpp @@ -43,7 +43,7 @@ arma::field sample_Q( const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, - arma::field& Z, + const arma::field& Z, const int& max_tries ) { diff --git a/src/sample_Q.h b/src/sample_Q.h index acf33a6..35f543c 100644 --- a/src/sample_Q.h +++ b/src/sample_Q.h @@ -14,7 +14,7 @@ arma::field sample_Q( const arma::cube& sign_irf, const arma::mat& sign_narrative, const arma::mat& sign_B, - arma::field& Z, + const arma::field& Z, const int& max_tries ); From d74c1ca1f6e6cf361b3633fb1f0091418c1534c2 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:24 +1000 Subject: [PATCH 34/51] Revert "new ZF ref #43" This reverts commit da269c625ec51d21d6b18f91e3fe86933c4760bc. --- src/restrictions_zero.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 4eeabc6..b0509a8 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -41,8 +41,7 @@ arma::colvec zero_restrictions( vec z = ZF(0).col(0); for (int j = 1; j < ZF.n_elem; j++) { - vec zj = ZF(j).col(j); - z = join_vert(z, zj); + z = join_vert(z, ZF(j).col(j)); } return z; From cf6d813544450e807b6d5a31f7d343e0c607cf77 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:25 +1000 Subject: [PATCH 35/51] Revert "Update restrictions_zero.cpp" This reverts commit 632e78133ddd7802764447c4212070137c8b4254. --- src/restrictions_zero.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index b0509a8..ea37764 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -35,7 +35,8 @@ arma::colvec zero_restrictions( ) { int N = Z(0).n_cols; - mat A0 = reshape(vec_structural.rows(0, N * N - 1), N, N); + mat A0 = vec_structural.rows(0, N * N - 1); + A0 = reshape(A0, N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); From 63385df320286c3c071e121075ff5a31df7ebeba Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:26 +1000 Subject: [PATCH 36/51] Revert "start with nonempty vec #43" This reverts commit 53b963bd0ea6c7e2a5fa8e3bffbc400fa1a7d8e9. --- src/restrictions_zero.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index ea37764..f4ae4d0 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -40,8 +40,8 @@ arma::colvec zero_restrictions( arma::field ZF = ZIRF(Z, inv(A0.t())); - vec z = ZF(0).col(0); - for (int j = 1; j < ZF.n_elem; j++) { + colvec z; + for (int j = 0; j < ZF.n_elem; j++) { z = join_vert(z, ZF(j).col(j)); } From 88eb74d5eee03319d3bd530adaa0929f1fb87927 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:35 +1000 Subject: [PATCH 37/51] Revert "remove const #43" This reverts commit 5a0d30f2291481de910a0f6315ef9dd0f398ac48. --- inst/include/bsvarSIGNs_RcppExports.h | 4 ++-- src/RcppExports.cpp | 6 +++--- src/restrictions_zero.cpp | 10 ++++------ src/utils.cpp | 6 ++---- src/utils.h | 5 +++-- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 82ca6be..e99db65 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural) { + inline arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); + validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 9e9e2e2..12d3c87 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -463,12 +463,12 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(const arma::field& Z, arma::vec vec_structural); +arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); - Rcpp::traits::input_parameter< arma::vec >::type vec_structural(vec_structuralSEXP); + Rcpp::traits::input_parameter< const arma::colvec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; END_RCPP_RETURN_ERROR @@ -1058,7 +1058,7 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); signatures.insert("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,arma::vec)"); + signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); signatures.insert("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index f4ae4d0..527a69d 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -18,7 +18,7 @@ arma::field ZIRF( arma::field ZIRF(Z.n_elem); - for (int j = 0; j < Z.n_elem; j++) { + for (int j=0; j ZIRF( // [[Rcpp::export]] arma::colvec zero_restrictions( const arma::field& Z, - arma::vec vec_structural + const arma::colvec vec_structural ) { int N = Z(0).n_cols; - - mat A0 = vec_structural.rows(0, N * N - 1); - A0 = reshape(A0, N, N); + mat A0 = reshape(vec_structural.rows(0, N*N-1), N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); colvec z; - for (int j = 0; j < ZF.n_elem; j++) { + for (int j=0; j& f, - const arma::vec& x + const arma::vec& x, + const double h = 1e-10 ) { - - double h = 1e-10; - colvec f_x = f(x); int n = x.n_elem; diff --git a/src/utils.h b/src/utils.h index f66e4ec..1ef8311 100644 --- a/src/utils.h +++ b/src/utils.h @@ -14,8 +14,9 @@ bool match_sign( ); arma::mat Df( - const std::function& f, - const arma::vec& x + const std::function& f, + const arma::colvec& x, + double h = 1e-10 ); arma::mat metropolis( From 41b444f83099b9228a5059633124bf474aa63939 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:37 +1000 Subject: [PATCH 38/51] Revert "const error #43" This reverts commit 0e5ec9503bc3027abcbaf18a06414e6b9272b6a1. --- src/sample_hyper.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/sample_hyper.cpp b/src/sample_hyper.cpp index b349147..3464a2c 100644 --- a/src/sample_hyper.cpp +++ b/src/sample_hyper.cpp @@ -110,25 +110,25 @@ double log_ml( double log_ml = 0; mat inv_Omega = diagmat(1 / Omega.diag()); - + try { mat XX = X.t() * X + inv_Omega; mat Bhat = solve(XX, X.t() * Y + inv_Omega * b, solve_opts::likely_sympd); mat ehat = Y - X * Bhat; - - log_ml += - N * T / 2.0 * log(M_PI); - log_ml += log_mvgamma(N, (T + d) / 2.0); - log_ml += -log_mvgamma(N, d / 2.0); - log_ml += - N / 2.0 * log_det_sympd(Omega); - log_ml += d / 2.0 * log_det_sympd(Psi); - log_ml += - N / 2.0 * log_det_sympd(XX); - mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); - log_ml += - (T + d) / 2.0 * log_det_sympd(A); - - } catch(const std::runtime_error& e) { + + log_ml += - N * T / 2.0 * log(M_PI); + log_ml += log_mvgamma(N, (T + d) / 2.0); + log_ml += -log_mvgamma(N, d / 2.0); + log_ml += - N / 2.0 * log_det_sympd(Omega); + log_ml += d / 2.0 * log_det_sympd(Psi); + log_ml += - N / 2.0 * log_det_sympd(XX); + mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); + log_ml += - (T + d) / 2.0 * log_det_sympd(A); + + } catch(std::runtime_error) { log_ml = -1e+10; } - + return log_ml; } From 56a2659d796873bcf1989519443154f6535b738a Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:37 +1000 Subject: [PATCH 39/51] Revert "remove scaling 3 #43" This reverts commit dc4daa2174eab4018fdbcbda75c465886b103c24. --- README.Rmd | 4 ++-- README.md | 4 ++-- man/bsvarSIGNs-package.Rd | 2 +- man/estimate.BSVARSIGN.Rd | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.Rmd b/README.Rmd index ff3c5a5..2f4dd56 100644 --- a/README.Rmd +++ b/README.Rmd @@ -103,7 +103,7 @@ data(optimism) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model -specification = specify_bsvarSIGN$new(optimism, +specification = specify_bsvarSIGN$new(optimism * 100, p = 4, sign_irf = sign_irf) @@ -134,7 +134,7 @@ sign_narrative = list( ) # specify the model -specification = specify_bsvarSIGN$new(monetary, +specification = specify_bsvarSIGN$new(monetary * 100, p = 12, sign_irf = sign_irf, sign_narrative = sign_narrative) diff --git a/README.md b/README.md index 2696d50..43548dc 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ data(optimism) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model -specification = specify_bsvarSIGN$new(optimism, +specification = specify_bsvarSIGN$new(optimism * 100, p = 4, sign_irf = sign_irf) @@ -176,7 +176,7 @@ sign_narrative = list( ) # specify the model -specification = specify_bsvarSIGN$new(monetary, +specification = specify_bsvarSIGN$new(monetary * 100, p = 12, sign_irf = sign_irf, sign_narrative = sign_narrative) diff --git a/man/bsvarSIGNs-package.Rd b/man/bsvarSIGNs-package.Rd index 5d8db3f..145d120 100644 --- a/man/bsvarSIGNs-package.Rd +++ b/man/bsvarSIGNs-package.Rd @@ -97,7 +97,7 @@ data(optimism) sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model -specification = specify_bsvarSIGN$new(optimism, +specification = specify_bsvarSIGN$new(optimism * 100, p = 4, sign_irf = sign_irf) diff --git a/man/estimate.BSVARSIGN.Rd b/man/estimate.BSVARSIGN.Rd index 635598f..a172042 100644 --- a/man/estimate.BSVARSIGN.Rd +++ b/man/estimate.BSVARSIGN.Rd @@ -79,7 +79,7 @@ sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) # specify the model and set seed set.seed(123) -specification = specify_bsvarSIGN$new(optimism, +specification = specify_bsvarSIGN$new(optimism * 100, p = 12, sign_irf = sign_irf) From e682d9430f9fbaaeefc7306e9bdd87b4a9d46ac1 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:38 +1000 Subject: [PATCH 40/51] Revert "narrow catch condition #43" This reverts commit c89c23bc7513f620c2c98ef8f154f9a7db34578b. --- src/sample_hyper.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/sample_hyper.cpp b/src/sample_hyper.cpp index 3464a2c..4e5ec8a 100644 --- a/src/sample_hyper.cpp +++ b/src/sample_hyper.cpp @@ -1,5 +1,4 @@ -#define ARMA_WARN_LEVEL 1 #include #include "utils.h" @@ -112,8 +111,7 @@ double log_ml( mat inv_Omega = diagmat(1 / Omega.diag()); try { - mat XX = X.t() * X + inv_Omega; - mat Bhat = solve(XX, X.t() * Y + inv_Omega * b, solve_opts::likely_sympd); + mat Bhat = inv_sympd(X.t() * X + inv_Omega) * (X.t() * Y + inv_Omega * b); mat ehat = Y - X * Bhat; log_ml += - N * T / 2.0 * log(M_PI); @@ -121,11 +119,11 @@ double log_ml( log_ml += -log_mvgamma(N, d / 2.0); log_ml += - N / 2.0 * log_det_sympd(Omega); log_ml += d / 2.0 * log_det_sympd(Psi); - log_ml += - N / 2.0 * log_det_sympd(XX); + log_ml += - N / 2.0 * log_det_sympd(X.t() * X + inv_Omega); mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); log_ml += - (T + d) / 2.0 * log_det_sympd(A); - } catch(std::runtime_error) { + } catch(...) { log_ml = -1e+10; } From 4d6094372bade4784a3bf95966a584f69795c100 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:39 +1000 Subject: [PATCH 41/51] Revert "remove scaling 2 #43" This reverts commit ebd40f5c1c3fa79d2db02ba3cbfe4e60e29d305d. --- R/estimate.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/estimate.R b/R/estimate.R index e57c680..429f60d 100644 --- a/R/estimate.R +++ b/R/estimate.R @@ -82,7 +82,7 @@ #' #' # specify the model and set seed #' set.seed(123) -#' specification = specify_bsvarSIGN$new(optimism, +#' specification = specify_bsvarSIGN$new(optimism * 100, #' p = 12, #' sign_irf = sign_irf) #' From b53defafe58cae8de153d3ed0e9be8e911eff340 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:40 +1000 Subject: [PATCH 42/51] Revert "remove scaling #43" This reverts commit eb51de4ce705202f277ff84945ddc050879a0a79. --- R/bsvarSIGNs-package.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/bsvarSIGNs-package.R b/R/bsvarSIGNs-package.R index cc9a3b4..0cc6442 100644 --- a/R/bsvarSIGNs-package.R +++ b/R/bsvarSIGNs-package.R @@ -136,7 +136,7 @@ #' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5) #' #' # specify the model -#' specification = specify_bsvarSIGN$new(optimism, +#' specification = specify_bsvarSIGN$new(optimism * 100, #' p = 4, #' sign_irf = sign_irf) #' From 868d6588fd4ada3fbbf3f9107e4a6cc9d6079d0f Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:25:41 +1000 Subject: [PATCH 43/51] Revert "Revert "new object for vec(A0) #43"" This reverts commit 50d95a2a1a0dca4e448787d4b5bcb4708985f7ba. --- src/restrictions_zero.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 527a69d..f091cae 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -34,7 +34,9 @@ arma::colvec zero_restrictions( const arma::colvec vec_structural ) { int N = Z(0).n_cols; - mat A0 = reshape(vec_structural.rows(0, N*N-1), N, N); + + mat A0 = vec_structural.rows(0, N * N - 1); + A0 = reshape(A0, N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); From f34ef73f0c2f60c43074b5ef7976446c9f9d1398 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:37:13 +1000 Subject: [PATCH 44/51] separate Df #43 --- inst/include/bsvarSIGNs_RcppExports.h | 42 +++++++++++++++ src/RcppExports.cpp | 78 +++++++++++++++++++++++++++ src/restrictions_zero.cpp | 67 ++++++++++++++++++++++- 3 files changed, 185 insertions(+), 2 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index e99db65..2a45b8e 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -340,6 +340,48 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } + inline arma::mat D_z(const arma::field& Z, const arma::vec& x, const double h = 1e-10) { + typedef SEXP(*Ptr_D_z)(SEXP,SEXP,SEXP); + static Ptr_D_z p_D_z = NULL; + if (p_D_z == NULL) { + validateSignature("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); + p_D_z = (Ptr_D_z)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_D_z"); + } + RObject rcpp_result_gen; + { + RNGScope RCPP_rngScope_gen; + rcpp_result_gen = p_D_z(Shield(Rcpp::wrap(Z)), Shield(Rcpp::wrap(x)), Shield(Rcpp::wrap(h))); + } + if (rcpp_result_gen.inherits("interrupted-error")) + throw Rcpp::internal::InterruptedException(); + if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen)) + throw Rcpp::LongjumpException(rcpp_result_gen); + if (rcpp_result_gen.inherits("try-error")) + throw Rcpp::exception(Rcpp::as(rcpp_result_gen).c_str()); + return Rcpp::as(rcpp_result_gen); + } + + inline arma::mat D_gf(const arma::field& Z, const arma::vec& x, const double h = 1e-10) { + typedef SEXP(*Ptr_D_gf)(SEXP,SEXP,SEXP); + static Ptr_D_gf p_D_gf = NULL; + if (p_D_gf == NULL) { + validateSignature("arma::mat(*D_gf)(const arma::field&,const arma::vec&,const double)"); + p_D_gf = (Ptr_D_gf)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_D_gf"); + } + RObject rcpp_result_gen; + { + RNGScope RCPP_rngScope_gen; + rcpp_result_gen = p_D_gf(Shield(Rcpp::wrap(Z)), Shield(Rcpp::wrap(x)), Shield(Rcpp::wrap(h))); + } + if (rcpp_result_gen.inherits("interrupted-error")) + throw Rcpp::internal::InterruptedException(); + if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen)) + throw Rcpp::LongjumpException(rcpp_result_gen); + if (rcpp_result_gen.inherits("try-error")) + throw Rcpp::exception(Rcpp::as(rcpp_result_gen).c_str()); + return Rcpp::as(rcpp_result_gen); + } + inline double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { typedef SEXP(*Ptr_log_volume_element)(SEXP,SEXP,SEXP); static Ptr_log_volume_element p_log_volume_element = NULL; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 12d3c87..42b6dcf 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -568,6 +568,78 @@ RcppExport SEXP _bsvarSIGNs_g_fh_vec(SEXP ZSEXP, SEXP vec_structuralSEXP) { UNPROTECT(1); return rcpp_result_gen; } +// D_z +arma::mat D_z(const arma::field& Z, const arma::vec& x, const double h); +static SEXP _bsvarSIGNs_D_z_try(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::vec& >::type x(xSEXP); + Rcpp::traits::input_parameter< const double >::type h(hSEXP); + rcpp_result_gen = Rcpp::wrap(D_z(Z, x, h)); + return rcpp_result_gen; +END_RCPP_RETURN_ERROR +} +RcppExport SEXP _bsvarSIGNs_D_z(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { + SEXP rcpp_result_gen; + { + Rcpp::RNGScope rcpp_rngScope_gen; + rcpp_result_gen = PROTECT(_bsvarSIGNs_D_z_try(ZSEXP, xSEXP, hSEXP)); + } + Rboolean rcpp_isInterrupt_gen = Rf_inherits(rcpp_result_gen, "interrupted-error"); + if (rcpp_isInterrupt_gen) { + UNPROTECT(1); + Rf_onintr(); + } + bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen); + if (rcpp_isLongjump_gen) { + Rcpp::internal::resumeJump(rcpp_result_gen); + } + Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error"); + if (rcpp_isError_gen) { + SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen); + UNPROTECT(1); + Rf_error("%s", CHAR(rcpp_msgSEXP_gen)); + } + UNPROTECT(1); + return rcpp_result_gen; +} +// D_gf +arma::mat D_gf(const arma::field& Z, const arma::vec& x, const double h); +static SEXP _bsvarSIGNs_D_gf_try(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::vec& >::type x(xSEXP); + Rcpp::traits::input_parameter< const double >::type h(hSEXP); + rcpp_result_gen = Rcpp::wrap(D_gf(Z, x, h)); + return rcpp_result_gen; +END_RCPP_RETURN_ERROR +} +RcppExport SEXP _bsvarSIGNs_D_gf(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { + SEXP rcpp_result_gen; + { + Rcpp::RNGScope rcpp_rngScope_gen; + rcpp_result_gen = PROTECT(_bsvarSIGNs_D_gf_try(ZSEXP, xSEXP, hSEXP)); + } + Rboolean rcpp_isInterrupt_gen = Rf_inherits(rcpp_result_gen, "interrupted-error"); + if (rcpp_isInterrupt_gen) { + UNPROTECT(1); + Rf_onintr(); + } + bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen); + if (rcpp_isLongjump_gen) { + Rcpp::internal::resumeJump(rcpp_result_gen); + } + Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error"); + if (rcpp_isError_gen) { + SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen); + UNPROTECT(1); + Rf_error("%s", CHAR(rcpp_msgSEXP_gen)); + } + UNPROTECT(1); + return rcpp_result_gen; +} // log_volume_element double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); static SEXP _bsvarSIGNs_log_volume_element_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { @@ -1061,6 +1133,8 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); + signatures.insert("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); + signatures.insert("arma::mat(*D_gf)(const arma::field&,const arma::vec&,const double)"); signatures.insert("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("double(*weight_zero)(const arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::mat(*rzeroQ)(const arma::field&,const arma::mat&)"); @@ -1090,6 +1164,8 @@ RcppExport SEXP _bsvarSIGNs_RcppExport_registerCCallable() { R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions", (DL_FUNC)_bsvarSIGNs_zero_restrictions_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh", (DL_FUNC)_bsvarSIGNs_g_fh_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh_vec", (DL_FUNC)_bsvarSIGNs_g_fh_vec_try); + R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_D_z", (DL_FUNC)_bsvarSIGNs_D_z_try); + R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_D_gf", (DL_FUNC)_bsvarSIGNs_D_gf_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_log_volume_element", (DL_FUNC)_bsvarSIGNs_log_volume_element_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_weight_zero", (DL_FUNC)_bsvarSIGNs_weight_zero_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_rzeroQ", (DL_FUNC)_bsvarSIGNs_rzeroQ_try); @@ -1118,6 +1194,8 @@ static const R_CallMethodDef CallEntries[] = { {"_bsvarSIGNs_zero_restrictions", (DL_FUNC) &_bsvarSIGNs_zero_restrictions, 2}, {"_bsvarSIGNs_g_fh", (DL_FUNC) &_bsvarSIGNs_g_fh, 3}, {"_bsvarSIGNs_g_fh_vec", (DL_FUNC) &_bsvarSIGNs_g_fh_vec, 2}, + {"_bsvarSIGNs_D_z", (DL_FUNC) &_bsvarSIGNs_D_z, 3}, + {"_bsvarSIGNs_D_gf", (DL_FUNC) &_bsvarSIGNs_D_gf, 3}, {"_bsvarSIGNs_log_volume_element", (DL_FUNC) &_bsvarSIGNs_log_volume_element, 3}, {"_bsvarSIGNs_weight_zero", (DL_FUNC) &_bsvarSIGNs_weight_zero, 4}, {"_bsvarSIGNs_rzeroQ", (DL_FUNC) &_bsvarSIGNs_rzeroQ, 2}, diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index f091cae..4ef714e 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -101,6 +101,69 @@ arma::colvec g_fh_vec( return g_fh(Z, A0, Aplus); } +// [[Rcpp::interfaces(cpp)]] +// [[Rcpp::export]] +arma::mat D_z( + const arma::field& Z, + const arma::vec& x, + const double h = 1e-10 +) +{ + vec f_x = zero_restrictions(Z, x); + + int n = x.n_elem; + int m = f_x.n_elem; + + mat result(m, n); + + for (int i = 0; i < n; i++) + { + vec x_plus_h = x; + x_plus_h(i) += h; + + vec f_plus_h = zero_restrictions(Z, x_plus_h); + + for (int j = 0; j < m; j++) + { + result(j, i) = (f_plus_h(j) - f_x(j)) / h; + } + } + + return result; +} + + +// [[Rcpp::interfaces(cpp)]] +// [[Rcpp::export]] +arma::mat D_gf( + const arma::field& Z, + const arma::vec& x, + const double h = 1e-10 +) +{ + vec f_x = g_fh_vec(Z, x); + + int n = x.n_elem; + int m = f_x.n_elem; + + mat result(m, n); + + for (int i = 0; i < n; i++) + { + vec x_plus_h = x; + x_plus_h(i) += h; + + vec f_plus_h = g_fh_vec(Z, x_plus_h); + + for (int j = 0; j < m; j++) + { + result(j, i) = (f_plus_h(j) - f_x(j)) / h; + } + } + + return result; +} + // log volume element // [[Rcpp::interfaces(cpp)]] @@ -112,8 +175,8 @@ double log_volume_element( ) { colvec vec_structural = join_vert(vectorise(A0), vectorise(Aplus)); - mat Dz = Df([Z](const colvec& x) { return zero_restrictions(Z, x); }, vec_structural); - mat Dgf = Df([Z](const colvec& x) { return g_fh_vec(Z, x); }, vec_structural); + mat Dz = D_z(Z, vec_structural); + mat Dgf = D_gf(Z, vec_structural); mat DN = Dgf * null(Dz); From 08412a5184be3a9a757908d0cf3cd3c77ebb5e39 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 12:42:53 +1000 Subject: [PATCH 45/51] debug messages --- inst/include/bsvarSIGNs_RcppExports.h | 4 ++-- src/RcppExports.cpp | 6 +++--- src/restrictions_zero.cpp | 18 +++++++++++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 2a45b8e..763b1e4 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural) { + inline arma::colvec zero_restrictions(const arma::field& Z, const arma::vec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); + validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,const arma::vec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 42b6dcf..5ee06c2 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -463,12 +463,12 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural); +arma::colvec zero_restrictions(const arma::field& Z, const arma::vec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); - Rcpp::traits::input_parameter< const arma::colvec >::type vec_structural(vec_structuralSEXP); + Rcpp::traits::input_parameter< const arma::vec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; END_RCPP_RETURN_ERROR @@ -1130,7 +1130,7 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); signatures.insert("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); + signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::vec)"); signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); signatures.insert("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 4ef714e..d553359 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -31,20 +31,32 @@ arma::field ZIRF( // [[Rcpp::export]] arma::colvec zero_restrictions( const arma::field& Z, - const arma::colvec vec_structural + const arma::vec vec_structural ) { int N = Z(0).n_cols; + Rcout << "debug 1" << std::endl; + mat A0 = vec_structural.rows(0, N * N - 1); A0 = reshape(A0, N, N); - arma::field ZF = ZIRF(Z, inv(A0.t())); + Rcout << "debug 2" << std::endl; + + field ZF = ZIRF(Z, inv(A0.t())); + + Rcout << "debug 3" << std::endl; + + vec z = vec(); + + Rcout << "debug 4" << std::endl; - colvec z; for (int j=0; j Date: Sat, 10 Aug 2024 13:02:53 +1000 Subject: [PATCH 46/51] debug --- src/restrictions_zero.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index d553359..2370545 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -42,7 +42,7 @@ arma::colvec zero_restrictions( Rcout << "debug 2" << std::endl; - field ZF = ZIRF(Z, inv(A0.t())); + // field ZF = ZIRF(Z, inv(A0.t())); Rcout << "debug 3" << std::endl; @@ -50,12 +50,18 @@ arma::colvec zero_restrictions( Rcout << "debug 4" << std::endl; - for (int j=0; j Date: Sat, 10 Aug 2024 13:20:52 +1000 Subject: [PATCH 47/51] Revert "debug" This reverts commit 455b3d0a75fc4c696f98da3afaa1596507f93cff. --- src/restrictions_zero.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 2370545..d553359 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -42,7 +42,7 @@ arma::colvec zero_restrictions( Rcout << "debug 2" << std::endl; - // field ZF = ZIRF(Z, inv(A0.t())); + field ZF = ZIRF(Z, inv(A0.t())); Rcout << "debug 3" << std::endl; @@ -50,18 +50,12 @@ arma::colvec zero_restrictions( Rcout << "debug 4" << std::endl; - mat irf_0 = inv(A0.t()); - - for (int j=0; j Date: Sat, 10 Aug 2024 13:20:58 +1000 Subject: [PATCH 48/51] Revert "debug messages" This reverts commit 08412a5184be3a9a757908d0cf3cd3c77ebb5e39. --- inst/include/bsvarSIGNs_RcppExports.h | 4 ++-- src/RcppExports.cpp | 6 +++--- src/restrictions_zero.cpp | 18 +++--------------- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 763b1e4..2a45b8e 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -277,11 +277,11 @@ namespace bsvarSIGNs { return Rcpp::as >(rcpp_result_gen); } - inline arma::colvec zero_restrictions(const arma::field& Z, const arma::vec vec_structural) { + inline arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural) { typedef SEXP(*Ptr_zero_restrictions)(SEXP,SEXP); static Ptr_zero_restrictions p_zero_restrictions = NULL; if (p_zero_restrictions == NULL) { - validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,const arma::vec)"); + validateSignature("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); p_zero_restrictions = (Ptr_zero_restrictions)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions"); } RObject rcpp_result_gen; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 5ee06c2..42b6dcf 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -463,12 +463,12 @@ RcppExport SEXP _bsvarSIGNs_ZIRF(SEXP ZSEXP, SEXP irf_0SEXP) { return rcpp_result_gen; } // zero_restrictions -arma::colvec zero_restrictions(const arma::field& Z, const arma::vec vec_structural); +arma::colvec zero_restrictions(const arma::field& Z, const arma::colvec vec_structural); static SEXP _bsvarSIGNs_zero_restrictions_try(SEXP ZSEXP, SEXP vec_structuralSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); - Rcpp::traits::input_parameter< const arma::vec >::type vec_structural(vec_structuralSEXP); + Rcpp::traits::input_parameter< const arma::colvec >::type vec_structural(vec_structuralSEXP); rcpp_result_gen = Rcpp::wrap(zero_restrictions(Z, vec_structural)); return rcpp_result_gen; END_RCPP_RETURN_ERROR @@ -1130,7 +1130,7 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("bool(*match_sign_narrative)(const arma::mat&,const arma::mat&,const arma::cube&)"); signatures.insert("double(*weight_narrative)(const int&,arma::mat,const arma::cube&)"); signatures.insert("arma::field(*ZIRF)(const arma::field&,const arma::mat&)"); - signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::vec)"); + signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); signatures.insert("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index d553359..4ef714e 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -31,32 +31,20 @@ arma::field ZIRF( // [[Rcpp::export]] arma::colvec zero_restrictions( const arma::field& Z, - const arma::vec vec_structural + const arma::colvec vec_structural ) { int N = Z(0).n_cols; - Rcout << "debug 1" << std::endl; - mat A0 = vec_structural.rows(0, N * N - 1); A0 = reshape(A0, N, N); - Rcout << "debug 2" << std::endl; - - field ZF = ZIRF(Z, inv(A0.t())); - - Rcout << "debug 3" << std::endl; - - vec z = vec(); - - Rcout << "debug 4" << std::endl; + arma::field ZF = ZIRF(Z, inv(A0.t())); + colvec z; for (int j=0; j Date: Sat, 10 Aug 2024 13:21:01 +1000 Subject: [PATCH 49/51] Revert "separate Df #43" This reverts commit f34ef73f0c2f60c43074b5ef7976446c9f9d1398. --- inst/include/bsvarSIGNs_RcppExports.h | 42 --------------- src/RcppExports.cpp | 78 --------------------------- src/restrictions_zero.cpp | 67 +---------------------- 3 files changed, 2 insertions(+), 185 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index 2a45b8e..e99db65 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -340,48 +340,6 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } - inline arma::mat D_z(const arma::field& Z, const arma::vec& x, const double h = 1e-10) { - typedef SEXP(*Ptr_D_z)(SEXP,SEXP,SEXP); - static Ptr_D_z p_D_z = NULL; - if (p_D_z == NULL) { - validateSignature("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); - p_D_z = (Ptr_D_z)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_D_z"); - } - RObject rcpp_result_gen; - { - RNGScope RCPP_rngScope_gen; - rcpp_result_gen = p_D_z(Shield(Rcpp::wrap(Z)), Shield(Rcpp::wrap(x)), Shield(Rcpp::wrap(h))); - } - if (rcpp_result_gen.inherits("interrupted-error")) - throw Rcpp::internal::InterruptedException(); - if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen)) - throw Rcpp::LongjumpException(rcpp_result_gen); - if (rcpp_result_gen.inherits("try-error")) - throw Rcpp::exception(Rcpp::as(rcpp_result_gen).c_str()); - return Rcpp::as(rcpp_result_gen); - } - - inline arma::mat D_gf(const arma::field& Z, const arma::vec& x, const double h = 1e-10) { - typedef SEXP(*Ptr_D_gf)(SEXP,SEXP,SEXP); - static Ptr_D_gf p_D_gf = NULL; - if (p_D_gf == NULL) { - validateSignature("arma::mat(*D_gf)(const arma::field&,const arma::vec&,const double)"); - p_D_gf = (Ptr_D_gf)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_D_gf"); - } - RObject rcpp_result_gen; - { - RNGScope RCPP_rngScope_gen; - rcpp_result_gen = p_D_gf(Shield(Rcpp::wrap(Z)), Shield(Rcpp::wrap(x)), Shield(Rcpp::wrap(h))); - } - if (rcpp_result_gen.inherits("interrupted-error")) - throw Rcpp::internal::InterruptedException(); - if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen)) - throw Rcpp::LongjumpException(rcpp_result_gen); - if (rcpp_result_gen.inherits("try-error")) - throw Rcpp::exception(Rcpp::as(rcpp_result_gen).c_str()); - return Rcpp::as(rcpp_result_gen); - } - inline double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { typedef SEXP(*Ptr_log_volume_element)(SEXP,SEXP,SEXP); static Ptr_log_volume_element p_log_volume_element = NULL; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 42b6dcf..12d3c87 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -568,78 +568,6 @@ RcppExport SEXP _bsvarSIGNs_g_fh_vec(SEXP ZSEXP, SEXP vec_structuralSEXP) { UNPROTECT(1); return rcpp_result_gen; } -// D_z -arma::mat D_z(const arma::field& Z, const arma::vec& x, const double h); -static SEXP _bsvarSIGNs_D_z_try(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); - Rcpp::traits::input_parameter< const arma::vec& >::type x(xSEXP); - Rcpp::traits::input_parameter< const double >::type h(hSEXP); - rcpp_result_gen = Rcpp::wrap(D_z(Z, x, h)); - return rcpp_result_gen; -END_RCPP_RETURN_ERROR -} -RcppExport SEXP _bsvarSIGNs_D_z(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { - SEXP rcpp_result_gen; - { - Rcpp::RNGScope rcpp_rngScope_gen; - rcpp_result_gen = PROTECT(_bsvarSIGNs_D_z_try(ZSEXP, xSEXP, hSEXP)); - } - Rboolean rcpp_isInterrupt_gen = Rf_inherits(rcpp_result_gen, "interrupted-error"); - if (rcpp_isInterrupt_gen) { - UNPROTECT(1); - Rf_onintr(); - } - bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen); - if (rcpp_isLongjump_gen) { - Rcpp::internal::resumeJump(rcpp_result_gen); - } - Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error"); - if (rcpp_isError_gen) { - SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen); - UNPROTECT(1); - Rf_error("%s", CHAR(rcpp_msgSEXP_gen)); - } - UNPROTECT(1); - return rcpp_result_gen; -} -// D_gf -arma::mat D_gf(const arma::field& Z, const arma::vec& x, const double h); -static SEXP _bsvarSIGNs_D_gf_try(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); - Rcpp::traits::input_parameter< const arma::vec& >::type x(xSEXP); - Rcpp::traits::input_parameter< const double >::type h(hSEXP); - rcpp_result_gen = Rcpp::wrap(D_gf(Z, x, h)); - return rcpp_result_gen; -END_RCPP_RETURN_ERROR -} -RcppExport SEXP _bsvarSIGNs_D_gf(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { - SEXP rcpp_result_gen; - { - Rcpp::RNGScope rcpp_rngScope_gen; - rcpp_result_gen = PROTECT(_bsvarSIGNs_D_gf_try(ZSEXP, xSEXP, hSEXP)); - } - Rboolean rcpp_isInterrupt_gen = Rf_inherits(rcpp_result_gen, "interrupted-error"); - if (rcpp_isInterrupt_gen) { - UNPROTECT(1); - Rf_onintr(); - } - bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen); - if (rcpp_isLongjump_gen) { - Rcpp::internal::resumeJump(rcpp_result_gen); - } - Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error"); - if (rcpp_isError_gen) { - SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen); - UNPROTECT(1); - Rf_error("%s", CHAR(rcpp_msgSEXP_gen)); - } - UNPROTECT(1); - return rcpp_result_gen; -} // log_volume_element double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); static SEXP _bsvarSIGNs_log_volume_element_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { @@ -1133,8 +1061,6 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); - signatures.insert("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); - signatures.insert("arma::mat(*D_gf)(const arma::field&,const arma::vec&,const double)"); signatures.insert("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("double(*weight_zero)(const arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::mat(*rzeroQ)(const arma::field&,const arma::mat&)"); @@ -1164,8 +1090,6 @@ RcppExport SEXP _bsvarSIGNs_RcppExport_registerCCallable() { R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions", (DL_FUNC)_bsvarSIGNs_zero_restrictions_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh", (DL_FUNC)_bsvarSIGNs_g_fh_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh_vec", (DL_FUNC)_bsvarSIGNs_g_fh_vec_try); - R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_D_z", (DL_FUNC)_bsvarSIGNs_D_z_try); - R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_D_gf", (DL_FUNC)_bsvarSIGNs_D_gf_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_log_volume_element", (DL_FUNC)_bsvarSIGNs_log_volume_element_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_weight_zero", (DL_FUNC)_bsvarSIGNs_weight_zero_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_rzeroQ", (DL_FUNC)_bsvarSIGNs_rzeroQ_try); @@ -1194,8 +1118,6 @@ static const R_CallMethodDef CallEntries[] = { {"_bsvarSIGNs_zero_restrictions", (DL_FUNC) &_bsvarSIGNs_zero_restrictions, 2}, {"_bsvarSIGNs_g_fh", (DL_FUNC) &_bsvarSIGNs_g_fh, 3}, {"_bsvarSIGNs_g_fh_vec", (DL_FUNC) &_bsvarSIGNs_g_fh_vec, 2}, - {"_bsvarSIGNs_D_z", (DL_FUNC) &_bsvarSIGNs_D_z, 3}, - {"_bsvarSIGNs_D_gf", (DL_FUNC) &_bsvarSIGNs_D_gf, 3}, {"_bsvarSIGNs_log_volume_element", (DL_FUNC) &_bsvarSIGNs_log_volume_element, 3}, {"_bsvarSIGNs_weight_zero", (DL_FUNC) &_bsvarSIGNs_weight_zero, 4}, {"_bsvarSIGNs_rzeroQ", (DL_FUNC) &_bsvarSIGNs_rzeroQ, 2}, diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index 4ef714e..f091cae 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -101,69 +101,6 @@ arma::colvec g_fh_vec( return g_fh(Z, A0, Aplus); } -// [[Rcpp::interfaces(cpp)]] -// [[Rcpp::export]] -arma::mat D_z( - const arma::field& Z, - const arma::vec& x, - const double h = 1e-10 -) -{ - vec f_x = zero_restrictions(Z, x); - - int n = x.n_elem; - int m = f_x.n_elem; - - mat result(m, n); - - for (int i = 0; i < n; i++) - { - vec x_plus_h = x; - x_plus_h(i) += h; - - vec f_plus_h = zero_restrictions(Z, x_plus_h); - - for (int j = 0; j < m; j++) - { - result(j, i) = (f_plus_h(j) - f_x(j)) / h; - } - } - - return result; -} - - -// [[Rcpp::interfaces(cpp)]] -// [[Rcpp::export]] -arma::mat D_gf( - const arma::field& Z, - const arma::vec& x, - const double h = 1e-10 -) -{ - vec f_x = g_fh_vec(Z, x); - - int n = x.n_elem; - int m = f_x.n_elem; - - mat result(m, n); - - for (int i = 0; i < n; i++) - { - vec x_plus_h = x; - x_plus_h(i) += h; - - vec f_plus_h = g_fh_vec(Z, x_plus_h); - - for (int j = 0; j < m; j++) - { - result(j, i) = (f_plus_h(j) - f_x(j)) / h; - } - } - - return result; -} - // log volume element // [[Rcpp::interfaces(cpp)]] @@ -175,8 +112,8 @@ double log_volume_element( ) { colvec vec_structural = join_vert(vectorise(A0), vectorise(Aplus)); - mat Dz = D_z(Z, vec_structural); - mat Dgf = D_gf(Z, vec_structural); + mat Dz = Df([Z](const colvec& x) { return zero_restrictions(Z, x); }, vec_structural); + mat Dgf = Df([Z](const colvec& x) { return g_fh_vec(Z, x); }, vec_structural); mat DN = Dgf * null(Dz); From cc6db2de58ca35ac11242af805535bb5a6921a6f Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 13:26:44 +1000 Subject: [PATCH 50/51] check if Z has 0 rows #43 --- src/restrictions_zero.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index f091cae..6090c6c 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -34,15 +34,15 @@ arma::colvec zero_restrictions( const arma::colvec vec_structural ) { int N = Z(0).n_cols; - - mat A0 = vec_structural.rows(0, N * N - 1); - A0 = reshape(A0, N, N); + + mat A0 = reshape(vec_structural.rows(0, N * N - 1), N, N); arma::field ZF = ZIRF(Z, inv(A0.t())); - colvec z; + vec z; for (int j=0; j 0) z = join_vert(z, ZF_j.col(j)); } return z; From 55a62a5eb30e5e7a2da37c3a91508e00016ed8d4 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 13:44:37 +1000 Subject: [PATCH 51/51] check sympd instead of catch #43 --- src/sample_hyper.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/sample_hyper.cpp b/src/sample_hyper.cpp index 4e5ec8a..28fb2c5 100644 --- a/src/sample_hyper.cpp +++ b/src/sample_hyper.cpp @@ -1,4 +1,5 @@ +#define ARMA_WARN_LEVEL 1 #include #include "utils.h" @@ -108,25 +109,26 @@ double log_ml( int N = Y.n_cols; double log_ml = 0; + mat inv_Omega = diagmat(1 / Omega.diag()); + mat XX = X.t() * X + inv_Omega; - try { - mat Bhat = inv_sympd(X.t() * X + inv_Omega) * (X.t() * Y + inv_Omega * b); - mat ehat = Y - X * Bhat; - - log_ml += - N * T / 2.0 * log(M_PI); - log_ml += log_mvgamma(N, (T + d) / 2.0); - log_ml += -log_mvgamma(N, d / 2.0); - log_ml += - N / 2.0 * log_det_sympd(Omega); - log_ml += d / 2.0 * log_det_sympd(Psi); - log_ml += - N / 2.0 * log_det_sympd(X.t() * X + inv_Omega); - mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); - log_ml += - (T + d) / 2.0 * log_det_sympd(A); - - } catch(...) { - log_ml = -1e+10; + if (!Omega.is_sympd() or !Psi.is_sympd() or !XX.is_sympd()) { + return -1e10; } + mat Bhat = solve(XX, X.t() * Y + inv_Omega * b, solve_opts::likely_sympd); + mat ehat = Y - X * Bhat; + + log_ml += - N * T / 2.0 * log(M_PI); + log_ml += log_mvgamma(N, (T + d) / 2.0); + log_ml += -log_mvgamma(N, d / 2.0); + log_ml += - N / 2.0 * log_det_sympd(Omega); + log_ml += d / 2.0 * log_det_sympd(Psi); + log_ml += - N / 2.0 * log_det_sympd(XX); + mat A = Psi + ehat.t() * ehat + (Bhat - b).t() * inv_Omega * (Bhat - b); + log_ml += - (T + d) / 2.0 * log_det_sympd(A); + return log_ml; }