ankargren / mfbvar

R package for Mixed-Frequency Bayesian VARs
https://ankargren.github.io/mfbvar
38 stars 21 forks source link

Setting prior_interval when d is matrix with more than 1 column #7

Closed alexhubbardOD closed 5 years ago

alexhubbardOD commented 5 years ago

Having difficulty setting the prior for a model with more than 1 deterministic variables.

In case 1 with 2 deterministic variables and 2 variables in Y, I set the prior_intervals to have 2 rows (for 2 variables) like:


  prior_obj <- set_prior(Y = mf_sweden[, 4:5], d = matrix(rnorm(nrow(mf_sweden)*2), ncol = 2),
                           freq = c("m", "q"), n_lags = 4, n_burnin = 20, n_reps = 20,
                           n_fcst = 4)
prior_intervals <- matrix(c(-0.1, 0.1,
                                0.4, 0.6), ncol = 2, byrow = TRUE)
psi_moments <- interval_to_moments(prior_intervals)
prior_psi_mean <- psi_moments$prior_psi_mean
prior_psi_Omega <- psi_moments$prior_psi_Omega
prior_obj <- update_prior(prior_obj,
prior_psi_mean = prior_psi_mean,
prior_psi_Omega = prior_psi_Omega)
mod_ss <- estimate_mfbvar(prior_obj, prior_type = "ss")

which gives the error "Error in t(U) %% (kronecker(crossprod(D_mat), chol2inv(chol(Sigma)))) %% : non-conformable arrays".

In case 2 with 2 deterministic variables and 2 variables in Y, I set the prior_intervals to have 4 rows (for 2 variables 2 deterministic) as is suggested in the documentation for interval_to_moments ("prior_psi_int | Matrix of size (n_determn_vars) * 2") like:


prior_obj <- set_prior(Y = mf_sweden[, 4:5], d = matrix(rnorm(nrow(mf_sweden)*2), ncol = 2),
                           freq = c("m", "q"), n_lags = 4, n_burnin = 20, n_reps = 20,
                           n_fcst = 4)
prior_intervals <- matrix(rep(c(-0.1, 0.1,
                                0.4, 0.6), 2), ncol = 2, byrow = TRUE)
psi_moments <- interval_to_moments(prior_intervals)
prior_psi_mean <- psi_moments$prior_psi_mean
prior_psi_Omega <- psi_moments$prior_psi_Omega
prior_obj <- update_prior(prior_obj,
                              prior_psi_mean = prior_psi_mean,
                              prior_psi_Omega = prior_psi_Omega)

which gives the error "prior_psi_mean has 4 elements, but there are 2 variables in Y.".

1) What is the correct way to set the prior for multiple deterministic variables? and 2) Is the model allowed to have multiple deterministic variables?

ankargren commented 5 years ago

The second way is the correct one, but for some reason there was a check that effectively enforced use of a single deterministic variable. I have fixed this now, so if you install the Github version (devtools::install_github("ankargren/mfbvar")) it should work properly now. Here is one example, where we have a dummy variable for the first 50 observations:

d <- matrix(1, nrow(mf_sweden), 2)
d[-(1:50), 2] <- 0
prior_obj <- set_prior(Y = mf_sweden[, 4:5], d = d,
                       d_fcst = d[51:54, ],
                       freq = c("m", "q"), n_lags = 4, n_burnin = 1000, n_reps = 1000,
                       n_fcst = 4)
prior_intervals <- matrix(c(-0.1, 0.1,
                            0.4, 0.6,
                            -0.2, 0.2,
                            -0.2, 0.2), ncol = 2, byrow = TRUE)
psi_moments <- interval_to_moments(prior_intervals)
prior_obj <- update_prior(prior_obj,
                          prior_psi_mean = psi_moments$prior_psi_mean,
                          prior_psi_Omega = psi_moments$prior_psi_Omega)
mod_ss <- estimate_mfbvar(prior_obj, prior_type = "ss")
mod_ss <- estimate_mfbvar(prior_obj, prior_type = "ss")
plot(mod_ss, plot_start = 1)

I will see if I can get this to CRAN soon too. Thanks for the report!

alexhubbardOD commented 5 years ago

Thanks. Glad to help!

On Wed, May 8, 2019 at 11:44 PM Sebastian Ankargren < notifications@github.com> wrote:

The second way is the correct one, but for some reason there was a check that effectively enforced use of a single deterministic variable. I have fixed this now, so if you install the Github version ( devtools::install_github("ankargren/mfbvar")) it should work properly now. Here is one example, where we have a dummy variable for the first 50 observations:

d <- matrix(1, nrow(mf_sweden), 2) d[-(1:50), 2] <- 0 prior_obj <- set_prior(Y = mf_sweden[, 4:5], d = d, d_fcst = d[51:54, ], freq = c("m", "q"), n_lags = 4, n_burnin = 1000, n_reps = 1000, n_fcst = 4) prior_intervals <- matrix(c(-0.1, 0.1, 0.4, 0.6, -0.2, 0.2, -0.2, 0.2), ncol = 2, byrow = TRUE) psi_moments <- interval_to_moments(prior_intervals) prior_obj$prior_psi_mean <- psi_moments$prior_psi_mean prior_obj$prior_psi_Omega <- psi_moments$prior_psi_Omega mod_ss <- estimate_mfbvar(prior_obj, prior_type = "ss") plot(mod_ss, plot_start = 1)

I will see if I can get this to CRAN soon too. Thanks for the report!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ankargren/mfbvar/issues/7#issuecomment-490765323, or mute the thread https://github.com/notifications/unsubscribe-auth/AJTB44RC4IKLKAQKW4V4CS3PUPB5RANCNFSM4HLTQFLA .

-- Alex Hubbard, PhD Portfolio and Macro Risk Associate, Economist Opendoor Labs, Inc. alex.hubbard@opendoor.com