Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version checked for CTRS and price neutrality #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions open_cge/equations.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def eqSp(ssp, pf, Ff, Fsh, Trf):
return Sp


def eqSg(mu, Td, Tz, Tm, XXg, Trf, pq):
def eqSg(mu, Td, Tz, Tm, Sg, Trf, pq):
'''
Total government savings.

Expand All @@ -281,8 +281,9 @@ def eqSg(mu, Td, Tz, Tm, XXg, Trf, pq):
Returns:
Sg (float): Total government savings
'''
Sg = Td + Tz.sum() + Tm.sum() - (Trf + XXg * (mu * pq).sum())
return Sg
XXg = (Td + Tz.sum() + Tm.sum() - Trf - Sg) / (mu * pq).sum()
XXg = XXg.iloc[0]
return XXg


def eqFsh(R, Kf, er):
Expand Down Expand Up @@ -321,6 +322,7 @@ def eqKd(g, Sp, lam, pq):
Kd (float): Domestically owned capital ??
'''
Kd = Sp / (g * (lam * pq).sum())
Kd = Kd.iloc[0]
return Kd


Expand Down Expand Up @@ -628,7 +630,7 @@ def eqDex(theta, xid, tauz, phi, pz, pd, Z):
return D


def eqpqerror(Q, Xp, Xg, Xv, X):
def eqpqerror(Xp, Xg, Xv, X):
'''
Resource constraint.

Expand All @@ -646,8 +648,8 @@ def eqpqerror(Q, Xp, Xg, Xv, X):
Returns:
pq_error (1D numpy array): Error in resource constraint for each good i
'''
pq_error = Q - (Xp + Xg + Xv + X.sum(axis=1))
return pq_error
Q = (Xp + Xg + Xv + X.sum(axis=1))
return Q

def eqpq(pm, pd, taum, eta, deltam, deltad, gamma):

Expand All @@ -656,7 +658,7 @@ def eqpq(pm, pd, taum, eta, deltam, deltad, gamma):
return pq


def eqpf(F, Ff0):
def eqpf(F, Ff):
'''
Comparing labor supply from the model to that in the data.

Expand All @@ -671,7 +673,7 @@ def eqpf(F, Ff0):
pf_error ():
'''
F1 = F.drop(['CAP'])
Ff1 = Ff0.drop(['CAP'])
Ff1 = Ff.drop(['CAP'])
pf_error = Ff1 - F1.sum(axis=1)
return pf_error

Expand Down
88 changes: 59 additions & 29 deletions open_cge/simpleCGE.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def cge_system(pvec, args):
Returns:
p_error (Numpy array): Errors from CGE equations
'''
(p, d, ind, h, Z, Q, Kd, pd, Ff, R, er) = args
(p, d, ind, h, Z, Q, Kd, pd, Ff, R, er, Sg) = args

py = pvec[0:len(ind)]
pf = pvec[len(ind): len(ind) + len(h)]
Expand All @@ -48,7 +48,6 @@ def cge_system(pvec, args):
Kk = eq.eqKk(pf, Ff, R, p.lam, pq)
XXv = eq.eqXXv(d.g, Kk)
Xv = eq.eqXv(p.lam, XXv)
Xg = eq.eqXg(p.mu, XXg)
Kf = eq.eqKf(Kk, Kd)
Fsh = eq.eqFsh(R, Kf, er)
Td = eq.eqTd(p.taud, pf, Ff)
Expand All @@ -63,9 +62,10 @@ def cge_system(pvec, args):
D = eq.eqDex(p.theta, p.xid, p.tauz, p.phi, pz, pd, Z)
M = eq.eqM(p.gamma, p.deltam, p.deltad, p.eta, Q, pq, pm, p.taum)
Tm = eq.eqTm(p.taum, pm, M)
XXg = eq.eqSg(p.mu, Td, Tz, Tm, Sg, Trf, pq)
Xg = eq.eqXg(p.mu, XXg)


pf_error = eq.eqpf(F, d.Ff0)
pf_error = eq.eqpf(F, Ff)
pk_error = eq.eqpk(F, Kk, d.Kk0, d.Ff0)
py_error = eq.eqpy(p.b, F, p.beta, Y)

Expand All @@ -86,9 +86,9 @@ def cge_system(pvec, args):
# solve cge_system
dist = 10
tpi_iter = 0
tpi_max_iter = 1000
tpi_tol = 1e-10
xi = 0.1
tpi_max_iter = 500
tpi_tol = 1e-5
xi = 0.15

# pvec = pvec_init
pvec = np.ones(len(ind) + len(h))
Expand All @@ -97,32 +97,58 @@ def cge_system(pvec, args):
d = calibrate.model_data(sam, h, u, ind)
p = calibrate.parameters(d, ind)

XXg = d.XXg0

R = d.R0
Sg = d.Sg0
er = 1
p.taum = p.taum * 1.1

Zbar = d.Z0
Ffbar = d.Ff0
Kdbar = d.Kd0
Qbar = d.Q0
Zbar = d.Z0 * 1
Ffbar = d.Ff0 * 1
Kdbar = d.Kd0 * 1
Qbar = d.Q0 * 1
pdbar = pvec[0:len(ind)]

pm = eq.eqpm(er, d.pWm)
test = eq.eqpq(pm, pdbar, p.taum, p.eta, p.deltam, p.deltad, p.gamma)


