@@ -15,6 +15,7 @@ class which was supposed to be an advanced data carrier class to be passed to th
15
15
import copy
16
16
from copy import deepcopy
17
17
import os
18
+ import subprocess
18
19
import sys
19
20
import re
20
21
from helpers import now_is
@@ -31,6 +32,15 @@ class which was supposed to be an advanced data carrier class to be passed to th
31
32
# Import PP, now comes supplied and installed with pymaclab
32
33
import pp
33
34
35
+ # Check if dynare++ is installed and in your path
36
+ dynare_flag = False
37
+ try :
38
+ dynret = subprocess .call ('dynare++' ,stdout = subprocess .PIPE ,stderr = subprocess .STDOUT )
39
+ dynare_flag = True
40
+ print 'Dynare++ found on your system path, accessible from PyMacLab...'
41
+ except :
42
+ dynare_flag = False
43
+
34
44
#NOTE: Imports from the refactor
35
45
from pymaclab .filters ._hpfilter import hpfilter
36
46
from pymaclab .filters ._bkfilter import bkfilter
@@ -42,6 +52,7 @@ class which was supposed to be an advanced data carrier class to be passed to th
42
52
from updaters .one_off import Updaters , dicwrap , dicwrap_deep , listwrap , matwrap
43
53
from updaters .queued import Updaters_Queued , dicwrap_queued , dicwrap_deep_queued , listwrap_queued ,\
44
54
matwrap_queued , Process_Queue , queue
55
+ from pymaclab .dsge .translators .translators import Translators
45
56
46
57
#Define a list of the Greek Alphabet for Latex
47
58
greek_alph = ['alpha' ,'beta' ,'gamma' ,'delta' ,'epsilon' ,
@@ -75,23 +86,26 @@ class which was supposed to be an advanced data carrier class to be passed to th
75
86
# Open mlab session
76
87
if use_matlab :
77
88
sess1 = mlabraw .open ('matlab - nojvm -nosplash' )
89
+
90
+ # Empty locdic for the locate helper function
91
+ locdic = {}
78
92
79
93
# Shell class for derivatives branch
80
94
class Derivatives (object ):
81
95
pass
82
96
83
- # Shell class for derivatives branch
97
+
84
98
class Inits (object ):
85
99
'''
86
- This is the factored-out macrolab DSGEmodel Initialisation class. This used to be part of the DSGEmodel
100
+ This is the factored-out macrolab DSGEmodel Initialisation class. This is used to be part of the DSGEmodel
87
101
class itself in the form of methods, but has been factored out into a separate class which takes a newly
88
102
instantiated DSGEmodel instance as and argument. It then possess several init methods which "work on the
89
- instance" and produce desired attributes
103
+ instance" and produce desired attributes to different depths of solution levels.
90
104
91
105
.. note::
92
106
93
- Notice that the various init method, i.e. init1, init1a, etc. are not called here, but they are called externally in the
94
- pymaclab package when instantiating a new DSGE model using pymaclab.newMOD().
107
+ Notice that the various init method, i.e. init1, init1a, etc. are not called here, but they are called
108
+ externally in the pymaclab package when instantiating a new DSGE model using pymaclab.newMOD().
95
109
96
110
:param other: The DSGEmodel instance to be passed to the Inits instance
97
111
:type other: DSGEmodel
@@ -304,7 +318,7 @@ def init2(self):
304
318
alldic = {}
305
319
alldic .update (other .paramdic )
306
320
intup = (other .manss_sys ,alldic )
307
- other .sssolvers .manss = ManualSteadyState (intup ,other = other )
321
+ other .sssolvers .manss = ManualSteadyState (intup ,other = other )
308
322
##################################### FULL PARSING AND UPDATER WRAPPING DONE #############################
309
323
def init3 (self ):
310
324
'''
@@ -510,6 +524,9 @@ def init4(self,no_wrap=False):
510
524
# We can also already compute a dictionary holding all the vars w.r.t which derivatives will be taken
511
525
# This will attach a dictionary to the model instance called self.differvardic
512
526
other .def_differ_periods ()
527
+ # Open the translators branch on the DSGE model instance
528
+ # This should be done here, so that the passed "other" model instance has the translator branch
529
+ other .translators = Translators (other = other )
513
530
514
531
if not no_wrap :
515
532
# Need to wrap variance covariance matrix here
@@ -542,8 +559,13 @@ def init5(self,update=False):
542
559
#TODO: delay above and only import if needed
543
560
from solvers .modsolvers import MODsolvers
544
561
# Open the model solution tree branch
545
- other .modsolvers = MODsolvers ()
546
- ######################## LINEAR METHODS !!! ############################
562
+ other .modsolvers = MODsolvers ()
563
+ ################################### DYNARE++ ####################################
564
+ # Open the dynare branch if dynare++ is installed on the system and in your PATH
565
+ if dynare_flag :
566
+ from solvers .modsolvers import Dynarepp
567
+ other .modsolvers .dynarepp = Dynarepp ({'_other' :other ,'_mesg' :other ._mesg })
568
+ ############################# LINEAR METHODS !!! ################################
547
569
# see if there are any log-linearized equations
548
570
if all ([False if 'None' in x else True for x in secs ['modeq' ][0 ]]):
549
571
from solvers .modsolvers import PyUhlig , MatUhlig , MatKlein , MatKleinD , ForKlein
@@ -693,12 +715,10 @@ def init_out(self):
693
715
694
716
'''
695
717
other = self ._other
696
- initlev = other ._initlev
718
+ initlev = other ._initlev
697
719
# Make sure the jobserver has done his jobs
698
720
jobserver .wait ()
699
721
700
- # Empty locdic for the locate helper function
701
- locdic = {}
702
722
703
723
##################THE DSGE MODEL CLASS (WORKS)#####################
704
724
class DSGEmodel (object ):
@@ -745,8 +765,6 @@ class DSGEmodel(object):
745
765
# Initializes the absolute basics, errors difficult to occur
746
766
def __init__ (self ,ffile = None ,dbase = None ,initlev = 2 ,mesg = False ,ncpus = 'auto' ,mk_hessian = True ,\
747
767
use_focs = False ,ssidic = None ,sstate = None ,vtiming = {'exo' :[- 1 ,0 ],'endo' :[- 1 ,0 ],'con' :[0 ,1 ]}):
748
- # Open the inits branch
749
- self .inits = Inits (other = self )
750
768
# Make sure the jobserver has done his global jobs each time you instantiate a new instance
751
769
jobserver .wait ()
752
770
if sstate != None :
@@ -770,83 +788,9 @@ def __init__(self,ffile=None,dbase=None,initlev=2,mesg=False,ncpus='auto',mk_hes
770
788
# Attach some model attributes
771
789
self .modfile = ffile
772
790
self .dbase = dbase
791
+ # Open the inits branch at the end
792
+ self .inits = Inits (other = self )
773
793
774
-
775
- def mk_dynare (self ,order = 1 ,centralize = False ,fpath = None ,focli = None ):
776
- # Import the template and other stuff
777
- from pymaclab .modfiles .templates import mako_dynare_template
778
- from scipy import io
779
- from solvers .modsolvers import Dynare
780
- import tempfile
781
- import os
782
- import glob
783
- template_dic = deepcopy (self .template_paramdic )
784
-
785
- # Render the template to be passed to dynare++
786
- tmp_dic = {}
787
- tmp_dic ['focli' ] = focli
788
- template_dic .update (tmp_dic )
789
- modstr = mako_dynare_template .render (** template_dic )
790
-
791
- # If a filepath has been passed then just write the Dynare++ modfile, but no more!
792
- if fpath != None :
793
- filo = open (fpath ,'w' )
794
- filo .write (modstr )
795
- filo .flush ()
796
- filo .close ()
797
- return
798
-
799
- filo = tempfile .NamedTemporaryFile ()
800
- fname = filo .name
801
- fname2 = fname .split ('/' )[- 1 ]
802
- '''
803
- filo2 = open(os.path.join(os.getcwd(),'test.mod'),'w')
804
- filo2.write(modstr)
805
- filo2.flush()
806
- filo2.close()
807
- '''
808
- filo .file .write (modstr )
809
- filo .file .flush ()
810
- self .modsolvers .dynare = Dynare ({})
811
- self .modsolvers .dynare .__setattr__ ('modfile' ,modstr )
812
- if not centralize :
813
- os .system ('dynare++ --no-centralize --order ' + str (order )+ ' ' + filo .name )
814
- else :
815
- os .system ('dynare++ --order ' + str (order )+ ' ' + filo .name )
816
- dynret = io .loadmat (os .path .join (os .getcwd (),filo .name .split ('/' )[- 1 ]+ '.mat' ))
817
- # Check if solution has been computed and attache all solution matrices to dynare instance
818
- if dynret .has_key ('dyn_g_1' ):
819
- self .modsolvers .dynare .__init__ (dynret )
820
- else :
821
- print "FAIL: Dynare could not determine solution."
822
- filo .close ()
823
- # Delete all of the files
824
- for filor in glob .glob (fname2 + '*.*' ):
825
- os .remove (filor )
826
-
827
- # Create some other objects on dynare branch
828
- self .modsolvers .dynare .sstate = {}
829
- self .modsolvers .dynare .dynorder = []
830
- self .modsolvers .dynare .dynsorder = []
831
- for i1 ,elem in enumerate (self .modsolvers .dynare .dyn_vars ):
832
- self .modsolvers .dynare .dynorder .append (str (elem ).strip ()+ '(t)' )
833
- if str (elem ).strip ()+ '_bar' in self .sstate .keys ():
834
- self .modsolvers .dynare .sstate [str (elem ).strip ()+ '_bar' ] = self .modsolvers .dynare .dyn_ss [i1 ,0 ]
835
- for i1 ,elem in enumerate (self .modsolvers .dynare .dyn_state_vars ):
836
- self .modsolvers .dynare .dynsorder .append (str (elem ).strip ()+ '(t)' )
837
-
838
- # Create PP and FF matrix reflecting ordering of pymaclab
839
- P = np .matlib .zeros ([self .nstat ,self .nstat ])
840
- F = np .matlib .zeros ([self .ncon ,self .nstat ])
841
- listo = [x [0 ] for x in self .vdic ['exo' ]]+ [x [0 ] for x in self .vdic ['endo' ]]
842
- for i1 ,elem in enumerate (listo ):
843
- for i2 ,elem2 in enumerate (listo ):
844
- P [i1 ,i2 ] = self .modsolvers .dynare .dyn_g_1 [self .modsolvers .dynare .dynorder .index (elem ),self .modsolvers .dynare .dynsorder .index (elem2 )]
845
- for i1 ,elem in enumerate ([x [0 ] for x in self .vdic ['con' ]]):
846
- for i2 ,elem2 in enumerate (listo ):
847
- F [i1 ,i2 ] = self .modsolvers .dynare .dyn_g_1 [self .modsolvers .dynare .dynorder .index (elem ),self .modsolvers .dynare .dynsorder .index (elem2 )]
848
- self .modsolvers .dynare .PP = deepcopy (P )
849
- self .modsolvers .dynare .FF = deepcopy (F )
850
794
851
795
def find_rss (self ,mesg = False ,rootm = 'hybr' ,scale = 0.0 ):
852
796
'''
@@ -1862,6 +1806,12 @@ def mkjahepp(self):
1862
1806
pos = ma .span ()[0 ]
1863
1807
poe = ma .span ()[1 ]
1864
1808
str_tmp = str_tmp [:pos ]+ 'sympycore.exp(' + str_tmp [poe :]
1809
+ eexp2 = re .compile ('(?<!sympycore\.)E\*\*' )
1810
+ while eexp2 .search (str_tmp ):
1811
+ ma = eexp2 .search (str_tmp )
1812
+ pos = ma .span ()[0 ]
1813
+ poe = ma .span ()[1 ]
1814
+ str_tmp = str_tmp [:pos ]+ 'sympycore.E**' + str_tmp [poe :]
1865
1815
try :
1866
1816
func .append (eval (str_tmp ))
1867
1817
except :
0 commit comments