Set a loop outside mod file

This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location where you will have to reset your password.
Forum rules
This forum is closed. You can read the posts but cannot write. We have migrated the forum to a new location (https://forum.dynare.org) where you will have to reset your password.

Set a loop outside mod file

Postby bigbigben » Mon Jan 01, 2007 8:08 pm

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.
bigbigben
 
Posts: 171
Joined: Sun May 28, 2006 1:19 am

Postby pburriel » Tue Jan 02, 2007 10:53 am

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
pburriel
 
Posts: 23
Joined: Wed Jun 29, 2005 4:42 am

Postby bigbigben » Tue Jan 02, 2007 4:15 pm

Hi, Pablo:
Could you show me how? I really appreciate it.
bigbigben
 
Posts: 171
Joined: Sun May 28, 2006 1:19 am

Postby iacoviel » Wed Jan 03, 2007 11:03 am

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.
iacoviel
 
Posts: 38
Joined: Wed Feb 01, 2006 6:25 pm

Postby tokhir » Fri Feb 23, 2007 4:05 am

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.
tokhir
 
Posts: 7
Joined: Wed Sep 20, 2006 6:01 pm

Postby muratK » Wed Oct 03, 2007 8:33 am

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
muratK
 
Posts: 9
Joined: Thu Aug 09, 2007 10:52 pm
Location: Seattle

Postby bigbigben » Thu Oct 04, 2007 1:28 am

[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.
bigbigben
 
Posts: 171
Joined: Sun May 28, 2006 1:19 am

Postby muratK » Thu Oct 04, 2007 9:08 am

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.


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
Attachments
para1.mod
mat file for parameters saved as a mod file
(937 Bytes) Downloaded 488 times
baxter1_incomplete.m
m file
(3.13 KiB) Downloaded 377 times
muratK
 
Posts: 9
Joined: Thu Aug 09, 2007 10:52 pm
Location: Seattle

Postby bigbigben » Thu Oct 04, 2007 2:32 pm

[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?
bigbigben
 
Posts: 171
Joined: Sun May 28, 2006 1:19 am

Postby muratK » Thu Oct 04, 2007 7:46 pm

Oh, sorry about that! Here is the original mod file:
Attachments
baxter1_incomplete.mod
(2.56 KiB) Downloaded 469 times
muratK
 
Posts: 9
Joined: Thu Aug 09, 2007 10:52 pm
Location: Seattle

Postby bigbigben » Thu Oct 04, 2007 8:14 pm

I also need the driver, which calls your mod file to see what happened. Your mod file works on my computer.
bigbigben
 
Posts: 171
Joined: Sun May 28, 2006 1:19 am

Postby muratK » Thu Oct 04, 2007 11:37 pm

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.
muratK
 
Posts: 9
Joined: Thu Aug 09, 2007 10:52 pm
Location: Seattle

Postby bigbigben » Sat Oct 06, 2007 7:00 am

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.
bigbigben
 
Posts: 171
Joined: Sun May 28, 2006 1:19 am

Postby muratK » Sun Oct 07, 2007 1:32 am

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!
Attachments
mainProg.m
driver
(1.25 KiB) Downloaded 463 times
muratK
 
Posts: 9
Joined: Thu Aug 09, 2007 10:52 pm
Location: Seattle

Postby muratK » Wed Oct 10, 2007 9:53 am

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!
muratK
 
Posts: 9
Joined: Thu Aug 09, 2007 10:52 pm
Location: Seattle


Return to Dynare help

Who is online

Users browsing this forum: Google [Bot] and 9 guests