dynare-python, yaml based formulas, nlsystemfit

120 views
Skip to first unread message

josef...@gmail.com

unread,
May 13, 2012, 6:45:39 AM5/13/12
to pystatsmodels
for a system of non-linear equations with cross-equation restrictions
and parameter restrictions

https://github.com/albop/dynare-python/blob/master/dolo/examples/global_models/open_economy.yaml

there are also some model definition "standards", but compared to
reading various field specific data files, I haven't seen much for
model definitions.

a quick survey: what do the others use:


from the Stata help

```
1. To fit the system of equations

y1 = a1 + b1*x^g1

y2 = a2 + b2*x^g2

by iterative FGNLS, where a1, a2, b1, b2, g1, and g2 are
parameters and 1 is a
reasonable starting value for both g1 and g2, you would type

. nlsur (y1 = {a1} + {b1}*x^{g1=1}) (y2 = {a2} +
{b2}*x^{g2=1}), ifgnls

2. nlsur makes imposing cross-equation parameter restrictions
easy. Say that you
want to fit a pair of exponential growth equations with
the restriction that
the constant terms in the two equations are equal,

y1 = a + b1*b2^x

y2 = a + c1*c2^x

where a, b1, b2, c1, and c2 are the parameters. To fit
this model, you would
type

. nlsur (y1 = {a} + {b1}*{b2}^x) (y2 = {a} + {c1}*{c2}^x)
```

from the R help

