ankargren / mfbvar

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

Model with many variables (about 20) and 4 lags causes error #12

Open alexhubbardOD opened 4 years ago

alexhubbardOD commented 4 years ago

Apologies for not being able to provide an example. When trying to estimate a model with a large number of variables (about 20, all differenced for stationarity) 4 lags and steady state prior, the estimation throws an error with message

"error: inv_sympd(): matrix is singular or not positive definite Error in mcmc_ssng_iw(Y[-(1:n_lags), ], Pi, Sigma, psi, phi_mu, lambda_mu, : inv_sympd(): matrix is singular or not positive definite"

This error only occurs in version 0.5.1 but 0.4.0. It's likely that there's high correlation among the variables. Is this error appropriate or should the estimation technique be able to handle a large system with highly correlated variables?

ankargren commented 4 years ago

Could you try some other specifications? In particular, a Minnesota-type specification and the same that you used but with tighter steady-state priors would be good to see. I've had similar issues with large models with the steady-state prior estimated on quarterly data, and the remedy has been to tighten up the prior intervals.

dinhyennhi93 commented 3 years ago

I have the same problem when I try a model with 21 variables.

library("mfbvar")
prior <- set_prior(Y = mf_list, n_lags = 4, n_reps = 1000)
c_interval = t(sapply(mf_list, CI, ci=0.95))
prior_intervals = c_interval[c(TRUE, TRUE, TRUE),c("upper","lower")]
moments <- interval_to_moments(prior_intervals)
prior <- update_prior(prior,
                      d = "intercept",
                      prior_psi_mean = moments$prior_psi_mean,
                      prior_psi_Omega = moments$prior_psi_Omega)
prior <- update_prior(prior, n_fcst = 24)
summary(prior)

So far, this works with the following result:

PRIOR SUMMARY
----------------------------
General specification:
  Y: 23 variables, 189 time points
  aggregation: average 
  freq: 17 monthly and 6 quarterly variables
  prior_Pi_AR1: 0 
  lambda1: 0.2 
  lambda2: 0.5 
  lambda3: 1 
  lambda4: 10000 
  block_exo: 0 block exogenous variables
  n_lags: 4 
  n_fcst: 24 
  n_burnin: 1000 
  n_reps: 1000 
----------------------------
Steady-state prior:
  d: intercept 
  d_fcst: intercept 
  prior_psi_mean: prior mean vector of steady states 
  prior_psi_Omega: prior covariance matrix of steady states 
  check_roots: FALSE 
----------------------------
Hierarchical steady-state prior:
  s: -1000 
  c0: 0.01 
  c1: 0.01 
----------------------------
Common stochastic volatility:
  prior_phi: mean = 0.9, var = 0.1 
  prior_sigma2: mean = 0.01, df = 4 
----------------------------
Factor stochastic volatility:
  n_fac: <missing> 
  n_cores: 1 
----------------------------
Other:
  verbose: FALSE

Then when I start using estimate_mfbvar function (mod_ss_iw <- estimate_mfbvar(prior, prior = "ss", variance = "iw"), error occur

error: inv_sympd(): matrix is singular or not positive definite Error in mcmc_ssng_iw(Y[-(1:n_lags), ], Pi, Sigma, psi, phi_mu, lambda_mu, : inv_sympd(): matrix is singular or not positive definite In addition: Warning messages: 1: In log(s2) : NaNs produced 2: In log(s2) : NaNs produced

I wonder do you have a specific solution for this?

ankargren commented 3 years ago

@dinhyennhi93 Does the error occur immediately when estimating the model, or after a while? Does it work if you change to a Minnesota prior? Any chance you could share the data you’re using so that I can take a look?

To tighten up the steady-state prior, you can do prior_psi_Omega = 0.5*moments$prior_psi_Omega. That will give you tighter prior variances.