'''
#checking calibration of model
cge_args = [p, d, ind, h, Zbar, Qbar, Kdbar, pdbar, Ffbar, R, er]
errors = cge_system(pvec, cge_args)
#cge_args = [p, d, ind, h, Zbar, Qbar, Kdbar, pdbar, Ffbar, R, er, Sg]
#errors = cge_system(pvec, cge_args)
#---------------------------------------------

py = pvec[0:len(ind)]
pf = pvec[len(ind): len(ind) + len(h)]
py = Series(py, index=list(ind))
pf = Series(pf, index=list(h))
pe = eq.eqpe(er, d.pWe)
pm = eq.eqpm(er, d.pWm)
pq = eq.eqpq(pm, pdbar, p.taum, p.eta, p.deltam, p.deltad, p.gamma)
pz = eq.eqpz(p.ay, p.ax, py, pq)
Kk = eq.eqKk(pf, Ffbar, R, p.lam, pq)
XXv = eq.eqXXv(d.g, Kk)
Xv = eq.eqXv(p.lam, XXv)
Kf = eq.eqKf(Kk, Kdbar)
Fsh = eq.eqFsh(R, Kf, er)
Td = eq.eqTd(p.taud, pf, Ffbar)
Trf = eq.eqTrf(p.tautr, pf, Ffbar)
Tz = eq.eqTz(p.tauz, pz, Zbar)
X = eq.eqX(p.ax, Zbar)
Y = eq.eqY(p.ay, Zbar)
F = eq.eqF(p.beta, py, Y, pf)
Sp = eq.eqSp(p.ssp, pf, Ffbar, Fsh, Trf)
Xp = eq.eqXp(p.alpha, pf, Ffbar, Sp, Td, Fsh, Trf, pq)
E = eq.eqE(p.theta, p.xie, p.tauz, p.phi, pz, pe, Zbar)
D = eq.eqDex(p.theta, p.xid, p.tauz, p.phi, pz, pdbar, Zbar)
M = eq.eqM(p.gamma, p.deltam, p.deltad, p.eta, Qbar, pq, pm, p.taum)
Tm = eq.eqTm(p.taum, pm, M)
XXg = eq.eqSg(p.mu, Td, Tz, Tm, Sg, Trf, pq)
Xg = eq.eqXg(p.mu, XXg)

'''

while (dist > tpi_tol) & (tpi_iter < tpi_max_iter):
tpi_iter += 1
cge_args = [p, d, ind, h, Zbar, Qbar, Kdbar, pdbar, Ffbar, R, er]
cge_args = [p, d, ind, h, Zbar, Qbar, Kdbar, pdbar, Ffbar, R, er, Sg]

results = opt.root(cge_system, pvec, args=cge_args, method='lm',
tol=1e-5)
Expand Down Expand Up @@ -152,28 +178,32 @@ def cge_system(pvec, args):
E = eq.eqE(p.theta, p.xie, p.tauz, p.phi, pz, pe, Zbar)
D = eq.eqDex(p.theta, p.xid, p.tauz, p.phi, pz, pdbar, Zbar)
M = eq.eqM(p.gamma, p.deltam, p.deltad, p.eta, Qbar, pq, pm, p.taum)
Qprime = eq.eqQ(p.gamma, p.deltam, p.deltad, p.eta, M, D)
X = eq.eqX(p.ax, Zbar)
Tm = eq.eqTm(p.taum, pm, M)
XXg = eq.eqSg(p.mu, Td, Tz, Tm, Sg, Trf, pq)
Xg = eq.eqXg(p.mu, XXg)
XXv = eq.eqXXv(d.g, Kk)
Xv = eq.eqXv(p.lam, XXv)
Qprime = eq.eqpqerror(Xp, Xg, Xv, X)
pdprime = eq.eqpd(p.gamma, p.deltam, p.deltad, p.eta, Qprime, pq, D)
Zprime = eq.eqZ(p.theta, p.xie, p.xid, p.phi, E, D)
# Zprime = Zprime.iloc[0]
Kdprime = eq.eqKd(d.g, Sp, p.lam, pq)
Ffprime = d.Ff0
# Ffprime['CAP'] = R * d.Kk * (p.lam * pq).sum() / pf[1]
Ffprime = Ffbar
Ffprime['CAP'] = R * Kk * (p.lam * pq).sum() / pfprime[1]

dist = (((Zbar - Zprime) ** 2 ) ** (1 / 2)).sum()
dist = (((Qbar - Qprime) ** 2 ) ** (1 / 2) + ((pdbar - pdprime) ** 2 ) ** (1 / 2)).sum()
print('Distance at iteration ', tpi_iter, ' is ', dist)

pdbar = xi * pdprime + (1 - xi) * pdbar
Zbar = xi * Zprime + (1 - xi) * Zbar
Kdbar = xi * Kdprime + (1 - xi) * Kdbar
Qbar = xi * Qprime + (1 - xi) * Qbar
Ffbar = xi * Ffprime + (1 - xi) * Ffbar

bop_error = eq.eqbop(d.pWe, d.pWm, E, M, Sf, Fsh, er)

pd = eq.eqpd(p.gamma, p.deltam, p.deltad, p.eta, Qprime, pq, D)
Z = eq.eqZ(p.theta, p.xie, p.xid, p.phi, E, D)
Kd = eq.eqKd(d.g, Sp, p.lam, pq)
Q = eq.eqQ(p.gamma, p.deltam, p.deltad, p.eta, M, D)

print('Model solved, Q = ', Q)
bop_error = eq.eqbop(d.pWe, d.pWm, E, M, Sf, Fsh, er)


print('Model solved, Q = ', Qprime)