def Euler_equation_solver(guesses, r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e): 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 b_guess = np.array(guesses[:S]) n_guess = np.array(guesses[S:]) b_s = np.array([0] + list(b_guess[:-1])) b_splus1 = b_guess b_splus2 = np.array(list(b_guess[1:]) + [0]) BQ = (1+r) * (b_guess * weights[:, j] * rho).sum() theta = tax.replacement_rate_vals(n_guess, w, factor, e[:,j], J, weights[:, j]) error1 = house.euler_savings_func(w, r, e[:, j], n_guess, b_s, b_splus1, b_splus2, BQ, factor, T_H, chi_b[j], params, theta, tau_bq[j], rho, lambdas[j]) error2 = house.euler_labor_leisure_func(w, r, e[:, j], n_guess, b_s, b_splus1, BQ, factor, T_H, chi_n, params, theta, tau_bq[j], lambdas[j]) # Put in constraints for consumption and savings. According to the euler equations, they can be negative. When # Chi_b is large, they will be. This prevents that from happening. # I'm not sure if the constraints are needed for labor. But we might as well put them in for now. mask1 = n_guess < 0 mask2 = n_guess > ltilde mask3 = b_guess <= 0 error2[mask1] += 1e14 error2[mask2] += 1e14 error1[mask3] += 1e14 tax1 = tax.total_taxes(r, b_s, w, e[:, j], n_guess, BQ, lambdas[j], factor, T_H, None, 'SS', False, params, theta, tau_bq[j]) cons = house.get_cons(r, b_s, w, e[:, j], n_guess, BQ, lambdas[j], b_splus1, params, tax1) mask4 = cons < 0 error1[mask4] += 1e14 # print np.append(error1.flatten(), error2.flatten()).max() return list(error1.flatten()) + list(error2.flatten())
def SS_TPI_firstdoughnutring(guesses, winit, rinit, BQinit, T_H_init): # This function does not work. The tax functions need to be changed. b2 = float(guesses[0]) n1 = float(guesses[1]) b1 = float(initial_b[-2, j]) # Euler 1 equations tax11 = tax.total_taxes_eul3_TPI(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], factor_ss, T_H_init, j) cons11 = house.get_cons(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], b2, g_y, tax11) bequest_ut = rho * np.exp(-sigma * g_y) * chi_b[-1, j] * b2 ** (-sigma) error1 = house.marg_ut_cons(cons11) - bequest_ut # Euler 2 equations tax2 = tax.total_taxes_eul3_TPI(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], factor_ss, T_H_init, j) cons2 = house.get_cons(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], b2, g_y, tax2) income2 = (rinit * b1 + winit * e[-1, j] * n1) * factor_ss deriv2 = 1 - tau_payroll - tax.tau_income(rinit, b1, winit, e[ -1, j], n1, factor_ss) - tax.tau_income_deriv( rinit, b1, winit, e[-1, j], n1, factor_ss) * income2 error2 = house.marg_ut_cons(cons2) * winit * e[-1, j] * deriv2 - house.marg_ut_labor(n1, chi_n[-1]) if n1 <= 0: error2 += 1e12 return [error1] + [error2]
def Euler_equation_solver(guesses, r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e): ''' Finds the euler error for certain b and n, one ability type at a time. Inputs: guesses = guesses for b and n (2Sx1 list) r = rental rate (scalar) w = wage rate (scalar) T_H = lump sum tax (scalar) factor = scaling factor to dollars (scalar) j = which ability group is being solved for (scalar) params = list of parameters (list) chi_b = chi^b_j (scalar) chi_n = chi^n_s (Sx1 array) tau_bq = bequest tax rate (scalar) rho = mortality rates (Sx1 array) lambdas = ability weights (scalar) weights = population weights (Sx1 array) e = ability levels (Sx1 array) Outputs: 2Sx1 list of euler errors ''' 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 b_guess = np.array(guesses[:S]) n_guess = np.array(guesses[S:]) b_s = np.array([0] + list(b_guess[:-1])) b_splus1 = b_guess b_splus2 = np.array(list(b_guess[1:]) + [0]) BQ = house.get_BQ(r, b_splus1, weights, lambdas[j], rho, g_n_ss) theta = tax.replacement_rate_vals(n_guess, w, factor, e[:,j], J, weights, lambdas[j]) error1 = house.euler_savings_func(w, r, e[:, j], n_guess, b_s, b_splus1, b_splus2, BQ, factor, T_H, chi_b[j], params, theta, tau_bq[j], rho, lambdas[j]) error2 = house.euler_labor_leisure_func(w, r, e[:, j], n_guess, b_s, b_splus1, BQ, factor, T_H, chi_n, params, theta, tau_bq[j], lambdas[j]) # Put in constraints for consumption and savings. According to the euler equations, they can be negative. When # Chi_b is large, they will be. This prevents that from happening. # I'm not sure if the constraints are needed for labor. But we might as well put them in for now. mask1 = n_guess < 0 mask2 = n_guess > ltilde mask3 = b_guess <= 0 error2[mask1] += 1e14 error2[mask2] += 1e14 error1[mask3] += 1e14 tax1 = tax.total_taxes(r, b_s, w, e[:, j], n_guess, BQ, lambdas[j], factor, T_H, None, 'SS', False, params, theta, tau_bq[j]) cons = house.get_cons(r, b_s, w, e[:, j], n_guess, BQ, lambdas[j], b_splus1, params, tax1) mask4 = cons < 0 error1[mask4] += 1e14 return list(error1.flatten()) + list(error2.flatten())
def Steady_State_SS(guesses, chi_params, params, weights_SS, rho_vec, lambdas, theta, tau_bq, e): ''' Parameters: Steady state distribution of capital guess as array size 2*S*J Returns: Array of 2*S*J Euler equation errors ''' 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 chi_b = np.tile(np.array(chi_params[:J]).reshape(1, J), (S, 1)) chi_n = np.array(chi_params[J:]) b_guess = guesses[0: S * J].reshape((S, J)) K = house.get_K(b_guess, weights_SS) n_guess = guesses[S * J:-1].reshape((S, J)) L = firm.get_L(e, n_guess, weights_SS) Y = firm.get_Y(K, L, params) w = firm.get_w(Y, L, params) r = firm.get_r(Y, K, params) BQ = (1 + r) * (b_guess * weights_SS * rho_vec.reshape(S, 1)).sum(0) b_s = np.array(list(np.zeros(J).reshape(1, J)) + list(b_guess[:-1, :])) b_splus1 = b_guess b_splus2 = np.array(list(b_guess[1:]) + list(np.zeros(J).reshape(1, J))) factor = guesses[-1] T_H = tax.get_lump_sum(r, b_s, w, e, n_guess, BQ, lambdas, factor, weights_SS, 'SS', params, theta, tau_bq) error1 = house.euler_savings_func(w, r, e, n_guess, b_s, b_splus1, b_splus2, BQ.reshape(1, J), factor, T_H, chi_b, params, theta, tau_bq, rho_vec, lambdas) error2 = house.euler_labor_leisure_func(w, r, e, n_guess, b_s, b_splus1, BQ.reshape(1, J), factor, T_H, chi_n, params, theta, tau_bq, lambdas) average_income_model = ((r * b_s + w * e * n_guess) * weights_SS).sum() error3 = [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(r, b_s, w, e, n_guess, BQ, lambdas, factor, T_H, None, 'SS', False, params, theta, tau_bq) cons = house.get_cons(r, b_s, w, e, n_guess, BQ.reshape(1, J), lambdas, b_splus1, params, 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()) + error3)).max() return list(error1.flatten()) + list( error2.flatten()) + error3
def SS_TPI_firstdoughnutring(guesses, winit, rinit, BQinit, T_H_init, j): ''' Solves the first entries of the upper triangle of the twist doughnut. This is separate from the main TPI function because the the values of b and n are scalars, so it is easier to just have a separate function for these cases. Inputs: guesses = guess for b and n (2x1 list) winit = initial wage rate (scalar) rinit = initial rental rate (scalar) BQinit = initial aggregate bequest (scalar) T_H_init = initial lump sum tax (scalar) j = which ability type is being solved for (scalar) Output: euler errors (2x1 list) ''' b2 = float(guesses[0]) n1 = float(guesses[1]) b1 = float(initial_b[-2, j]) # Euler 1 equations tax1 = tax.total_taxes(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], factor_ss, T_H_init, j, 'TPI_scalar', False, parameters, theta, tau_bq) cons1 = house.get_cons(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], b2, parameters, tax1) bequest_ut = rho[-1] * np.exp(-sigma * g_y) * chi_b[-1, j] * b2**(-sigma) error1 = house.marg_ut_cons(cons1, parameters) - bequest_ut # Euler 2 equations income2 = (rinit * b1 + winit * e[-1, j] * n1) * factor_ss deriv2 = 1 - tau_payroll - tax.tau_income( rinit, b1, winit, e[-1, j], n1, factor_ss, parameters) - tax.tau_income_deriv( rinit, b1, winit, e[-1, j], n1, factor_ss, parameters) * income2 error2 = house.marg_ut_cons( cons1, parameters) * winit * e[-1, j] * deriv2 - house.marg_ut_labor( n1, chi_n[-1], parameters) if n1 <= 0 or n1 >= 1: error2 += 1e12 if b2 <= 0: error1 += 1e12 if cons1 <= 0: error1 += 1e12 return [error1] + [error2]
def wrguess(X, bssmat, nssmat, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e): 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 w, r, T_H, factor = X for j in xrange(J): # Solve the euler equations guesses = np.append(bssmat[:, j], nssmat[:, j]) solutions = opt.fsolve(Euler_equation_solver, guesses * .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13) bssmat[:,j] = solutions[:S] nssmat[:,j] = solutions[S:] theta = tax.replacement_rate_vals(nssmat[:, j], w, factor, e[:, j], J, weights[:, j]) # print np.array(Euler_equation_solver(np.append(bssmat[:, j], nssmat[:, j]), r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e)).max() #Calculate demand for C, I, and Y BQ = (1+r) * (weights * rho.reshape(S,1)*bssmat).sum(0) theta = tax.replacement_rate_vals(nssmat, w, factor, e, J, weights) net_tax = tax.total_taxes(r, bssmat, w, e, nssmat, BQ, lambdas, factor, T_H, 0, 'SS', False, params, theta, tau_bq) b_s = np.array(list(np.zeros(J).reshape(1,J)) + list(bssmat[:-1])) cons = house.get_cons(r, b_s, w, e, nssmat, BQ, lambdas, bssmat, params, net_tax) C = (weights*cons).sum() B = house.get_K(bssmat, weights) Y = C / (1-(delta*alpha/(r+delta))) #Get demand for K and L K_demand = alpha*Y / (r+delta) L_demand = (1-alpha)*Y / w #Get the 2 errors from difference between supply and demand of K and L error1 = K_demand - B error2 = L_demand - firm.get_L(e, nssmat, weights) #Get other 2 errors from the factor equation and government constraint error3 = T_H - tax.get_lump_sum(r, b_s, w, e, nssmat, BQ, lambdas, factor, weights, 'SS', params, theta, tau_bq) # error3 = T_H - (weights*revenue).sum() mean_income_model = ((r * b_s + w * e * nssmat) * weights).sum() error4 = factor - mean_income_data / mean_income_model error = [error1, error2, error3, error4] # print error print max([abs(error1), abs(error2), abs(error3), abs(error4)]) return error
def SS_TPI_firstdoughnutring(guesses, winit, rinit, BQinit, T_H_init): b2 = float(guesses[0]) n1 = float(guesses[1]) b1 = float(initial_b[-2, j]) # Euler 1 equations tax1 = tax.total_taxes(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], factor_ss, T_H_init, j, 'TPI_scalar', False, parameters, theta, tau_bq) cons1 = house.get_cons(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], b2, parameters, tax1) bequest_ut = rho[-1] * np.exp(-sigma * g_y) * chi_b[-1, j] * b2 ** (-sigma) error1 = house.marg_ut_cons(cons1, parameters) - bequest_ut # Euler 2 equations income2 = (rinit * b1 + winit * e[-1, j] * n1) * factor_ss deriv2 = 1 - tau_payroll - tax.tau_income(rinit, b1, winit, e[ -1, j], n1, factor_ss, parameters) - tax.tau_income_deriv( rinit, b1, winit, e[-1, j], n1, factor_ss, parameters) * income2 error2 = house.marg_ut_cons(cons1, parameters) * winit * e[-1, j] * deriv2 - house.marg_ut_labor(n1, chi_n[-1], parameters) if n1 <= 0 or n1 >= 1: error2 += 1e12 if b2 <=0: error1 += 1e12 if cons1 <= 0: error1 += 1e12 return [error1] + [error2]
def Euler_equation_solver(guesses, r, w, T_H, factor, j, params, chi_b, chi_n, theta, tau_bq, rho, lambdas, weights): b_guess = np.array(guesses[:S]) n_guess = np.array(guesses[S:]) b_s = np.array([0] + list(b_guess[:-1])) b_splus1 = b_guess b_splus2 = np.array(list(b_guess[1:]) + [0]) BQ = (1+r) * (b_guess * weights[:, j] * rho).sum() error1 = house.euler_savings_func(w, r, e[:, j], n_guess, b_s, b_splus1, b_splus2, BQ, factor, T_H, chi_b[j], params, theta[j], tau_bq[j], rho, lambdas[j]) error2 = house.euler_labor_leisure_func(w, r, e[:, j], n_guess, b_s, b_splus1, BQ, factor, T_H, chi_n, params, theta[j], tau_bq[j], lambdas[j]) # Put in constraints mask1 = n_guess < 0 mask2 = n_guess > ltilde mask3 = b_guess <= 0 error2[mask1] += 1e9 error2[mask2] += 1e9 error1[mask3] += 1e9 tax1 = tax.total_taxes(r, b_s, w, e[:, j], n_guess, BQ, lambdas[j], factor, T_H, None, 'SS', False, params, theta[j], tau_bq[j]) cons = house.get_cons(r, b_s, w, e[:, j], n_guess, BQ, lambdas[j], b_splus1, params, tax1) mask4 = cons < 0 error1[mask4] += 1e9 return list(error1.flatten()) + list(error2.flatten())
def SS_TPI_firstdoughnutring(guesses, winit, rinit, BQinit, T_H_init, j): ''' Solves the first entries of the upper triangle of the twist doughnut. This is separate from the main TPI function because the the values of b and n are scalars, so it is easier to just have a separate function for these cases. Inputs: guesses = guess for b and n (2x1 list) winit = initial wage rate (scalar) rinit = initial rental rate (scalar) BQinit = initial aggregate bequest (scalar) T_H_init = initial lump sum tax (scalar) j = which ability type is being solved for (scalar) Output: euler errors (2x1 list) ''' b2 = float(guesses[0]) n1 = float(guesses[1]) b1 = float(initial_b[-2, j]) # Euler 1 equations tax1 = tax.total_taxes(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], factor_ss, T_H_init, j, 'TPI_scalar', False, parameters, theta, tau_bq) cons1 = house.get_cons(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], b2, parameters, tax1) bequest_ut = rho[-1] * np.exp(-sigma * g_y) * chi_b[-1, j] * b2 ** (-sigma) error1 = house.marg_ut_cons(cons1, parameters) - bequest_ut # Euler 2 equations income2 = (rinit * b1 + winit * e[-1, j] * n1) * factor_ss deriv2 = 1 - tau_payroll - tax.tau_income(rinit, b1, winit, e[ -1, j], n1, factor_ss, parameters) - tax.tau_income_deriv( rinit, b1, winit, e[-1, j], n1, factor_ss, parameters) * income2 error2 = house.marg_ut_cons(cons1, parameters) * winit * e[-1, j] * deriv2 - house.marg_ut_labor(n1, chi_n[-1], parameters) if n1 <= 0 or n1 >= 1: error2 += 1e12 if b2 <=0: error1 += 1e12 if cons1 <= 0: error1 += 1e12 return [error1] + [error2]
K0 = house.get_K(initial_b, omega_stationary[0].reshape(S, 1), lambdas, g_n_vector[0]) b_sinit = np.array(list(np.zeros(J).reshape(1, J)) + list(initial_b[:-1])) b_splus1init = initial_b L0 = firm.get_L(e, initial_n, omega_stationary[0].reshape(S, 1), lambdas) Y0 = firm.get_Y(K0, L0, parameters) w0 = firm.get_w(Y0, L0, parameters) r0 = firm.get_r(Y0, K0, parameters) BQ0 = house.get_BQ(r0, initial_b, omega_stationary[0].reshape(S, 1), lambdas, rho.reshape(S, 1), g_n_vector[0]) T_H_0 = tax.get_lump_sum(r0, b_sinit, w0, e, initial_n, BQ0, lambdas, factor_ss, omega_stationary[0].reshape(S, 1), 'SS', parameters, theta, tau_bq) tax0 = tax.total_taxes(r0, b_sinit, w0, e, initial_n, BQ0, lambdas, factor_ss, T_H_0, None, 'SS', False, parameters, theta, tau_bq) c0 = house.get_cons(r0, b_sinit, w0, e, initial_n, BQ0.reshape(1, J), lambdas.reshape(1, J), b_splus1init, parameters, tax0) ''' ------------------------------------------------------------------------ Solve for equilibrium transition path by TPI ------------------------------------------------------------------------ ''' def SS_TPI_firstdoughnutring(guesses, winit, rinit, BQinit, T_H_init, j): ''' Solves the first entries of the upper triangle of the twist doughnut. This is separate from the main TPI function because the the values of b and n are scalars, so it is easier to just have a separate function for these cases. Inputs: guesses = guess for b and n (2x1 list) winit = initial wage rate (scalar)
bq = solutions[(S-1)*J:S*J] bssmat_s = np.array(list(np.zeros(J).reshape(1, J)) + list(bssmat)) bssmat_splus1 = np.array(list(bssmat) + list(bq.reshape(1, J))) nssmat = solutions[S * J:2*S*J].reshape(S, J) wss, rss, factor_ss, T_Hss = solutions[2*S*J:] Kss = house.get_K(bssmat_splus1, omega_SS) Lss = firm.get_L(e, nssmat, omega_SS) Yss = firm.get_Y(Kss, Lss, parameters) theta = tax.replacement_rate_vals(nssmat, wss, factor_ss, e, J, omega_SS) BQss = (1+rss)*(np.array(list(bssmat) + list(bq.reshape(1, J))).reshape( S, J) * omega_SS * rho.reshape(S, 1)).sum(0) b_s = np.array(list(np.zeros(J).reshape((1, J))) + list(bssmat)) taxss = tax.total_taxes(rss, b_s, wss, e, nssmat, BQss, lambdas, factor_ss, T_Hss, None, 'SS', False, parameters, theta, tau_bq) cssmat = house.get_cons(rss, b_s, wss, e, nssmat, BQss.reshape(1, J), lambdas.reshape(1, J), bssmat_splus1, parameters, taxss) house.constraint_checker_SS(bssmat, nssmat, cssmat, parameters) ''' ------------------------------------------------------------------------ Generate variables for graphs ------------------------------------------------------------------------ b_s = SxJ array of bssmat in period t b_splus1 = SxJ array of bssmat in period t+1 b_splus2 = SxJ array of bssmat in period t+2 euler_savings = euler errors from savings euler equation euler_labor_leisure = euler errors from labor leisure euler equation ------------------------------------------------------------------------ ''' b_s = np.array(list(np.zeros(J).reshape((1, J))) + list(bssmat))
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 SS_solver(b_guess_init, n_guess_init, wguess, rguess, T_Hguess, factorguess, chi_n, chi_b, params, iterative_params, tau_bq, rho, lambdas, weights, e): 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 maxiter, mindist_SS = iterative_params w = wguess r = rguess T_H = T_Hguess factor = factorguess bssmat = b_guess_init nssmat = n_guess_init dist = 10 iteration = 0 dist_vec = np.zeros(maxiter) w_step = .1 r_step = .01 w_down = True r_down = True while (dist > mindist_SS) and (iteration < maxiter): for j in xrange(J): # Solve the euler equations guesses = np.append(bssmat[:, j], nssmat[:, j]) solutions = opt.fsolve(Euler_equation_solver, guesses * .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13) bssmat[:,j] = solutions[:S] nssmat[:,j] = solutions[S:] # print np.array(Euler_equation_solver(np.append(bssmat[:, j], nssmat[:, j]), r, w, T_H, factor, j, params, chi_b, chi_n, theta, tau_bq, rho, lambdas, e)).max() # Update factor, T_H b_s = np.array(list(np.zeros(J).reshape(1, J)) + list(bssmat[:-1, :])) average_income_model = ((r * b_s + w * e * nssmat) * weights).sum() new_factor = mean_income_data / average_income_model BQ = (1+r)*(bssmat * weights * rho.reshape(S, 1)).sum(0) theta = tax.replacement_rate_vals(nssmat, w, factor, e, J, weights) new_T_H = tax.get_lump_sum(r, b_s, w, e, nssmat, BQ, lambdas, new_factor, weights, 'SS', params, theta, tau_bq) # Update w, r B_supply = house.get_K(bssmat, weights) L_supply = firm.get_L(e, nssmat, weights) total_tax = tax.total_taxes(r, b_s, w, e, nssmat, BQ, lambdas, new_factor, new_T_H, None, 'SS', False, params, theta, tau_bq) c_mat = house.get_cons(r, b_s, w, e, nssmat, BQ, lambdas, bssmat, params, total_tax) C = (c_mat*weights).sum() Y = C / (1-(delta*alpha/(r+delta))) B_demand = alpha * Y / (r + delta) L_demand = (1-alpha) * Y / w if B_demand - B_supply > mindist_SS: if r_down: r_step /= 2.0 r_down = False r += r_step else: if not(r_down): r_step /= 2.0 r_down = True r -= r_step if L_demand - L_supply > mindist_SS: if w_down: w_step /=2.0 w_down = False w += w_step else: if not(w_down): w_step /= 2.0 w_down = True w -= w_step factor = misc_funcs.convex_combo(new_factor, factor, params) T_H = misc_funcs.convex_combo(new_T_H, T_H, params) dist = np.array([misc_funcs.perc_dif_func(new_T_H, T_H)] + [misc_funcs.perc_dif_func(new_factor, factor)] + [misc_funcs.perc_dif_func(B_demand, B_supply)] + [misc_funcs.perc_dif_func(L_demand, L_supply)]).max() dist_vec[iteration] = dist if iteration > 10: if dist_vec[iteration] - dist_vec[iteration-1] > 0: nu /= 2.0 print 'New value of nu:', nu iteration += 1 print "Iteration: %02d" % iteration, " Distance: ", dist eul_errors = np.ones(J) b_mat = np.zeros((S, J)) n_mat = np.zeros((S, J)) for j in xrange(J): solutions1 = opt.fsolve(Euler_equation_solver, np.append(bssmat[:, j], nssmat[:, j])* .9, args=(r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e), xtol=1e-13) eul_errors[j] = np.array(Euler_equation_solver(solutions1, r, w, T_H, factor, j, params, chi_b, chi_n, tau_bq, rho, lambdas, weights, e)).max() b_mat[:, j] = solutions1[:S] n_mat[:, j] = solutions1[S:] print 'SS fsolve euler error:', eul_errors.max() solutions = np.append(b_mat.flatten(), n_mat.flatten()) other_vars = np.array([w, r, factor, T_H]) solutions = np.append(solutions, other_vars) return solutions
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())
bssmat_s = np.array(list(np.zeros(J).reshape(1, J)) + list(bssmat)) bssmat_splus1 = np.array(list(bssmat) + list(bq.reshape(1, J))) nssmat = solutions[S * J:2*S*J].reshape(S, J) wss, rss, factor_ss, T_Hss = solutions[2*S*J:] Kss = house.get_K(bssmat_splus1, omega_SS.reshape(S, 1), lambdas, g_n_ss) Lss = firm.get_L(e, nssmat, omega_SS.reshape(S, 1), lambdas) Yss = firm.get_Y(Kss, Lss, parameters) Iss = firm.get_I(Kss, Kss, delta, g_y, g_n_ss) theta = tax.replacement_rate_vals(nssmat, wss, factor_ss, e, J, omega_SS.reshape(S, 1), lambdas) BQss = house.get_BQ(rss, bssmat_splus1, omega_SS.reshape(S, 1), lambdas, rho.reshape(S, 1), g_n_ss) b_s = np.array(list(np.zeros(J).reshape((1, J))) + list(bssmat)) taxss = tax.total_taxes(rss, b_s, wss, e, nssmat, BQss, lambdas, factor_ss, T_Hss, None, 'SS', False, parameters, theta, tau_bq) cssmat = house.get_cons(rss, b_s, wss, e, nssmat, BQss.reshape(1, J), lambdas.reshape(1, J), bssmat_splus1, parameters, taxss) Css = house.get_C(cssmat, omega_SS.reshape(S, 1), lambdas) resource_constraint = Yss - (Css + Iss) print 'Resource Constraint Difference:', resource_constraint house.constraint_checker_SS(bssmat, nssmat, cssmat, parameters) b_s = np.array(list(np.zeros(J).reshape((1, J))) + list(bssmat)) b_splus1 = bssmat_splus1 b_splus2 = np.array(list(bssmat_splus1[1:]) + list(np.zeros(J).reshape((1, J)))) chi_b = np.tile(chi_params[:J].reshape(1, J), (S, 1)) chi_n = np.array(chi_params[J:])
initial_b = bssmat_splus1 initial_n = nssmat else: initial_b = bssmat_init initial_n = nssmat_init K0 = house.get_K(initial_b, omega_stationary[0]) b_sinit = np.array(list(np.zeros(J).reshape(1, J)) + list(initial_b[:-1])) b_splus1init = initial_b L0 = firm.get_L(e, initial_n, omega_stationary[0]) Y0 = firm.get_Y(K0, L0, parameters) w0 = firm.get_w(Y0, L0, parameters) r0 = firm.get_r(Y0, K0, parameters) BQ0 = (1+r0)*(initial_b * omega_stationary[0] * rho.reshape(S, 1)).sum(0) T_H_0 = tax.get_lump_sum(r0, b_sinit, w0, e, initial_n, BQ0, lambdas, factor_ss, omega_stationary[0], 'SS', parameters, theta, tau_bq) tax0 = tax.total_taxes(r0, b_sinit, w0, e, initial_n, BQ0, lambdas, factor_ss, T_H_0, None, 'SS', False, parameters, theta, tau_bq) c0 = house.get_cons(r0, b_sinit, w0, e, initial_n, BQ0.reshape(1, J), lambdas.reshape(1, J), b_splus1init, parameters, tax0) ''' ------------------------------------------------------------------------ Solve for equilibrium transition path by TPI ------------------------------------------------------------------------ ''' def SS_TPI_firstdoughnutring(guesses, winit, rinit, BQinit, T_H_init): b2 = float(guesses[0]) n1 = float(guesses[1]) b1 = float(initial_b[-2, j]) # Euler 1 equations tax1 = tax.total_taxes(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], factor_ss, T_H_init, j, 'TPI_scalar', False, parameters, theta, tau_bq) cons1 = house.get_cons(rinit, b1, winit, e[-1, j], n1, BQinit, lambdas[j], b2, parameters, tax1)
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())