Problem with BVAR and Metropolis-Hastings

Dear Members,

I have some issues for two mod files that I cannot solve:

- The bvar_us.mod does not work. I get error messages and I can’t find why:

Warning: Range cannot be used in ‘basic’ mode. The entire sheet will be loaded. _
> In xlsread (line 211)
_ In load_xls_file_data (line 77)

_ In read_variables (line 92)_
_ In bvar_toolbox (line 65)_
_ In bvar_density (line 37)_
_ In bvar_us (line 75)_
_ In dynare (line 223) _
Error using error
The message must be specified as either a character vector or a message structure.
Error in read_variables (line 104)
_ error([strtrim(var_names_01(dyn_i_01,:)) ’ not found in ’ fullname])_
Error in bvar_toolbox (line 65)
dataset = read_variables(options.datafile, options_.varobs, [],_
options.xls_sheet, options_.xls_range);_
Error in bvar_density (line 37)
_ [ny, nx, posterior, prior] = bvar_toolbox(nlags);_
Error in bvar_us (line 75)
bvar_density(4);
Error in dynare (line 223)
evalin(‘base’,fname) ;

- The NK3eq_estim does not work as well, especially for the Metropolis-Hastings.

Warning: Range cannot be used in ‘basic’ mode. The entire sheet will be loaded. _
> In xlsread (line 211)
_ In load_xls_file_data (line 77)

_ In dseries (line 130)_
_ In makedataset (line 121)_
_ In dynare_estimation_init (line 538)_
_ In dynare_estimation_1 (line 116)_
_ In dynare_estimation (line 105)_
_ In NK3eq_estim (line 208)_
_ In dynare (line 223) _

Warning: The results below are most likely wrong! _
> In dynare_estimation_1 (line 316)
_ In dynare_estimation (line 105)

_ In NK3eq_estim (line 208)_
_ In dynare (line 223) _
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN. _
> In dynare_estimation_1 (line 339)
_ In dynare_estimation (line 105)

_ In NK3eq_estim (line 208)_
_ In dynare (line 223) _

Error using chol
Matrix must be positive definite.
Error in posterior_sampler_initialization (line 84)
d = chol(vv);
Error in posterior_sampler (line 60)
_ posterior_sampler_initialization(TargetFun, xparam1, vv,_
_ mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);_
Error in dynare_estimation_1 (line 447)
_ posterior_sampler(objective_function,posterior_sampler_options.proposal_distribution,xparam1,posterior_sampler_options,bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);_
_ Error in dynare_estimation (line 105)_
_ dynare_estimation_1(var_list,dname);_
Error in NK3eq_estim (line 208)
oo_recursive=dynare_estimation(var_list_);_
Error in dynare (line 223)
evalin(‘base’,fname) ;

bvar_us.mod (226 Bytes)
NK3eq_estim.mod (3.2 KB)
data_us_nk.xls.zip (23.7 KB)

Thank you in advance,

Best regards

This is a problem with Excel compatibility on your machine. Which operating system are you using? And do you have Excel installed?

Dear Jpfeifer,

Thank you for your answer. I am using the latest version of Excel (2017). Excel is installed. What do you mean by operating system?

How do I solve this problem?

Thank you,

Best regards

Try saving the file as an xlsx instead of xls-file.

Thank you, it almost works. I still have a problem with the bayesian estimation, the Metropolis-Hastings does not appear when running the model:

POSTERIOR KERNEL OPTIMIZATION PROBLEM!
(minus) the hessian matrix at the “mode” is not positive definite!
=> posterior variance of the estimated parameters are not positive.
You should try to change the initial values of the parameters using
the estimated_params_init block, or use another optimization routine.
Warning: The results below are most likely wrong!

In dynare_estimation_1 (line 316)
In dynare_estimation (line 105)
In NK3eq_estim (line 206)
In dynare (line 223)
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN.
In dynare_estimation_1 (line 339)
In dynare_estimation (line 105)
In NK3eq_estim (line 206)
In dynare (line 223)

