Page 1 of 3

Forecasting in Dynare

PostPosted: Wed Mar 04, 2015 1:09 pm
by Grant
Dear Jpfeifer,

I have constructed a large-scale DSGE model and solved well though Dynare. Now I want to use my model to do forecasting about some variables. Is Dynare equiped to do out-of-sample forecasting? If possible, is it very time-consuming since it takes more than 10 hours to estimate and solve my model? I look forward to your reply.

Regards,

Re: Forecasting in Dynare

PostPosted: Wed Mar 04, 2015 1:26 pm
by jpfeifer
It depends on what you want to do. Bayesian forecasts may take a long time, because you need to estimate the model. Computing the forecasts itself after the model is estimated is rather straightforward. If you calibrate your model, you could use the forecast command (see the manual)

Re: Forecasting in Dynare

PostPosted: Mon Mar 23, 2015 1:41 pm
by Grant
Dear Jpfeifer,

Thanks for your reply. My code runs very well and get good identification and convergence results of Bayesian estimation. However, when I use the forecast command, dynare shows the error message:
Code: Select all
Log data density [Laplace approximation] is -963.453472.

Error using chol
Matrix must be positive definite.

Error in metropolis_hastings_initialization (line 68)
d = chol(vv);

Error in random_walk_metropolis_hastings (line 62)
[ ix2, ilogpo2, ModelName, MetropolisFolder, fblck, fline, npar, nblck, nruns, NewFile,
MAX_nruns, d ] = ...

Error in dynare_estimation_1 (line 782)
            feval(options_.posterior_sampling_method,objective_function,options_.proposal_distribution,xparam1,invhess,bounds,dataset_,options_,M_,estim_params_,bayestopt_,oo_);
           
Error in dynare_estimation (line 77)
        dynare_estimation_1(var_list,M_.dname);

Error in code (line 975)
dynare_estimation(var_list_);

Error in dynare (line 180)
evalin('base',fname) ;


my estimation block is:
Code: Select all
estimation(datafile=data100,mh_jscale=0.26, mode_compute =4, mh_nblocks = 2,moments_varendo,conditional_variance_decomposition=[1 2 4 8], mh_replic=200000, bayesian_irf, irf=20,nobs=[77:117],forecast=8)y n p_dc pi_c_h r c ;


What is the problem and how to fix it?

Regards,

Re: Forecasting in Dynare

PostPosted: Mon Mar 23, 2015 1:44 pm
by jpfeifer
Are there negative standard deviations estimated? Please attach the full log-file.

Re: Forecasting in Dynare

PostPosted: Tue Mar 24, 2015 3:05 am
by Grant
Dear Jpfeifer,

I have no negative std. estimated. Is it possible that there is something related to the data window? Because without forecasting, the data contains 117 periods, while I use 1 to 76 periods data as in-sample estimation and forecasting 77 to 117 periods.

Regards,

Re: Forecasting in Dynare

PostPosted: Tue Mar 24, 2015 9:03 am
by jpfeifer
I would need to see the files to replicate the issue.

Re: Forecasting in Dynare

PostPosted: Thu Mar 26, 2015 10:22 am
by Grant
Dear Jpfeifer,

Thanks for your reply. I have fixed this problem. However, I encountered another problem. As I have said my code needs to run about several hours to get the estimation results. Fore the out of sample forecast, it is way too time-consuming since the model has to be re-estimated several times according to the out of sample forecasting experiments. I found one paper by Wang (2009)http://onlinelibrary.wiley.com/doi/10.1002/for.1081/abstract use a sightly modified version of the RWMH algorithm. Thai is the authors "adjust the scale parameter automatically within the RWMH algorithm", "finding the appropriate scale parameter manually for the jumping distribution for each rolling sample is too tedious and a time-consuming task". The author also states that this method "does not violate any regularity conditions which guarantee the convergence of the Markov chains since they only use the draws with constant jumping distribution".

I noticed that the scale parameter is set by the command "mh_jscale" in dynare. Is is possible to use the above method in dynare to save the computation time? If not, is there any way to speed up the out of sample forecasting task in dynare?

Your help is highly appreciated.

Regards,

Re: Forecasting in Dynare

PostPosted: Fri Mar 27, 2015 8:11 am
by jpfeifer
It depends on what takes a lot of time. Upon running the MCMC once, you should have a correct mode-file. Moreover, you should have the correct mh_jscale (mode_compute=6 tweaks it to have a decent acceptance). When running the file the next time, you should provide these to Dynare. The obviates the need to run e.g. mode_compute=6 another time. But there is no way to speed up the MCMC itself.

