def EulerSys_b(b, *objs): ''' Generates vector of all Euler errors for a given b, which errors characterize all optimal lifetime savings decisions Inputs: b = [S-1,] vector, lifetime savings decisions objs = length 9 tuple, (alpha, beta, sigma, r, w, p, p, c_bar, n) alpha = [I,] vector, expenditure shares on each good beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r = scalar > 0, interest rate w = scalar > 0, real wage p_tilde = scalar > 0, composite good price p_c = [I,] vector, price for each consumption good c_bar = [I,] vector, minimum consumption values for all goods n = [S,] vector, exogenous labor supply n_{s} Functions called: firm.get_c_tilde firm.get_c firm.get_b_errors Objects in function: c_params = length 3 tuple, parameters for get_c (r, w, p) c_tilde = [S,] vector, remaining lifetime consumption levels implied by b c_tilde_cstr = [S, ] boolean vector, =True if c_{s,t}<=0 c_params = length 2 tuple, parameters for get_c (alpha, p) c = [I,S] matrix, consumption values for each good and age c_{i,s} c_cstr = [I,S] boolean matrix, =True if c_{i,s}<=0 b_err_params = length 2 tuple, parameters to pass into get_b_errors (beta, sigma) b_err_vec = [S-1,] vector, Euler errors from lifetime consumption vector Returns: b_err_vec ''' alpha, beta, sigma, r, w, p_tilde, p_c, c_bar, I, S, n = objs c_tilde, c_tilde_cstr = firm.get_c_tilde(c_bar, r, w, p_c, p_tilde, n, np.append([0], b)) c, c_cstr = firm.get_c(np.tile(np.reshape(alpha, (I, 1)), (1, S)), np.tile(np.reshape(c_bar, (I, 1)), (1, S)), np.tile(c_tilde, (I, 1)), np.tile(np.reshape(p_c, (I, 1)), (1, S)), p_tilde) b_err_params = (beta, sigma) b_err_vec = firm.get_b_errors(b_err_params, r, c_tilde, c_tilde_cstr, diff=True) return b_err_vec
def get_cbess(params, b_guess, p_c, c_bar, I, S, n): ''' Generates vectors for individual savings, composite consumption, industry-specific consumption, constraint vectors, and Euler errors for a given set of prices (r, w, p, p). Inputs: params = length 7 tuple, (alpha, beta, sigma, r, w, p ss_tol) alpha = [I,] vector, expenditure shares on each good beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r = scalar > 0, interest rate w = scalar > 0, real wage p_tilde = scalar > 0, composite good price ss_tol = scalar > 0, tolerance level for steady-state fsolve b_guess = [S-1,] vector, initial guess for savings vector p_c = [I,] vector, prices in each industry c_bar = [I,] vector, minimum consumption values for all goods n = [S,] vector, exogenous labor supply n_{s} Functions called: EulerSys_b firm.get_c_tilde firm.get_c firm.get_b_errors Objects in function: eulb_objs = length 9 tuple, objects to be passed in to EulerSys_b: (alpha, beta, sigma, r, w, p, p, c_bar, n) b = [S-1,] vector, optimal lifetime savings decisions c_tidle_params = length 3 tuple, parameters for get_c_tilde(r, w, p_tilde) c_tilde = [S,] vector, optimal lifetime consumption c_tilde_cstr = [S,] boolean vector, =True if c_s<=0 c_params = length 2 tuple, parameters for get_c (alpha, p) c = [I,S] matrix, optimal consumption of each good given prices c_cstr = [I,S] boolean matrix, =True if c_{i,s}<=0 for given c_s eul_params = length 2 tuple, parameters for get_b_errors (beta, sigma) euler_errors = [S-1,] vector, Euler errors from savings decisions Returns: b, c, c_cstr, c, c_cstr, euler_errors ''' alpha, beta, sigma, r, w, p_tilde, ss_tol = params eulb_objs = (alpha, beta, sigma, r, w, p_tilde, p_c, c_bar, I, S, n) b = opt.fsolve(EulerSys_b, b_guess, args=(eulb_objs), xtol=ss_tol) c_tilde, c_tilde_cstr = firm.get_c_tilde(c_bar, r, w, p_c, p_tilde, n, np.append([0], b)) c_params = (alpha, p_tilde) c, c_cstr = firm.get_c(np.tile(np.reshape(alpha,(I,1)),(1,S)), np.tile(np.reshape(c_bar,(I,1)),(1,S)), np.tile(c_tilde,(I,1)), np.tile(np.reshape(p_c,(I,1)),(1,S)) , p_tilde) eul_params = (beta, sigma) euler_errors = firm.get_b_errors(eul_params, r, c_tilde, c_tilde_cstr, diff=True) return b, c, c_cstr, c, c_cstr, euler_errors
def EulerSys_b(b, *objs): ''' Generates vector of all Euler errors for a given b, which errors characterize all optimal lifetime savings decisions Inputs: b = [S-1,] vector, lifetime savings decisions objs = length 9 tuple, (alpha, beta, sigma, r, w, p, p, c_bar, n) alpha = [I,] vector, expenditure shares on each good beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r = scalar > 0, interest rate w = scalar > 0, real wage p_tilde = scalar > 0, composite good price p_c = [I,] vector, price for each consumption good c_bar = [I,] vector, minimum consumption values for all goods n = [S,] vector, exogenous labor supply n_{s} Functions called: firm.get_c_tilde firm.get_c firm.get_b_errors Objects in function: c_params = length 3 tuple, parameters for get_c (r, w, p) c_tilde = [S,] vector, remaining lifetime consumption levels implied by b c_tilde_cstr = [S, ] boolean vector, =True if c_{s,t}<=0 c_params = length 2 tuple, parameters for get_c (alpha, p) c = [I,S] matrix, consumption values for each good and age c_{i,s} c_cstr = [I,S] boolean matrix, =True if c_{i,s}<=0 b_err_params = length 2 tuple, parameters to pass into get_b_errors (beta, sigma) b_err_vec = [S-1,] vector, Euler errors from lifetime consumption vector Returns: b_err_vec ''' alpha, beta, sigma, r, w, p_tilde, p_c, c_bar, I, S, n = objs c_tilde, c_tilde_cstr = firm.get_c_tilde(c_bar, r, w, p_c, p_tilde, n, np.append([0], b)) c, c_cstr = firm.get_c(np.tile(np.reshape(alpha,(I,1)),(1,S)), np.tile(np.reshape(c_bar,(I,1)),(1,S)), np.tile(c_tilde,(I,1)), np.tile(np.reshape(p_c,(I,1)),(1,S)) , p_tilde) b_err_params = (beta, sigma) b_err_vec = firm.get_b_errors(b_err_params, r, c_tilde, c_tilde_cstr, diff=True) return b_err_vec
def paths_life(params, beg_age, beg_wealth, c_bar, n, r_path, w_path, p_c_path, p_tilde_path, b_init): ''' Solve for the remaining lifetime savings decisions of an individual who enters the model at age beg_age, with corresponding initial wealth beg_wealth. Inputs: params = length 5 tuple, (S, alpha, beta, sigma, tp_tol) S = integer in [3,80], number of periods an individual lives alpha = [I,S-beg_age+1], expenditure share on good for remaing lifetime beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion tp_tol = scalar > 0, tolerance level for fsolve's in TPI beg_age = integer in [1,S-1], beginning age of remaining life beg_wealth = scalar, beginning wealth at beginning age n = [S-beg_age+1,] vector, remaining exogenous labor supplies r_path = [S-beg_age+1,] vector, remaining lifetime interest rates w_path = [S-beg_age+1,] vector, remaining lifetime wages p_c_path = [I, S-beg_age+1] matrix, remaining lifetime consumption good prices p_tilde_path = [S-beg_age+1,] vector, remaining lifetime composite goods prices b_init = [S-beg_age,] vector, initial guess for remaining lifetime savings Functions called: LfEulerSys firm.get_c_tilde firm.get_c firm.get_b_errors Objects in function: u = integer in [2,S], remaining periods in life b_guess = [u-1,] vector, initial guess for lifetime savings decisions eullf_objs = length 9 tuple, objects to be passed in to LfEulerSys: (p, beta, sigma, beg_wealth, n, r_path, w_path, p_path, p_tilde_path) b_path = [u-1,] vector, optimal remaining lifetime savings decisions c_tilde_path = [u,] vector, optimal remaining lifetime consumption decisions c_path = [u,I] martrix, remaining lifetime consumption decisions by consumption good c_constr = [u,] boolean vector, =True if c_{u}<=0, b_err_params = length 2 tuple, parameters to pass into firm.get_b_errors (beta, sigma) b_err_vec = [u-1,] vector, Euler errors associated with optimal savings decisions Returns: b_path, c_tilde_path, c_path, b_err_vec ''' S, alpha, beta, sigma, tp_tol = params u = int(S - beg_age + 1) if beg_age == 1 and beg_wealth != 0: sys.exit("Beginning wealth is nonzero for age s=1.") if len(r_path) != u: #print len(r_path), S-beg_age+1 sys.exit("Beginning age and length of r_path do not match.") if len(w_path) != u: sys.exit("Beginning age and length of w_path do not match.") if len(n) != u: sys.exit("Beginning age and length of n do not match.") b_guess = 1.01 * b_init eullf_objs = (u, beta, sigma, beg_wealth, c_bar, n, r_path, w_path, p_c_path, p_tilde_path) b_path = opt.fsolve(LfEulerSys, b_guess, args=(eullf_objs), xtol=tp_tol) c_tilde_path, c_tilde_cstr = firm.get_c_tilde(c_bar, r_path, w_path, p_c_path, p_tilde_path, n, np.append(beg_wealth, b_path)) c_path, c_cstr = firm.get_c(alpha[:,:u], c_bar, c_tilde_path, p_c_path, p_tilde_path) b_err_params = (beta, sigma) b_err_vec = firm.get_b_errors(b_err_params, r_path[1:], c_tilde_path, c_tilde_cstr, diff=True) return b_path, c_tilde_path, c_path, b_err_vec
def get_cbepath(params, Gamma1, r_path, w_path, p_c_path, p_tilde_path, c_bar, I, n): ''' Generates matrices for the time path of the distribution of individual savings, individual composite consumption, individual consumption of each type of good, and the Euler errors associated with the savings decisions. Inputs: params = length 6 tuple, (S, T, alpha, beta, sigma, tp_tol) S = integer in [3,80], number of periods an individual lives T = integer > S, number of time periods until steady state I = integer, number unique consumption goods alpha = [I,T+S-1] matrix, expenditure shares on each good along time path beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion tp_tol = scalar > 0, tolerance level for fsolve's in TPI Gamma1 = [S-1,] vector, initial period savings distribution r_path = [T+S-1,] vector, the time path of the interest rate w_path = [T+S-1,] vector, the time path of the wage p_c_path = [I, T+S-1] matrix, time path of consumption goods prices p_tilde_path = [T+S-1] vector, time path of composite price c_bar = [I,T+S-1] matrix, minimum consumption values for all goods along time path n = [S,] vector, exogenous labor supply n_{s} Functions called: paths_life Objects in function: b_path = [S-1, T+S-1] matrix, distribution of savings along time path c_tilde_path = [S, T+S-1] matrix, distribution of composite consumption along time path c_path = [S, T+S-1, I] array, distribution of consumption of each cons good along time path eulerr_path = [S-1, T+S-1] matrix, Euler equation errors along the time path pl_params = length 4 tuple, parameters to pass into paths_life (S, beta, sigma, TP_tol) u = integer >= 2, represents number of periods remaining in a lifetime, used to solve incomplete lifetimes b_guess = [u-1,] vector, initial guess for remaining lifetime savings, taken from previous cohort's choices b_lf = [u-1,] vector, optimal remaining lifetime savings decisions c_tilde_lf = [u,] vector, optimal remaining lifetime composite consumption decisions c_lf = [u,I] matrix, optimal remaining lifetime invididual consumption decisions b_err_vec_lf = [u-1,] vector, Euler errors associated with optimal remaining lifetime savings decisions DiagMaskb = [u-1, u-1] boolean identity matrix DiagMaskc = [u, u] boolean identity matrix Returns: b_path, c_tilde_path, c_path, eulerr_path ''' S, T, alpha, beta, sigma, tp_tol = params b_path = np.append(Gamma1.reshape((S-1,1)), np.zeros((S-1, T+S-2)), axis=1) c_tilde_path = np.zeros((S, T+S-1)) c_path = np.zeros((S, T+S-1, I)) eulerr_path = np.zeros((S-1, T+S-1)) # Solve the incomplete remaining lifetime decisions of agents alive # in period t=1 but not born in period t=1 c_tilde_path[S-1, 0], c_tilde_cstr = firm.get_c_tilde(c_bar[:,0],r_path[0],w_path[0], p_c_path[:,0], p_tilde_path[0],n[S-1],Gamma1[S-2]) c_path[S-1, 0, :], c_cstr = firm.get_c(alpha[:,0],c_bar[:,0],c_tilde_path[S-1,0], p_c_path[:,0],p_tilde_path[0]) for u in xrange(2, S): # b_guess = b_ss[-u+1:] b_guess = np.diagonal(b_path[S-u:, :u-1]) pl_params = (S, alpha[:,:u], beta, sigma, tp_tol) b_lf, c_tilde_lf, c_lf, b_err_vec_lf = paths_life(pl_params, S-u+1, Gamma1[S-u-1], c_bar[:,:u], n[-u:], r_path[:u], w_path[:u], p_c_path[:, :u], p_tilde_path[:u], b_guess) # Insert the vector lifetime solutions diagonally (twist donut) # into the c_tilde_path, b_path, and EulErrPath matrices DiagMaskb = np.eye(u-1, dtype=bool) DiagMaskc = np.eye(u, dtype=bool) b_path[S-u:, 1:u] = DiagMaskb * b_lf + b_path[S-u:, 1:u] c_tilde_path[S-u:, :u] = DiagMaskc * c_tilde_lf + c_tilde_path[S-u:, :u] DiagMaskc_tiled = np.tile(np.expand_dims(np.eye(u, dtype=bool),axis=2),(1,1,I)) c_lf = np.tile(np.expand_dims(c_lf.transpose(),axis=0),((c_lf.shape)[1],1,1)) c_path[S-u:, :u, :] = (DiagMaskc_tiled * c_lf + c_path[S-u:, :u, :]) eulerr_path[S-u:, 1:u] = (DiagMaskb * b_err_vec_lf + eulerr_path[S-u:, 1:u]) # Solve for complete lifetime decisions of agents born in periods # 1 to T and insert the vector lifetime solutions diagonally (twist # donut) into the c_tilde_path, b_path, and EulErrPath matrices DiagMaskb = np.eye(S-1, dtype=bool) DiagMaskc = np.eye(S, dtype=bool) DiagMaskc_tiled = np.tile(np.expand_dims(np.eye(S, dtype=bool),axis=2),(1,1,I)) for t in xrange(1, T+1): # Go from periods 1 to T # b_guess = b_ss b_guess = np.diagonal(b_path[:, t-1:t+S-2]) pl_params = (S, alpha[:,t-1:t+S-1], beta, sigma, tp_tol) b_lf, c_lf, c_i_lf, b_err_vec_lf = paths_life(pl_params, 1, 0, c_bar[:,t-1:t+S-1], n, r_path[t-1:t+S-1], w_path[t-1:t+S-1], p_c_path[:, t-1:t+S-1], p_tilde_path[t-1:t+S-1], b_guess) c_i_lf = np.tile(np.expand_dims(c_i_lf.transpose(),axis=0),((c_i_lf.shape)[1],1,1)) # Insert the vector lifetime solutions diagonally (twist donut) # into the c_tilde_path, b_path, and EulErrPath matrices b_path[:, t:t+S-1] = DiagMaskb * b_lf + b_path[:, t:t+S-1] c_tilde_path[:, t-1:t+S-1] = DiagMaskc * c_lf + c_tilde_path[:, t-1:t+S-1] c_path[:, t-1:t+S-1, :] = (DiagMaskc_tiled * c_i_lf + c_path[:, t-1:t+S-1, :]) eulerr_path[:, t:t+S-1] = (DiagMaskb * b_err_vec_lf + eulerr_path[:, t:t+S-1]) return b_path, c_tilde_path, c_path, eulerr_path
def get_cbess(params, b_guess, p_c, c_bar, I, S, n): ''' Generates vectors for individual savings, composite consumption, industry-specific consumption, constraint vectors, and Euler errors for a given set of prices (r, w, p, p). Inputs: params = length 7 tuple, (alpha, beta, sigma, r, w, p ss_tol) alpha = [I,] vector, expenditure shares on each good beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r = scalar > 0, interest rate w = scalar > 0, real wage p_tilde = scalar > 0, composite good price ss_tol = scalar > 0, tolerance level for steady-state fsolve b_guess = [S-1,] vector, initial guess for savings vector p_c = [I,] vector, prices in each industry c_bar = [I,] vector, minimum consumption values for all goods n = [S,] vector, exogenous labor supply n_{s} Functions called: EulerSys_b firm.get_c_tilde firm.get_c firm.get_b_errors Objects in function: eulb_objs = length 9 tuple, objects to be passed in to EulerSys_b: (alpha, beta, sigma, r, w, p, p, c_bar, n) b = [S-1,] vector, optimal lifetime savings decisions c_tidle_params = length 3 tuple, parameters for get_c_tilde(r, w, p_tilde) c_tilde = [S,] vector, optimal lifetime consumption c_tilde_cstr = [S,] boolean vector, =True if c_s<=0 c_params = length 2 tuple, parameters for get_c (alpha, p) c = [I,S] matrix, optimal consumption of each good given prices c_cstr = [I,S] boolean matrix, =True if c_{i,s}<=0 for given c_s eul_params = length 2 tuple, parameters for get_b_errors (beta, sigma) euler_errors = [S-1,] vector, Euler errors from savings decisions Returns: b, c, c_cstr, c, c_cstr, euler_errors ''' alpha, beta, sigma, r, w, p_tilde, ss_tol = params eulb_objs = (alpha, beta, sigma, r, w, p_tilde, p_c, c_bar, I, S, n) b = opt.fsolve(EulerSys_b, b_guess, args=(eulb_objs), xtol=ss_tol) c_tilde, c_tilde_cstr = firm.get_c_tilde(c_bar, r, w, p_c, p_tilde, n, np.append([0], b)) c_params = (alpha, p_tilde) c, c_cstr = firm.get_c(np.tile(np.reshape(alpha, (I, 1)), (1, S)), np.tile(np.reshape(c_bar, (I, 1)), (1, S)), np.tile(c_tilde, (I, 1)), np.tile(np.reshape(p_c, (I, 1)), (1, S)), p_tilde) eul_params = (beta, sigma) euler_errors = firm.get_b_errors(eul_params, r, c_tilde, c_tilde_cstr, diff=True) return b, c, c_cstr, c, c_cstr, euler_errors