11
11
import re
12
12
from copy import deepcopy
13
13
import numpy .matlib as mat
14
+ import numpy as np
14
15
import sys
15
16
import os
16
17
# This used to import sympycore, but should now also work with sympy
@@ -23,6 +24,9 @@ def populate_model_stage_one(self, secs):
23
24
1st stage population of DSGE model. Does not need Steady State.
24
25
"""
25
26
27
+ # Establish the timing convention of variables in the model file
28
+ self = def_timing (self ,** self ._vtiming )
29
+
26
30
# This is a special dictionary which can be handed over to the template engines (i.e. Jinja2)
27
31
if 'template_paramdic' not in dir (self ): self .template_paramdic = {}
28
32
@@ -284,7 +288,7 @@ def populate_model_stage_one(self, secs):
284
288
285
289
286
290
############# BELOW HERE IS ALL FOR 2ND STAGE ###########
287
- def def_timing (self ,endo = [- 1 ,0 ],exo = [- 1 ,0 ],con = [0 ,1 ]):
291
+ def def_timing (self ,exo = [- 1 ,0 ],endo = [- 1 ,0 ],con = [0 ,1 ]):
288
292
'''
289
293
A small method which can be used in order to set the timings in stone.
290
294
This will then be used in other parts of the code, also in macrolab package
@@ -480,7 +484,7 @@ def mkaug1(self, insys,othersys):
480
484
# Now change the system to include possible augmented variables
481
485
endo_r = filter (lambda x : x [1 ] != endotime , spvdic2 ['endo' ])
482
486
if endo_r :
483
- endo_r = [[x [0 ],[abs (x [1 ][0 ]+ 1 ) ,x [1 ][1 ]]] for x in endo_r ]
487
+ endo_r = [[x [0 ],[abs (x [1 ][0 ]) - abs ( endotime [ 0 ]) ,x [1 ][1 ]- abs ( endotime [ 1 ]) ]] for x in endo_r ]
484
488
# Create lags and forwards equations
485
489
for vari in endo_r :
486
490
for lag in range (abs (vari [1 ][0 ])):
@@ -503,7 +507,7 @@ def mkaug1(self, insys,othersys):
503
507
list_tmp1 .append (vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind + '(t)' + ' - ' + 'E(t)|' + vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind1 + '(t+1)' )
504
508
exo_r = filter (lambda x : x [1 ] != exotime , spvdic2 ['exo' ])
505
509
if exo_r :
506
- exo_r = [[x [0 ],[abs (x [1 ][0 ])- 1 ,x [1 ][1 ]]] for x in exo_r ]
510
+ exo_r = [[x [0 ],[abs (x [1 ][0 ])- abs ( exotime [ 0 ]) ,x [1 ][1 ]- abs ( exotime [ 1 ]) ]] for x in exo_r ]
507
511
# Create lags and forwards equations
508
512
for vari in exo_r :
509
513
for lag in range (abs (vari [1 ][0 ])):
@@ -527,7 +531,7 @@ def mkaug1(self, insys,othersys):
527
531
list_tmp1 .append (vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind + '(t)' + ' - ' + 'E(t)|' + vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind1 + '(t+1)' )
528
532
con_r = filter (lambda x : x [1 ] != contime , spvdic2 ['con' ])
529
533
if con_r :
530
- con_r = [[x [0 ],[abs (x [1 ][0 ]),x [1 ][1 ]- 1 ]] for x in con_r ]
534
+ con_r = [[x [0 ],[abs (x [1 ][0 ])- abs ( contime [ 0 ]) ,x [1 ][1 ]- abs ( contime [ 1 ]) ]] for x in con_r ]
531
535
# Create lags and forwards equations
532
536
for vari in con_r :
533
537
for lag in range (abs (vari [1 ][0 ])):
@@ -739,7 +743,7 @@ def mkaug2(self, insys):
739
743
# Now change the system to include possible augmented variables
740
744
endo_r = filter (lambda x : x [1 ] != endotime , spvdic2 ['endo' ])
741
745
if endo_r :
742
- endo_r = [[x [0 ],[abs (x [1 ][0 ]+ 1 ) ,x [1 ][1 ]]] for x in endo_r ]
746
+ endo_r = [[x [0 ],[abs (x [1 ][0 ]) - abs ( endotime [ 0 ]) ,x [1 ][1 ]- abs ( endotime [ 1 ]) ]] for x in endo_r ]
743
747
# Create lags and forwards equations
744
748
for vari in endo_r :
745
749
for lag in range (abs (vari [1 ][0 ])):
@@ -762,7 +766,7 @@ def mkaug2(self, insys):
762
766
list_tmp1 .append (vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind + '(t)' + ' - ' + 'E(t)|' + vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind1 + '(t+1)' )
763
767
exo_r = filter (lambda x : x [1 ] != exotime , spvdic2 ['exo' ])
764
768
if exo_r :
765
- exo_r = [[x [0 ],[abs (x [1 ][0 ])- 1 ,x [1 ][1 ]]] for x in exo_r ]
769
+ exo_r = [[x [0 ],[abs (x [1 ][0 ])- abs ( exotime [ 0 ]) ,x [1 ][1 ]- abs ( exotime [ 1 ]) ]] for x in exo_r ]
766
770
# Create lags and forwards equations
767
771
for vari in exo_r :
768
772
for lag in range (abs (vari [1 ][0 ])):
@@ -786,7 +790,7 @@ def mkaug2(self, insys):
786
790
list_tmp1 .append (vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind + '(t)' + ' - ' + 'E(t)|' + vari [0 ][0 ].split ('(' )[0 ]+ '_F' + tind1 + '(t+1)' )
787
791
con_r = filter (lambda x : x [1 ] != contime , spvdic2 ['con' ])
788
792
if con_r :
789
- con_r = [[x [0 ],[abs (x [1 ][0 ]),x [1 ][1 ]- 1 ]] for x in con_r ]
793
+ con_r = [[x [0 ],[abs (x [1 ][0 ])- abs ( contime [ 0 ]) ,x [1 ][1 ]- abs ( contime [ 1 ]) ]] for x in con_r ]
790
794
# Create lags and forwards equations
791
795
for vari in con_r :
792
796
for lag in range (abs (vari [1 ][0 ])):
@@ -1702,8 +1706,6 @@ def mknonlinsys(self, secs):
1702
1706
Create Non-Linear FOC System
1703
1707
"""
1704
1708
list_tmp1 = deepcopy (self .foceqs2 )
1705
- # Establish the timing convention of variables in the model file
1706
- self = def_timing (self )
1707
1709
self , list_tmp1 = mkaug2 (self , list_tmp1 )
1708
1710
1709
1711
if any ([False if 'None' in x else True for x in secs ['vsfocs' ][0 ]]):
@@ -1763,7 +1765,7 @@ def mknonlinsys(self, secs):
1763
1765
self .nlsys_list = list_tmp1
1764
1766
return self
1765
1767
1766
- def mkloglinsys2 (inlist ):
1768
+ def mkloglinsys2 (self , inlist ):
1767
1769
"""
1768
1770
This function simply takes the left-hand side
1769
1771
of each equation and takes it over to the
@@ -1866,6 +1868,14 @@ def mkloglinsys2(inlist):
1866
1868
self .nlsys_list = deepcopy (list_tmp1 )
1867
1869
return list_tmp1
1868
1870
1871
+ def subs_in_loglinsys (self ):
1872
+ list_tmp1 = deepcopy (self .llsys_list )
1873
+ subsdic = deepcopy (self .nlsubs )
1874
+ for i1 ,eqo in enumerate (list_tmp1 ):
1875
+ while "@" in list_tmp1 [i1 ]:
1876
+ for keyo in subsdic .keys ():
1877
+ if keyo in list_tmp1 [i1 ]: list_tmp1 [i1 ] = list_tmp1 [i1 ].replace (keyo ,subsdic [keyo ])
1878
+ return list_tmp1
1869
1879
1870
1880
# Creates the log-linear system
1871
1881
def mkloglinsys1 (secs ):
@@ -1944,13 +1954,19 @@ def mkeqtype(self):
1944
1954
1945
1955
# Make symbolic system and numeric as well
1946
1956
def mksymsys (self ):
1957
+ _mreg = 'E{1,1}[^(]'
1958
+ mreg = re .compile (_mreg )
1947
1959
func = []
1948
1960
subli = []
1949
1961
patup = ('{-10,10}|None' ,'all' ,'{-10,10}' )
1950
1962
for x in self .sstate .keys ()+ self .paramdic .keys ():
1951
1963
locals ()[x ] = eval ("SP.Symbol('" + x + "')" )
1952
1964
for y in self .llsys_list :
1953
1965
str_tmp = y [:]
1966
+ while 'EXP(' in str_tmp :
1967
+ str_tmp = str_tmp .replace ('EXP(' ,'SP.exp(' )
1968
+ while 'LOG(' in str_tmp :
1969
+ str_tmp = str_tmp .replace ('LOG(' ,'SP.log(' )
1954
1970
vali = [x [0 ] for x in self .vreg (patup ,y ,True ,'min' )]
1955
1971
vali2 = [[x ,'sub' + str (u )] for x ,u in zip (vali ,range (0 ,len (vali ),1 ))]
1956
1972
vali3 = [(x [0 ],x [3 ]) for x in self .vreg (patup ,str_tmp ,True ,'max' )]
@@ -1978,7 +1994,16 @@ def mksymsys(self):
1978
1994
tmpdic = {}
1979
1995
for y in x .keys ():
1980
1996
# tmpdic[y] = eval(x[y].tostr())
1981
- tmpdic [y ] = eval (str (x [y ]))
1997
+ str_tmp2 = str (x [y ])
1998
+ while mreg .search (str_tmp2 ):
1999
+ maout = [por for por in mreg .finditer (str_tmp2 )]
2000
+ maout .reverse ()
2001
+ for mato in maout :
2002
+ starts = mato .span ()[0 ]
2003
+ ends = mato .span ()[1 ]
2004
+ expo = mato .group ()
2005
+ str_tmp2 = str_tmp2 [:starts ]+ 'np.exp(1)' + str_tmp2 [ends - 1 :]
2006
+ tmpdic [y ] = eval (str_tmp2 )
1982
2007
1983
2008
diffli2 .append (tmpdic )
1984
2009
self .diffli2 = diffli2
@@ -2112,12 +2137,11 @@ def populate_model_stage_one_bb(self, secs):
2112
2137
self .foceqs2 = deepcopy (self .foceqs )
2113
2138
# Also create a steady state version
2114
2139
self = mk_steady2 (self )
2140
+ # Also save a copy of the focs with replaced substitutions
2141
+ self .template_paramdic ['focs_list2' ] = deepcopy (self .foceqs2 )
2115
2142
else :
2116
2143
# Save for template instantiation
2117
2144
self .template_paramdic ['focs_list' ] = False
2118
-
2119
- # Also save a copy of the focs with replaced substitutions
2120
- self .template_paramdic ['focs_list2' ] = deepcopy (self .foceqs2 )
2121
2145
return self
2122
2146
2123
2147
@@ -2126,13 +2150,14 @@ def populate_model_stage_two(self, secs):
2126
2150
2nd stage population of DSGE model. Needs Steady State.
2127
2151
"""
2128
2152
# Creates nonlinear foc system
2129
- if any ([False if 'None' in x else True for x in secs ['focs' ][0 ]]):
2153
+ if all ([False if 'None' in x else True for x in secs ['focs' ][0 ]]):
2130
2154
self = mknonlinsys (self , secs )
2131
2155
2132
2156
# Creates loglinear system
2133
- if any ([False if 'None' in x else True for x in secs ['modeq' ][0 ]]):
2157
+ if all ([False if 'None' in x else True for x in secs ['modeq' ][0 ]]):
2134
2158
llsys_list = mkloglinsys1 (secs )
2135
- self .llsys_list = mkloglinsys2 (llsys_list )
2159
+ self .llsys_list = mkloglinsys2 (self ,llsys_list )
2160
+ self .llsys_list = subs_in_loglinsys (self )
2136
2161
# Save for template instantiation
2137
2162
self .template_paramdic ['llsys_list' ] = deepcopy (self .llsys_list )
2138
2163
self = mksymsys (self ) # creates symbolic and numerical system
@@ -2142,7 +2167,7 @@ def populate_model_stage_two(self, secs):
2142
2167
self .template_paramdic ['llsys_list' ] = False
2143
2168
2144
2169
# Creates variance/covariance
2145
- if any ([False if 'None' in x else True for x in secs ['vcvm' ][0 ]]) and \
2170
+ if all ([False if 'None' in x else True for x in secs ['vcvm' ][0 ]]) and \
2146
2171
'sstate' in dir (self ):
2147
2172
self .sigma = mksigmat (self , secs )
2148
2173
# Save for template instantiation
0 commit comments