Repeatedly estimate DSGE model using Dynare

Hello,

I want to estimate (ML estimation) a model using Dynare. There are 6 variables in the model, and I have 1,000 artificial data for each variable for 200 periods. Therefore, every variable is esentially a 200 by 1,000 matrix. However, I tried many times and cannot get Dynare to work properly. I wrote the code as the following:

@#for i in 1:1000 estimation(datafile=mydata@{y1(:,i),c1(:,i),pi1(:,i),r1(:,i),g1(:,i),z1(:,i)},nograph); @#endfor
y1, c1, pi1,r1, g1 and z1 are 6 variables. The error message says: “unknown variable: y1”, but I declare all the variables before doing estimation. I would like to know how I should fix my errors. Thanks for your help.

jygg1

Hi

First, you don’t need the macro-language for this kind of task, it would repeat the command 1000 times instead of putting it in a Matlab loop.

Second, you can’t specify a sub-matrix as argument of “datafile=”. You should create the appropriate MAT-file at each iteration.

Try the following:

for i = 1:1000
  mydata2 = mydata(y1(:,i),c1(:,i),pi1(:,i),r1(:,i),g1(:,i),z1(:,i));
  save mydata3.mat mydata2;
  estimation(datafile=mydata3,nograph);
end

Best

Sébastien

Hi Sébastien,

Thanks for your help. But I tried many times, the program still cannot work. The error message is:

Configuring Dynare …
[mex] Generalized QZ.
[mex] Sylvester equation solution.
[mex] Kronecker products.
[mex] Sparse kronecker products.

Starting Dynare …
Starting preprocessing of the model file …
8 equation(s) found
Processing derivation …
Processing Order 1… done
Processing Order 2… done
Processing outputs …
Preprocessing completed.
Starting Matlab computing …

??? Undefined variable y1.

Error in ==> mletry3 at 173
mydata2=mydata(y1(:,i),c1(:,i),pi1(:,i),r1(:,i),g1(:,i),z1(:,i));

Error in ==> dynare at 102
evalin(‘base’,fname) ;


It seems not clear to me why it is still a problem. Could you help me on this? I will also appreciate the help from others. Thank you very much.

After some trials, I modify my code as follows:

load gapfirst; for i=1:1000 my2=[y1(:,i),c1(:,i),pi1(:,i),r1(:,i),g1(:,i),z1(:,i)]; save my3.mat my2; estimation(datafile=my3,nograph); end

“gapfirst” is my original data file. However, this still cannot work. The error message is:

??? Reference to non-existent field ‘y1’.

I think the problem is: now the variable is my2 in my3.mat, but the estimation process requires y1, c1, pi1, r1, g1 and z1. I try to add something like y1=my2(:,1) in the loop, but dynare does not accept the colon (:). Thus I think my problem is: how to make the variable my2 become y1, c1, pi1, r1, g1 and z1 in a way that is readable for dynare. Any help will be deeply appreciated.

Hi,

After many trials, I still cannot get Dynare to work. The attachment is my code. Any suggestions and advices are greatly appreciated.

Mletry3.mod (1.27 KB)

Hi

Ok I understand the problem. The point is that the MAT-file created must constain variables with the same names than the variables estimated by Dynare (those declared by the “varobs” statement). I had forgotten that point.

With the example that you’ve just posted, you can try:

load gaponetry;
for i=1:1000
    y = y1(:,i);
    pi = pi1(:,i);
    r = r1(:,i);
    g = g1(:,i);
    z = z1(:,i);
    save mydata.mat y pi r g z;
    estimation(datafile=mydata,nograph);
end

Best

Sébastien

Hi Sébastien,

Thanks for your kind help. Unfortunately, your code still does not work. The error message is:

Starting Dynare …
Starting preprocessing of the model file …
ERROR: mletry3.mod:69.9: syntax error, unexpected ‘:’

??? Error using ==> dynare
DYNARE: preprocessing failed

It seems that Dynare does not accept ':'. However, this is the way that matlab uses to express all columns (or all rows). Thus it is not clear to me why Dynare does not accept ':'. Could this be a bug in Dynare?

jygg1

I have some “new” findings about this problem. When I use Dynare v.3.065, it works!!! However, the procedure fails in v.4. It seems that version 4 does not recognize the colon operator, and this is weird.

May I ask a related question? I have to estimate the model for 1,000 times. However, MLE in Dynare seems pretty slow. In some preliminary trials, it could take 17-18 mins to finish one estimation. When I do this, I already incorporate the options like "nograph", "noprint", ... to expedite the process, but it is still quite time-consuming. Thus I would like to know if there are other ways to make MLE much faster (say, limit the number of iterations...). Thank you very much.

jygg1

Hi

Actually your problem comes from the fact that in some circumstances, it is difficult for Dynare to distinguish between Matlab statements and Dynare statements: mixing both of them in the same file is a kind of hack. The code that I gave you doesn’t work in v4 because there is a name clash between Dynare endogenous variables, and Matlab variables.

I think that the following code should work in v4 (at last!):

load gaponetry;
for i=1:1000
    datastruct.y = y1(:,i);
    datastruct.pi = pi1(:,i);
    datastruct.r = r1(:,i);
    datastruct.g = g1(:,i);
    datastruct.z = z1(:,i);
    save mydata.mat -struct datastruct;
    estimation(datafile=mydata,nograph);
end

(the idea is to enclose data variables within a structure, to avoid the name clash).

Best

Sébastien

Thank you very much. It works on version 4 now.

jygg1