I want to calibrate a DSGE model to match some certain moments, thus I need iteration outside the mod file to find the optimal parameter values. Have any idea about how to do it with Dynare? I need to change the parameter values at each iteration.

I had the same problem. I solved it by doing the loop inside of the mod file. The only problem is that your file may get very long.

Pablo

Hi, Pablo:

Could you show me how? I really appreciate it.

It might be tedious and long to program, but it might be worthwhile to write a Matlab function outside the mod file that calculates the moment(s) of the model you are interested in and stores them in a vector. You should have the same moments from the data stored in a similar vector…

Then you should use one of the matlab functions such as lsqnonlin to minimize some distance between the model moments and the data. It is much more efficient than looping, especially because matching moments is somewhat easier than maximizing likelihood.

For each parameter that you want to change, you can always save it in the matlab workspace outside the mod file and then load it within the mod file before solving the model.

I have not done it with Dynare, but a similar procedure worked well using Uhlig’s toolkit.

To set a loop that changes parameters you may want to modify the .Mod file so that it takes parameters from outside the code (i.e. rather than change the values mannually at each iteration). In the main Matlab file you would have something like

parm1 = 1, parm2 = 2, etc.

save my_parameters.mat parm1 parm2

(the previous file saves the parameters as a data file

Inside the .mod file you’ll need to remove the lines that assign parameter values and put the following:

load my_parameters.mat

(this would load your parameters). Make sure that the parameters in my_parameters.mat include all the params listed on top of the MOD file.

In the loop, after the parameters are updated, repeat the save procedure and run dynare with the new params.

Did anybody ever try tokhir’s suggestion? It seems like the perfect idea, but I have a hard time using it; when I save a fresh mat file dynare loads it fine, but when I update it in my m file that has a loop running dynare multiple times it gives me division by zero during the solution step. I have tried this numerous times, and I am sure that parameter values are the same.

Can somebody please help me?

Thanks in advance,

MuratK

[quote=“muratK”]Did anybody ever try tokhir’s suggestion? It seems like the perfect idea, but I have a hard time using it; when I save a fresh mat file dynare loads it fine, but when I update it in my m file that has a loop running dynare multiple times it gives me division by zero during the solution step. I have tried this numerous times, and I am sure that parameter values are the same.

Can somebody please help me?

Thanks in advance,

MuratK[/quote]

You have to save and load all the numbers in a *.mat file every time when you run Dynare, since by default Dynare will clear everything when it is called. My suggestion is that you can use the *.m file generated by dynare directly. For example, if you have a XXX.mod file. Dynare will generate a file named as “XXX.m”. run the .m file is equivalent to run “dynare XXX.mod”, but you can remove the tope line in XXX.m, which is “clear all”.

I guess the problem you run into is because you forgot to save something not changing in the loop in your MAT file.

[quote] By bigbigben:

I guess the problem you run into is because you forgot to save something not changing in the loop in your MAT file.[/quote]

Well, that was my initial guess too, but the parameters in my mat file are exactly the same as the ones written in the code.

I am attaching the m file and the mat file, though I had to change the extension of the mat file to mod file since the forum won’t accept mat extensions. Just change the file extension back to mat in Matlab. If you could have a look at it and possibly run the code with and without the mat file, I will truly appreciate it.

If I can’t find a way to do this, I will have to do thousands of runs manually!!!

Thanks so much,

Murat

para1.mod (937 Bytes)

baxter1_incomplete.m (3.13 KB)

[quote=“muratK”]

[quote] By bigbigben:

I guess the problem you run into is because you forgot to save something not changing in the loop in your MAT file.[/quote]

Well, that was my initial guess too, but the parameters in my mat file are exactly the same as the ones written in the code.

I am attaching the m file and the mat file, though I had to change the extension of the mat file to mod file since the forum won’t accept mat extensions. Just change the file extension back to mat in Matlab. If you could have a look at it and possibly run the code with and without the mat file, I will truly appreciate it.

If I can’t find a way to do this, I will have to do thousands of runs manually!!!

Thanks so much,

Murat[/quote]

Could you upload the original mod file? No equation shows up in your file uploaded. How can you get the steady state value and dyanmics? I guess you can check whether your steady state is the real steady state. Is there some variable should be 1 instead of 0?

I also need the driver, which calls your mod file to see what happened. Your mod file works on my computer.

You mean the mod file worked using the mat file or did you just run the mod file as I attached? Because I have forgotten to include the ‘load para1.mat’ line in that one, it has the parameters written down instead. It should work as it is.

If it works with the mat file that is a mystery to me, because here is what I get when I run it with that mat I attached:

Warning: Divide by zero.

In solve1 at 80

In dynare_solve at 73

In resol at 43

In check at 18

In baxter1_incomplete at 115

In dynare at 26

Warning: Some element of Newton direction isn’t finite. Jacobian maybe singular or there is a problem with initial values

In lnsrch1 at 16

In solve1 at 87

In dynare_solve at 73

In resol at 43

In check at 18

In baxter1_incomplete at 115

In dynare at 26

??? Error using ==> print_info

Impossible to find the steady state. Either the model doesn’t have a unique steady state of the guess values are too far from the solution

Error in ==> check at 21

print_info(info);

Error in ==> baxter1_incomplete at 115

check;

Error in ==> dynare at 26

evalin(‘base’,fname) ;

There was no driver involved in this run. It is just straightforward ‘dynare baxter1_incomplete.mod’.

Are you using Dynare 4?

Thanks for all your help.

The way I fix it is to save all the parameter values in another .MAT file. It works !!!. Something mysterious is in your para1.mat. I don’t know what it is.

You are right, it works when I save the parameters in a fresh mat file, but when I make a few changes on that new file after the first run, it stops working as well!!!

Moreover, that parameter file is exactly as it is supposed to be… It is really weird.

I attached the driver here as well, please try it with your fresh and working MAT file and see if it suddenly stops working.

And thanks again and again!

mainProg.m (1.25 KB)

I finally figured out what I was doing wrong… One of the steady state values was equal to one of the parameters, when I update the parameters but not update the initial values, Dynare gave error.

Anyway, thanks so much for your help bigbigben, I truly appreciate it!