def euler_savings_func(w, r, e, n_guess, b_s, b_splus1, b_splus2, BQ, factor, T_H, chi_b, params, theta, tau_bq, rho, lambdas): ''' This function is usually looped through over J, so it does one ability group at a time. Inputs: w = wage rate (scalar) r = rental rate (scalar) e = ability levels (Sx1 array) n_guess = labor distribution (Sx1 array) b_s = wealth holdings at the start of a period (Sx1 array) b_splus1 = wealth holdings for the next period (Sx1 array) b_splus2 = wealth holdings for 2 periods ahead (Sx1 array) BQ = aggregate bequests for a certain ability (scalar) factor = scaling factor to convert to dollars (scalar) T_H = lump sum tax (scalar) chi_b = chi^b_j for a certain ability (scalar) params = parameter list (list) theta = replacement rate for a certain ability (scalar) tau_bq = bequest tax rate (scalar) rho = mortality rate (Sx1 array) lambdas = ability weight (scalar) Output: euler = Value of savings euler error (Sx1 array) ''' J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, g_n_ss, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params # In order to not have 2 savings euler equations (one that solves the first S-1 equations, and one that solves the last one), # we combine them. In order to do this, we have to compute a consumption term in period t+1, which requires us to have a shifted # e and n matrix. We append a zero on the end of both of these so they will be the right size. We could append any value to them, # since in the euler equation, the coefficient on the marginal utility of consumption for this term will be zero (since rho is one). e_extended = np.array(list(e) + [0]) n_extended = np.array(list(n_guess) + [0]) tax1 = tax.total_taxes(r, b_s, w, e, n_guess, BQ, lambdas, factor, T_H, None, 'SS', False, params, theta, tau_bq) tax2 = tax.total_taxes(r, b_splus1, w, e_extended[1:], n_extended[1:], BQ, lambdas, factor, T_H, None, 'SS', True, params, theta, tau_bq) cons1 = get_cons(r, b_s, w, e, n_guess, BQ, lambdas, b_splus1, params, tax1) cons2 = get_cons(r, b_splus1, w, e_extended[1:], n_extended[1:], BQ, lambdas, b_splus2, params, tax2) income = (r * b_splus1 + w * e_extended[1:] * n_extended[1:]) * factor deriv = (1 + r * (1 - tax.tau_income(r, b_splus1, w, e_extended[1:], n_extended[1:], factor, params) - tax.tau_income_deriv(r, b_splus1, w, e_extended[1:], n_extended[1:], factor, params) * income) - tax.tau_w_prime(b_splus1, params) * b_splus1 - tax.tau_wealth(b_splus1, params)) savings_ut = rho * np.exp(-sigma * g_y) * chi_b * b_splus1**(-sigma) # Again, not who in this equation, the (1-rho) term will zero out in the last period, so the last entry of cons2 can be complete # gibberish (which it is). It just has to exist so cons2 is the right size to match all other arrays in the equation. euler = marg_ut_cons(cons1, params) - beta * (1 - rho) * deriv * marg_ut_cons( cons2, params) * np.exp(-sigma * g_y) - savings_ut return euler
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, 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, 1) * 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 euler_savings_func(w, r, e, n_guess, b_s, b_splus1, b_splus2, BQ, factor, T_H, chi_b, params, theta, tau_bq, rho, lambdas): ''' This function is usually looped through over J, so it does one ability group at a time. Inputs: w = wage rate (scalar) r = rental rate (scalar) e = ability levels (Sx1 array) n_guess = labor distribution (Sx1 array) b_s = wealth holdings at the start of a period (Sx1 array) b_splus1 = wealth holdings for the next period (Sx1 array) b_splus2 = wealth holdings for 2 periods ahead (Sx1 array) BQ = aggregate bequests for a certain ability (scalar) factor = scaling factor to convert to dollars (scalar) T_H = lump sum tax (scalar) chi_b = chi^b_j for a certain ability (scalar) params = parameter list (list) theta = replacement rate for a certain ability (scalar) tau_bq = bequest tax rate (scalar) rho = mortality rate (Sx1 array) lambdas = ability weight (scalar) Output: euler = Value of savings euler error (Sx1 array) ''' J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, g_n_ss, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params # In order to not have 2 savings euler equations (one that solves the first S-1 equations, and one that solves the last one), # we combine them. In order to do this, we have to compute a consumption term in period t+1, which requires us to have a shifted # e and n matrix. We append a zero on the end of both of these so they will be the right size. We could append any value to them, # since in the euler equation, the coefficient on the marginal utility of consumption for this term will be zero (since rho is one). e_extended = np.array(list(e) + [0]) n_extended = np.array(list(n_guess) + [0]) tax1 = tax.total_taxes(r, b_s, w, e, n_guess, BQ, lambdas, factor, T_H, None, 'SS', False, params, theta, tau_bq) tax2 = tax.total_taxes(r, b_splus1, w, e_extended[1:], n_extended[1:], BQ, lambdas, factor, T_H, None, 'SS', True, params, theta, tau_bq) cons1 = get_cons(r, b_s, w, e, n_guess, BQ, lambdas, b_splus1, params, tax1) cons2 = get_cons(r, b_splus1, w, e_extended[1:], n_extended[1:], BQ, lambdas, b_splus2, params, tax2) income = (r * b_splus1 + w * e_extended[1:] * n_extended[1:]) * factor deriv = ( 1 + r*(1-tax.tau_income(r, b_splus1, w, e_extended[1:], n_extended[1:], factor, params)-tax.tau_income_deriv( r, b_splus1, w, e_extended[1:], n_extended[1:], factor, params)*income)-tax.tau_w_prime(b_splus1, params)*b_splus1-tax.tau_wealth(b_splus1, params)) savings_ut = rho * np.exp(-sigma * g_y) * chi_b * b_splus1 ** (-sigma) # Again, not who in this equation, the (1-rho) term will zero out in the last period, so the last entry of cons2 can be complete # gibberish (which it is). It just has to exist so cons2 is the right size to match all other arrays in the equation. euler = marg_ut_cons(cons1, params) - beta * (1-rho) * deriv * marg_ut_cons( cons2, params) * np.exp(-sigma * g_y) - savings_ut return euler
def Steady_state_TPI_solver(guesses, winit, rinit, BQinit, T_H_init, factor, j, s, t, params, theta, tau_bq, rho, lambdas, e, initial_b, chi_b, chi_n): ''' Parameters: guesses = distribution of capital and labor in period t ((S-1)*S*J x 1 list) winit = wage rate (scalar) rinit = rental rate (scalar) t = time period Returns: Value of Euler error. (as an 2*S*J x 1 list) ''' J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params length = len(guesses)/2 b_guess = np.array(guesses[:length]) n_guess = np.array(guesses[length:]) if length == S: b_s = np.array([0] + list(b_guess[:-1])) else: b_s = np.array([(initial_b[-(s+3), j])] + list(b_guess[:-1])) b_splus1 = b_guess b_splus2 = np.array(list(b_guess[1:]) + [0]) w_s = winit[t:t+length] w_splus1 = winit[t+1:t+length+1] r_s = rinit[t:t+length] r_splus1 = rinit[t+1:t+length+1] n_s = n_guess n_extended = np.array(list(n_guess[1:]) + [0]) e_s = e[-length:, j] e_extended = np.array(list(e[-length+1:, j]) + [0]) BQ_s = BQinit[t:t+length] BQ_splus1 = BQinit[t+1:t+length+1] T_H_s = T_H_init[t:t+length] T_H_splus1 = T_H_init[t+1:t+length+1] # Savings euler equations tax_s = tax.total_taxes(r_s, b_s, w_s, e_s, n_s, BQ_s, lambdas[j], factor, T_H_s, j, 'TPI', False, params, theta, tau_bq) tax_splus1 = tax.total_taxes(r_splus1, b_splus1, w_splus1, e_extended, n_extended, BQ_splus1, lambdas[j], factor, T_H_splus1, j, 'TPI', True, params, theta, tau_bq) cons_s = house.get_cons(r_s, b_s, w_s, e_s, n_s, BQ_s, lambdas[j], b_splus1, params, tax_s) cons_splus1 = house.get_cons(r_splus1, b_splus1, w_splus1, e_extended, n_extended, BQ_splus1, lambdas[j], b_splus2, params, tax_splus1) income_splus1 = (r_splus1 * b_splus1 + w_splus1 * e_extended * n_extended) * factor savings_ut = rho[-(length):] * np.exp(-sigma * g_y) * chi_b[-(length):, j] * b_splus1 ** (-sigma) deriv_savings = 1 + r_splus1 * (1 - tax.tau_income( r_splus1, b_splus1, w_splus1, e_extended, n_extended, factor, params) - tax.tau_income_deriv( r_splus1, b_splus1, w_splus1, e_extended, n_extended, factor, params) * income_splus1) - tax.tau_w_prime( b_splus1, params)*b_splus1 - tax.tau_wealth(b_splus1, params) error1 = house.marg_ut_cons(cons_s, params) - beta * (1-rho[-(length):]) * np.exp(-sigma * g_y) * deriv_savings * house.marg_ut_cons( cons_splus1, params) - savings_ut # Labor leisure euler equations income_s = (r_s * b_s + w_s * e_s * n_s) * factor deriv_laborleisure = 1 - tau_payroll - tax.tau_income(r_s, b_s, w_s, e_s, n_s, factor, params) - tax.tau_income_deriv( r_s, b_s, w_s, e_s, n_s, factor, params) * income_s error2 = house.marg_ut_cons(cons_s, params) * w_s * e[-(length):, j] * deriv_laborleisure - house.marg_ut_labor(n_s, chi_n[-length:], params) # Check and punish constraint violations mask1 = n_guess < 0 error2[mask1] += 1e12 mask2 = n_guess > ltilde error2[mask2] += 1e12 mask3 = cons_s < 0 error2[mask3] += 1e12 mask4 = b_guess <= 0 error2[mask4] += 1e12 mask5 = cons_splus1 < 0 error2[mask5] += 1e12 return list(error1.flatten()) + list(error2.flatten())
def Steady_state_TPI_solver(guesses, winit, rinit, BQinit, T_H_init, factor, j, s, t, params, theta, tau_bq, rho, lambdas, e, initial_b, chi_b, chi_n): ''' Parameters: guesses = distribution of capital and labor (various length list) winit = wage rate ((T+S)x1 array) rinit = rental rate ((T+S)x1 array) BQinit = aggregate bequests ((T+S)x1 array) T_H_init = lump sum tax over time ((T+S)x1 array) factor = scaling factor (scalar) j = which ability type is being solved for (scalar) s = which upper triangle loop is being solved for (scalar) t = which diagonal is being solved for (scalar) params = list of parameters (list) theta = replacement rates (Jx1 array) tau_bq = bequest tax rate (Jx1 array) rho = mortalit rate (Sx1 array) lambdas = ability weights (Jx1 array) e = ability type (SxJ array) initial_b = capital stock distribution in period 0 (SxJ array) chi_b = chi^b_j (Jx1 array) chi_n = chi^n_s (Sx1 array) Output: Value of Euler error (various length list) ''' J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, g_n_ss, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params length = len(guesses)/2 b_guess = np.array(guesses[:length]) n_guess = np.array(guesses[length:]) if length == S: b_s = np.array([0] + list(b_guess[:-1])) else: b_s = np.array([(initial_b[-(s+3), j])] + list(b_guess[:-1])) b_splus1 = b_guess b_splus2 = np.array(list(b_guess[1:]) + [0]) w_s = winit[t:t+length] w_splus1 = winit[t+1:t+length+1] r_s = rinit[t:t+length] r_splus1 = rinit[t+1:t+length+1] n_s = n_guess n_extended = np.array(list(n_guess[1:]) + [0]) e_s = e[-length:, j] e_extended = np.array(list(e[-length+1:, j]) + [0]) BQ_s = BQinit[t:t+length] BQ_splus1 = BQinit[t+1:t+length+1] T_H_s = T_H_init[t:t+length] T_H_splus1 = T_H_init[t+1:t+length+1] # Savings euler equations tax_s = tax.total_taxes(r_s, b_s, w_s, e_s, n_s, BQ_s, lambdas[j], factor, T_H_s, j, 'TPI', False, params, theta, tau_bq) tax_splus1 = tax.total_taxes(r_splus1, b_splus1, w_splus1, e_extended, n_extended, BQ_splus1, lambdas[j], factor, T_H_splus1, j, 'TPI', True, params, theta, tau_bq) cons_s = house.get_cons(r_s, b_s, w_s, e_s, n_s, BQ_s, lambdas[j], b_splus1, params, tax_s) cons_splus1 = house.get_cons(r_splus1, b_splus1, w_splus1, e_extended, n_extended, BQ_splus1, lambdas[j], b_splus2, params, tax_splus1) income_splus1 = (r_splus1 * b_splus1 + w_splus1 * e_extended * n_extended) * factor savings_ut = rho[-(length):] * np.exp(-sigma * g_y) * chi_b[-(length):, j] * b_splus1 ** (-sigma) deriv_savings = 1 + r_splus1 * (1 - tax.tau_income( r_splus1, b_splus1, w_splus1, e_extended, n_extended, factor, params) - tax.tau_income_deriv( r_splus1, b_splus1, w_splus1, e_extended, n_extended, factor, params) * income_splus1) - tax.tau_w_prime( b_splus1, params)*b_splus1 - tax.tau_wealth(b_splus1, params) error1 = house.marg_ut_cons(cons_s, params) - beta * (1-rho[-(length):]) * np.exp(-sigma * g_y) * deriv_savings * house.marg_ut_cons( cons_splus1, params) - savings_ut # Labor leisure euler equations income_s = (r_s * b_s + w_s * e_s * n_s) * factor deriv_laborleisure = 1 - tau_payroll - tax.tau_income(r_s, b_s, w_s, e_s, n_s, factor, params) - tax.tau_income_deriv( r_s, b_s, w_s, e_s, n_s, factor, params) * income_s error2 = house.marg_ut_cons(cons_s, params) * w_s * e[-(length):, j] * deriv_laborleisure - house.marg_ut_labor(n_s, chi_n[-length:], params) # Check and punish constraint violations mask1 = n_guess < 0 error2[mask1] += 1e12 mask2 = n_guess > ltilde error2[mask2] += 1e12 mask3 = cons_s < 0 error2[mask3] += 1e12 mask4 = b_guess <= 0 error2[mask4] += 1e12 mask5 = cons_splus1 < 0 error2[mask5] += 1e12 return list(error1.flatten()) + list(error2.flatten())
def Euler_Error(guesses, winit, rinit, Binit, Tinit, t): ''' Parameters: guesses = distribution of capital and labor in period t ((S-1)*S*J x 1 list) winit = wage rate (scalar) rinit = rental rate (scalar) t = time period Returns: Value of Euler error. (as an 2*S*J x 1 list) ''' length = len(guesses) / 2 K_guess = np.array(guesses[:length]) L_guess = np.array(guesses[length:]) if length == S: K1 = np.array([0] + list(K_guess[:-2])) else: K1 = np.array([(initial_K[-(s + 2), j])] + list(K_guess[:-2])) K2 = K_guess[:-1] K3 = K_guess[1:] w1 = winit[t:t + length - 1] w2 = winit[t + 1:t + length] r1 = rinit[t:t + length - 1] r2 = rinit[t + 1:t + length] l1 = L_guess[:-1] l2 = L_guess[1:] e1 = e[-length:-1, j] e2 = e[-length + 1:, j] B1 = Binit[t:t + length - 1] B2 = Binit[t + 1:t + length] T1 = Tinit[t:t + length - 1] T2 = Tinit[t + 1:t + length] # Euler 1 equations tax11 = tax.total_taxes_TPI1(r1, K1, w1, e1, l1, B1, bin_weights[j], factor_ss, T1, j) tax12 = tax.total_taxes_TPI1_2(r2, K2, w2, e2, l2, B2, bin_weights[j], factor_ss, T2, j) cons11 = get_cons(r1, K1, w1, e1, l1, B1, bin_weights[j], K2, g_y, tax11) cons12 = get_cons(r2, K2, w2, e2, l2, B2, bin_weights[j], K3, g_y, tax12) wealth1 = (r2 * K2 + w2 * e2 * l2) * factor_ss bequest_ut = (1 - surv_rate[-(length):-1]) * np.exp( -sigma * g_y) * chi_b[-(length):-1, j] * K2**(-sigma) deriv1 = 1 + r2 * (1 - tax.tau_income(r2, K2, w2, e2, l2, factor_ss) - tax.tau_income_deriv(r2, K2, w2, e2, l2, factor_ss) * wealth1) - tax.tau_w_prime(K2) * K2 - tax.tau_wealth(K2) error1 = MUc(cons11) - beta * surv_rate[-(length):-1] * np.exp( -sigma * g_y) * deriv1 * MUc(cons12) - bequest_ut # Euler 2 equations if length == S: K1_2 = np.array([0] + list(K_guess[:-1])) else: K1_2 = np.array([(initial_K[-(s + 2), j])] + list(K_guess[:-1])) K2_2 = K_guess w = winit[t:t + length] r = rinit[t:t + length] B = Binit[t:t + length] Tl = Tinit[t:t + length] tax2 = tax.total_taxes_TPI2(r, K1_2, w, e[-(length):, j], L_guess, B, bin_weights[j], factor_ss, Tl, j) cons2 = get_cons(r, K1_2, w, e[-(length):, j], L_guess, B, bin_weights[j], K2_2, g_y, tax2) wealth2 = (r * K1_2 + w * e[-(length):, j] * L_guess) * factor_ss deriv2 = 1 - tau_payroll - tax.tau_income( r, K1_2, w, e[-(length):, j], L_guess, factor_ss) - tax.tau_income_deriv( r, K1_2, w, e[-(length):, j], L_guess, factor_ss) * wealth2 error2 = MUc(cons2) * w * e[-(length):, j] * deriv2 - MUl2( L_guess, chi_n[-length:]) # Euler 3 equations tax3 = tax.total_taxes_eul3_TPI(r[-1], K_guess[-2], w[-1], e[-1, j], L_guess[-1], B[-1], bin_weights[j], factor_ss, Tl[-1], j) cons3 = get_cons(r[-1], K_guess[-2], w[-1], e[-1, j], L_guess[-1], B[-1], bin_weights[j], K_guess[-1], g_y, tax3) error3 = MUc(cons3) - np.exp(-sigma * g_y) * MUb2(K_guess[-1], chi_b[:, j]) # Check and punish constraint violations mask1 = L_guess < 0 error2[mask1] += 1e12 mask2 = L_guess > ltilde error2[mask2] += 1e12 mask3 = cons2 < 0 error2[mask3] += 1e12 mask4 = K_guess <= 0 error2[mask4] += 1e12 return list(error1.flatten()) + list(error2.flatten()) + list( error3.flatten())
def Euler_Error(guesses, winit, rinit, Binit, Tinit, t): """ Parameters: guesses = distribution of capital and labor in period t ((S-1)*S*J x 1 list) winit = wage rate (scalar) rinit = rental rate (scalar) t = time period Returns: Value of Euler error. (as an 2*S*J x 1 list) """ length = len(guesses) / 2 K_guess = np.array(guesses[:length]) L_guess = np.array(guesses[length:]) if length == S: K1 = np.array([0] + list(K_guess[:-2])) else: K1 = np.array([(initial_K[-(s + 2), j])] + list(K_guess[:-2])) K2 = K_guess[:-1] K3 = K_guess[1:] w1 = winit[t : t + length - 1] w2 = winit[t + 1 : t + length] r1 = rinit[t : t + length - 1] r2 = rinit[t + 1 : t + length] l1 = L_guess[:-1] l2 = L_guess[1:] e1 = e[-length:-1, j] e2 = e[-length + 1 :, j] B1 = Binit[t : t + length - 1] B2 = Binit[t + 1 : t + length] T1 = Tinit[t : t + length - 1] T2 = Tinit[t + 1 : t + length] # Euler 1 equations tax11 = tax.total_taxes_TPI1(r1, K1, w1, e1, l1, B1, bin_weights[j], factor_ss, T1, j) tax12 = tax.total_taxes_TPI1_2(r2, K2, w2, e2, l2, B2, bin_weights[j], factor_ss, T2, j) cons11 = get_cons(r1, K1, w1, e1, l1, B1, bin_weights[j], K2, g_y, tax11) cons12 = get_cons(r2, K2, w2, e2, l2, B2, bin_weights[j], K3, g_y, tax12) wealth1 = (r2 * K2 + w2 * e2 * l2) * factor_ss bequest_ut = (1 - surv_rate[-(length):-1]) * np.exp(-sigma * g_y) * chi_b[-(length):-1, j] * K2 ** (-sigma) deriv1 = ( 1 + r2 * ( 1 - tax.tau_income(r2, K2, w2, e2, l2, factor_ss) - tax.tau_income_deriv(r2, K2, w2, e2, l2, factor_ss) * wealth1 ) - tax.tau_w_prime(K2) * K2 - tax.tau_wealth(K2) ) error1 = MUc(cons11) - beta * surv_rate[-(length):-1] * np.exp(-sigma * g_y) * deriv1 * MUc(cons12) - bequest_ut # Euler 2 equations if length == S: K1_2 = np.array([0] + list(K_guess[:-1])) else: K1_2 = np.array([(initial_K[-(s + 2), j])] + list(K_guess[:-1])) K2_2 = K_guess w = winit[t : t + length] r = rinit[t : t + length] B = Binit[t : t + length] Tl = Tinit[t : t + length] tax2 = tax.total_taxes_TPI2(r, K1_2, w, e[-(length):, j], L_guess, B, bin_weights[j], factor_ss, Tl, j) cons2 = get_cons(r, K1_2, w, e[-(length):, j], L_guess, B, bin_weights[j], K2_2, g_y, tax2) wealth2 = (r * K1_2 + w * e[-(length):, j] * L_guess) * factor_ss deriv2 = ( 1 - tau_payroll - tax.tau_income(r, K1_2, w, e[-(length):, j], L_guess, factor_ss) - tax.tau_income_deriv(r, K1_2, w, e[-(length):, j], L_guess, factor_ss) * wealth2 ) error2 = MUc(cons2) * w * e[-(length):, j] * deriv2 - MUl2(L_guess, chi_n[-length:]) # Euler 3 equations tax3 = tax.total_taxes_eul3_TPI( r[-1], K_guess[-2], w[-1], e[-1, j], L_guess[-1], B[-1], bin_weights[j], factor_ss, Tl[-1], j ) cons3 = get_cons(r[-1], K_guess[-2], w[-1], e[-1, j], L_guess[-1], B[-1], bin_weights[j], K_guess[-1], g_y, tax3) error3 = MUc(cons3) - np.exp(-sigma * g_y) * MUb2(K_guess[-1], chi_b[:, j]) # Check and punish constraint violations mask1 = L_guess < 0 error2[mask1] += 1e12 mask2 = L_guess > ltilde error2[mask2] += 1e12 mask3 = cons2 < 0 error2[mask3] += 1e12 mask4 = K_guess <= 0 error2[mask4] += 1e12 return list(error1.flatten()) + list(error2.flatten()) + list(error3.flatten())
def Steady_state_TPI_solver(guesses, winit, rinit, BQinit, T_H_init, factor, j, s, t, params, theta, tau_bq, rho, lambdas, e, initial_b, chi_b, chi_n): ''' Parameters: guesses = distribution of capital and labor (various length list) winit = wage rate ((T+S)x1 array) rinit = rental rate ((T+S)x1 array) BQinit = aggregate bequests ((T+S)x1 array) T_H_init = lump sum tax over time ((T+S)x1 array) factor = scaling factor (scalar) j = which ability type is being solved for (scalar) s = which upper triangle loop is being solved for (scalar) t = which diagonal is being solved for (scalar) params = list of parameters (list) theta = replacement rates (Jx1 array) tau_bq = bequest tax rate (Jx1 array) rho = mortalit rate (Sx1 array) lambdas = ability weights (Jx1 array) e = ability type (SxJ array) initial_b = capital stock distribution in period 0 (SxJ array) chi_b = chi^b_j (Jx1 array) chi_n = chi^n_s (Sx1 array) Output: Value of Euler error (various length list) ''' J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, g_n_ss, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params length = len(guesses) / 2 b_guess = np.array(guesses[:length]) n_guess = np.array(guesses[length:]) if length == S: b_s = np.array([0] + list(b_guess[:-1])) else: b_s = np.array([(initial_b[-(s + 3), j])] + list(b_guess[:-1])) b_splus1 = b_guess b_splus2 = np.array(list(b_guess[1:]) + [0]) w_s = winit[t:t + length] w_splus1 = winit[t + 1:t + length + 1] r_s = rinit[t:t + length] r_splus1 = rinit[t + 1:t + length + 1] n_s = n_guess n_extended = np.array(list(n_guess[1:]) + [0]) e_s = e[-length:, j] e_extended = np.array(list(e[-length + 1:, j]) + [0]) BQ_s = BQinit[t:t + length] BQ_splus1 = BQinit[t + 1:t + length + 1] T_H_s = T_H_init[t:t + length] T_H_splus1 = T_H_init[t + 1:t + length + 1] # Savings euler equations tax_s = tax.total_taxes(r_s, b_s, w_s, e_s, n_s, BQ_s, lambdas[j], factor, T_H_s, j, 'TPI', False, params, theta, tau_bq) tax_splus1 = tax.total_taxes(r_splus1, b_splus1, w_splus1, e_extended, n_extended, BQ_splus1, lambdas[j], factor, T_H_splus1, j, 'TPI', True, params, theta, tau_bq) cons_s = house.get_cons(r_s, b_s, w_s, e_s, n_s, BQ_s, lambdas[j], b_splus1, params, tax_s) cons_splus1 = house.get_cons(r_splus1, b_splus1, w_splus1, e_extended, n_extended, BQ_splus1, lambdas[j], b_splus2, params, tax_splus1) income_splus1 = (r_splus1 * b_splus1 + w_splus1 * e_extended * n_extended) * factor savings_ut = rho[-(length):] * np.exp( -sigma * g_y) * chi_b[-(length):, j] * b_splus1**(-sigma) deriv_savings = 1 + r_splus1 * ( 1 - tax.tau_income(r_splus1, b_splus1, w_splus1, e_extended, n_extended, factor, params) - tax.tau_income_deriv(r_splus1, b_splus1, w_splus1, e_extended, n_extended, factor, params) * income_splus1 ) - tax.tau_w_prime(b_splus1, params) * b_splus1 - tax.tau_wealth( b_splus1, params) error1 = house.marg_ut_cons( cons_s, params) - beta * (1 - rho[-(length):]) * np.exp( -sigma * g_y) * deriv_savings * house.marg_ut_cons( cons_splus1, params) - savings_ut # Labor leisure euler equations income_s = (r_s * b_s + w_s * e_s * n_s) * factor deriv_laborleisure = 1 - tau_payroll - tax.tau_income( r_s, b_s, w_s, e_s, n_s, factor, params) - tax.tau_income_deriv( r_s, b_s, w_s, e_s, n_s, factor, params) * income_s error2 = house.marg_ut_cons(cons_s, params) * w_s * e[ -(length):, j] * deriv_laborleisure - house.marg_ut_labor( n_s, chi_n[-length:], params) # Check and punish constraint violations mask1 = n_guess < 0 error2[mask1] += 1e12 mask2 = n_guess > ltilde error2[mask2] += 1e12 mask3 = cons_s < 0 error2[mask3] += 1e12 mask4 = b_guess <= 0 error2[mask4] += 1e12 mask5 = cons_splus1 < 0 error2[mask5] += 1e12 return list(error1.flatten()) + list(error2.flatten())
def euler_savings_func(w, r, e, n_guess, b_s, b_splus1, b_splus2, BQ, factor, T_H, chi_b, params, theta, tau_bq, rho, lambdas): ''' Inputs: w = wage rate (scalar) r = rental rate (scalar) e = distribution of abilities (SxJ array) n_guess = distribution of labor (SxJ array) b_s = distribution of capital in period t (S x J array) b_splus1 = distribution of capital in period t+1 (S x J array) b_splus2 = distribution of capital in period t+2 (S x J array) BQ = 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 chi_b = discount factor of savings params = parameters theta = replacement rates tau_bq = bequest tax parameters rho = mortality rates lambdas = bin weights Returns: Value of Euler error. ''' J, S, T, beta, sigma, alpha, Z, delta, ltilde, nu, g_y, tau_payroll, retire, mean_income_data, a_tax_income, b_tax_income, c_tax_income, d_tax_income, h_wealth, p_wealth, m_wealth, b_ellipse, upsilon = params e_extended = np.array(list(e) + [0]) n_extended = np.array(list(n_guess) + [0]) tax1 = tax.total_taxes(r, b_s, w, e, n_guess, BQ, lambdas, factor, T_H, None, 'SS', False, params, theta, tau_bq) tax2 = tax.total_taxes(r, b_splus1, w, e_extended[1:], n_extended[1:], BQ, lambdas, factor, T_H, None, 'SS', True, params, theta, tau_bq) cons1 = get_cons(r, b_s, w, e, n_guess, BQ, lambdas, b_splus1, params, tax1) cons2 = get_cons(r, b_splus1, w, e_extended[1:], n_extended[1:], BQ, lambdas, b_splus2, params, tax2) income = (r * b_splus1 + w * e_extended[1:] * n_extended[1:]) * factor deriv = ( 1 + r*(1-tax.tau_income(r, b_s, w, e_extended[1:], n_extended[1:], factor, params)-tax.tau_income_deriv( r, b_s, w, e_extended[1:], n_extended[1:], factor, params)*income)-tax.tau_w_prime(b_splus1, params)*b_splus1-tax.tau_wealth(b_splus1, params)) savings_ut = rho * np.exp(-sigma * g_y) * chi_b * b_splus1 ** (-sigma) euler = marg_ut_cons(cons1, params) - beta * (1-rho) * deriv * marg_ut_cons( cons2, params) * np.exp(-sigma * g_y) - savings_ut return euler