Forecasting in Dynare

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,

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)

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]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) ;[/code]

my estimation block is:

What is the problem and how to fix it?

Regards,

Are there negative standard deviations estimated? Please attach the full log-file.

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,

I would need to see the files to replicate the issue.

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)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,

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.

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 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

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.

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,

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.

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®. 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,
code.pdf (290 KB)

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.

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,
data100.xls (21.8 KB)
code.mod (9.11 KB)

Getting rid of the posterior IRFs should already solve your issue. Also, if the problem always occurs with the same figure, the reason is that your path is too long. In that case, use a shorter name for the mod-file and fewer subfolders.

Also, to speed up computations, provide a mode-file.

Thanks Jpfeifer.

What do you mean by using a shorter name for the mod-file and fewer subfolders? Do you mean that I should use less letters to name the mod-file? As you see my mod-file is called “code” which only contains 4 letters?

As for providing a mode-file, do you mean that I just use the estimation mode as the mode file to run forecasting? Say, I use “mode_compute=0” and “mode_file=the mode file name”.

I look forward to your reply.

Regards,

I said that sometimes there is a systematical problem if the path where the figure is saved is too long. But in your case, that seems unlikely (unless the mod-file is located very deep in the file tree within lots of subfolders)

I was suggesting to speed up computations by using the mode as the starting value for the next iteration. That is use the same mode_compute but also use “mode_file=the mode file name”.

Dear Jpfeifer,

Thanks for your helpful advice.

I haven’t put the mod-file very deep in the file tree. And I also follow your suggestion and get rid of the command “bayesian_irf” which is used to compute the posterior irf. However, the problem still exists. Dynare still shows 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."

Could you help me fix this problem? I don’t know which part of my code should be attributed to this problem.

Regards,

You did not provide the code for this problem. My guess is that your mh_replic is not sufficient.