Page 1 of 1

MATLAB functions in Dynare 4

PostPosted: Wed Sep 01, 2010 7:22 pm
by tanya
How can we call MATLAB functions from Dynare? In Dynare 3.064 I used to write

# [output params] = namefunc(input params);

and it used to work (here namefunc.m is my own function written in a separate file) . Now it doesn't. I could not easily find any documentations about it. Could anyone help?

Many thanks

Re: MATLAB functions in Dynare 4

PostPosted: Thu Sep 02, 2010 12:08 pm
by StephaneAdjemian
Depending on what you want to do, two approaches are possible:

(1) Declare the outputs in the list of the endogenous (the initialization is not necessary), compute the outputs in the steadystate file and update the values in the corresponding rows in M_.params (Look at the forum, I posted an example a couple of years ago).

(2) Use external functions as described here on the wiki.

The limit of the first approach is that it can only handle the computation of parameters (endogenous variables cannot be input/ouput arguments). The limit of the second approach is that each external function has to return only one argument (plus eventually some first and second order derivates). Also, external function are only implemented in the snapshot version.

Best,
Stéphane.

Re: MATLAB functions in Dynare 4

PostPosted: Thu Sep 02, 2010 7:39 pm
by tanya
Dear Stephane

Many many thanks for the links. The only page I can find on wiki suggests external_function which I cannot make working. More specifically, I have the following model that used to work under dynare 3 (see below). Adding external_function(myfunc) (after "parameters"?) did not help. I cannot provide any analytical derivatives, and I also have min 20 parameters a (can go up to 70) and about 10 of b's. I will be very grateful for any hints how to deal with this.

PS. Am I right it wants a scalar output? If so, is there any way out? My myfunc is extremely computationally intensive (my next challenge will be to parallel MH alg, this is why I want dynare 4 not 3), and I would prefer to call it once and get a vector output of size 20-70 as all parameters determined simultaneously, rather than call it 20-70 times and return only one parameter at a time.

PPS. Am i right in if I declare a1 a2 a3 as endogenous variables and provide steady state for them, then my model becomes non-linear and this is even bigger computational problem then to call my routine 20-70 times?


...
var X X1 X2 X3 X4 ...;
parameters b1 b2 b3;
parameters a1 a2 a3 a4;

model(linear);
...
[a1,a2,a3,a4] = myfunc(b1,b2,b3);
X = a1*X1(-1)+a2*X2(-1)+a3*X3(-1)+a4*X4(-1);
...
end;
...
b1,BETA_PDF,0.5,0.15;
...

Re: MATLAB functions in Dynare 4

PostPosted: Fri Sep 10, 2010 8:09 am
by StephaneAdjemian
Dear Tanya, If my understanding is correct your function returns more than one output argument. This is not allowed with the external_function syntax. The simplest approach is to call this function and update a1, a2, a3 and a4 in the steadystate file. I posted an example in this thread last year.

Best, Stéphane.