RESULTS FROM POSTERIOR ESTIMATION
parameters
prior mean mode s.d. prior pstdev

rho_D 0.500 0.9738 NaN beta 0.2000
rho_S 0.500 0.9996 NaN beta 0.2000
rho_R 0.500 0.3463 NaN beta 0.2000
sigma 1.000 3.2743 NaN gamm 0.4000
varphi 2.000 1.3990 NaN gamm 0.7500
phi_pi 1.500 1.5913 NaN norm 0.1000
phi_y 0.050 -0.0002 NaN norm 0.0500
rho 0.750 0.6801 NaN beta 0.0500
theta 0.500 0.8077 NaN beta 0.1000

standard deviation of shocks
prior mean mode s.d. prior pstdev

eta_D 0.100 0.0399 NaN invg 2.0000
eta_S 0.100 0.2169 NaN invg 2.0000
eta_R 0.100 0.2621 NaN invg 2.0000

Log data density [Laplace approximation] is NaN.

Error using chol
Matrix must be positive definite.
Error in posterior_sampler_initialization (line 84)
d = chol(vv);
Error in posterior_sampler (line 60)
posterior_sampler_initialization(TargetFun, xparam1, vv,
mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
Error in dynare_estimation_1 (line 447)
posterior_sampler(objective_function,posterior_sampler_options.proposal_distribution,xparam1,posterior_sampler_options,bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
Error in dynare_estimation (line 105)
dynare_estimation_1(var_list,dname);
Error in NK3eq_estim (line 206)
oo_recursive_=dynare_estimation(var_list_);
Error in dynare (line 223)
evalin(‘base’,fname) ;

Thank you in advance,

Best regards

DC

What do the mode_check plots say?

I only get plots with the priors… nothing about the posteriors or the mean. Here is attached the plots.

Furthermore, when I run the bar_us, I get this error message

"Error using error
The message must be specified as either a character vector or a message structure.
Error in read_variables (line 104)
error([strtrim(var_names_01(dyn_i_01,:)) ’ not found in ’ fullname])
Error in bvar_toolbox (line 65)
dataset = read_variables(options_.datafile, options_.varobs, ,
options_.xls_sheet, options_.xls_range);
Error in bvar_density (line 37)
[ny, nx, posterior, prior] = bvar_toolbox(nlags);
Error in bvar_us (line 74)
bvar_density(4);
Error in dynare (line 223)
evalin(‘base’,fname) ;

"

plots.fig (146.5 KB)

Thank you in advance,

Best regards

You need to enable mode_check. To fix the problem you are now reporting, please fix the read_variables-file as in https://github.com/DynareTeam/dynare/pull/1568/commits/7059cef909e1f248d61fd91ca2aca2316e1ed32b

Dear Jpfeifer,

Thank you, my bvar file works just fine now as it gives me the posterior BVAR impulse responses.

Nonetheless, I still have issues with my NK3eq_estim file as it gives me the following errors, and no posterior or means on the bayesian plots and no metropolis hastings:

Warning: The results below are most likely wrong!

In dynare_estimation_1 (line 316)
In dynare_estimation (line 105)
In NK3eq_estim (line 206)
In dynare (line 223)
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN.
In dynare_estimation_1 (line 339)
In dynare_estimation (line 105)
In NK3eq_estim (line 206)
In dynare (line 223)

RESULTS FROM POSTERIOR ESTIMATION
parameters
prior mean mode s.d. prior pstdev

rho_D 0.500 0.9738 NaN beta 0.2000
rho_S 0.500 0.9996 NaN beta 0.2000
rho_R 0.500 0.3463 NaN beta 0.2000
sigma 1.000 3.2743 NaN gamm 0.4000
varphi 2.000 1.3990 NaN gamm 0.7500
phi_pi 1.500 1.5913 NaN norm 0.1000
phi_y 0.050 -0.0002 NaN norm 0.0500
rho 0.750 0.6801 NaN beta 0.0500
theta 0.500 0.8077 NaN beta 0.1000

standard deviation of shocks
prior mean mode s.d. prior pstdev

eta_D 0.100 0.0399 NaN invg 2.0000
eta_S 0.100 0.2169 NaN invg 2.0000
eta_R 0.100 0.2621 NaN invg 2.0000

Log data density [Laplace approximation] is NaN.

Error using chol
Matrix must be positive definite.
Error in posterior_sampler_initialization (line 84)
d = chol(vv);
Error in posterior_sampler (line 60)
posterior_sampler_initialization(TargetFun, xparam1, vv,
mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
Error in dynare_estimation_1 (line 447)
posterior_sampler(objective_function,posterior_sampler_options.proposal_distribution,xparam1,posterior_sampler_options,bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
Error in dynare_estimation (line 105)
dynare_estimation_1(var_list,dname);
Error in NK3eq_estim (line 206)
oo_recursive_=dynare_estimation(var_list_);
Error in dynare (line 223)
evalin(‘base’,fname) ;

Thank you,

Best regards

As I said several times now, you should investigate the mode_check plots from estimation to find out where the problem is.

Sorry, I did not get your point last time. I get quite standard plots… I do not find them useful to see why the Metropolis-Hastings algorithm is not running… and why my posterior variances are not positive.

untitled.fig (116.7 KB)
untitled1.fig (46.4 KB)

Sorry for the inconveniences,

Best regards

The plots indeed look good. Have you tried mode_compute=5?

It still doesn’t work… Really strange. I get the same error messages. The M-H box should appear, but nothing is happening. And it is not a problem with my machine as other codes work just fine.

A mystery

Thank you for your help,

DC

The reason is that your Hessian at the mode is not positive definite. But the question is why that is the case. Could you please upload the mode-file.

Here are the mod and excel file.

Much thanks

NK3eq_estim.mod (3.0 KB)

https://forum.dynare.org/uploads/default/original/2X/2/2237dad8cde5837491b537948e40706a639a3b10.zip

Your observation equations are wrong. The model is linearized, but the data are not mean 0. Please take a look at Pfeifer(2013): “A Guide to Specifying Observation Equations for the Estimation of DSGE Models”

Those damn observation equation. But I thought my data were demeaned as you can see in the ‘obs’ sheet of excel…

Thank you

I have demeaned my data an other way by writing a Matlab file. However, when running the mod file, I still get the same error messages…

r_obs was clearly not mean 0. With your old dataset, you had one autocorrelation close to a unit root. How do the parameter estimates look like now?

Dear Jpfeifer,

Sorry, I’m very very new to coding on Dynare and I am trying to solve some exercices to practice. This exercise is tiring me, I think I’m going to give up on that one.

I get this :

RESULTS FROM POSTERIOR ESTIMATION
parameters
prior mean mode s.d. prior pstdev

rho_D 0.500 0.8688 NaN beta 0.2000
rho_S 0.500 0.9987 NaN beta 0.2000
rho_R 0.500 0.4099 NaN beta 0.2000
sigma 1.000 0.9908 NaN gamm 0.4000
varphi 2.000 0.9952 NaN gamm 0.7500
phi_pi 1.500 1.5026 NaN norm 0.1000
phi_y 0.050 -0.0011 NaN norm 0.0500
rho 0.750 0.7647 NaN beta 0.0500
theta 0.500 0.5595 NaN beta 0.1000

standard deviation of shocks
prior mean mode s.d. prior pstdev

eta_D 0.100 0.0118 NaN invg 2.0000
eta_S 0.100 0.1811 NaN invg 2.0000
eta_R 0.100 0.0969 NaN invg 2.0000

I do not even know if my Matlab file is ok.

datatest.m (5.5 KB)

Thank you,

Best regards