Skip to content

Commit

Permalink
zero logvars for homoscedastic factors
Browse files Browse the repository at this point in the history
  • Loading branch information
Gruber committed Feb 2, 2024
1 parent 19573f6 commit 86895cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# bayesianVARs (development version)

* bugfix concerning VAR with factor structure on errors with homoscedastic factors.

# bayesianVARs 0.1.2

* Added minimum version to factorstochvol in the Imports field of the DESCRIPTION file in order to avoid unnecessary building errors. Thanks to Sergey Fedorov for pointing this out.
Expand Down
2 changes: 1 addition & 1 deletion R/bvar_wrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ bvar <- function(data,
phi = c(rep(.8, M), rep(.8, prior_sigma$factor_factors)) + pmin(rnorm(M + prior_sigma$factor_factors, sd=.06), .095),
sigma = rep(.1, M + prior_sigma$factor_factors) + rgamma(M + prior_sigma$factor_factors, 1, 10))
startlogvar <- matrix(startpara["mu",][1] + rnorm(Tobs*(M + prior_sigma$factor_factors)), Tobs, M + prior_sigma$factor_factors)
startlogvar[,M+which(isFALSE(prior_sigma$sv_heteroscedastic[-c(1:M)]))] <- 0 # !!! important, needed for factorstochvol: if factor is assumed to be homoscedastic, the corresponding column in logvar has to be 0!!!
startlogvar[,M+which(prior_sigma$sv_heteroscedastic[-c(1:M)]==FALSE)] <- 0 # !!! important, needed for factorstochvol: if factor is assumed to be homoscedastic, the corresponding column in logvar has to be 0!!!
startlogvar0 <- startpara["mu",][1] + rnorm(M + prior_sigma$factor_factors)
starttau2 <- if(!prior_sigma$factor_ngprior){ # if prior is 'normal'
prior_sigma$factor_starttau2
Expand Down
20 changes: 18 additions & 2 deletions tests/testthat/test-bvar.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ test_that("flat prior factor", {
factor_priorfacloadtype = "normal",
factor_priorfacload = 1e6,
factor_heteroskedastic = c(FALSE, FALSE),
factor_priorhomoskedastic = matrix(c(0.01,.01), ncol(data), 2))
factor_priorhomoskedastic = matrix(c(1e-6,1e-6), ncol(data), 2))
set.seed(123)
mod <- bvar(data, lags = 2, prior_intercept = 1e6, prior_phi = phi,
prior_sigma = sigma, draws = 10000)
Expand All @@ -122,7 +122,7 @@ test_that("flat prior cholesky", {
cholesky_U_prior = "normal",
cholesky_normal_sds = 1e6,
cholesky_heteroscedastic = FALSE,
cholesky_priorhomoscedastic = matrix(c(0.01,0.01), ncol(data), 2))
cholesky_priorhomoscedastic = matrix(c(1e-6,1e-6), ncol(data), 2))
set.seed(123)
mod <- bvar(data, lags = 2, prior_intercept = 1e6, prior_phi = phi,
prior_sigma = sigma, draws = 10000)
Expand All @@ -131,3 +131,19 @@ test_that("flat prior cholesky", {

expect_lt(max(abs(ols-phi_post_mean)),0.01)
})

test_that("homoscedastic factor VAR input", {
data <- usmacro_growth[,c("GDPC1","CPIAUCSL","FEDFUNDS")]
phi <- specify_prior_phi(data = data, lags = 2L, prior = "normal",
normal_sds = 1e6)
sigma <- specify_prior_sigma(data = data, type = "factor",
factor_factors = 2,
factor_priorfacloadtype = "normal",
factor_priorfacload = 1e6,
factor_heteroskedastic = c(FALSE, FALSE),
factor_priorhomoskedastic = matrix(c(1e-6,1e-6), ncol(data), 2))

mod <- bvar(data, lags = 2, prior_intercept = 1e6, prior_phi = phi,
prior_sigma = sigma, draws = 10, burnin = 10)
expect_true(all(mod$logvar[,-seq_len(ncol(mod$Y)),]==0))
})

0 comments on commit 86895cd

Please sign in to comment.