Skip to content

Commit

Permalink
Add post_gamma_state_variance
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmohr committed Oct 19, 2023
1 parent 4144d63 commit ccae0f2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ post_coint_kls_sur <- function(y, beta, w, sigma_i, v_i, p_tau_i, g_i, x = NULL,
#'
#' @return A matrix.
#'
post_gamma_state_variance <- function(a, a_init, shape_prior, rate_prior, inverse = FALSE) {
post_gamma_state_variance <- function(a, a_init, shape_prior, rate_prior, inverse) {
.Call(`_bvartools_post_gamma_state_variance`, a, a_init, shape_prior, rate_prior, inverse)
}

Expand Down
2 changes: 1 addition & 1 deletion man/post_gamma_state_variance.Rd

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

14 changes: 7 additions & 7 deletions src/post_gamma_state_variance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//' @return A matrix.
//'
// [[Rcpp::export]]
arma::mat post_gamma_state_variance(arma::mat a, arma::vec a_init, arma::vec shape_prior, arma::vec rate_prior, bool inverse = false) {
arma::mat post_gamma_state_variance(arma::mat a, arma::vec a_init, arma::vec shape_prior, arma::vec rate_prior, bool inverse) {

int k = a.n_rows;
int tt = a.n_cols;
Expand All @@ -59,16 +59,16 @@ arma::mat post_gamma_state_variance(arma::mat a, arma::vec a_init, arma::vec sha
a_lag.cols(1, tt - 1) = a.cols(0, tt - 2);
arma::mat a_v = arma::trans(a - a_lag);
arma::vec psi_sigma_v_post_scale = 1 / (rate_prior + arma::vectorise(arma::sum(arma::pow(a_v, 2))) * 0.5);
arma::mat psi_sigma = arma::zeros<arma::mat>(k, k);
arma::mat result = arma::zeros<arma::mat>(k, k);
arma::vec shape_post = shape_prior + tt * 0.5;
for (int i = 0; i < k; i++) {
psi_sigma(i, i) = arma::randg<double>(arma::distr_param(shape_post(i), psi_sigma_v_post_scale(i)));
if (inverse) {
psi_sigma(i, i) = 1 / psi_sigma(i, i);
// Inverse
result(i, i) = arma::randg<double>(arma::distr_param(shape_post(i), psi_sigma_v_post_scale(i)));
if (!inverse) {
result(i, i) = 1 / result(i, i);
}
}

return psi_sigma;
return result;
}

/*** R
Expand Down

0 comments on commit ccae0f2

Please sign in to comment.