```
library( systemfit )
data( ppine )

hg.formula <- hg ~ exp( h0 + h1*log(tht) + h2*tht^2 + h3*elev + h4*cr)
dg.formula <- dg ~ exp( d0 + d1*log(dbh) + d2*hg + d3*cr + d4*ba )
labels <- list( "height.growth", "diameter.growth" )
inst <- ~ tht + dbh + elev + cr + ba
start.values <- c(h0=-0.5, h1=0.5, h2=-0.001, h3=0.0001, h4=0.08,
d0=-0.5, d1=0.009, d2=0.25, d3=0.005, d4=-0.02 )
model <- list( hg.formula, dg.formula )

model.sur <- nlsystemfit( "SUR", model, start.values, data=ppine,
eqnlabels=labels )
print( model.sur )
```
http://lojze.lugos.si/~darja/software/r/library/systemfit/html/nlsystemfit.html
(google doesn't find any canonical help web site for R packages)

from the SAS help

```
proc model data=test2;
exogenous x1 x2;
parms a1 a2 b2 2.5 c2 55 d1;

y1 = a1 * y2 + b2 * x1 * x1 + d1;
y2 = a2 * y1 + b2 * x2 * x2 + c2 / x2 + d1;

fit y1 y2 / 2sls;
instruments b2 c2 _exog_;
run;
```
http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/viewer.htm#etsug_model_sect004.htm

EVIEWS gretl

```
On Wed, 21 Apr 2010, Erhan Azrai wrote:

> I am having trouble to convert a piece eviews code below to gretl
> system. The code is used to solve an OLG model of 8 simultaneous
> equations. Can it even be done? Thank you.
>
> model mv20den
>
> 'equations for the model = 8 simultaneous equations
>
> 'Equation 1 : gamma_EA ~human capital accumulation
> mv20den.append gamma_EA = (A_9 * (z_tilde ^ (-nu_3))) - 1
>
> 'Equation 2 : gamma_M ~growth accumulation
> mv20den.append gamma_M = A_12 * (z_tilde ^ (1 -phi_2) ) * chi_tilde * phi_tilde
>
> 'Equation 3 : z2 ~
> mv20den.append z2 = A_12 * (z_tilde ^ (1 -phi_2) ) * chi_tilde * phi_tilde

At present gretl's "system" command does not support systems of
nonlinear equations. The most promising option in gretl may be to
use gmm, since you can specify multiple orthogonality conditions
and are not limited to linear specifications.
```
http://lists.wfu.edu/pipermail/gretl-users/2010-April/004753.html

gretl gmm, something like

```
# initializations go here
series e1 = 0
series e2 = 0
matrix W = I(8)
scalar alpha = 0
scalar beta = 0
list IVs = const CONSUMPTION_DET(-1) GDP_DETREND(-1) R_DETREND(-1)
gmm
e1 = (alpha / beta) * CONSUMPTION_DET
e2 = (1 / alpha) * GDP_DETREND
orthog e1 ; IVs
orthog e2 ; IVs
weights W
params alpha beta
end gmm
```
http://lists.wfu.edu/pipermail/gretl-users/2009-October/003885.html



TSP
```
These are the equations which are used to estimate the illustrative
model by three stage least squares in LSQ:

FRML CONSEQ CONS=A+B*GNP ;
FRML INVEQ I=LAMBDA*I(-1) + ALPHA*GNP/(DELTA+R) ;
FRML INTRSTEQ R=D+F*(LOG(GNP)+LP-LM) ;
FRML PRICEQ LP = LP(-1)+PSI*(LP(-1)-LP(-2))+PHI*LOG(GNP)+TREND*TIME +P0;

In these equations, the dependent variables are CONS, I, R, and LP.
The other series are GNP, LM, and TIME. Note the use of lagged series
in the equation also. The other variables, A, B, LAMBDA, ALPHA, DELTA,
D, F, PSI, PHI, TREND, and P0, are parameters and constants
```
and
```
The next example shows how to set up the well-known Klein Model I for
simulation. The equations are linear, so they are formed after the
corresponding instrumental variables estimation.

INST CX C P P(-1) W INVR C P(-1) K(-1) E(-1) TM W2 G TX ;
FORM CONS ;
INST I C P P(-1) K(-1) INVR C P(-1) K(-1) E(-1) TM W2 G TX ;
FORM INV ;
INST W1 C E E(-1) TM INVR C P(-1) K(-1) E(-1) TM W2 G TX ;
FORM WAGES ;
IDENT WAGE W = W1+W2 ;
IDENT BALANCE CX+I+G-(TX+W+P) ;
IDENT PPROD E-P-TX-W1 ;
IDENT CAPSTK K=K(-1)+I ;
SIML (PRNRES,TAG=S,ENDOG=(CX,I,W1,W,E,P,K))
CONS INV WAGES WAGE BALANCE PPROD CAPSTK ;
```
http://www.nber.org/tsp/tsphelp/frml.htm
http://www.nber.org/tsp/tsphelp/siml.htm


GAUSS is barebones, given a brief search
```
library nlsys;
#include nlsys.ext;
nlset;

proc fsys(x);
local f1,f2,f3,f4,f5,f6,f7,P;
P = 20;
f1 = 0.5*x[1] + x[2] + 0.5*x[3] - x[6]/x[7];
f2 = x[3] + x[4] + 2*x[5] - 2/x[7];
f3 = x[1] + x[2] + x[5] - 1/x[7];
f4 = -28837*x[1] - 139009*x[2] - 78213*x3 + 18927*x[4] +
8427*x[5] + 13492/x[7] - 10690*x[6]/x[7];
f5 = x[1] + x[2] + x[3] + x[4] + x[5] - 1;
f6 = (P^2)*x[1]*x[4]^3 - 1.7837*1e5*x[3]*x[5];
f7 = x[1]*x[3] - 2.6058*x[2]*x[4];
retp(f1|f2|f3|f4|f5|f6|f7);
endp;
```
pdf only ciser.cornell.edu/CRADC/FAQ/GAUSS/Gauss%20Manuals/nle.pdf

MATLAB
similar to scipy, gauss for solving system of equations, defined as
regular functions
a quick google search didn't find estimation with NL2SLS


pymc
??? didn't check

statsmodels

???
(In analogy to GRETL, the GMM estimation might actually already work,
since it doesn't care what the moment conditions are. requires
equations to be defined as method in subclass)


Josef
somewhere in the future ...
being distracted following a link on the scipy-dev list.

Pablo W.

unread,
May 20, 2012, 2:54:13 PM5/20/12
to pystat...@googlegroups.com
Hi Josef-from-the-future,

I read this email almost by accident. I can only guess about your plans concerning a model format but as the historical developper of dolo [dynare-python], I have some comments regarding the choice of YAML to store model definition. [The actual definition of the model is a competely different story though]
The idea was to simplify as much as possible the writing and the parsing of model file so I looked for an existing serialization format (equations are parsed directly by using sympy). I considered two options seriously: XML and YAML but I kept the last one because it is much more concise. Overall I have been very pleased with it, except for some minor disadvantages:
- it has no attributes for nodes like xml. In theory it is not a problem but one has to increase the number of nodes to represent, for instance a simple function with a main argument and some non-mandatory options
- in a list of equations, each line is prefixed by '-' so 'a-b' becomes '- a-b' which can be confusing. I solved this problem by replacing everywhere '-' by '--' so the line can be written '-- a - bwhich is slightly better.
- associative mappings in YAML are represented as unordered dictionaries in python. I would like to keep the order from the yaml file. I believe there is a workaround but I haven't found it yet.
These shortcomings really come from the YAML definition but I haven't found an existing serialization language that is better suited to my need. I'm curious to see what choice you will make for pystatsmodel .

Best,

Pablo

Reply all
Reply to author
Forward
0 new messages