Skip to content

Commit 93b0404

Browse files
committed
Temporary commit after much work on abcs_rbcs model branch and implementing test cases
1 parent 857ad2f commit 93b0404

19 files changed

+3401
-2222
lines changed

pymaclab.wpu

Lines changed: 2580 additions & 2072 deletions
Large diffs are not rendered by default.

pymaclab/dsge/inits/_inits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def init1(self,no_wrap=False):
109109
if txtpars.fmt == 'pymaclab':
110110
other = populate_model_stage_one(other, secs)
111111
elif txtpars.fmt == 'dynarepp':
112-
pml_modfile = dynarepp_to_pml_translate(secs=secs)
112+
pml_modfile = dynarepp_to_pml.translate(secs=secs)
113113

114114
if not no_wrap:
115115
# Open updaters path

pymaclab/dsge/parsers/_dsgeparser.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,8 @@ def populate_model_stage_one(self, secs):
6868
audic['iid']['mod'] = []
6969

7070
for x in secs['varvec'][0]:
71-
if viiexp.search(x):
72-
ma = viiexp.search(x)
73-
vari = ma.group('vari').strip()
74-
viid = ma.group('viid').strip()
75-
varn = ma.group('varn').strip()
76-
vtype = ma.group('vtype').strip()
77-
mods = ma.group('mod')
78-
vardic[vtype]['var'].append([vari,varn,viid])
79-
if mods != None:
80-
mods = mods.strip()
81-
if ',' in mods:
82-
vardic[vtype]['mod'].append(mods[1:-1].strip().split(','))
83-
else:
84-
vardic[vtype]['mod'].append([mods[1:-1].strip(),])
85-
else:
86-
vardic[vtype]['mod'].append([])
8771

88-
elif vtexp.search(x):
72+
if vtexp.search(x):
8973
ma = vtexp.search(x)
9074
vari = ma.group('vari').strip()
9175
varn = ma.group('varn').strip()
@@ -99,7 +83,7 @@ def populate_model_stage_one(self, secs):
9983
else:
10084
vardic[vtype]['mod'].append([mods[1:-1].strip(),])
10185
else:
102-
vardic[vtype]['mod'].append([])
86+
vardic[vtype]['mod'].append([''])
10387

10488
elif voexp.search(x):
10589
ma = voexp.search(x)
@@ -115,7 +99,7 @@ def populate_model_stage_one(self, secs):
11599
else:
116100
vardic['other']['mod'].append([mods[1:-1].strip(),])
117101
else:
118-
vardic['other']['mod'].append([])
102+
vardic['other']['mod'].append([''])
119103

120104
self.nendo = len(vardic['endo']['var'])
121105
self.nexo = len(vardic['exo']['var'])

pymaclab/dsge/solvers/modsolvers.py

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pymaclab.dsge.helpers import _helpers as HLP
1717
from scipy.linalg import qz
1818
import numpy as np
19+
import scipy
1920
import pylab as P
2021
from matplotlib import pyplot as PLT
2122
import copy as COP
@@ -294,22 +295,91 @@ def mkmats(self):
294295
# Change NN matrix by swapping signs
295296
self.N = self.N*(-np.matlib.eye(self.N.shape[0]))
296297

297-
def solve(self,sol_method='do_QZ',Xi_select='all'):
298+
def solve(self,sol_method='do_QZ2',Xi_select='all'):
298299
self.output = {}
299300
self.Xi_select = Xi_select
300301
self.sol_method = sol_method
301302
CC = self.C
302303
try:
303-
testo = inv(C)
304+
testo = inv(CC)
304305
except:
305306
print "Error: Matrix C is not of full rank!"
306307
return False
307308
if self.sol_method == 'do_PP':
308309
self.do_PP(self.Xi_select)
309310
elif self.sol_method == 'do_QZ':
310311
self.do_QZ(self.Xi_select,Tol=1.0000e-06)
312+
elif self.sol_method == 'do_QZ2':
313+
self.do_QZ2(self.Xi_select,Tol=1.0000e-06)
311314
self.do_QRS()
312315

