def Steady_State(guesses, params): ''' Parameters: Steady state distribution of capital guess as array size 2*S*J Returns: Array of 2*S*J Euler equation errors ''' chi_b = np.tile(np.array(params[:J]).reshape(1, J), (S, 1)) chi_n = np.array(params[J:]) K_guess = guesses[0:S * J].reshape((S, J)) B = (K_guess * omega_SS * mort_rate.reshape(S, 1)).sum(0) K = (omega_SS * K_guess).sum() L_guess = guesses[S * J:-1].reshape((S, J)) L = get_L(e, L_guess) Y = get_Y(K, L) w = get_w(Y, L) r = get_r(Y, K) BQ = (1 + r) * B K1 = np.array(list(np.zeros(J).reshape(1, J)) + list(K_guess[:-2, :])) K2 = K_guess[:-1, :] K3 = K_guess[1:, :] K1_2 = np.array(list(np.zeros(J).reshape(1, J)) + list(K_guess[:-1, :])) K2_2 = K_guess factor = guesses[-1] taulump = tax.tax_lump(r, K1_2, w, e, L_guess, BQ, bin_weights, factor, omega_SS) error1 = Euler1(w, r, e, L_guess, K1, K2, K3, BQ, factor, taulump, chi_b) error2 = Euler2(w, r, e, L_guess, K1_2, K2_2, BQ, factor, taulump, chi_n) error3 = Euler3(w, r, e, L_guess, K_guess, BQ, factor, chi_b, taulump) avI = ((r * K1_2 + w * e * L_guess) * omega_SS).sum() error4 = [mean_income - factor * avI] # Check and punish constraint violations mask1 = L_guess < 0 error2[mask1] += 1e9 mask2 = L_guess > ltilde error2[mask2] += 1e9 if K_guess.sum() <= 0: error1 += 1e9 tax1 = tax.total_taxes_SS(r, K1_2, w, e, L_guess, BQ, bin_weights, factor, taulump) cons = get_cons(r, K1_2, w, e, L_guess, BQ.reshape(1, J), bin_weights, K2_2, g_y, tax1) mask3 = cons < 0 error2[mask3] += 1e9 mask4 = K_guess[:-1] <= 0 error1[mask4] += 1e9 # print np.abs(np.array(list(error1.flatten()) + list( # error2.flatten()) + list(error3.flatten()) + error4)).max() return list(error1.flatten()) + list(error2.flatten()) + list( error3.flatten()) + error4
def Steady_State(guesses, params): ''' Parameters: Steady state distribution of capital guess as array size 2*S*J Returns: Array of 2*S*J Euler equation errors ''' chi_b = np.tile(np.array(params[:J]).reshape(1, J), (S, 1)) chi_n = np.array(params[J:]) b_guess = guesses[0: S * J].reshape((S, J)) B = (b_guess * omega_SS * rho.reshape(S, 1)).sum(0) K = (omega_SS * b_guess).sum() n_guess = guesses[S * J:-1].reshape((S, J)) L = get_L(e, n_guess) Y = get_Y(K, L) w = get_w(Y, L) r = get_r(Y, K) BQ = (1 + r) * B b1 = np.array(list(np.zeros(J).reshape(1, J)) + list(b_guess[:-2, :])) b2 = b_guess[:-1, :] b3 = b_guess[1:, :] b1_2 = np.array(list(np.zeros(J).reshape(1, J)) + list(b_guess[:-1, :])) b2_2 = b_guess factor = guesses[-1] T_H = tax.tax_lump(r, b1_2, w, e, n_guess, BQ, lambdas, factor, omega_SS) error1 = Euler1(w, r, e, n_guess, b1, b2, b3, BQ, factor, T_H, chi_b) error2 = Euler2(w, r, e, n_guess, b1_2, b2_2, BQ, factor, T_H, chi_n) error3 = Euler3(w, r, e, n_guess, b_guess, BQ, factor, chi_b, T_H) average_income_model = ((r * b1_2 + w * e * n_guess) * omega_SS).sum() error4 = [mean_income_data - factor * average_income_model] # Check and punish constraint violations mask1 = n_guess < 0 error2[mask1] += 1e9 mask2 = n_guess > ltilde error2[mask2] += 1e9 if b_guess.sum() <= 0: error1 += 1e9 tax1 = tax.total_taxes_SS(r, b1_2, w, e, n_guess, BQ, lambdas, factor, T_H) cons = get_cons(r, b1_2, w, e, n_guess, BQ.reshape(1, J), lambdas, b2_2, g_y, tax1) mask3 = cons < 0 error2[mask3] += 1e9 mask4 = b_guess[:-1] <= 0 error1[mask4] += 1e9 print np.abs(np.array(list(error1.flatten()) + list( error2.flatten()) + list(error3.flatten()) + error4)).max() return list(error1.flatten()) + list( error2.flatten()) + list(error3.flatten()) + error4
def Steady_State(guesses, params): ''' Parameters: Steady state distribution of capital guess as array size 2*S*J Returns: Array of 2*S*J Euler equation errors ''' chi_b = np.tile(np.array(params[:J]).reshape(1, J), (S, 1)) chi_n = np.array(params[J:]) K_guess = guesses[0: S * J].reshape((S, J)) B = (K_guess * omega_SS * mort_rate.reshape(S, 1)).sum(0) K = (omega_SS * K_guess).sum() L_guess = guesses[S * J:-1].reshape((S, J)) L = get_L(e, L_guess) Y = get_Y(K, L) w = get_w(Y, L) r = get_r(Y, K) BQ = (1 + r) * B K1 = np.array(list(np.zeros(J).reshape(1, J)) + list(K_guess[:-2, :])) K2 = K_guess[:-1, :] K3 = K_guess[1:, :] K1_2 = np.array(list(np.zeros(J).reshape(1, J)) + list(K_guess[:-1, :])) K2_2 = K_guess factor = guesses[-1] taulump = tax.tax_lump(r, K1_2, w, e, L_guess, BQ, bin_weights, factor, omega_SS) error1 = Euler1(w, r, e, L_guess, K1, K2, K3, BQ, factor, taulump, chi_b) error2 = Euler2(w, r, e, L_guess, K1_2, K2_2, BQ, factor, taulump, chi_n) error3 = Euler3(w, r, e, L_guess, K_guess, BQ, factor, chi_b, taulump) avI = ((r * K1_2 + w * e * L_guess) * omega_SS).sum() error4 = [mean_income - factor * avI] # Check and punish constraint violations mask1 = L_guess < 0 error2[mask1] += 1e9 mask2 = L_guess > ltilde error2[mask2] += 1e9 if K_guess.sum() <= 0: error1 += 1e9 tax1 = tax.total_taxes_SS(r, K1_2, w, e, L_guess, BQ, bin_weights, factor, taulump) cons = get_cons(r, K1_2, w, e, L_guess, BQ.reshape(1, J), bin_weights, K2_2, g_y, tax1) mask3 = cons < 0 error2[mask3] += 1e9 mask4 = K_guess[:-1] <= 0 error1[mask4] += 1e9 # print np.abs(np.array(list(error1.flatten()) + list( # error2.flatten()) + list(error3.flatten()) + error4)).max() return list(error1.flatten()) + list( error2.flatten()) + list(error3.flatten()) + error4
bssmat = solutions[0:(S-1) * J].reshape(S-1, J) BQ = solutions[(S-1)*J:S*J] Bss = (np.array(list(bssmat) + list(BQ.reshape(1, J))).reshape( S, J) * omega_SS * rho.reshape(S, 1)).sum(0) bssmat2 = np.array(list(np.zeros(J).reshape(1, J)) + list(bssmat)) bssmat3 = np.array(list(bssmat) + list(BQ.reshape(1, J))) Kss = (omega_SS[:-1, :] * bssmat).sum() + (omega_SS[-1, :]*BQ).sum() nssmat = solutions[S * J:-1].reshape(S, J) Lss = get_L(e, nssmat) Yss = get_Y(Kss, Lss) wss = get_w(Yss, Lss) rss = get_r(Yss, Kss) b1_2 = np.array(list(np.zeros(J).reshape((1, J))) + list(bssmat)) factor_ss = solutions[-1] BQ = Bss * (1+rss) T_Hss = tax.tax_lump(rss, bssmat2, wss, e, nssmat, BQ, lambdas, factor_ss, omega_SS) taxss = tax.total_taxes_SS(rss, bssmat2, wss, e, nssmat, BQ, lambdas, factor_ss, T_Hss) cssmat = get_cons(rss, bssmat2, wss, e, nssmat, BQ.reshape(1, J), lambdas.reshape(1, J), bssmat3, g_y, taxss) constraint_checker(bssmat, nssmat, cssmat) ''' ------------------------------------------------------------------------ Generate variables for graphs ------------------------------------------------------------------------ b1 = (S-1)xJ array of bssmat in period t-1 b2 = copy of bssmat b3 = (S-1)xJ array of bssmat in period t+1 b1_2 = SxJ array of bssmat in period t b2_2 = SxJ array of bssmat in period t+1 euler1 = euler errors from first euler equation
Kssvec = Kssmat.sum(1) Kss = (omega_SS[:-1, :] * Kssmat).sum() + (omega_SS[-1, :] * BQ).sum() Kssavg = Kssvec.mean() Kssvec = np.array([0] + list(Kssvec)) Lssmat = solutions[S * J:-1].reshape(S, J) Lssvec = Lssmat.sum(1) Lss = get_L(e, Lssmat) Lssavg = Lssvec.mean() Yss = get_Y(Kss, Lss) wss = get_w(Yss, Lss) rss = get_r(Yss, Kss) k1_2 = np.array(list(np.zeros(J).reshape((1, J))) + list(Kssmat)) factor_ss = solutions[-1] B = Bss * (1 + rss) Tss = tax.tax_lump(rss, Kssmat2, wss, e, Lssmat, B, bin_weights, factor_ss, omega_SS) taxss = tax.total_taxes_SS(rss, Kssmat2, wss, e, Lssmat, B, bin_weights, factor_ss, Tss) cssmat = get_cons(rss, Kssmat2, wss, e, Lssmat, (1 + rss) * B.reshape(1, J), bin_weights.reshape(1, J), Kssmat3, g_y, taxss) constraint_checker(Kssmat, Lssmat, wss, rss, e, cssmat, BQ) ''' ------------------------------------------------------------------------ Generate variables for graphs ------------------------------------------------------------------------ k1 = (S-1)xJ array of Kssmat in period t-1 k2 = copy of Kssmat k3 = (S-1)xJ array of Kssmat in period t+1 k1_2 = SxJ array of Kssmat in period t
Kssvec = Kssmat.sum(1) Kss = (omega_SS[:-1, :] * Kssmat).sum() + (omega_SS[-1, :]*BQ).sum() Kssavg = Kssvec.mean() Kssvec = np.array([0]+list(Kssvec)) Lssmat = solutions[S * J:-1].reshape(S, J) Lssvec = Lssmat.sum(1) Lss = get_L(e, Lssmat) Lssavg = Lssvec.mean() Yss = get_Y(Kss, Lss) wss = get_w(Yss, Lss) rss = get_r(Yss, Kss) k1_2 = np.array(list(np.zeros(J).reshape((1, J))) + list(Kssmat)) factor_ss = solutions[-1] B = Bss * (1+rss) Tss = tax.tax_lump(rss, Kssmat2, wss, e, Lssmat, B, bin_weights, factor_ss, omega_SS) taxss = tax.total_taxes_SS(rss, Kssmat2, wss, e, Lssmat, B, bin_weights, factor_ss, Tss) cssmat = get_cons(rss, Kssmat2, wss, e, Lssmat, B.reshape(1, J), bin_weights.reshape(1, J), Kssmat3, g_y, taxss) constraint_checker(Kssmat, Lssmat, wss, rss, e, cssmat, BQ) ''' ------------------------------------------------------------------------ Generate variables for graphs ------------------------------------------------------------------------ k1 = (S-1)xJ array of Kssmat in period t-1 k2 = copy of Kssmat k3 = (S-1)xJ array of Kssmat in period t+1 k1_2 = SxJ array of Kssmat in period t k2_2 = SxJ array of Kssmat in period t+1 euler1 = euler errors from first euler equation
if TPI_initial_run: initial_K = np.array(list(Kssmat) + list(BQ.reshape(1, J))) initial_L = Lssmat else: initial_K = Kssmat_init initial_L = Lssmat_init K0 = (omega_stationary[0] * initial_K[:, :]).sum() K1_2init = np.array(list(np.zeros(J).reshape(1, J)) + list(initial_K[:-1])) K2_2init = initial_K L0 = get_L(e, initial_L) Y0 = get_Y(K0, L0) w0 = get_w(Y0, L0) r0 = get_r(Y0, K0) B0 = (initial_K * omega_stationary[0] * mort_rate.reshape(S, 1)).sum(0) tau_lump_0 = tax.tax_lump(r0, K1_2init, w0, e, initial_L, B0, bin_weights, factor_ss, omega_stationary[0]) tax0 = tax.total_taxes_SS(r0, K1_2init, w0, e, initial_L, B0, bin_weights, factor_ss, tau_lump_0) c0 = get_cons(r0, K1_2init, w0, e, initial_L, (1 + r0) * Bss.reshape(1, J), bin_weights.reshape(1, J), K2_2init, g_y, tax0) constraint_checker1(initial_K[:-1], initial_L, w0, r0, e, c0, B0) ''' ------------------------------------------------------------------------ Solve for equilibrium transition path by TPI ------------------------------------------------------------------------ Kinit = 1 x T+S vector, initial time path of aggregate capital stock Linit = 1 x T+S vector, initial time path of aggregate labor demand. This is just equal to a 1 x T+S vector of Lss because labor is supplied inelastically Yinit = 1 x T+S vector, initial time path of aggregate output
if TPI_initial_run: initial_K = np.array(list(Kssmat) + list(BQ.reshape(1, J))) initial_L = Lssmat else: initial_K = Kssmat_init initial_L = Lssmat_init K0 = (omega_stationary[0] * initial_K[:, :]).sum() K1_2init = np.array(list(np.zeros(J).reshape(1, J)) + list(initial_K[:-1])) K2_2init = initial_K L0 = get_L(e, initial_L) Y0 = get_Y(K0, L0) w0 = get_w(Y0, L0) r0 = get_r(Y0, K0) B0 = (initial_K * omega_stationary[0] * mort_rate.reshape(S, 1)).sum(0) tau_lump_0 = tax.tax_lump(r0, K1_2init, w0, e, initial_L, B0, bin_weights, factor_ss, omega_stationary[0]) tax0 = tax.total_taxes_SS(r0, K1_2init, w0, e, initial_L, B0, bin_weights, factor_ss, tau_lump_0) c0 = get_cons( r0, K1_2init, w0, e, initial_L, (1 + r0) * Bss.reshape(1, J), bin_weights.reshape(1, J), K2_2init, g_y, tax0 ) constraint_checker1(initial_K[:-1], initial_L, w0, r0, e, c0, B0) """ ------------------------------------------------------------------------ Solve for equilibrium transition path by TPI ------------------------------------------------------------------------ Kinit = 1 x T+S vector, initial time path of aggregate capital stock Linit = 1 x T+S vector, initial time path of aggregate labor demand. This is just equal to a 1 x T+S vector of Lss because labor is supplied inelastically
def Steady_State(guesses, params): ''' Parameters: Steady state interest rate, wage rate, distribution of bequests government transfers Returns: Array of 2*S*J Euler equation errors ''' b_guess_init = np.ones((S, J)) * .01 # should be better way to update these after run for first time... n_guess_init = np.ones((S, J)) * .99 * ltilde r = guesses[0] w = guesses[1] bq =guesses[2: 1 * J].reshape((1, J)) T_H = guesses[-1] guesses = list(b_guess_init.flatten()) + list(n_guess_init.flatten()) HH_solve_X2 = lambda x: HH_solve(x, r, w, bq, T_H, final_chi_b_params) b, n , c = opt.fsolve(HH_solve_X2, guesses, xtol=1e-13) Steady_State_X2 = lambda x: Steady_State(x, final_chi_b_params) solutions = opt.fsolve(Steady_State_X2, guesses, xtol=1e-13) chi_b = np.tile(np.array(params[:J]).reshape(1, J), (S, 1)) chi_n = np.array(params[J:]) r_guess = guesses[0] w_guess = guesses[1] bq_guess =guesses[2: 1 * J].reshape((1, J)) T_H_guess = guesses[-1] b_guess = guesses[0: S * J].reshape((S, J)) B = (b_guess * omega_SS * rho.reshape(S, 1)).sum(0) K = (omega_SS * b_guess).sum() n_guess = guesses[S * J:-1].reshape((S, J)) L = hh_focs.get_L(e, n_guess, omega_SS) Y = hh_focs.get_Y(K, L) w = hh_focs.get_w(Y, L) r = hh_focs.get_r(Y, K) BQ = (1 + r) * B b1 = np.array(list(np.zeros(J).reshape(1, J)) + list(b_guess[:-2, :])) b2 = b_guess[:-1, :] b3 = b_guess[1:, :] b1_2 = np.array(list(np.zeros(J).reshape(1, J)) + list(b_guess[:-1, :])) b2_2 = b_guess factor = guesses[-1] T_H = tax.tax_lump(r, b1_2, w, e, n_guess, BQ, lambdas, factor, omega_SS) error1 = hh_focs.Euler1(w, r, e, n_guess, b1, b2, b3, BQ, factor, T_H, chi_b, rho) error2 = hh_focs.Euler2(w, r, e, n_guess, b1_2, b2_2, BQ, factor, T_H, chi_n) error3 = hh_focs.Euler3(w, r, e, n_guess, b_guess, BQ, factor, chi_b, T_H) average_income_model = ((r * b1_2 + w * e * n_guess) * omega_SS).sum() error4 = [mean_income_data - factor * average_income_model] # Check and punish constraint violations mask1 = n_guess < 0 error2[mask1] += 1e9 mask2 = n_guess > ltilde error2[mask2] += 1e9 if b_guess.sum() <= 0: error1 += 1e9 tax1 = tax.total_taxes_SS(r, b1_2, w, e, n_guess, BQ, lambdas, factor, T_H) cons = hh_focs.get_cons(r, b1_2, w, e, n_guess, BQ.reshape(1, J), lambdas, b2_2, g_y, tax1) mask3 = cons < 0 error2[mask3] += 1e9 mask4 = b_guess[:-1] <= 0 error1[mask4] += 1e9 # print np.abs(np.array(list(error1.flatten()) + list( # error2.flatten()) + list(error3.flatten()) + error4)).max() return list(error1.flatten()) + list( error2.flatten()) + list(error3.flatten()) + error4