external function in dynare 4.2

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.

external function in dynare 4.2

Postby tanya » Wed Apr 04, 2012 5:26 pm

Dear All

I am estimating a linear model with highly nonlinear constraints on coefficients.
These constraints can be written as function(coefficients)=function_name(parameters) as an external (MATLAB) function.
Because parameters are to be estimated I need to call it within the model(linear) block.

However, when I try to do so, by declaring external_function(name=function_name) and inserting line
function(COEFFCIENTS)=function_name(PARAMETERS)
inside the model block, dynare tells me that I have more equations than variables and refuses to work.

What is the way around it?

Additionally, because dynare seems to be uncomfortable with many arguments of an external function, I pass them as globals, by including lines
global PARAMETERS COEFFICIENTS;
parameters PARAMETERS COEFFICIENTS;
into both dynare file and into the corresponding MATLAB function. The function is then written as function[scalar_out) = function_name(scalar_in)
Dynare seems to forget globals half way. What is the way around it?

Many thanks

Tanya
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Re: external function in dynare 4.2

Postby kyri82 » Thu Apr 05, 2012 8:37 am

Not sure I can help you much but have you tried "evalin"? I also call an external function like this:

[x,fval, exitflag] = evalin('base', 'fsolve(@myfun,x0)') ;

But I do not use it in the model block.
kyri82
 
Posts: 141
Joined: Thu Feb 10, 2011 6:14 pm
Location: Toulouse - France

Re: external function in dynare 4.2

Postby jpfeifer » Wed Apr 18, 2012 7:11 pm

I am not sure I understand your problem completely. But if your coefficients are a function of the parameters you want to estimate and need to be updated in every iteration, the best way to go is to write a steady state file. In the steady state file you can call every Matlab function you like and assign computed values to parameters (which is what your coefficients are for Dynare).
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: external function in dynare 4.2

Postby tanya » Fri Apr 20, 2012 2:25 pm

Dear jpfeifer

how this can be done?

See the code below. I need to (Bayesian) estimate parameters a1,a2,a3, but my linear system is written using parameters b. There is an external matlab function which is written
function[b11,b12,b21,b22] = namefunction(a1,a2,a3). In the code below where exactly I call this steady state thing? I would have thought it should be called within the model block, but dynare does not eat this (Version 3 did everything fine). How this external function can be called in the steady state file and how this steady state file can be included such that the estimation of a1,a2,a3 would become possible

parameters a1 a2 a3 b11 b12 b21 b22
variables x1 x2
exogenous variables shocks

a1 =
a2 =
a3 =

model(linear)

x1 = b11*x1(-1) + b12*x2(-1) + shock1
x2 = b21*x1(-1) + b22*x2(-1) + shock2

define shocks

end;

block of priors on parameters a1, a2, a3 and shocks

estimation command;

PS. I am sure I am not the only one with this problem: this is a very standard problem of imposing (non-linear) restrictions on coefficients. I am either blind or I cannot find anything in the documentation.

Many thanks for the help.
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Re: external function in dynare 4.2

Postby jpfeifer » Fri Apr 20, 2012 7:30 pm

