Page 1 of 2

Linking Dynare to Matlab code

PostPosted: Sat Jun 20, 2015 7:06 am
by macroresearch123
Hey Johannes,

Is it possible to link Dynare to matlab code in the following way? Suppose I want to do simulated method of moments and use Dynare to compute the stochastic simulated time series under a different set of parameters -- in other words, I want to find the parameters that match the data, which involves solving Dynare under each parametric guess.

How would I want to go about coding this -- do you have some pseudocode in mind? I think the main stumbling block for me is that dynare erases whatever is in matlab memory. With that aside, my general structure is:
a) solve the dynamic model with dynare
b) call the data and compute actual moments
3) take the difference between actual and simulated moments
4) send that objective function to fminsearch

On a related note, if I just want to pass a numerical steady state computed in one function file onto dynare, would I do that?

Re: Linking Dynare to Matlab code

PostPosted: Sat Jun 20, 2015 8:42 pm
by jpfeifer
You can call Dynare with the noclearall option:
Code: Select all
dynare myfile noclearall

For implementing a SMM-approach see e.g. the replication material to Born/Pfeifer (2014): "Risk Matters: The Real Effects of Volatility Shocks: Comment" at [url]Born/Pfeifer (2014): "Risk Matters: A comment"[/url]

Re: Linking Dynare to Matlab code

PostPosted: Sun Jun 21, 2015 2:15 am
by macroresearch123
Wow, that's great, thank you!

Re: Linking Dynare to Matlab code

PostPosted: Sun Jun 21, 2015 5:27 am
by macroresearch123
It's definitely amazing how flexible Dynare is and all that can be done with it. Just to follow up on your suggestion with "noclearall":

My SMM code looks a little different than your AER comment code. I have one matlab file that computes moments and then calls fminsearch to optimize over a separate file; in that file, I create the GMM objective function and, to do that, I call Dynare to get the simulated moments.

The problem is that I call Dynare .mod file in the matlab function file and the results of the file don't show up. It's super odd because after I get the error, all the relevant Dynare created variables appear. In other words, in my function file that computes the simulated moments, I call Dynare:

dynare benchmark.mod noclearall %addpath C:\dynare\4.4.2\matlab
load dynarerocks

and then I want to use variables created from Dynare, call it X, to created moments F(X) that go into the objective function. And yet, none of the content shows up after calling the .mod file.