316+
def do_QZ2(self,Xi_select='all',Tol=1.0000e-06):
317+
# Make uhlig matrices locally available for computations
318+
AA = self.A
319+
BB = self.B
320+
CC = self.C
321+
DD = self.D
322+
FF = self.F
323+
GG = self.G
324+
HH = self.H
325+
JJ = self.J
326+
KK = self.K
327+
LL = self.L
328+
MM = self.M
329+
NN = self.N
330+
q_expectational_equ = np.shape(FF)[0]
331+
m_states = np.shape(FF)[1]
332+
l_equ = np.shape(CC)[0]
333+
n_endog = np.shape(CC)[1]
334+
k_exog = min(np.shape(NN))
335+
336+
# Do some test
337+
if matrix_rank(CC) < n_endog:
338+
print 'uhlerror: Rank(CC) needs to be at least\n\
339+
equal to the number of endogenous variables'
340+
return False
341+
342+
# Set up and find the solution to the matrix quadratic polynomial
343+
I1 = MAT.eye(m_states)
344+
Z1 = MAT.zeros((m_states,m_states))
345+
invC = inv(CC);
346+
psy = FF-JJ*invC*AA;
347+
lambdar = JJ*invC*BB-GG+KK*invC*AA;
348+
TT = KK*invC*BB-HH;
349+
AA1 = MAT.vstack((MAT.hstack((lambdar,TT)),
350+
MAT.hstack((I1,Z1))
351+
))
352+
AA2 = MAT.vstack((MAT.hstack((psy,Z1)),
353+
MAT.hstack((Z1,I1))
354+
))
355+
356+
# Find the generalized eigenvalues and eigenvectors
357+
# Scipy returns eigval,eigvec, BUT Matlab returns eigvec,eigval !!!
358+
[eigval,eigvecl,eigvecr] = scipy.linalg.eig(AA1,AA2,left=True,right=True)
359+
eigval = MAT.real(eigval)
360+
eigvecr = MAT.real(eigvecr)
361+
362+
# Generate selection index
363+
dtype = [('eigvals',float),('index', float)]
364+
values = [(x,i1) for i1,x in enumerate(eigval)]
365+
eigval = np.array(values,dtype=dtype)
366+
eigval = np.sort(eigval,axis=0,order=['eigvals','index'])
367+
eigpick = [x for i1,x in enumerate(eigval) if eigval[i1][0] < 1.0]
368+
eigindex = np.array([x[1] for x in eigpick])
369+
370+
# Use the selection index on eigval
371+
eigval = np.array([x[0] for x in eigpick])
372+
373+
# Generate DD and EE matrices and calculate PP
374+
DD = MAT.zeros([m_states,m_states])
375+
for i1 in range(m_states):
376+
DD[i1,i1] = eigval[i1]
377+
EE = eigvecr[:,list(eigindex)][-m_states:,:]
378+
# Find the matrix P
379+
PP = EE*DD*inv(EE)
380+
self.P = PP
381+
self.CC_plus = LIN.pinv(CC)
382+
313383
def do_QZ(self,Xi_select='all',Tol=1.0000e-06):
314384
# Make uhlig matrices locally available for computations
315385
AA = self.A
@@ -371,11 +441,6 @@ def do_QZ(self,Xi_select='all',Tol=1.0000e-06):
371441
np.concatenate((MAT.zeros((m_states,m_states)),MAT.identity(m_states)),1)\
372442
))
373443

374-
# (Delta_up,Xi_up,UUU,VVV)=\
375-
# HLP.qz(Delta_mat,Xi_mat,\
376-
# mode='complex',\
377-
# lapackname=lapackname,\
378-
# lapackpath=lapackpath)
379444
(Delta_up,Xi_up,UUU,VVV) = qz(Delta_mat,Xi_mat)
380445

381446