Alternatively, you could work with the forecast command itself, but this is somewhat more involved as you need to run the calib_smoother first, I think.

Re: Forecasting in Dynare

PostPosted: Sun Mar 29, 2015 7:17 am
by Grant
Thanks Jpfeifer!

Actually, I have already set mh_jscale in the estimation block and get decent acceptance ratio. I also set mode_compute=4 and the code runs well.

For the purpose of forecasting, I set
Code: Select all
estimation(datafile=data100,mh_jscale=0.26, mode_compute =4, mh_nblocks = 2,moments_varendo,conditional_variance_decomposition=[1 2 4 8], mh_replic=200000, bayesian_irf, irf=20,nobs=[70:117], forecast=8)y n p_dc pi_c_h r c ;


Is it right? My whole data set includes 116 observations and I use period 1 to period 86 as in-sample estimation and forecast the variables ranging from period 87 to period 116.

I am looking forward to your reply.

Regards

Re: Forecasting in Dynare

PostPosted: Sun Mar 29, 2015 9:14 am
by jpfeifer
You are currently doing recursive forecasting. The model is estimated first on the observations 1 to 70 and forecasts are performed for 8 periods. Then the model is again reestimated on observations 1 to 71 and forecasts are performed. This continues until the last observation, i.e. 117.

Re: Forecasting in Dynare

PostPosted: Mon Mar 30, 2015 4:55 am
by Grant
Thanks Jpfeifer.

However, as my previous post said, the recursive forecasting is very time-consuming and the code has run more than 2 days. Is there any method which could speed up the recursive forecasting? Like what Wang (2009) states "adjust the scale parameter automatically within the RWMH algorithm", which "does not violate any regularity conditions which guarantee the convergence of the Markov chains since they only use the draws with constant jumping distribution". I don't understand how to set the scale parameter automatically within the RWMH algorithm? The scale parameter is set by the command "mh_jscale" in dynare, right? If yes, it is set manually rather than automatically.

I am looking for your reply.

Regards,

Re: Forecasting in Dynare

PostPosted: Mon Mar 30, 2015 8:17 am
by jpfeifer
Again: tuning the scale parameter in every single estimation step would only help marginally. Once you have set it manually to a decent value in order to get a good acceptance rate, there is not much improvement to be gained. It is not as if the optimal tuning parameter should change a lot when you add observations at the end of the sample. The exercise you are doing is simply very time-consuming. What you can do is switch off all features you do not need, i.e. drop the second MCM chain, the Bayesian IRFs, the conditional variance decomposition, etc.

Re: Forecasting in Dynare

PostPosted: Thu Apr 02, 2015 3:17 am
by Grant
Thanks Jpfeifer.

When I do recursive forecasting, Dynare shows some error messages: the first one is that
"posterior_moments: There are not enough draws computes to compute HPD Intervals. Skipping their computation.
posterior_moments: There are not enough draws computes to compute deciles. Skipping their computation."

The second one is that "
Windows returned error message: The handle is invalid." After this message, the code got stuck.

Here attached the log file. Could you help me find out a way to fix this problem?

Another question is: My understanding is that estimation is separated from forecasting. Hence, if I want to do recursive forecasting, what I need to do is
(1) Estimate the model M using data in period t = 1,2,... R (R<T), and obtain the parameters Theta(R). I can use the parameters to do 1-step ahead forecast, 2-step ahead forecast and so on with information until t= R.
(2) Estimate the model M using data in period t = 1,2,... R, R+1, and obtain the parameters Theta(R+1). Using Theta(R+1) you can do 1-step, 2-step ahead forecast and so on with information until t= R+1.
(3) and so on.
Thus, in my case, I choose the last 30 periods of the data set as forecasting sample, which means that dynare needs to do only 30 times estimation. I guess that the estimation part is the most time-consuming task. Since my code only need about 1 hour to just do estimation, if my understand about recursive forecasting is right, why dynare needs several days to get the attached forecasting results?

Hope to get your help.

Regards,

Re: Forecasting in Dynare

PostPosted: Thu Apr 02, 2015 9:50 am
by jpfeifer
Please provide your files.
The handle problem is a problem with Matlab, not Dynare. It arises when many figures need to be plotted. Regarding the time it takes, the reason is that you are requesting many unnecessary computations like Bayesian IRFs.

Re: Forecasting in Dynare

PostPosted: Thu Apr 02, 2015 10:01 am
by Grant
Thanks Jpfeifer. Your advice is very helpful. I will try to delete other unnecessary computations and run again.

Here attached the code and data file. Please help me check the first problem. Thanks in advance.

Regards,