(The "load dynarerocks" also doesn't work for me -- I spent a while searching and it looked like some files use that.)

Again, really appreciate your always being available to give suggestions -- reminds me to check the forum to learn more! I'm sure my question here has a simple answer.

Re: Linking Dynare to Matlab code

PostPosted: Sun Jun 21, 2015 7:36 pm
by jpfeifer
The bottom part of the mod-file in our replication files does exactly what you describe. We added essentially Matlab code to a mod-file, but you can put that part just in a Matlab file.

Please don't use the Den Haan dynarerocks. It is hugely inefficient.

From what you describe, the problem is the scope of variables. Try defining
Code: Select all
global oo_ M_ options_

before calling Dynare the first time and use the same statement within your objective function (or alternatively, pass these three variables as function arguments to the objective function)

Re: Linking Dynare to Matlab code

PostPosted: Wed Jun 24, 2015 10:07 am
by mharding
Dear Johannes,

I am trying to implement a SMM procedure and I'm using the aforementioned 2014 AER code as guidance. When I try to run your code setting
Code: Select all
@#define estimation = 1
(this being the only change made to the code posted on your website), I get the following error:

Code: Select all
Index exceeds matrix dimensions.
Error in Born_Pfeifer_RM_Comment (line 455)
[fhat,xhat] =
csminwel(@smm_diff_function,x_start,H0,[],crit,nit,target,estimation_replications,shock_mat(:,:,1:estimation_replications));
Error in dynare (line 180)
evalin('base',fname) ;


Do you know what might be the reason for this? (I'm using Dynare 4.4.3 and Matlab R2014b).

I also wanted to ask you how the shocks enter the csminwel function. In the code they enter as:
Code: Select all
shock_mat(:,:,1:estimation_replications))
however, in the code the matrix shock_mat is defined as a 2-dimensional matrix (npts x M_.exo_nbr).

Finally, how does the simult_FV differ from the simult_ function? I didn't find the definition.

Thank you!
Martin

Re: Linking Dynare to Matlab code

PostPosted: Sun Jun 28, 2015 8:26 pm
by jpfeifer
Hi Martin,
this seems to be a path conflict. The csminwel-function we used was the original one of Chris Sims. But Dynare has its own one with the same name, but a somewhat different syntax. It is best if you download the original one to the same folder, rename it and then call this function.

There should not be a simult_FV but only a simult_FGRU. The difference to the Dynare one is that it uses the FGRU pruning, not the Andreasen et al one of Dynare. Always use the latter (for differences, see the Appendix to our comment)

Re: Linking Dynare to Matlab code

PostPosted: Mon Jun 29, 2015 10:12 pm
by mharding
Dear Johannes,

thank you. I tried downloading and renaming the original csminwel (and the functions it calls), but I get exactly the same error message. As I mentioned before, I think it might be related to the dimensions of the shock_mat matrix, but I might be overlooking something else...

As for the "simult_FV", I was asking because, if I get it correctly, this function shows up in the "smm_diff_function.m" file of the replication files posted on your website.

Re: Linking Dynare to Matlab code

PostPosted: Wed Jul 01, 2015 6:32 am
by jpfeifer
Thanks for pointing this out. There seem to have happened a few copy and paste mistakes when we prepared the codes for the AER. I will post an updated version soon.

Re: Linking Dynare to Matlab code

PostPosted: Thu Jul 02, 2015 7:51 pm
by macroresearch123
Hi Johannes,

I spent some additional time looking at this, but I might still be misunderstanding; I added:
global moments_model oo_ M_ options_

in both matlab files. What do you mean "use the same statement within your objective function" -- what statement, the global? My objective function is:
gmm=(moments_data - moments_model)'*weight*(moments_data - moments_model)

which is assembled after calling dynare to get the simulated moments.

Just to make sure we both are talking about the same problem -- feel free to skip this if you know we're talking about the same thing -- the issue that I was running into was that I had computed all my empirical moments with the code for doing smm, and then call the dynare file; but, after I call the file and introduce the objective function (moments_data - moments_simulated), the dynare simulations dont show up. It's odd to explain -- the dynare simulations show up after I let it run, but while it's running the dynare produced simulations dont show, and thus the program doesnt work since the simulations dont show up "in time".

I was hoping to get this working in my original format since I don't understand yours entirely in your paper -- although you did nice job documenting it, excellent compared the the vast majority of paper documentation! Just to better understand your lines of code, maybe I could clarify a few additional things:

The line below feeds in the simulated data and computes the relevant moments?
[moments_short]=get_quarterly_moments(out_withshock(:,:,1:200),ergodicmean_no_shocks,M_,oo_);
What's the difference with the below (is it that there are only a subset of moments here?)
// moments_short(9,:)=[nx_y_share nx_y_share];
[moments_long, row_names, column_names, std_nx]=get_quarterly_moments(out_withshock,ergodicmean_no_shocks,M_,oo_);

Related to mharding's point:

Are sigma_x, phipar,D_bar,exp(phi) the only parameters you are trying to calibrate? This code below is where you're actually calling the optimizer to do smm? (in my case, I have a separate matlab file.)
@#if estimation == 1
x_start=[sigma_x,phipar,D_bar,exp(Phi)]; //use calibration as starting point
target=[moments_emp(1:3);moments_emp(8)]'; //define target moments
//optimizer options
H0 = 1e-2*eye(length(x_start)); //Initial Hessian
crit = 1e-7; //Tolerance
nit = 1000; //Number of iterations
//make sure Dynare does not print out stuff during runs
options_.nocorr=1;
options_.noprint=1;
options_.verbosity=0;

//options_.qz_criterium = 1+1e-6; //required because it is empty by default, leading to a crash in k_order_pert
[fhat,xhat] = csminwel(@smm_diff_function,x_start,H0,[],crit,nit,target,estimation_replications,shock_mat(:,:,1:estimation_replications));
@# endif

Again, excellent advice and appreciate your sharing all your expertise!

Re: Linking Dynare to Matlab code

PostPosted: Mon Jul 06, 2015 5:44 pm
by jpfeifer
@mharding: the updated files are now available at my homepage. Please try them out. Thanks again for pointing this out. The two mistakes were:
[list=]
[*] In smm_diff_function.m the simult_FV should be simult_FGRU throughout
[*] The Born_Pfeifer_RM_Comment.mod is missing the line
Code: Select all
    [shock_mat]=generate_FGRU_shocks(estimation_replications,winsorizing_dummy);

before the call to csminwel.

[/list]

@macroresearch123
Please provide me with the files to replicate the issue.

Re: Linking Dynare to Matlab code

PostPosted: Tue Jul 07, 2015 6:02 pm
by macroresearch123
Hey Johannes -- I was able to get the issue I mentioned above to work -- a very simple thing, I just needed to save variables X via X=oo_.endog_ etc. The moment matching isn't working, but that's a separate and non-Dynare issue-- will keep working on that!

Thanks again for your time and energy in answering all these questions, and posting the correction to your prior code!

Re: Linking Dynare to Matlab code

PostPosted: Wed Jul 08, 2015 11:36 am
by mharding
Thank you for the update Johannes! I tried the files and everything works fine.

Re: Linking Dynare to Matlab code

PostPosted: Thu Jul 09, 2015 6:37 am
by jpfeifer
Great. The AER has also updated the files.

Re: Linking Dynare to Matlab code

PostPosted: Sun Jul 12, 2015 3:16 am
by macroresearch123
Sorry to bump the thread again, but it's somewhat related to the initial question.

After some time, I got the SMM working no problem -- what is a slight issue, however, is that there are some moments I want to match that are long run growth rates of some variables. However, as you know better than anyone else, dynare focuses more on cyclical fluctuations. My initial understanding that long run growth was built in merely by doing:

z_t = rho*z_t-1 + (1-rho)*growth + eps

where growth = .03, for example. However, taking the average across the variables is still zero (over the simulated series). I looked for previous threads on this and the only related one, I think, was http://www.dynare.org/phpBB3/viewtopic.php?f=1&t=4661&p=11620&hilit=long+run+growth#p11620, however, it didn't have a response.

One way to deal with this, of course, is to "add in" the long run growth component afterwards, but that would change the dynamic choices I'd think. Is there a simple way to do this I'm overlooking? Note that the issue isn't solved by detrending since I want the model to be able to produce some long run growth.