diff --git a/open_cge/equations.py b/open_cge/equations.py index c4f877c..ef611bd 100644 --- a/open_cge/equations.py +++ b/open_cge/equations.py @@ -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. @@ -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): @@ -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 @@ -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. @@ -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): @@ -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. @@ -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 diff --git a/open_cge/simpleCGE.py b/open_cge/simpleCGE.py index b007975..81fb4b2 100644 --- a/open_cge/simpleCGE.py +++ b/open_cge/simpleCGE.py @@ -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)] @@ -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) @@ -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) @@ -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)) @@ -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) @@ -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) +