External functions

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):wink: that uses the root function.

Thank you in advance for your help!
root.m (60 Bytes)
basicmodel3.mod (3.73 KB)

There has been a bug related to external functions in 4.4.2. Please try the most recent unstable version and report back.

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

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): github.com/DynareTeam/dynare

  4. You don’t need the root function. Just declare an externalfunction called nthroot, as in:

Replace line 81 with:

and delete root.m

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!

basicmodel3.mod (3.78 KB)

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.