def Euler1(w, r, e, L_guess, K1, K2, K3, B, factor, taulump, chi_b): ''' Parameters: w = wage rate (scalar) r = rental rate (scalar) e = distribution of abilities (SxJ array) L_guess = distribution of labor (SxJ array) K1 = distribution of capital in period t ((S-1) x J array) K2 = distribution of capital in period t+1 ((S-1) x J array) K3 = distribution of capital in period t+2 ((S-1) x J array) B = distribution of incidental bequests (1 x J array) factor = scaling value to make average income match data taulump = lump sum transfer from the government to the households xi = coefficient of relative risk aversion chi_b = discount factor of savings Returns: Value of Euler error. ''' B_euler = B.reshape(1, J) tax1 = tax.total_taxes_SS(r, K1, w, e[:-1, :], L_guess[:-1, :], B_euler, bin_weights, factor, taulump) tax2 = tax.total_taxes_SS2(r, K2, w, e[1:, :], L_guess[1:, :], B_euler, bin_weights, factor, taulump) cons1 = get_cons(r, K1, w, e[:-1, :], L_guess[:-1, :], B_euler, bin_weights, K2, g_y, tax1) cons2 = get_cons(r, K2, w, e[1:, :], L_guess[1:, :], B_euler, bin_weights, K3, g_y, tax2) income = (r * K2 + w * e[1:, :] * L_guess[1:, :]) * factor deriv = ( 1 + r*(1-tax.tau_income(r, K1, w, e[1:, :], L_guess[1:, :], factor)-tax.tau_income_deriv( r, K1, w, e[1:, :], L_guess[1:, :], factor)*income)-tax.tau_w_prime(K2)*K2-tax.tau_wealth(K2)) bequest_ut = (1-surv_rate[:-1].reshape(S-1, 1)) * np.exp(-sigma * g_y) * chi_b[:-1].reshape(S-1, J) * K2 ** (-sigma) euler = MUc(cons1) - beta * surv_rate[:-1].reshape(S-1, 1) * deriv * MUc( cons2) * np.exp(-sigma * g_y) - bequest_ut return euler
def Euler1(w, r, e, n_guess, b1, b2, b3, BQ, factor, T_H, chi_b): ''' Parameters: w = wage rate (scalar) r = rental rate (scalar) e = distribution of abilities (SxJ array) n_guess = distribution of labor (SxJ array) b1 = distribution of capital in period t ((S-1) x J array) b2 = distribution of capital in period t+1 ((S-1) x J array) b3 = distribution of capital in period t+2 ((S-1) x J array) B = distribution of incidental bequests (1 x J array) factor = scaling value to make average income match data T_H = lump sum transfer from the government to the households xi = coefficient of relative risk aversion chi_b = discount factor of savings Returns: Value of Euler error. ''' BQ_euler = BQ.reshape(1, J) tax1 = tax.total_taxes_SS(r, b1, w, e[:-1, :], n_guess[:-1, :], BQ_euler, lambdas, factor, T_H) tax2 = tax.total_taxes_SS2(r, b2, w, e[1:, :], n_guess[1:, :], BQ_euler, lambdas, factor, T_H) cons1 = get_cons(r, b1, w, e[:-1, :], n_guess[:-1, :], BQ_euler, lambdas, b2, g_y, tax1) cons2 = get_cons(r, b2, w, e[1:, :], n_guess[1:, :], BQ_euler, lambdas, b3, g_y, tax2) income = (r * b2 + w * e[1:, :] * n_guess[1:, :]) * factor deriv = ( 1 + r*(1-tax.tau_income(r, b1, w, e[1:, :], n_guess[1:, :], factor)-tax.tau_income_deriv( r, b1, w, e[1:, :], n_guess[1:, :], factor)*income)-tax.tau_w_prime(b2)*b2-tax.tau_wealth(b2)) bequest_ut = rho[:-1].reshape(S-1, 1) * np.exp(-sigma * g_y) * chi_b[:-1].reshape(S-1, J) * b2 ** (-sigma) euler = MUc(cons1) - beta * (1-rho[:-1].reshape(S-1, 1)) * deriv * MUc( cons2) * np.exp(-sigma * g_y) - bequest_ut return euler
def Euler2(w, r, e, L_guess, K1_2, K2_2, B, factor, taulump): ''' Parameters: w = wage rate (scalar) r = rental rate (scalar) e = distribution of abilities (SxJ array) L_guess = distribution of labor (SxJ array) K1_2 = distribution of capital in period t (S x J array) K2_2 = distribution of capital in period t+1 (S x J array) B = distribution of incidental bequests (1 x J array) factor = scaling value to make average income match data taulump = lump sum transfer from the government to the households Returns: Value of Euler error. ''' B = B.reshape(1, J) tax1 = tax.total_taxes_SS(r, K1_2, w, e, L_guess, B, bin_weights, factor, taulump) cons = get_cons(r, K1_2, w, e, L_guess, B, bin_weights, K2_2, g_y, tax1) income = (r * K1_2 + w * e * L_guess) * factor deriv = 1 - tau_payroll - tax.tau_income( r, K1_2, w, e, L_guess, factor) - tax.tau_income_deriv(r, K1_2, w, e, L_guess, factor) * income euler = (MUc(cons)) * w * deriv * e - MUl(L_guess) return euler
def Euler2(w, r, e, L_guess, K1_2, K2_2, B, factor, taulump, chi_n): """ Parameters: w = wage rate (scalar) r = rental rate (scalar) e = distribution of abilities (SxJ array) L_guess = distribution of labor (SxJ array) K1_2 = distribution of capital in period t (S x J array) K2_2 = distribution of capital in period t+1 (S x J array) B = distribution of incidental bequests (1 x J array) factor = scaling value to make average income match data taulump = lump sum transfer from the government to the households Returns: Value of Euler error. """ B = B.reshape(1, J) tax1 = tax.total_taxes_SS(r, K1_2, w, e, L_guess, B, bin_weights, factor, taulump) cons = get_cons(r, K1_2, w, e, L_guess, B, bin_weights, K2_2, g_y, tax1) income = (r * K1_2 + w * e * L_guess) * factor deriv = ( 1 - tau_payroll - tax.tau_income(r, K1_2, w, e, L_guess, factor) - tax.tau_income_deriv(r, K1_2, w, e, L_guess, factor) * income ) euler = MUc(cons) * w * deriv * e - MUl(L_guess, chi_n) return euler
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
def Euler2(w, r, e, n_guess, b1_2, b2_2, BQ, factor, T_H, chi_n): ''' Parameters: w = wage rate (scalar) r = rental rate (scalar) e = distribution of abilities (SxJ array) n_guess = distribution of labor (SxJ array) b1_2 = distribution of capital in period t (S x J array) b2_2 = distribution of capital in period t+1 (S x J array) B = distribution of incidental bequests (1 x J array) factor = scaling value to make average income match data T_H = lump sum transfer from the government to the households Returns: Value of Euler error. ''' BQ = BQ.reshape(1, J) 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, lambdas, b2_2, g_y, tax1) income = (r * b1_2 + w * e * n_guess) * factor deriv = 1 - tau_payroll - tax.tau_income(r, b1_2, w, e, n_guess, factor) - tax.tau_income_deriv( r, b1_2, w, e, n_guess, factor) * income euler = MUc(cons) * w * deriv * e - MUl(n_guess, chi_n) return euler
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 euler2 = euler errors from second euler equation
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 k2_2 = SxJ array of Kssmat 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, 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 euler2 = euler errors from second euler equation
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 winit = 1 x T+S vector, initial time path of real wage rinit = 1 x T+S vector, initial time path of real interest rate
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) tax0 = tax.total_taxes_SS(r0, initial_K, w0, e, initial_L, B0, bin_weights, factor_ss, omega_stationary[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 winit = 1 x T+S vector, initial time path of real wage rinit = 1 x T+S vector, initial time path of real interest rate
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