@@ -542,16 +607,39 @@ def do_QRS(self):
542607
(l_equ,m_states) = MAT.shape(AA)
543608
(l_equ,n_endog) = MAT.shape(CC)
544609
(l_equ,k_exog) = MAT.shape(DD)
610+
611+
# Compute the R matrix
545612
RR = -CC_plus*(AA*PP+BB)
546613
self.R = RR
547614
Qone = MAT.kron(NN.T,(FF-JJ*inv(CC)*AA))
548615
Qtwo = MAT.kron(MAT.eye(k_exog),(FF*PP+GG+JJ*RR-KK*inv(CC)*AA))
549616
Q = Qone+Qtwo
550617
invQ = inv(Q)
551-
Qthree = ((JJ*inv(CC)*DD-LL)*NN+KK*inv(CC)*DD-MM).T
552-
QQ = (invQ*Qthree).T
553-
self.Q = QQ
554-
SS = -CC_plus*(AA*QQ+DD)
618+
self.invQ = invQ
619+
Qthree = ((JJ*inv(CC)*DD-LL)*NN+KK*inv(CC)*DD-MM)
620+
self.Qthree = Qthree
621+
622+
# Some further manipulation to get Q
623+
sizo,sizp = Qthree.shape
624+
for i1 in range(sizp):
625+
if i1 == 0:
626+
Qfour = Qthree[:,i1]
627+
else:
628+
Qfour = MAT.vstack((Qfour,Qthree[:,i1]))
629+
self.Qfour = Qfour
630+
QQ = invQ*Qfour
631+
self.QQ = QQ
632+
for i1 in range(sizp):
633+
begini = i1*sizo
634+
endi = (i1+1)*sizo
635+
if i1 == 0:
636+
QQQ = QQ[begini:endi,0]
637+
else:
638+
QQQ = MAT.hstack((QQQ,QQ[begini:endi,0]))
639+
self.Q = QQQ
640+
641+
# Compute S matrix
642+
SS = -CC_plus*(AA*QQQ+DD)
555643
self.S = SS
556644
del self.CC_plus
557645

pymaclab/dsge/translators/dynarepp_to_pml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def translate(secs=None,fpath=None):
1+
def translate(secs=None,fpath=None,dyn_vtimings={'exo':[-1,0],'endo':[-1,0],'iid':[0,1],'con':[0,1]}):
22
from pymaclab.modfiles.templates import mako_pml_template
33

44
# Render the template to be passed to dynare++
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def translate(template_paramdic=None,fpath=None):
2+
import pymaclab.modfiles.templates.wheezy_template as template
3+
4+
# Render the template to be passed to wheezy pymaclab template
5+
modstr = template.render(template_paramdic)
6+
7+
# If a filepath has been passed then just write the pymaclab modfile, but no more!
8+
if fpath != None:
9+
filo = open(fpath,'w')
10+
filo.write(modstr)
11+
filo.flush()
12+
filo.close()
13+
return
14+
else:
15+
return modstr

pymaclab/dsge/translators/translators.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from copy import deepcopy
22
from pymaclab.dsge.translators import pml_to_dynarepp
33
from pymaclab.dsge.translators import dynarepp_to_pml
4+
from pymaclab.dsge.translators import pml_to_pml
45

56

67
class Translators(object):
@@ -29,4 +30,16 @@ def dynarepp_to_pml(self,template_paramdic=None,fpath=None,focli=None):
2930
if template_paramdic == None:
3031
dynarepp_to_pml.translate(template_paramdic=self.template_paramdic,fpath=fpath,focli=focli)
3132
else:
32-
dynarepp_to_pml.translate(template_paramdic=template_paramdic,fpath=fpath,focli=focli)
33+
dynarepp_to_pml.translate(template_paramdic=template_paramdic,fpath=fpath,focli=focli)
34+
35+
def pml_to_pml(self,template_paramdic=None,fpath=None):
36+
if fpath == None:
37+
if template_paramdic == None:
38+
return pml_to_pml.translate(template_paramdic=self.template_paramdic)
39+
else:
40+
return pml_to_pml.translate(template_paramdic=template_paramdic)
41+
else:
42+
if template_paramdic == None:
43+
pml_to_pml.translate(template_paramdic=self.template_paramdic,fpath=fpath)
44+
else:
45+
pml_to_pml.translate(template_paramdic=template_paramdic,fpath=fpath)

pymaclab/modfiles/models/abcs_rbcs/cooley_hansen_cia_cf.txt

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ sigma_epsu = 0.01;
2727

