Page 1 of 1

external functions

PostPosted: Tue Apr 08, 2014 8:54 pm
by Guangye
Dear all,

I'm a novice at using Dynare and I'd greatly appreciate any suggestions you might have regarding this possibly simple problem. I'd like to use a standard matlab function (nthroot), but Dynare doesn't support it. I want to calculate an odd number root of a negative number (for example (-1)^(1/3)), but typing (-1)^(1/3) gives 0.5+0.0866i. To get -1, I need to use the nthroot function. So I can I use it in Dynare?

I tried following the instructions in the Dynare guide by creating a function called root which uses nthroot to do the calculation, and declared root in the mod file with "external_function(name = root,nargs = 3);" before using the function in one of the model equations. However I got this error "terminate called after throwing an instance of 'ExternalFunctionNode::UnknownFunctionNameAndArgs'".

Attached are the mod and m files. The function is declared after the block of parameters, and it's the last model equation (taud = root((d(-1)/(p_N^(1-gamma))),(k1/k2),tau);) that uses the root function.

Thank you in advance for your help!

Re: external functions

PostPosted: Wed Apr 09, 2014 8:35 am
by jpfeifer
There has been a bug related to external functions in 4.4.2. Please try the most recent unstable version and report back.

Re: external functions

PostPosted: Thu Apr 10, 2014 3:57 am
by Guangye
Hello Prof. Pfeifer,

Thank you for the reply. I tried the same codes with the unstable version 4.4_2013-10-31 for Mac, and it still isn't working without giving an error message to indicate the cause (model diagnostics did not return anything). Below are the output. Could there be syntactical errors in my attempt to declare the external function? I tried following the manual. Anything suggestion would be greatly appreciated! Thank you!

"/Applications/Dynare/4.4_2013-10-31/matlab/dynare_m" basicmodel3.mod: Bus error
Starting Dynare (version 4.4-unstable).
Starting preprocessing of the model file ...
Found 14 equation(s).
Evaluating expressions...done
Computing static model derivatives:
- order 1
Computing dynamic model derivatives:
- order 1
- order 2

Error using dynare (line 141)
DYNARE: preprocessing failed

Re: external functions

PostPosted: Thu Apr 10, 2014 10:12 am
by HoutanBastani
Several things:

1) This is a bug with the Mac snapshot and, unfortunately, more recent versions of the snapshot don't exist for lack of resources.

2) The external functions fix was made after 31 October, so you need a more recent build

3) Out of curiosity, what version of OS X are you using? Anyway, you can build Dynare on your computer following the instructions here (under Mac OS X): https://github.com/DynareTeam/dynare

4) You don't need the root function. Just declare an externalfunction called nthroot, as in:
Code: Select all
external_function(name = nthroot,nargs = 2);

Replace line 81 with:
Code: Select all
taud = tau*nthroot((d(-1)/(p_N^(1-gamma))),(k1/k2));

and delete root.m

Re: external functions

PostPosted: Mon Apr 21, 2014 7:14 pm
by Guangye
Thank you very Houtan.

I have OS X 10.8.5, but we ended up compiling Dynare on a cluster and now the external function is working. However, Dynare cannot find the steady state, it's giving the following error whenever I try to take a root of greater than 1 with the nthroot external function:
"
Error using print_info (line 74)
Impossible to find the steady state. Either the model doesn't have a steady state, there are
an infinity of steady states, or the guess values are too far from the solution
"
Is that because there's still something wrong with the external function, or because one of my equations is wrong? I attached the mod file.

Thank you so much, I really appreciate your help!

Guangye

Re: external functions

PostPosted: Wed May 07, 2014 9:02 am
by jpfeifer
It seems you just need to provide better starting values. If you put resid before steady you will see that particularly equation 9 has a big residual.