Skip to content

Commit

Permalink
changing notation to report A_c and Sigma_c in arrays #2
Browse files Browse the repository at this point in the history
+ required correcting the sampler with thinning
+ implemented via specify_posterior 😄
  • Loading branch information
donotdespair committed May 12, 2024
1 parent 47aae7e commit 906a6bd
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 34 deletions.
12 changes: 6 additions & 6 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ sample_s <- function(aux_A, aux_V, aux_Sigma, aux_m, prior) {
.Call(`_bvarPANELs_sample_s`, aux_A, aux_V, aux_Sigma, aux_m, prior)
}

log_kernel_nu <- function(aux_nu, aux_Sigma_c, aux_Sigma, prior_lambda, C, N, K) {
.Call(`_bvarPANELs_log_kernel_nu`, aux_nu, aux_Sigma_c, aux_Sigma, prior_lambda, C, N, K)
log_kernel_nu <- function(aux_nu, aux_Sigma_c_cpp, aux_Sigma, prior_lambda, C, N, K) {
.Call(`_bvarPANELs_log_kernel_nu`, aux_nu, aux_Sigma_c_cpp, aux_Sigma, prior_lambda, C, N, K)
}

sample_nu <- function(aux_nu, aux_Sigma_c, aux_Sigma, prior) {
.Call(`_bvarPANELs_sample_nu`, aux_nu, aux_Sigma_c, aux_Sigma, prior)
sample_nu <- function(aux_nu, aux_Sigma_c_cpp, aux_Sigma, prior) {
.Call(`_bvarPANELs_sample_nu`, aux_nu, aux_Sigma_c_cpp, aux_Sigma, prior)
}

sample_Sigma <- function(aux_Sigma_c_inv, aux_s, aux_nu, prior) {
.Call(`_bvarPANELs_sample_Sigma`, aux_Sigma_c_inv, aux_s, aux_nu, prior)
}

sample_AV <- function(aux_A_c, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior) {
.Call(`_bvarPANELs_sample_AV`, aux_A_c, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior)
sample_AV <- function(aux_A_c_cpp, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior) {
.Call(`_bvarPANELs_sample_AV`, aux_A_c_cpp, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior)
}

sample_A_c_Sigma_c <- function(Y_c, X_c, aux_A, aux_V, aux_Sigma, aux_nu) {
Expand Down
2 changes: 1 addition & 1 deletion R/estimate.bvarPANEL.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Autoregression using Gibbs sampler
#'
#' @description Estimates the Bayesian Hierarchical Panel VAR using the Gibbs
#' sampler proposed by Woźniak (2024).
#' sampler proposed by Sanchez-Martinez & Woźniak (2024).
#'
#' @details
#' The homoskedastic SVAR model is given by the reduced form equation:
Expand Down
16 changes: 16 additions & 0 deletions R/specify_bvarpanel.R
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,22 @@ specify_posterior_bvarPANEL = R6::R6Class(

self$last_draw = specification_bvarPANEL
self$posterior = posterior_bvarPANEL

N = dim(specification_bvarPANEL$starting_values$A_c)[2]
K = dim(specification_bvarPANEL$starting_values$A_c)[1]
C = dim(specification_bvarPANEL$starting_values$A_c)[3]
S = dim(posterior_bvarPANEL$A)[3]

Sigma_c = array(NA, c(N, N, C, S))
A_c = array(NA, c(K, N, C, S))
for (s in 1:S) {
A_c[,,,s] = posterior_bvarPANEL$A_c_cpp[s,1][[1]]
Sigma_c[,,,s] = posterior_bvarPANEL$Sigma_c_cpp[s,1][[1]]
}
self$posterior$Sigma_c = Sigma_c
self$posterior$A_c = A_c


}, # END initialize

#' @description
Expand Down
2 changes: 1 addition & 1 deletion man/estimate.BVARPANEL.Rd

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

2 changes: 1 addition & 1 deletion man/estimate.PosteriorBVARPANEL.Rd

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

24 changes: 12 additions & 12 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,33 @@ BEGIN_RCPP
END_RCPP
}
// log_kernel_nu
double log_kernel_nu(const double& aux_nu, const arma::cube& aux_Sigma_c, const arma::mat& aux_Sigma, const double& prior_lambda, const int& C, const int& N, const int& K);
RcppExport SEXP _bvarPANELs_log_kernel_nu(SEXP aux_nuSEXP, SEXP aux_Sigma_cSEXP, SEXP aux_SigmaSEXP, SEXP prior_lambdaSEXP, SEXP CSEXP, SEXP NSEXP, SEXP KSEXP) {
double log_kernel_nu(const double& aux_nu, const arma::cube& aux_Sigma_c_cpp, const arma::mat& aux_Sigma, const double& prior_lambda, const int& C, const int& N, const int& K);
RcppExport SEXP _bvarPANELs_log_kernel_nu(SEXP aux_nuSEXP, SEXP aux_Sigma_c_cppSEXP, SEXP aux_SigmaSEXP, SEXP prior_lambdaSEXP, SEXP CSEXP, SEXP NSEXP, SEXP KSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const double& >::type aux_nu(aux_nuSEXP);
Rcpp::traits::input_parameter< const arma::cube& >::type aux_Sigma_c(aux_Sigma_cSEXP);
Rcpp::traits::input_parameter< const arma::cube& >::type aux_Sigma_c_cpp(aux_Sigma_c_cppSEXP);
Rcpp::traits::input_parameter< const arma::mat& >::type aux_Sigma(aux_SigmaSEXP);
Rcpp::traits::input_parameter< const double& >::type prior_lambda(prior_lambdaSEXP);
Rcpp::traits::input_parameter< const int& >::type C(CSEXP);
Rcpp::traits::input_parameter< const int& >::type N(NSEXP);
Rcpp::traits::input_parameter< const int& >::type K(KSEXP);
rcpp_result_gen = Rcpp::wrap(log_kernel_nu(aux_nu, aux_Sigma_c, aux_Sigma, prior_lambda, C, N, K));
rcpp_result_gen = Rcpp::wrap(log_kernel_nu(aux_nu, aux_Sigma_c_cpp, aux_Sigma, prior_lambda, C, N, K));
return rcpp_result_gen;
END_RCPP
}
// sample_nu
double sample_nu(const double& aux_nu, const arma::cube& aux_Sigma_c, const arma::mat& aux_Sigma, const Rcpp::List& prior);
RcppExport SEXP _bvarPANELs_sample_nu(SEXP aux_nuSEXP, SEXP aux_Sigma_cSEXP, SEXP aux_SigmaSEXP, SEXP priorSEXP) {
double sample_nu(const double& aux_nu, const arma::cube& aux_Sigma_c_cpp, const arma::mat& aux_Sigma, const Rcpp::List& prior);
RcppExport SEXP _bvarPANELs_sample_nu(SEXP aux_nuSEXP, SEXP aux_Sigma_c_cppSEXP, SEXP aux_SigmaSEXP, SEXP priorSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const double& >::type aux_nu(aux_nuSEXP);
Rcpp::traits::input_parameter< const arma::cube& >::type aux_Sigma_c(aux_Sigma_cSEXP);
Rcpp::traits::input_parameter< const arma::cube& >::type aux_Sigma_c_cpp(aux_Sigma_c_cppSEXP);
Rcpp::traits::input_parameter< const arma::mat& >::type aux_Sigma(aux_SigmaSEXP);
Rcpp::traits::input_parameter< const Rcpp::List& >::type prior(priorSEXP);
rcpp_result_gen = Rcpp::wrap(sample_nu(aux_nu, aux_Sigma_c, aux_Sigma, prior));
rcpp_result_gen = Rcpp::wrap(sample_nu(aux_nu, aux_Sigma_c_cpp, aux_Sigma, prior));
return rcpp_result_gen;
END_RCPP
}
Expand All @@ -130,18 +130,18 @@ BEGIN_RCPP
END_RCPP
}
// sample_AV
arma::field<arma::mat> sample_AV(const arma::cube& aux_A_c, const arma::cube& aux_Sigma_c_inv, const double& aux_s, const double& aux_m, const double& aux_w, const Rcpp::List& prior);
RcppExport SEXP _bvarPANELs_sample_AV(SEXP aux_A_cSEXP, SEXP aux_Sigma_c_invSEXP, SEXP aux_sSEXP, SEXP aux_mSEXP, SEXP aux_wSEXP, SEXP priorSEXP) {
arma::field<arma::mat> sample_AV(const arma::cube& aux_A_c_cpp, const arma::cube& aux_Sigma_c_inv, const double& aux_s, const double& aux_m, const double& aux_w, const Rcpp::List& prior);
RcppExport SEXP _bvarPANELs_sample_AV(SEXP aux_A_c_cppSEXP, SEXP aux_Sigma_c_invSEXP, SEXP aux_sSEXP, SEXP aux_mSEXP, SEXP aux_wSEXP, SEXP priorSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const arma::cube& >::type aux_A_c(aux_A_cSEXP);
Rcpp::traits::input_parameter< const arma::cube& >::type aux_A_c_cpp(aux_A_c_cppSEXP);
Rcpp::traits::input_parameter< const arma::cube& >::type aux_Sigma_c_inv(aux_Sigma_c_invSEXP);
Rcpp::traits::input_parameter< const double& >::type aux_s(aux_sSEXP);
Rcpp::traits::input_parameter< const double& >::type aux_m(aux_mSEXP);
Rcpp::traits::input_parameter< const double& >::type aux_w(aux_wSEXP);
Rcpp::traits::input_parameter< const Rcpp::List& >::type prior(priorSEXP);
rcpp_result_gen = Rcpp::wrap(sample_AV(aux_A_c, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior));
rcpp_result_gen = Rcpp::wrap(sample_AV(aux_A_c_cpp, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior));
return rcpp_result_gen;
END_RCPP
}
Expand Down
28 changes: 15 additions & 13 deletions src/bvarPANEL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ Rcpp::List bvarPANEL(
const int N = aux_A.n_cols;
const int K = aux_A.n_rows;

field<cube> posterior_A_c(S);
field<cube> posterior_Sigma_c(S);
cube posterior_A(K, N, S);
cube posterior_V(K, K, S);
cube posterior_Sigma(N, N, S);
vec posterior_nu(S);
vec posterior_m(S);
vec posterior_w(S);
vec posterior_s(S);
const int SS = floor(S / thin);

field<cube> posterior_A_c_cpp(SS);
field<cube> posterior_Sigma_c_cpp(SS);
cube posterior_A(K, N, SS);
cube posterior_V(K, K, SS);
cube posterior_Sigma(N, N, SS);
vec posterior_nu(SS);
vec posterior_m(SS);
vec posterior_w(SS);
vec posterior_s(SS);

field<mat> y(C);
field<mat> x(C);
Expand Down Expand Up @@ -120,8 +122,8 @@ Rcpp::List bvarPANEL(
} // END c loop

if (s % thin == 0) {
posterior_A_c(ss) = aux_A_c;
posterior_Sigma_c(ss) = aux_Sigma_c;
posterior_A_c_cpp(ss) = aux_A_c;
posterior_Sigma_c_cpp(ss) = aux_Sigma_c;
posterior_A.slice(ss) = aux_A;
posterior_V.slice(ss) = aux_V;
posterior_Sigma.slice(ss) = aux_Sigma;
Expand All @@ -147,8 +149,8 @@ Rcpp::List bvarPANEL(
_["s"] = aux_s
),
_["posterior"] = List::create(
_["A_c"] = posterior_A_c,
_["Sigma_c"] = posterior_Sigma_c,
_["A_c_cpp"] = posterior_A_c_cpp,
_["Sigma_c_cpp"] = posterior_Sigma_c_cpp,
_["A"] = posterior_A,
_["V"] = posterior_V,
_["Sigma"] = posterior_Sigma,
Expand Down

0 comments on commit 906a6bd

Please sign in to comment.