def TP_fsolve(guesses, params, K_ss, X_ss, Gamma1, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, graphs): ''' Generates equilibrium time path for all endogenous objects from initial state (Gamma1) to the steady state using initial guesses r_path_init and w_path_init. Inputs: params = length 11 tuple, (S, T, alpha, beta, sigma, r_ss, w_ss, maxiter, mindist, xi, 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 M = integer, number unique production industires alpha = [I,T+S-1] matrix, expenditure share on each good along the time path beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r_ss = scalar > 0, steady-state interest rate w_ss = scalar > 0, steady-state wage tp_tol = scalar > 0, tolerance level for fsolve's in TP solution r_path_init = [T+S-1,] vector, initial guess for the time path of the interest rate w_path_init = [T+S-1,] vector, initial guess for the time path of the wage X_ss = [M,] vector, steady-state industry output levels Gamma1 = [S-1,] vector, initial period savings distribution c_bar = [M,T+S-1] matrix, minimum consumption values for all goods A = [M,T+S-1] matrix, total factor productivity values for all industries gamma = [M,T+S-1] matrix, capital shares of income for all industries epsilon = [M,T+S-1] matrix, elasticities of substitution between capital and labor for all industries delta = [M,T+S-1] matrix, model period depreciation rates for all industries xi = [M,M] matrix, element i,j gives the fraction of capital used by industry j that comes from the output of industry i pi = [I,M] matrix, element i,j gives the fraction of consumption n = [S,] vector, exogenous labor supply n_{s} graphs = boolean, =True if want graphs of TPI objects Functions called: firm.get_p firm.get_p_tilde get_cbepath Objects in function: start_time = scalar, current processor time in seconds (float) r_path_new = [T+S-2,] vector, new time path of the interest rate implied by household and firm optimization w_path_new = [T+S-2,] vector, new time path of the wage implied by household and firm optimization p_params = length 4 tuple, objects to be passed to get_p_path function: (A, gamma, epsilon, delta) p_path = [M, T+S-1] matrix, time path of industry output prices p_c_path = [I, T+S-1] matrix, time path of consumption good prices p_tilde_path = [T+S-1] vector, time path of composite price r_params = length 3 tuple, parameters passed in to get_r w_params = length 2 tuple, parameters passed in to get_w cbe_params = length 5 tuple. parameters passed in to get_cbepath r_path = [T+S-2,] vector, equilibrium time path of the interest rate w_path = [T+S-2,] vector, equilibrium time path of the real wage c_tilde_path = [S, T+S-2] matrix, equilibrium time path values of individual consumption c_{s,t} b_path = [S-1, T+S-2] matrix, equilibrium time path values of individual savings b_{s+1,t+1} EulErrPath = [S-1, T+S-2] matrix, equilibrium time path values of Euler errors corresponding to individual savings b_{s+1,t+1} (first column is zeros) K_path_constr = [T+S-2,] boolean vector, =True if K_t<=0 K_path = [T+S-2,] vector, equilibrium time path of the aggregate capital stock X_params = length 2 tuple, parameters to be passed to get_X X_path = [M,T+S-2] matrix, equilibrium time path of industry output C_path = [I, T+S-2] matrix, equilibrium time path of aggregate consumption Returns: b_path, c_tilde_path, w_path, r_path, K_path, X_path, Cpath, EulErr_path ''' (S, T, alpha, beta, sigma, r_ss, w_ss, tp_tol) = params r_path = np.zeros(T+S-1) w_path = np.zeros(T+S-1) r_path[:T] = guesses[0:T] w_path[:T] = guesses[T:] r_path[T:] = r_ss w_path[T:] = w_ss p_params = (A, gamma, epsilon, delta) p_path = firm.get_p(p_params, r_path, w_path) p_c_path = get_p_c_path(p_path, pi, I) p_tilde_path = firm.get_p_tilde(alpha, p_c_path) cbe_params = (S, T, alpha, beta, sigma, tp_tol) b_path, c_tilde_path, c_path, eulerr_path = get_cbepath(cbe_params, Gamma1, r_path, w_path, p_c_path, p_tilde_path, c_bar, I, n) C_path = firm.get_C(c_path[:, :T, :]) X_params = (T, K_ss) X_path = get_X_path(X_params, r_path[:T], w_path[:T], C_path[:,:T], A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], xi, pi, I, M) K_path = firm.get_K(r_path[:T], w_path[:T], X_path, A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T]) L_path = firm.get_L(r_path[:T], w_path[:T], K_path, gamma[:,:T], epsilon[:,:T], delta[:,:T]) # Check market clearing in each period K_market_error = b_path[:, :T].sum(axis=0) - K_path[:, :].sum(axis=0) L_market_error = n.sum() - L_path[:, :].sum(axis=0) # Checking resource constraint along the path: Inv_path = np.zeros((M,T)) X_inv_path = np.zeros((M,T)) X_c_path = np.zeros((M,T)) Inv_path[:,:T-1] = K_path[:,1:] - (1-delta[:,:T-1])*K_path[:,:T-1] Inv_path[:,T-1] = K_ss - (1-delta[:,T-1])*K_path[:,T-1] for t in range(0,T): X_inv_path[:,t] = np.dot(Inv_path[:,t],xi) X_c_path[:,t] = np.dot(np.reshape(C_path[:,t],(1,I)),pi) RCdiff_path = (X_path - X_c_path - X_inv_path) print 'the max RC diff is: ', np.absolute(RCdiff_path).max(axis=1) # Check and punish constraing violations mask1 = r_path[:T] <= 0 mask2 = w_path[:T] <= 0 mask3 = np.isnan(r_path[:T]) mask4 = np.isnan(w_path[:T]) K_market_error[mask1] += 1e14 L_market_error[mask2] += 1e14 K_market_error[mask3] += 1e14 L_market_error[mask4] += 1e14 print 'max capital market clearing distance: ', np.absolute(K_market_error).max() print 'max labor market clearing distance: ', np.absolute(L_market_error).max() print 'min capital market clearing distance: ', np.absolute(K_market_error).min() print 'min labor market clearing distance: ', np.absolute(L_market_error).min() errors = np.append(K_market_error, L_market_error) return errors
def SS(params, rw_init, b_guess, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, graphs): ''' Generates all endogenous steady-state objects Inputs: params = length 5 tuple, (S, alpha, beta, sigma, ss_tol) S = integer in [3,80], number of periods an individual lives alpha = [I,] vectors, expenditure share on all consumption goods beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion ss_tol = scalar > 0, tolerance level for steady-state fsolve rw_init = [2,] vector, initial guesses for steady-state r and w b_guess = [S-1,] vector, initial guess for savings to use in fsolve in get_cbess c_bar = [I,] vector, minimum consumption values for all goods A = [M,] vector, total factor productivity values for all industries gamma = [M,] vector, capital shares of income for all industries epsilon = [M,] vector, elasticities of substitution between capital and labor for all industries delta = [M,] vector, model period depreciation rates for all industries xi = [M,M] matrix, element i,j gives the fraction of capital used by industry j that comes from the output of industry i pi = [I,M] matrix, element i,j gives the fraction of consumption I = number of consumption goods n = [S,] vector, exogenous labor supply n_{s} graphs = boolean, =True if want graphs of steady-state objects Functions called: MCerrs firm.get_p firm.get_p_tilde get_cbess firm.get_C firm.get_K firm.get_L Objects in function: start_time = scalar, current processor time in seconds (float) MCerrs_objs = length 12 tuple, objects to be passed in to MCerrs function: (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, n, ss_tol) rw_ss = [2,] vector, steady-state r and w r_ss = scalar, steady-state interest rate w_ss = scalar > 0, steady-state wage p_c_params = length 4 tuple, vectors to be passed in to get_p_c (A, gamma, epsilon, delta) p_ss = [M,] vector, steady-state output prices for each industry p_c_ss = [I,] vector, steady-state consumption good prices p_tilde_ss = scalar > 0, steady-state composite good price cbe_params = length 7 tuple, parameters for get_cbess function (alpha, beta, sigma, r_ss, w_ss, p_ss, ss_tol) b_ss = [S-1,] vector, steady-state savings c_ss = [S,] vector, steady-state composite consumption c_cstr = [S,] boolean vector, =True if cbar_s<=0 for some s c_ss = [I,S] matrix, steady-state consumption of each good c_cstr = [I,S] boolean matrix, =True if c_ss{i,s}<=0 for given c_ss EulErr_ss = [S-1,] vector, steady-state Euler errors C_ss = [I,] vector, total demand for goods from each industry X_params = length 2 tuple, parameters for get_XK function: (r_ss, w_ss) X_ss = [M,] vector, steady-state total output for each industry K_ss = [M,] vector, steady-state capital demand for each industry L_params = length 2 tuple, parameters for get_Lvec function: (r_ss, w_ss) L_ss = [M,] vector, steady-state labor demand for each industry MCK_err_ss = scalar, steady-state capital market clearing error MCL_err_ss = scalar, steady-state labor market clearing error MCerr_ss = [2,] vector, steady-state capital and labor market clearing errors ss_time = scalar, time to compute SS solution (seconds) svec = [S,] vector, age-s indices from 1 to S b_ss0 = [S,] vector, age-s wealth levels including b_1=0 Returns: r_ss, w_ss, p_ss, p_ss, b_ss, c_ss, c_ss, EulErr_ss, Cm_ss, X_ss, K_ss, L_ss, MCK_err_ss, MCL_err_ss, ss_time ''' start_time = time.clock() S, alpha, beta, sigma, ss_tol = params MCerrs_objs = (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, ss_tol) rw_ss = opt.fsolve(MCerrs, rw_init, args=(MCerrs_objs), xtol=ss_tol) r_ss, w_ss = rw_ss p_params = (A, gamma, epsilon, delta) p_ss = firm.get_p(p_params, r_ss, w_ss) p_c_ss = np.dot(pi, p_ss) p_tilde_ss = firm.get_p_tilde(alpha, p_c_ss) cbe_params = (alpha, beta, sigma, r_ss, w_ss, p_tilde_ss, ss_tol) b_ss, c_tilde_ss, c_tilde_cstr, c_ss, c_cstr, EulErr_ss = \ get_cbess(cbe_params, b_guess, p_c_ss, c_bar, I, S, n) C_ss = firm.get_C(c_ss.transpose()) X_params = (r_ss, w_ss) X_init = (np.dot(np.reshape(C_ss, (1, I)), pi)) / I X_ss = opt.fsolve(solve_X, X_init, args=(X_params, C_ss, A, gamma, epsilon, delta, xi, pi, I, M), xtol=ss_tol, col_deriv=1) K_ss = firm.get_K(r_ss, w_ss, X_ss, A, gamma, epsilon, delta) L_ss = firm.get_L(r_ss, w_ss, K_ss, gamma, epsilon, delta) MCK_err_ss = K_ss.sum() - b_ss.sum() MCL_err_ss = L_ss.sum() - n.sum() MCerr_ss = np.array([MCK_err_ss, MCL_err_ss]) ss_time = time.clock() - start_time if graphs == True: # Plot steady-state distribution of savings svec = np.linspace(1, S, S) b_ss0 = np.append([0], b_ss) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(svec, b_ss0) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Steady-state distribution of savings') plt.xlabel(r'Age $s$') plt.ylabel(r'Individual savings $\bar{b}_{s}$') # plt.savefig('b_ss_Chap11') plt.show() # Plot steady-state distribution of composite consumption fig, ax = plt.subplots() plt.plot(svec, c_tilde_ss) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Steady-state distribution of composite consumption') plt.xlabel(r'Age $s$') plt.ylabel(r'Individual consumption $\tilde{c}_{s}$') # plt.savefig('c_ss_Chap11') plt.show() # Plot steady-state distribution of individual good consumption fig, ax = plt.subplots() plt.plot(svec, c_ss[0, :], 'r--', label='Good 1') plt.plot(svec, c_ss[1, :], 'b', label='Good 2') # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.legend(loc='center right') plt.title('Steady-state distribution of goods consumption') plt.xlabel(r'Age $s$') plt.ylabel(r'Individual consumption $\bar{c}_{m,s}$') # plt.savefig('c_ss_Chap11') plt.show() return (r_ss, w_ss, p_c_ss, p_tilde_ss, b_ss, c_tilde_ss, c_ss, EulErr_ss, C_ss, X_ss, K_ss, L_ss, MCK_err_ss, MCL_err_ss, ss_time)
def TP(params, r_path_init, w_path_init, K_ss, X_ss, Gamma1, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, graphs): ''' Generates equilibrium time path for all endogenous objects from initial state (Gamma1) to the steady state using initial guesses r_path_init and w_path_init. Inputs: params = length 11 tuple, (S, T, alpha, beta, sigma, r_ss, w_ss, maxiter, mindist, xi, 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 M = integer, number unique production industires alpha = [I,T+S-1] matrix, expenditure share on each good along the time path beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r_ss = scalar > 0, steady-state interest rate w_ss = scalar > 0, steady-state wage tp_tol = scalar > 0, tolerance level for fsolve's in TP solution r_path_init = [T+S-1,] vector, initial guess for the time path of the interest rate w_path_init = [T+S-1,] vector, initial guess for the time path of the wage X_ss = [M,] vector, steady-state industry output levels Gamma1 = [S-1,] vector, initial period savings distribution c_bar = [I,T+S-1] matrix, minimum consumption values for all goods A = [M,T+S-1] matrix, total factor productivity values for all industries gamma = [M,T+S-1] matrix, capital shares of income for all industries epsilon = [M,T+S-1] matrix, elasticities of substitution between capital and labor for all industries delta = [M,T+S-1] matrix, model period depreciation rates for all industries xi = [M,M] matrix, element i,j gives the fraction of capital used by industry j that comes from the output of industry i pi = [I,M] matrix, element i,j gives the fraction of consumption n = [S,] vector, exogenous labor supply n_{s} graphs = boolean, =True if want graphs of TPI objects Functions called: firm.get_p firm.get_p_tilde get_cbepath Objects in function: start_time = scalar, current processor time in seconds (float) p_params = length 4 tuple, objects to be passed to get_p_path function: (A, gamma, epsilon, delta) p_path = [M, T+S-1] matrix, time path of industry output prices p_c_path = [I, T+S-1] matrix, time path of consumption good prices p_tilde_path = [T+S-1] vector, time path of composite price r_params = length 3 tuple, parameters passed in to get_r w_params = length 2 tuple, parameters passed in to get_w cbe_params = length 5 tuple. parameters passed in to get_cbepath r_path = [T+S-2,] vector, equilibrium time path of the interest rate w_path = [T+S-2,] vector, equilibrium time path of the real wage c_tilde_path = [S, T+S-2] matrix, equilibrium time path values of individual consumption c_{s,t} b_path = [S-1, T+S-2] matrix, equilibrium time path values of individual savings b_{s+1,t+1} EulErrPath = [S-1, T+S-2] matrix, equilibrium time path values of Euler errors corresponding to individual savings b_{s+1,t+1} (first column is zeros) K_path_constr = [T+S-2,] boolean vector, =True if K_t<=0 K_path = [T+S-2,] vector, equilibrium time path of the aggregate capital stock X_params = length 2 tuple, parameters to be passed to get_X X_path = [M,T+S-2] matrix, equilibrium time path of industry output C_path = [I, T+S-2] matrix, equilibrium time path of aggregate consumption elapsed_time = scalar, time to compute TPI solution (seconds) Returns: b_path, c_tilde_path, w_path, r_path, K_path, X_path, Cpath, EulErr_path, elapsed_time ''' (S, T, alpha, beta, sigma, r_ss, w_ss, tp_tol) = params r_path = np.zeros(T+S-1) w_path = np.zeros(T+S-1) r_path[:T] = r_path_init[:T] w_path[:T] = w_path_init[:T] r_path[T:] = r_ss w_path[T:] = w_ss p_params = (A, gamma, epsilon, delta) p_path = firm.get_p(p_params, r_path, w_path) p_c_path = get_p_c_path(p_path, pi, I) p_tilde_path = firm.get_p_tilde(alpha, p_c_path) cbe_params = (S, T, alpha, beta, sigma, tp_tol) b_path, c_tilde_path, c_path, eulerr_path = get_cbepath(cbe_params, Gamma1, r_path, w_path, p_c_path, p_tilde_path, c_bar, I, n) C_path = firm.get_C(c_path[:, :T, :]) X_params = (T, K_ss) X_path = get_X_path(X_params, r_path[:T], w_path[:T], C_path[:,:T], A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], xi, pi, I, M) K_path = firm.get_K(r_path[:T], w_path[:T], X_path, A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T]) L_path = firm.get_L(r_path[:T], w_path[:T], K_path, gamma[:,:T], epsilon[:,:T], delta[:,:T]) # Checking resource constraint along the path: Inv_path = np.zeros((M,T)) X_inv_path = np.zeros((M,T)) X_c_path = np.zeros((M,T)) Inv_path[:,:T-1] = K_path[:,1:] - (1-delta[:,:T-1])*K_path[:,:T-1] Inv_path[:,T-1] = K_ss - (1-delta[:,T-1])*K_path[:,T-1] for t in range(0,T): X_inv_path[:,t] = np.dot(Inv_path[:,t],xi) X_c_path[:,t] = np.dot(np.reshape(C_path[:,t],(1,I)),pi) RCdiff_path = (X_path - X_c_path - X_inv_path) MCKerr_path = b_path[:, :T].sum(axis=0) - K_path.sum(axis=0) MCLerr_path = n.sum() - L_path.sum(axis=0) if graphs == True: # Plot time path of aggregate capital stock tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() #plt.plot(tvec, K_path[0,:T]) plt.plot(tvec, K_path[1,:T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Time path for aggregate capital stock') plt.xlabel(r'Period $t$') plt.ylabel(r'Aggregate capital $K_{t}$') # plt.savefig('Kt_Sec2') plt.show() # Plot time path of aggregate output (GDP) tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, X_path[0,:T]) plt.plot(tvec, X_path[1,:T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Time path for aggregate output (GDP)') plt.xlabel(r'Period $t$') plt.ylabel(r'Aggregate output $X_{t}$') # plt.savefig('Yt_Sec2') plt.show() # Plot time path of aggregate consumption tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, C_path[0,:T]) plt.plot(tvec, C_path[1,:T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Time path for aggregate consumption') plt.xlabel(r'Period $t$') plt.ylabel(r'Aggregate consumption $C_{t}$') # plt.savefig('Ct_Sec2') plt.show() # Plot time path of real wage tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, w_path[:T]) plt.plot(tvec, np.ones(T)*w_ss) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Time path for real wage') plt.xlabel(r'Period $t$') plt.ylabel(r'Real wage $w_{t}$') # plt.savefig('wt_Sec2') plt.show() # Plot time path of real interest rate tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, r_path[:T]) plt.plot(tvec, np.ones(T)*r_ss) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Time path for real interest rate') plt.xlabel(r'Period $t$') plt.ylabel(r'Real interest rate $r_{t}$') # plt.savefig('rt_Sec2') plt.show() # Plot time path of the differences in the resource constraint tvec = np.linspace(1, T-1, T-1) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, ResmDiff[0,:T-1]) plt.plot(tvec, ResmDiff[1,:T-1]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Time path for resource constraint') plt.xlabel(r'Period $t$') plt.ylabel(r'RC Difference') # plt.savefig('wt_Sec2') plt.show() # Plot time path of the differences in the market clearing conditions tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, MCKerr_path[:T]) plt.plot(tvec, MCLerr_path[:T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Time path for resource constraint') plt.xlabel(r'Period $t$') plt.ylabel(r'RC Difference') # plt.savefig('wt_Sec2') plt.show() # Plot time path of individual savings distribution tgrid = np.linspace(1, T, T) sgrid = np.linspace(2, S, S - 1) tmat, smat = np.meshgrid(tgrid, sgrid) cmap_bp = matplotlib.cm.get_cmap('summer') fig = plt.figure() ax = fig.gca(projection='3d') ax.set_xlabel(r'period-$t$') ax.set_ylabel(r'age-$s$') ax.set_zlabel(r'individual savings $b_{s,t}$') strideval = max(int(1), int(round(S/10))) ax.plot_surface(tmat, smat, b_path[:, :T], rstride=strideval, cstride=strideval, cmap=cmap_bp) # plt.savefig('b_path') plt.show() # Plot time path of individual savings distribution tgrid = np.linspace(1, T-1, T-1) sgrid = np.linspace(1, S, S) tmat, smat = np.meshgrid(tgrid, sgrid) cmap_cp = matplotlib.cm.get_cmap('summer') fig = plt.figure() ax = fig.gca(projection='3d') ax.set_xlabel(r'period-$t$') ax.set_ylabel(r'age-$s$') ax.set_zlabel(r'individual consumption $c_{s,t}$') strideval = max(int(1), int(round(S/10))) ax.plot_surface(tmat, smat, c_tilde_path[:, :T-1], rstride=strideval, cstride=strideval, cmap=cmap_cp) # plt.savefig('b_path') plt.show() return (r_path, w_path, p_path, p_tilde_path, b_path, c_tilde_path, c_path, eulerr_path, C_path, X_path, K_path, L_path, MCKerr_path, MCLerr_path, RCdiff_path)
def MCerrs(rwvec, *objs): ''' Returns capital and labor market clearing condition errors given particular values of r and w Inputs: rwvec = [2,] vector, given values of r and w objs = length 12 tuple, (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, n, ss_tol) S = integer in [3,80], number of periods an individual lives alpha = [I,] vector, expenditure shares on all consumption goods beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion b_guess = [S-1,] vector, initial guess for savings to use in fsolve in get_cbess c_bar = [I,] vector, minimum consumption values for all goods A = [I,] vector, total factor productivity values for all industries gamma = [I,] vector, capital shares of income for all industries epsilon = [I,] vector, elasticities of substitution between capital and labor for all industries delta = [I,] vector, model period depreciation rates for all industries n = [S,] vector, exogenous labor supply n_{s} ss_tol = scalar > 0, tolerance level for steady-state fsolve Functions called: firm.get_p firm.get_p_tilde get_cbess firm.get_C firm.get_K firm.get_L Objects in function: r = scalar > 0, interest rate w = scalar > 0, real wage MCKerr = scalar, error in capital market clearing condition given r and w MCLerr = scalar, error in labor market clearing condition given r and w p_c_params = length 4 tuple, vectors to be passed in to get_p_c (A, gamma, epsilon, delta) p = [2,] vector, prices in each industry p_tilde = scalar > 0, composite good price cbe_params = length 7 tuple, parameters for get_cbess function (alpha, beta, sigma, r, w, p, ss_tol) b = [S-1,] vector, optimal savings given prices c = [S,] vector, optimal composite consumption given prices c_cstr = [S,] boolean vector, =True if c_s<=0 for some s c = [2,S] matrix, optimal consumption of each good given prices c_cstr = [2,S] boolean matrix, =True if c_{m,s}<=0 for given c_s euler_errors = [S-1,] vector, Euler equations from optimal savings C = [2,] vector, total consumption demand for each industry X_params = length 2 tuple, parameters for get_XK function: (r, w) X = [2,] vector, total output for each industry K = [2,] vector, capital demand for each industry L_params = length 2 tuple, parameters for get_Lvec function: (r, w) Lvec = [2,] vector, labor demand for each industry MC_errs = [2,] vector, capital and labor market clearing errors given r ans w Returns: MC_errs ''' (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, ss_tol) = objs r, w = rwvec if (r + delta).min() <= 0 or w <= 0: MCKerr = 9999. MCLerr = 9999. MC_errs = np.array((MCKerr, MCLerr)) elif (r + delta).min() > 0 and w > 0: p_params = (A, gamma, epsilon, delta) p = firm.get_p(p_params, r, w) p_c = np.dot(pi, p) p_tilde = firm.get_p_tilde(alpha, p_c) cbe_params = (alpha, beta, sigma, r, w, p_tilde, ss_tol) b, c_tilde, c_tilde_cstr, c, c_cstr, euler_errors = \ get_cbess(cbe_params, b_guess, p_c, c_bar, I, S, n) C = firm.get_C(c.transpose()) X_params = (r, w) X_init = (np.dot(np.reshape(C, (1, I)), pi)) / I X = opt.fsolve(solve_X, X_init, args=(X_params, C, A, gamma, epsilon, delta, xi, pi, I, M), xtol=ss_tol, col_deriv=1) K = firm.get_K(r, w, X, A, gamma, epsilon, delta) L = firm.get_L(r, w, K, gamma, epsilon, delta) MCKerr = K.sum() - b.sum() MCLerr = L.sum() - n.sum() MC_errs = np.array((MCKerr, MCLerr)) return MC_errs
def TP_fsolve(guesses, params, K_ss, X_ss, Gamma1, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, graphs): ''' Generates equilibrium time path for all endogenous objects from initial state (Gamma1) to the steady state using initial guesses r_path_init and w_path_init. Inputs: params = length 11 tuple, (S, T, alpha, beta, sigma, r_ss, w_ss, maxiter, mindist, xi, 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 M = integer, number unique production industires alpha = [I,T+S-1] matrix, expenditure share on each good along the time path beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r_ss = scalar > 0, steady-state interest rate w_ss = scalar > 0, steady-state wage tp_tol = scalar > 0, tolerance level for fsolve's in TP solution r_path_init = [T+S-1,] vector, initial guess for the time path of the interest rate w_path_init = [T+S-1,] vector, initial guess for the time path of the wage X_ss = [M,] vector, steady-state industry output levels Gamma1 = [S-1,] vector, initial period savings distribution c_bar = [M,T+S-1] matrix, minimum consumption values for all goods A = [M,T+S-1] matrix, total factor productivity values for all industries gamma = [M,T+S-1] matrix, capital shares of income for all industries epsilon = [M,T+S-1] matrix, elasticities of substitution between capital and labor for all industries delta = [M,T+S-1] matrix, model period depreciation rates for all industries xi = [M,M] matrix, element i,j gives the fraction of capital used by industry j that comes from the output of industry i pi = [I,M] matrix, element i,j gives the fraction of consumption n = [S,] vector, exogenous labor supply n_{s} graphs = boolean, =True if want graphs of TPI objects Functions called: firm.get_p firm.get_p_tilde get_cbepath Objects in function: start_time = scalar, current processor time in seconds (float) r_path_new = [T+S-2,] vector, new time path of the interest rate implied by household and firm optimization w_path_new = [T+S-2,] vector, new time path of the wage implied by household and firm optimization p_params = length 4 tuple, objects to be passed to get_p_path function: (A, gamma, epsilon, delta) p_path = [M, T+S-1] matrix, time path of industry output prices p_c_path = [I, T+S-1] matrix, time path of consumption good prices p_tilde_path = [T+S-1] vector, time path of composite price r_params = length 3 tuple, parameters passed in to get_r w_params = length 2 tuple, parameters passed in to get_w cbe_params = length 5 tuple. parameters passed in to get_cbepath r_path = [T+S-2,] vector, equilibrium time path of the interest rate w_path = [T+S-2,] vector, equilibrium time path of the real wage c_tilde_path = [S, T+S-2] matrix, equilibrium time path values of individual consumption c_{s,t} b_path = [S-1, T+S-2] matrix, equilibrium time path values of individual savings b_{s+1,t+1} EulErrPath = [S-1, T+S-2] matrix, equilibrium time path values of Euler errors corresponding to individual savings b_{s+1,t+1} (first column is zeros) K_path_constr = [T+S-2,] boolean vector, =True if K_t<=0 K_path = [T+S-2,] vector, equilibrium time path of the aggregate capital stock X_params = length 2 tuple, parameters to be passed to get_X X_path = [M,T+S-2] matrix, equilibrium time path of industry output C_path = [I, T+S-2] matrix, equilibrium time path of aggregate consumption Returns: b_path, c_tilde_path, w_path, r_path, K_path, X_path, Cpath, EulErr_path ''' (S, T, alpha, beta, sigma, r_ss, w_ss, tp_tol) = params r_path = np.zeros(T + S - 1) w_path = np.zeros(T + S - 1) r_path[:T] = guesses[0:T] w_path[:T] = guesses[T:] r_path[T:] = r_ss w_path[T:] = w_ss p_params = (A, gamma, epsilon, delta) p_path = firm.get_p(p_params, r_path, w_path) p_c_path = get_p_c_path(p_path, pi, I) p_tilde_path = firm.get_p_tilde(alpha, p_c_path) cbe_params = (S, T, alpha, beta, sigma, tp_tol) b_path, c_tilde_path, c_path, eulerr_path = get_cbepath( cbe_params, Gamma1, r_path, w_path, p_c_path, p_tilde_path, c_bar, I, n) C_path = firm.get_C(c_path[:, :T, :]) X_params = (T, K_ss) X_path = get_X_path(X_params, r_path[:T], w_path[:T], C_path[:, :T], A[:, :T], gamma[:, :T], epsilon[:, :T], delta[:, :T], xi, pi, I, M) K_path = firm.get_K(r_path[:T], w_path[:T], X_path, A[:, :T], gamma[:, :T], epsilon[:, :T], delta[:, :T]) L_path = firm.get_L(r_path[:T], w_path[:T], K_path, gamma[:, :T], epsilon[:, :T], delta[:, :T]) # Check market clearing in each period K_market_error = b_path[:, :T].sum(axis=0) - K_path[:, :].sum(axis=0) L_market_error = n.sum() - L_path[:, :].sum(axis=0) # Checking resource constraint along the path: Inv_path = np.zeros((M, T)) X_inv_path = np.zeros((M, T)) X_c_path = np.zeros((M, T)) Inv_path[:, :T - 1] = K_path[:, 1:] - (1 - delta[:, :T - 1]) * K_path[:, :T - 1] Inv_path[:, T - 1] = K_ss - (1 - delta[:, T - 1]) * K_path[:, T - 1] for t in range(0, T): X_inv_path[:, t] = np.dot(Inv_path[:, t], xi) X_c_path[:, t] = np.dot(np.reshape(C_path[:, t], (1, I)), pi) RCdiff_path = (X_path - X_c_path - X_inv_path) print('the max RC diff is: ', np.absolute(RCdiff_path).max(axis=1)) # Check and punish constraing violations mask1 = r_path[:T] <= 0 mask2 = w_path[:T] <= 0 mask3 = np.isnan(r_path[:T]) mask4 = np.isnan(w_path[:T]) K_market_error[mask1] += 1e14 L_market_error[mask2] += 1e14 K_market_error[mask3] += 1e14 L_market_error[mask4] += 1e14 print('max capital market clearing distance: ', np.absolute(K_market_error).max()) print('max labor market clearing distance: ', np.absolute(L_market_error).max()) print('min capital market clearing distance: ', np.absolute(K_market_error).min()) print('min labor market clearing distance: ', np.absolute(L_market_error).min()) errors = np.append(K_market_error, L_market_error) return errors
def TP(params, r_path_init, w_path_init, K_ss, X_ss, Gamma1, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, graphs): ''' Generates equilibrium time path for all endogenous objects from initial state (Gamma1) to the steady state using initial guesses r_path_init and w_path_init. Inputs: params = length 11 tuple, (S, T, alpha, beta, sigma, r_ss, w_ss, maxiter, mindist, xi, 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 M = integer, number unique production industires alpha = [I,T+S-1] matrix, expenditure share on each good along the time path beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion r_ss = scalar > 0, steady-state interest rate w_ss = scalar > 0, steady-state wage tp_tol = scalar > 0, tolerance level for fsolve's in TP solution r_path_init = [T+S-1,] vector, initial guess for the time path of the interest rate w_path_init = [T+S-1,] vector, initial guess for the time path of the wage X_ss = [M,] vector, steady-state industry output levels Gamma1 = [S-1,] vector, initial period savings distribution c_bar = [I,T+S-1] matrix, minimum consumption values for all goods A = [M,T+S-1] matrix, total factor productivity values for all industries gamma = [M,T+S-1] matrix, capital shares of income for all industries epsilon = [M,T+S-1] matrix, elasticities of substitution between capital and labor for all industries delta = [M,T+S-1] matrix, model period depreciation rates for all industries xi = [M,M] matrix, element i,j gives the fraction of capital used by industry j that comes from the output of industry i pi = [I,M] matrix, element i,j gives the fraction of consumption n = [S,] vector, exogenous labor supply n_{s} graphs = boolean, =True if want graphs of TPI objects Functions called: firm.get_p firm.get_p_tilde get_cbepath Objects in function: start_time = scalar, current processor time in seconds (float) p_params = length 4 tuple, objects to be passed to get_p_path function: (A, gamma, epsilon, delta) p_path = [M, T+S-1] matrix, time path of industry output prices p_c_path = [I, T+S-1] matrix, time path of consumption good prices p_tilde_path = [T+S-1] vector, time path of composite price r_params = length 3 tuple, parameters passed in to get_r w_params = length 2 tuple, parameters passed in to get_w cbe_params = length 5 tuple. parameters passed in to get_cbepath r_path = [T+S-2,] vector, equilibrium time path of the interest rate w_path = [T+S-2,] vector, equilibrium time path of the real wage c_tilde_path = [S, T+S-2] matrix, equilibrium time path values of individual consumption c_{s,t} b_path = [S-1, T+S-2] matrix, equilibrium time path values of individual savings b_{s+1,t+1} EulErrPath = [S-1, T+S-2] matrix, equilibrium time path values of Euler errors corresponding to individual savings b_{s+1,t+1} (first column is zeros) K_path_constr = [T+S-2,] boolean vector, =True if K_t<=0 K_path = [T+S-2,] vector, equilibrium time path of the aggregate capital stock X_params = length 2 tuple, parameters to be passed to get_X X_path = [M,T+S-2] matrix, equilibrium time path of industry output C_path = [I, T+S-2] matrix, equilibrium time path of aggregate consumption elapsed_time = scalar, time to compute TPI solution (seconds) Returns: b_path, c_tilde_path, w_path, r_path, K_path, X_path, Cpath, EulErr_path, elapsed_time ''' (S, T, alpha, beta, sigma, r_ss, w_ss, tp_tol) = params r_path = np.zeros(T + S - 1) w_path = np.zeros(T + S - 1) r_path[:T] = r_path_init[:T] w_path[:T] = w_path_init[:T] r_path[T:] = r_ss w_path[T:] = w_ss p_params = (A, gamma, epsilon, delta) p_path = firm.get_p(p_params, r_path, w_path) p_c_path = get_p_c_path(p_path, pi, I) p_tilde_path = firm.get_p_tilde(alpha, p_c_path) cbe_params = (S, T, alpha, beta, sigma, tp_tol) b_path, c_tilde_path, c_path, eulerr_path = get_cbepath( cbe_params, Gamma1, r_path, w_path, p_c_path, p_tilde_path, c_bar, I, n) C_path = firm.get_C(c_path[:, :T, :]) X_params = (T, K_ss) X_path = get_X_path(X_params, r_path[:T], w_path[:T], C_path[:, :T], A[:, :T], gamma[:, :T], epsilon[:, :T], delta[:, :T], xi, pi, I, M) K_path = firm.get_K(r_path[:T], w_path[:T], X_path, A[:, :T], gamma[:, :T], epsilon[:, :T], delta[:, :T]) L_path = firm.get_L(r_path[:T], w_path[:T], K_path, gamma[:, :T], epsilon[:, :T], delta[:, :T]) # Checking resource constraint along the path: Inv_path = np.zeros((M, T)) X_inv_path = np.zeros((M, T)) X_c_path = np.zeros((M, T)) Inv_path[:, :T - 1] = K_path[:, 1:] - (1 - delta[:, :T - 1]) * K_path[:, :T - 1] Inv_path[:, T - 1] = K_ss - (1 - delta[:, T - 1]) * K_path[:, T - 1] for t in range(0, T): X_inv_path[:, t] = np.dot(Inv_path[:, t], xi) X_c_path[:, t] = np.dot(np.reshape(C_path[:, t], (1, I)), pi) RCdiff_path = (X_path - X_c_path - X_inv_path) MCKerr_path = b_path[:, :T].sum(axis=0) - K_path.sum(axis=0) MCLerr_path = n.sum() - L_path.sum(axis=0) if graphs == True: # Plot time path of aggregate capital stock tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() #plt.plot(tvec, K_path[0,:T]) plt.plot(tvec, K_path[1, :T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Time path for aggregate capital stock') plt.xlabel(r'Period $t$') plt.ylabel(r'Aggregate capital $K_{t}$') # plt.savefig('Kt_Sec2') plt.show() # Plot time path of aggregate output (GDP) tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, X_path[0, :T]) plt.plot(tvec, X_path[1, :T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Time path for aggregate output (GDP)') plt.xlabel(r'Period $t$') plt.ylabel(r'Aggregate output $X_{t}$') # plt.savefig('Yt_Sec2') plt.show() # Plot time path of aggregate consumption tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, C_path[0, :T]) plt.plot(tvec, C_path[1, :T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Time path for aggregate consumption') plt.xlabel(r'Period $t$') plt.ylabel(r'Aggregate consumption $C_{t}$') # plt.savefig('Ct_Sec2') plt.show() # Plot time path of real wage tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, w_path[:T]) plt.plot(tvec, np.ones(T) * w_ss) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Time path for real wage') plt.xlabel(r'Period $t$') plt.ylabel(r'Real wage $w_{t}$') # plt.savefig('wt_Sec2') plt.show() # Plot time path of real interest rate tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, r_path[:T]) plt.plot(tvec, np.ones(T) * r_ss) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Time path for real interest rate') plt.xlabel(r'Period $t$') plt.ylabel(r'Real interest rate $r_{t}$') # plt.savefig('rt_Sec2') plt.show() # Plot time path of the differences in the resource constraint tvec = np.linspace(1, T - 1, T - 1) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, ResmDiff[0, :T - 1]) plt.plot(tvec, ResmDiff[1, :T - 1]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Time path for resource constraint') plt.xlabel(r'Period $t$') plt.ylabel(r'RC Difference') # plt.savefig('wt_Sec2') plt.show() # Plot time path of the differences in the market clearing conditions tvec = np.linspace(1, T, T) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(tvec, MCKerr_path[:T]) plt.plot(tvec, MCLerr_path[:T]) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65', linestyle='-') plt.title('Time path for resource constraint') plt.xlabel(r'Period $t$') plt.ylabel(r'RC Difference') # plt.savefig('wt_Sec2') plt.show() # Plot time path of individual savings distribution tgrid = np.linspace(1, T, T) sgrid = np.linspace(2, S, S - 1) tmat, smat = np.meshgrid(tgrid, sgrid) cmap_bp = matplotlib.cm.get_cmap('summer') fig = plt.figure() ax = fig.gca(projection='3d') ax.set_xlabel(r'period-$t$') ax.set_ylabel(r'age-$s$') ax.set_zlabel(r'individual savings $b_{s,t}$') strideval = max(int(1), int(round(S / 10))) ax.plot_surface(tmat, smat, b_path[:, :T], rstride=strideval, cstride=strideval, cmap=cmap_bp) # plt.savefig('b_path') plt.show() # Plot time path of individual savings distribution tgrid = np.linspace(1, T - 1, T - 1) sgrid = np.linspace(1, S, S) tmat, smat = np.meshgrid(tgrid, sgrid) cmap_cp = matplotlib.cm.get_cmap('summer') fig = plt.figure() ax = fig.gca(projection='3d') ax.set_xlabel(r'period-$t$') ax.set_ylabel(r'age-$s$') ax.set_zlabel(r'individual consumption $c_{s,t}$') strideval = max(int(1), int(round(S / 10))) ax.plot_surface(tmat, smat, c_tilde_path[:, :T - 1], rstride=strideval, cstride=strideval, cmap=cmap_cp) # plt.savefig('b_path') plt.show() return (r_path, w_path, p_path, p_tilde_path, b_path, c_tilde_path, c_path, eulerr_path, C_path, X_path, K_path, L_path, MCKerr_path, MCLerr_path, RCdiff_path)
def SS(params, rw_init, b_guess, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, graphs): ''' Generates all endogenous steady-state objects Inputs: params = length 5 tuple, (S, alpha, beta, sigma, ss_tol) S = integer in [3,80], number of periods an individual lives alpha = [I,] vectors, expenditure share on all consumption goods beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion ss_tol = scalar > 0, tolerance level for steady-state fsolve rw_init = [2,] vector, initial guesses for steady-state r and w b_guess = [S-1,] vector, initial guess for savings to use in fsolve in get_cbess c_bar = [I,] vector, minimum consumption values for all goods A = [M,] vector, total factor productivity values for all industries gamma = [M,] vector, capital shares of income for all industries epsilon = [M,] vector, elasticities of substitution between capital and labor for all industries delta = [M,] vector, model period depreciation rates for all industries xi = [M,M] matrix, element i,j gives the fraction of capital used by industry j that comes from the output of industry i pi = [I,M] matrix, element i,j gives the fraction of consumption I = number of consumption goods n = [S,] vector, exogenous labor supply n_{s} graphs = boolean, =True if want graphs of steady-state objects Functions called: MCerrs firm.get_p firm.get_p_tilde get_cbess firm.get_C firm.get_K firm.get_L Objects in function: start_time = scalar, current processor time in seconds (float) MCerrs_objs = length 12 tuple, objects to be passed in to MCerrs function: (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, n, ss_tol) rw_ss = [2,] vector, steady-state r and w r_ss = scalar, steady-state interest rate w_ss = scalar > 0, steady-state wage p_c_params = length 4 tuple, vectors to be passed in to get_p_c (A, gamma, epsilon, delta) p_ss = [M,] vector, steady-state output prices for each industry p_c_ss = [I,] vector, steady-state consumption good prices p_tilde_ss = scalar > 0, steady-state composite good price cbe_params = length 7 tuple, parameters for get_cbess function (alpha, beta, sigma, r_ss, w_ss, p_ss, ss_tol) b_ss = [S-1,] vector, steady-state savings c_ss = [S,] vector, steady-state composite consumption c_cstr = [S,] boolean vector, =True if cbar_s<=0 for some s c_ss = [I,S] matrix, steady-state consumption of each good c_cstr = [I,S] boolean matrix, =True if c_ss{i,s}<=0 for given c_ss EulErr_ss = [S-1,] vector, steady-state Euler errors C_ss = [I,] vector, total demand for goods from each industry X_params = length 2 tuple, parameters for get_XK function: (r_ss, w_ss) X_ss = [M,] vector, steady-state total output for each industry K_ss = [M,] vector, steady-state capital demand for each industry L_params = length 2 tuple, parameters for get_Lvec function: (r_ss, w_ss) L_ss = [M,] vector, steady-state labor demand for each industry MCK_err_ss = scalar, steady-state capital market clearing error MCL_err_ss = scalar, steady-state labor market clearing error MCerr_ss = [2,] vector, steady-state capital and labor market clearing errors ss_time = scalar, time to compute SS solution (seconds) svec = [S,] vector, age-s indices from 1 to S b_ss0 = [S,] vector, age-s wealth levels including b_1=0 Returns: r_ss, w_ss, p_ss, p_ss, b_ss, c_ss, c_ss, EulErr_ss, Cm_ss, X_ss, K_ss, L_ss, MCK_err_ss, MCL_err_ss, ss_time ''' start_time = time.clock() S, alpha, beta, sigma, ss_tol = params MCerrs_objs = (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, ss_tol) rw_ss = opt.fsolve(MCerrs, rw_init, args=(MCerrs_objs), xtol=ss_tol) r_ss, w_ss = rw_ss p_params = (A, gamma, epsilon, delta) p_ss = firm.get_p(p_params, r_ss, w_ss) p_c_ss = np.dot(pi,p_ss) p_tilde_ss = firm.get_p_tilde(alpha, p_c_ss) cbe_params = (alpha, beta, sigma, r_ss, w_ss, p_tilde_ss, ss_tol) b_ss, c_tilde_ss, c_tilde_cstr, c_ss, c_cstr, EulErr_ss = \ get_cbess(cbe_params, b_guess, p_c_ss, c_bar, I, S, n) C_ss = firm.get_C(c_ss.transpose()) X_params = (r_ss, w_ss) X_init = (np.dot(np.reshape(C_ss,(1,I)),pi))/I X_ss = opt.fsolve(solve_X, X_init, args=(X_params, C_ss, A, gamma, epsilon, delta, xi, pi, I, M), xtol=ss_tol, col_deriv=1) K_ss = firm.get_K(r_ss, w_ss, X_ss, A, gamma, epsilon, delta) L_ss = firm.get_L(r_ss, w_ss, K_ss, gamma, epsilon, delta) MCK_err_ss = K_ss.sum() - b_ss.sum() MCL_err_ss = L_ss.sum() - n.sum() MCerr_ss = np.array([MCK_err_ss, MCL_err_ss]) ss_time = time.clock() - start_time if graphs == True: # Plot steady-state distribution of savings svec = np.linspace(1, S, S) b_ss0 = np.append([0], b_ss) minorLocator = MultipleLocator(1) fig, ax = plt.subplots() plt.plot(svec, b_ss0) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Steady-state distribution of savings') plt.xlabel(r'Age $s$') plt.ylabel(r'Individual savings $\bar{b}_{s}$') # plt.savefig('b_ss_Chap11') plt.show() # Plot steady-state distribution of composite consumption fig, ax = plt.subplots() plt.plot(svec, c_tilde_ss) # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.title('Steady-state distribution of composite consumption') plt.xlabel(r'Age $s$') plt.ylabel(r'Individual consumption $\tilde{c}_{s}$') # plt.savefig('c_ss_Chap11') plt.show() # Plot steady-state distribution of individual good consumption fig, ax = plt.subplots() plt.plot(svec, c_ss[0,:], 'r--', label='Good 1') plt.plot(svec, c_ss[1,:], 'b', label='Good 2') # for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator) plt.grid(b=True, which='major', color='0.65',linestyle='-') plt.legend(loc='center right') plt.title('Steady-state distribution of goods consumption') plt.xlabel(r'Age $s$') plt.ylabel(r'Individual consumption $\bar{c}_{m,s}$') # plt.savefig('c_ss_Chap11') plt.show() return (r_ss, w_ss, p_c_ss, p_tilde_ss, b_ss, c_tilde_ss, c_ss, EulErr_ss, C_ss, X_ss, K_ss, L_ss, MCK_err_ss, MCL_err_ss, ss_time)
def MCerrs(rwvec, *objs): ''' Returns capital and labor market clearing condition errors given particular values of r and w Inputs: rwvec = [2,] vector, given values of r and w objs = length 12 tuple, (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, n, ss_tol) S = integer in [3,80], number of periods an individual lives alpha = [I,] vector, expenditure shares on all consumption goods beta = scalar in [0,1), discount factor for each model period sigma = scalar > 0, coefficient of relative risk aversion b_guess = [S-1,] vector, initial guess for savings to use in fsolve in get_cbess c_bar = [I,] vector, minimum consumption values for all goods A = [I,] vector, total factor productivity values for all industries gamma = [I,] vector, capital shares of income for all industries epsilon = [I,] vector, elasticities of substitution between capital and labor for all industries delta = [I,] vector, model period depreciation rates for all industries n = [S,] vector, exogenous labor supply n_{s} ss_tol = scalar > 0, tolerance level for steady-state fsolve Functions called: firm.get_p firm.get_p_tilde get_cbess firm.get_C firm.get_K firm.get_L Objects in function: r = scalar > 0, interest rate w = scalar > 0, real wage MCKerr = scalar, error in capital market clearing condition given r and w MCLerr = scalar, error in labor market clearing condition given r and w p_c_params = length 4 tuple, vectors to be passed in to get_p_c (A, gamma, epsilon, delta) p = [2,] vector, prices in each industry p_tilde = scalar > 0, composite good price cbe_params = length 7 tuple, parameters for get_cbess function (alpha, beta, sigma, r, w, p, ss_tol) b = [S-1,] vector, optimal savings given prices c = [S,] vector, optimal composite consumption given prices c_cstr = [S,] boolean vector, =True if c_s<=0 for some s c = [2,S] matrix, optimal consumption of each good given prices c_cstr = [2,S] boolean matrix, =True if c_{m,s}<=0 for given c_s euler_errors = [S-1,] vector, Euler equations from optimal savings C = [2,] vector, total consumption demand for each industry X_params = length 2 tuple, parameters for get_XK function: (r, w) X = [2,] vector, total output for each industry K = [2,] vector, capital demand for each industry L_params = length 2 tuple, parameters for get_Lvec function: (r, w) Lvec = [2,] vector, labor demand for each industry MC_errs = [2,] vector, capital and labor market clearing errors given r ans w Returns: MC_errs ''' (S, alpha, beta, sigma, b_guess, c_bar, A, gamma, epsilon, delta, xi, pi, I, M, S, n, ss_tol) = objs r, w = rwvec if (r + delta).min() <= 0 or w <=0: MCKerr = 9999. MCLerr = 9999. MC_errs = np.array((MCKerr, MCLerr)) elif (r + delta).min() > 0 and w > 0: p_params = (A, gamma, epsilon, delta) p = firm.get_p(p_params, r, w) p_c = np.dot(pi,p) p_tilde = firm.get_p_tilde(alpha, p_c) cbe_params = (alpha, beta, sigma, r, w, p_tilde, ss_tol) b, c_tilde, c_tilde_cstr, c, c_cstr, euler_errors = \ get_cbess(cbe_params, b_guess, p_c, c_bar, I, S, n) C = firm.get_C(c.transpose()) X_params = (r, w) X_init = (np.dot(np.reshape(C,(1,I)),pi))/I X = opt.fsolve(solve_X, X_init, args=(X_params, C, A, gamma, epsilon, delta, xi, pi, I, M), xtol=ss_tol, col_deriv=1) K = firm.get_K(r, w, X, A, gamma, epsilon, delta) L = firm.get_L(r, w, K, gamma, epsilon, delta) MCKerr = K.sum() - b.sum() MCLerr = L.sum() - n.sum() MC_errs = np.array((MCKerr, MCLerr)) return MC_errs