2828
%Variable Vectors+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2929
[1] k(t):capital{endo}[log,bk]
30-
[2] c(t):consumption{con}[log,bk]
31-
[3] h(t):labour{con}[log,bk]
32-
[4] w(t):real_wage{con}[log,bk]
33-
[5] r(t):real_rate_capital{con}[log,bk]
34-
[6] p(t):rprice{con}[log,bk]
30+
[4] r(t):real_rate_capital{con}[log,bk]
31+
[3] w(t):real_wage{con}[log,bk]
32+
[2] h(t):labour{con}[log,bk]
33+
[5] p(t):rprice{con}[log,bk]
34+
[6] lam(t):productivity{exo}[log,bk]
3535
[7] mg(t):mgrowth{exo}[log,bk]
36-
[8] lam(t):productivity{exo}[log,bk]
37-
[9] epsg(t):prod_shock{iid}[]
38-
[10] epsu(t):mgro_shock{iid}[]
36+
[8] epsg(t):prod_shock{iid}[]
37+
[9] epsu(t):mgro_shock{iid}[]
3938

4039
%Boundary Conditions++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4140
None
@@ -55,15 +54,6 @@ None
5554
[9] @Fh(t+1) = FF_1{@Fh(t)};
5655
[10] @Fh_bar = SS{@Fh(t)};
5756

58-
# Utility function and derivatives
59-
[11] @U(t) = LOG(c(t))+B*h(t);
60-
[12] @MU_c(t) = DIFF{@U(t),c(t)};
61-
[13] @MU_c_bar = SS{@MU_c(t)};
62-
[14] @MU_c(t+1) = FF_1{@MU_c(t)};
63-
[15] @MU_h(t) = DIFF{@U(t),h(t)};
64-
[16] @MU_h_bar = SS{@MU_h(t)};
65-
[17] @MU_h(t+1) = FF_1{@MU_h(t)};
66-
6757

6858

6959
%Non-Linear First-Order Conditions++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -72,17 +62,14 @@ None
7262
# The economy's budget constraint
7363
[1] k(t)+(1/p(t))-(1-delta)*k(t-1)-w(t)*h(t)-r(t)*k(t-1) = 0;
7464

75-
# Cash-in-advance constraint
76-
[2] p(t)*c(t)-1 = 0;
77-
7865
# Defining the real return on physical capital
7966
[3] r(t)-@Fk(t) = 0;
8067

8168
# Defining the economy's real wage
8269
[4] w(t)-@Fh(t) = 0;
8370

8471
# The MRS between consumption and leisure
85-
[5] (B/(w(t)*p(t)))+(betta/(E(t)|p(t+1)*E(t)|c(t+1)*mg(t))) = 0;
72+
[5] (B/(w(t)*p(t)))+(betta/mg(t)) = 0;
8673

8774
# The asset equation for bonds
8875
[6] betta*(w(t)/E(t)|w(t+1))*(1+E(t)|r(t+1)-delta)-1 = 0;
@@ -95,14 +82,12 @@ None
9582

9683

9784
%Steady States [Closed Form]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
98-
[2] r_bar = (1/betta)-(1-delta);
99-
[3] w_bar = (1-theta)*(r_bar/theta)**(theta/(theta-1.0));
100-
[4] c_bar = -(betta*w_bar)/(mg_bar*B);
101-
[5] p_bar = 1/c_bar;
102-
[6] k_bar = c_bar/((r_bar/theta)-delta);
103-
[7] h_bar = (r_bar/theta)**(1/(1-theta))*k_bar;
104-
[8] l_bar = 1.0-h_bar;
105-
[9] y_bar = c_bar+delta*k_bar;
85+
[1] r_bar = (1/betta)-(1-delta);
86+
[2] w_bar = (1-theta)*(r_bar/theta)**(theta/(theta-1.0));
87+
[3] c_bar = -(betta*w_bar)/(mg_bar*B);
88+
[4] p_bar = 1/c_bar;
89+
[5] k_bar = c_bar/((r_bar/theta)-delta);
90+
[6] h_bar = (r_bar/theta)**(1/(1-theta))*k_bar;
10691

10792

10893
%Steady State Non-Linear System [Manual]+++++++++++++++++++++++++++++++++++++++++++++++++

0 commit comments

Comments
 (0)