It can be done by supplying a steady state-file (http://www.dynare.org/phpBB3/viewtopic.php?f=1&t=1834), which computes the values for b given the values of a (which can be accessed in the steady state file). As the steady state file is a Matlab file, it follows Matlab-syntax.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: external function in dynare 4.2

Postby tanya » Sat May 05, 2012 2:19 pm

I managed to run a simulation, but I need to estimate parameters.

All I got was the message "This version of Dynare cannot estimate non linearized models!" Obviously, my model is linearised, coefficients are non-linear in parameters. Is this a real problem, or is this just a random diagnostics?

I attach the files: the model and the state file. In principle, I need something much more complicated than this, but even this does not work. What is wrong here?
Attachments
US_data_80.m
data file
(7.07 KiB) Downloaded 285 times
rule_X4.mod
(1.96 KiB) Downloaded 371 times
rule_X4_steadystate.m
(1.14 KiB) Downloaded 399 times
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Re: external function in dynare 4.2

Postby jpfeifer » Sat May 05, 2012 6:18 pm

This is a warning, not an error. By default, the check command sets order=2, but estimation currently only works at order=1 and hence sets it to 1. It has nothing to do with the parameters you want to estimate. If the warning bothers you, set order=1 in the estimation command.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: external function in dynare 4.2

Postby tanya » Sat May 05, 2012 9:42 pm

I realize it is a warning, but it crashes after that warning. It crashes in doing something with inverse gamma, which is the first declared distribution it sees. I appreciate that dynare error messages do not correspond to the problem very often, but I find it extremely annoying that dynare 4 does not do what dynare 3 used to do easily and in an intuitive way.

Even if I make this steady state file work, how can I be sure that it calls it every time inside the loop? I would have thought that in linear models the steady state is zero and it should not be updated at every draw. If it does update it by default, it means that a lot of machine time is spent on it, even if it is explicitly declared that the model is linear. If this is the programming standard then I should just forget that dynare exists... Which is a pity.

I understand that I should stop complaining but just program all I need. I was sure that I was doing something very standard: imposing restrictions on estimated coefficients, and wanted to use the advantage dynare 4 might give, like paralelling the MH algorithm.

I would really want to see a single working example of bayesian estimation of a linear model with constraints on coefficients...
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Re: external function in dynare 4.2

Postby jpfeifer » Sun May 06, 2012 9:20 am

You should always post the exact error message. Guessing from your description, you are running Dynare 4.2.5. There is a documented bug in the inverse gamma function http://www.dynare.org/DynareWiki/KnownBugs. The fix can be found here: http://www.dynare.org/phpBB3/viewtopic.php?f=1&t=3750. Just replace the one file and it should run. It did on my computer, which indicates your steady state file works.

A couple more things:
1. You seem to misinterpret what linear means. There could always be an intercept term in the linear specification. Such mod-files are rare, but they exist. Hence, the option to supply a steady state file. And as the name says, it is only an option. If no steady state file exists (which Dynare checks once), a steady state of 0 is used and everything runs faster. In the latter case, there is no updating every period.
2. If your nonlinear constraints have a closed form solution, you can use the #-operator to define restrictions on the parameters. This is numerically more efficient than always calling a steady state file. But your initial problem description said something about "fsolve" from which I inferred that you don't have an analytical solution. In this case, updating the parameters in the steady state file is the easiest way to do it and hardly inefficient as you would need to do the parameter updating even if you program everything yourself.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: external function in dynare 4.2

Postby tanya » Sun May 06, 2012 10:40 am

Many many thanks! It did produce something! I hope that I can now deal with my "complications".
Many thanks again!
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Re: external function in dynare 4.2

Postby tanya » Sun May 06, 2012 12:07 pm

I managed to replicate the result from dynare 3 for one of my big toys. So, some confidence is there...

Can I, using the opportunity ask about the "output file" which is "not found". I guess this is not essential, but maybe it is something useful and nice to have?...
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Re: external function in dynare 4.2

Postby jpfeifer » Sun May 06, 2012 12:51 pm

What do you mean and what are you trying to do? Could you please post the exact message and, if necessary, the mod-file.
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: external function in dynare 4.2

Postby tanya » Sun May 06, 2012 1:03 pm

The mod file is the one attached above. Dynare estimates and then gives on the screen the following (see below)
It also produces two graphs: smoothed shocks and "Historical and smoothd variables". In the second graps I can only see historical variables. I suspect can also be something to do with the way the output is handled.

Please do not spend too much time on this. At the very end I could do it manually. I just thought it might be convenient.

...

eps_A 0.250 16.3980 3.4786 4.7140 invg 2.0000
eps_MU 0.050 0.0230 0.0094 2.4500 invg 2.0000
eps_XI 0.250 4.1266 0.6845 6.0287 invg 2.0000

Log data density [Laplace approximation] is -475.194057.

Warning: File 'rule_X4/Output' not found.
> In CheckPath at 42
In dynare_estimation_1 at 665
In dynare_estimation at 62
In rule_X4 at 196
In dynare at 120
Total computing time : 0h00m17s
>>
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Re: external function in dynare 4.2

Postby jpfeifer » Fri May 11, 2012 6:31 pm

To get correct smoothed observable plots, you need to replace two files in the matlab folder of Dynare 4.2.5 with the attached two files.
Attachments
dynare_estimation_init.m
(13.22 KiB) Downloaded 315 times
dynare_estimation_1.m
(65.26 KiB) Downloaded 296 times
------------
Johannes Pfeifer
University of Cologne
https://sites.google.com/site/pfeiferecon/
jpfeifer
 
Posts: 6940
Joined: Sun Feb 21, 2010 4:02 pm
Location: Cologne, Germany

Re: external function in dynare 4.2

Postby tanya » Sat May 12, 2012 1:47 pm

Many many thanks! It works!
tanya
 
Posts: 37
Joined: Tue Sep 12, 2006 6:41 pm

Next

Return to Dynare help

Who is online

Users browsing this forum: No registered users and 11 guests