Example #1
0
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, p_ss, r_ss, w_ss, tp_tol) = params

    r_path = np.zeros(T+S-1)
    w_path = np.zeros(T+S-1)
    p_path = np.zeros((M,T+S-1))
    r_path[:T] = guesses[0:T].reshape(T)
    w_path[:T] = guesses[T:2*T].reshape(T)
    r_path[T:] = r_ss
    w_path[T:] = w_ss

    p_path[:,:T] = guesses[2*T:].reshape(M,T)
    p_path[:,T:] = np.ones((M,S-1))*np.tile(np.reshape(p_ss,(M,1)),(1,S-1))
    p_path = p_path/p_ss[0]

    p_k_path = get_p_k_path(p_path, xi, M)
    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[1:T+1], w_path[:T], C_path[:,:T], p_k_path[:,:T], A[:,:T], gamma[:,:T],
                            epsilon[:,:T], delta[:,:T], xi, pi, I, M)
    K_path = get_K_path(r_path[1:T+1], w_path[:T], X_path, p_k_path[:,:T], A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], M, T)
    L_path = get_L_path(r_path[1:T+1], w_path[:T], K_path, p_k_path[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], M, T)

    # Calculate the time path of firm values
    V_path = p_k_path[:,:T]*K_path

    # Check market clearing in each period
    K_market_error = b_path[:, :T].sum(axis=0) - V_path.sum(axis=0)
    L_market_error = n.sum() - L_path[:, :].sum(axis=0)

    # Check errors between guessed and implied prices
    p_params = (A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], K_ss, M, T)
    p_error  = p_path[:,:T] - get_p_path(p_params, r_path[1:T+1], w_path[:T], p_path[:,:T], p_k_path[:,:T+1], K_path, X_path)


    # X_path = get_X_path(X_params, r_path[:T], w_path[:T], C_path[:,:T], p_k_path[:,:T], A[:,:T], gamma[:,:T],
    #                         epsilon[:,:T], delta[:,:T], xi, pi, I, M)
    # K_path = get_K_path(r_path[:T], w_path[:T], X_path, p_k_path[:,:T], A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], M, T)
    # L_path = get_L_path(r_path[:T], w_path[:T], K_path, p_k_path[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], M, T)

    # # Calculate the time path of firm values
    # V_path = p_k_path[:,:T]*K_path

    # # Check market clearing in each period
    # K_market_error = b_path[:, :T].sum(axis=0) - V_path.sum(axis=0)
    # L_market_error = n.sum() - L_path[:, :].sum(axis=0)

    # # Check errors between guessed and implied prices
    # p_params = (A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], K_ss, M, T)
    # p_error  = p_path[:,:T] - get_p_path(p_params, r_path[:T], w_path[:T], p_path[:,:T], p_k_path[:,:T+1], K_path, X_path)



    # 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

    mask5 = p_path[:,:T] <= 0
    mask6 = np.isnan(p_path[:,:T])
    p_error[mask5] = 1e14
    p_error[mask6] = 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()

    print 'the max pricing error is: ', np.absolute(p_error).max()
    print 'the min pricing error is: ', np.absolute(p_error).min()

    V_alt_path = (((p_path[:,:T-1]*X_path[:,:T-1] - w_path[:T-1]*L_path[:,:T-1] - 
                            p_k_path[:,:T-1]*(K_path[:,1:T]-(1-delta[:,:T-1])*K_path[:,:T-1])) + (p_k_path[:,1:T]*K_path[:,1:T])) /(1+r_path[1:T]))
    V_path = p_k_path[:,:T]*K_path[:,:T]

    print 'the max V error is: ', np.absolute(V_alt_path-V_path[:,:T-1]).max()
    print 'the min V error is: ', np.absolute(V_alt_path-V_path[:,:T-1]).min()

    # get implied r:
    r_implied = ((p_path[:,:T]/p_k_path[:,:T])*((A[:,:T]**((epsilon[:,:T]-1)/epsilon[:,:T]))*(((gamma[:,:T]*X_path)/K_path)**(1/epsilon[:,:T]))) 
                 + (1-delta[:,:T])*(p_k_path[:,1:T+1]/p_path[:,1:T+1]) - (p_k_path[:,:T]/p_path[:,1:T+1]))


    print 'the max r error is: ', np.absolute(r_implied-r_path[:T]).max()
    print 'the min r error is: ', np.absolute(r_implied-r_path[:T]).min()

    

    errors = np.insert(np.reshape(p_error,(T*M)),0,np.append(K_market_error, L_market_error))

    return errors
Example #2
0
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
        get_p
        firm.get_p_tilde
        get_cbess
        firm.get_C
        get_K
        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, 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
        p_errors    = [M,] vector, differences between guessed and implied prices
        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 = rw_ss[0]
    w_ss = rw_ss[1]

    p_guess = np.ones(M)
    p_params = A, gamma, epsilon, delta
    p_ss = opt.fsolve(solve_p,
                      p_guess,
                      args=(p_params, r_ss, w_ss, xi),
                      xtol=ss_tol,
                      col_deriv=1)
    p_ss = p_ss / p_ss[0]  # need a normalization for prices
    p_k_ss = np.dot(xi, p_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, p_k_ss, A, gamma, epsilon, delta,
                            xi, pi, I, M),
                      xtol=ss_tol,
                      col_deriv=1)
    K_ss = get_K(r_ss, w_ss, X_ss, p_k_ss, A, gamma, epsilon, delta)
    L_ss = get_L(r_ss, w_ss, K_ss, p_k_ss, gamma, epsilon, delta)

    # Calculate firm value
    V_ss = p_k_ss * K_ss

    MCK_err_ss = V_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_ss, p_k_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)
Example #3
0
def TP(params, p_path_init, 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
        p_ss       = [M,] vector, SS output prices for each industry
        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
        p_path_init       = [M, T+S-1] matrix, time path of industry output prices
        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_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
        p_path       = [M, T+S-1] matrix, time path of industry output prices
        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, p_ss, 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_path = np.zeros((M,T+S-1))
    p_path[:,:T] = p_path_init[:,:T]
    p_path[:,T:] = np.ones((M,S-1))*np.tile(np.reshape(p_ss,(M,1)),(1,S-1))
    p_path = p_path/p_ss[0]
    p_k_path = get_p_k_path(p_path, xi, M)
    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[1:T+1], w_path[:T], C_path[:,:T], p_k_path[:,:T], A[:,:T], gamma[:,:T],
                            epsilon[:,:T], delta[:,:T], xi, pi, I, M)
    K_path = get_K_path(r_path[1:T+1], w_path[:T], X_path, p_k_path[:,:T], A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], M, T)
    L_path = get_L_path(r_path[1:T+1], w_path[:T], K_path, p_k_path[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], M, T)
    

    # Calculate the time path of firm values
    V_path = p_k_path[:,:T]*K_path

    # 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) 
    
    # Checking market clearing conditions
    MCKerr_path = b_path[:, :T].sum(axis=0) - V_path.sum(axis=0)
    MCLerr_path = n.sum() - L_path.sum(axis=0)
    p_params = (A[:,:T], gamma[:,:T], epsilon[:,:T], delta[:,:T], K_ss, M, T)
    p_err_path  = p_path[:,:T] - get_p_path(p_params, r_path[1:T+1], w_path[:T], p_path[:,:T], p_k_path[:,:T+1], K_path, X_path)

    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_k_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, p_err_path)
Example #4
0
def MCerrs(rw_vec, *objs):
    '''
    Returns capital and labor market clearing condition errors given
    particular values of r and w

    Inputs:
        rw_vec    = [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:
        solve_p
        firm.get_p_tilde
        get_cbess
        firm.get_C
        solve_X
        get_K
        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      = [M,] 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      = [I,S] matrix, optimal consumption of each good
                     given prices
        c_cstr    = [M,S] boolean matrix, =True if c_{m,s}<=0 for
                     given c_s
        euler_errors     = [S-1,] vector, Euler equations from optimal savings
        C      = [I,] vector, total consumption demand for each
                     industry
        X_params  = length 2 tuple, parameters for get_XK function:
                     (r, w)
        X      = [M,] vector, total output for each industry
        K      = [M,] vector, capital demand for each industry
        L_params  = length 2 tuple, parameters for get_Lvec function:
                     (r, w)
        Lvec      = [M,] vector, labor demand for each industry
        MCp_errs    = [2+M,] vector, capital and labor market clearing
                     errors and pricing equation errors given r, w, and p

    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 = rw_vec[0]
    w = rw_vec[1]
    if r <= 0 or w <= 0:
        MCKerr = 9999.
        MCLerr = 9999.
        MC_errs = np.array([MCKerr, MCLerr])
    elif r > 0 and w > 0:
        p_guess = np.ones(M)
        p_params = A, gamma, epsilon, delta
        p = opt.fsolve(solve_p,
                       p_guess,
                       args=(p_params, r, w, xi),
                       xtol=ss_tol,
                       col_deriv=1)
        p = p / p[0]  # need a normalization for prices
        p_k = np.dot(xi, p)
        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, p_k, A, gamma, epsilon, delta, xi,
                             pi, I, M),
                       xtol=ss_tol,
                       col_deriv=1)
        K = get_K(r, w, X, p_k, A, gamma, epsilon, delta)
        L = get_L(r, w, K, p_k, gamma, epsilon, delta)

        # Calculate firm value
        V = p_k * K

        MCKerr = V.sum() - b.sum()
        MCLerr = L.sum() - n.sum()
        MC_errs = np.array([MCKerr, MCLerr])

    return MC_errs
Example #5
0
def feasible(params, rw_init, b_guess, c_bar, A, gamma, epsilon, delta, xi, pi,
             M, I, S, n):
    '''
    Determines whether a particular guess for the steady-state values
    or r and w are feasible. Feasibility means that
    r + delta > 0, w > 0, implied c_s>0, c_{i,s}>0 for all i and
    all s and implied sum of K_{m} > 0

    Inputs:
        params   = length 5 tuple, (S, alpha, beta, sigma, ss_tol)
        S        = integer in [3,80], number of periods in a life
        alpha    = scalar in (0,1), expenditure share on good 1
        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 vector
        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
               good i that comes from the output of industry j
        n     = [S,] vector, exogenous labor supply n_{s}

    Functions called:
        firm.get_p    = generates vector of industry prices
        firm.get_p_tilde     = generates composite goods price
        get_cbess = generates savings, consumption, Euler, and
                    constraint vectors

    Objects in function:
        r          = scalar > 0, initial guess for interest rate
        w          = scalar > 0, initial guess for real wage
        GoodGuess  = boolean, =True if initial steady-state guess is
                     feasible
        r_cstr     = boolean, =True if r + delta <= 0
        w_cstr     = boolean, =True if w <= 0
        c_cstr     = [S,] boolean vector, =True if c_s<=0 for some s
        K_cstr     = boolean, =True if sum of K_{m} <= 0
        p_params  = length 4 tuple, vectors to be passed in to get_p
                     (A, gamma, epsilon, delta)
        p      = [M,] vector, output prices from each industry
        p_c      = [I,] vector, consumption goods prices from each industry
        p          = 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      = [I,S] matrix, optimal consumption of each good
                     given prices
        c_cstr    = [2,S] boolean matrix, =True if c_{i,s}<=0 for
                     given c_s
        euler_errors     = [S-1,] vector, Euler equations from optimal savings
        K_s        = scalar, sum of all savings (capital supply)

    Returns: GoodGuess, r_cstr, w_cstr, c_cstr, c_cstr, K_cstr
    '''
    S, alpha, beta, sigma, ss_tol = params
    r = rw_init[0]
    w = rw_init[1]
    GoodGuess = True
    r_cstr = False
    w_cstr = False
    c_cstr = np.zeros(S, dtype=bool)
    K_cstr = False
    if r <= 0 and w <= 0:
        r_cstr = True
        w_cstr = True
        GoodGuess = False
    elif r <= 0 and w > 0:
        r_cstr = True
        GoodGuess = False
    elif r > 0 and w <= 0:
        w_cstr = True
        GoodGuess = False
    elif r > 0 and w > 0:
        p_guess = np.ones(M)
        p_params = A, gamma, epsilon, delta
        p = opt.fsolve(solve_p,
                       p_guess,
                       args=(p_params, r, w, xi),
                       xtol=ss_tol,
                       col_deriv=1)
        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, c_cstr, c, c_cstr, euler_errors = \
            get_cbess(cbe_params, b_guess, p_c, c_bar, I, S, n)
        # Check K1 + K2
        K_s = b.sum()
        K_cstr = K_s <= 0
        if K_cstr == True or c_cstr.max() == 1 or c_cstr.max() == 1:
            GoodGuess = False

    return GoodGuess, r_cstr, w_cstr, c_cstr, c_cstr, K_cstr
Example #6
0
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
        get_p
        firm.get_p_tilde
        get_cbess
        firm.get_C
        get_K
        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, 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
        p_errors    = [M,] vector, differences between guessed and implied prices
        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 = rw_ss[0]
    w_ss = rw_ss[1]

    p_guess = np.ones(M)
    p_params =  A, gamma, epsilon, delta
    p_ss = opt.fsolve(solve_p, p_guess, args=(p_params, r_ss, w_ss, xi), xtol=ss_tol, col_deriv=1)
    p_ss = p_ss/p_ss[0] # need a normalization for prices
    p_k_ss = np.dot(xi,p_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, p_k_ss, A, gamma, epsilon, delta, xi, pi, I, M), xtol=ss_tol, col_deriv=1)
    K_ss = get_K(r_ss, w_ss, X_ss, p_k_ss, A, gamma, epsilon, delta)
    L_ss = get_L(r_ss, w_ss, K_ss, p_k_ss, gamma, epsilon, delta)

    # Calculate firm value
    V_ss = p_k_ss*K_ss

    MCK_err_ss = V_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_ss, p_k_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)
Example #7
0
def feasible(params, rw_init, b_guess, c_bar, A, gamma, epsilon,
  delta, xi, pi, M, I, S, n):
    '''
    Determines whether a particular guess for the steady-state values
    or r and w are feasible. Feasibility means that
    r + delta > 0, w > 0, implied c_s>0, c_{i,s}>0 for all i and
    all s and implied sum of K_{m} > 0

    Inputs:
        params   = length 5 tuple, (S, alpha, beta, sigma, ss_tol)
        S        = integer in [3,80], number of periods in a life
        alpha    = scalar in (0,1), expenditure share on good 1
        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 vector
        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
               good i that comes from the output of industry j
        n     = [S,] vector, exogenous labor supply n_{s}

    Functions called:
        firm.get_p    = generates vector of industry prices
        firm.get_p_tilde     = generates composite goods price
        get_cbess = generates savings, consumption, Euler, and
                    constraint vectors

    Objects in function:
        r          = scalar > 0, initial guess for interest rate
        w          = scalar > 0, initial guess for real wage
        GoodGuess  = boolean, =True if initial steady-state guess is
                     feasible
        r_cstr     = boolean, =True if r + delta <= 0
        w_cstr     = boolean, =True if w <= 0
        c_cstr     = [S,] boolean vector, =True if c_s<=0 for some s
        K_cstr     = boolean, =True if sum of K_{m} <= 0
        p_params  = length 4 tuple, vectors to be passed in to get_p
                     (A, gamma, epsilon, delta)
        p      = [M,] vector, output prices from each industry
        p_c      = [I,] vector, consumption goods prices from each industry
        p          = 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      = [I,S] matrix, optimal consumption of each good
                     given prices
        c_cstr    = [2,S] boolean matrix, =True if c_{i,s}<=0 for
                     given c_s
        euler_errors     = [S-1,] vector, Euler equations from optimal savings
        K_s        = scalar, sum of all savings (capital supply)

    Returns: GoodGuess, r_cstr, w_cstr, c_cstr, c_cstr, K_cstr
    '''
    S, alpha, beta, sigma, ss_tol = params
    r = rw_init[0]
    w = rw_init[1]
    GoodGuess = True
    r_cstr = False
    w_cstr = False
    c_cstr = np.zeros(S, dtype=bool)
    K_cstr = False
    if r <= 0 and w <= 0:
        r_cstr = True
        w_cstr = True
        GoodGuess = False
    elif r <= 0 and w > 0:
        r_cstr = True
        GoodGuess = False
    elif r > 0 and w <= 0:
        w_cstr = True
        GoodGuess = False
    elif r > 0 and w > 0:
        p_guess = np.ones(M)
        p_params =  A, gamma, epsilon, delta
        p = opt.fsolve(solve_p, p_guess, args=(p_params, r, w, xi), xtol=ss_tol, col_deriv=1)
        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, c_cstr, c, c_cstr, euler_errors = \
            get_cbess(cbe_params, b_guess, p_c, c_bar, I, S, n)
        # Check K1 + K2
        K_s = b.sum()
        K_cstr = K_s <= 0
        if K_cstr == True or c_cstr.max() == 1 or c_cstr.max() == 1:
            GoodGuess = False

    return GoodGuess, r_cstr, w_cstr, c_cstr, c_cstr, K_cstr
Example #8
0
def MCerrs(rw_vec, *objs):
    '''
    Returns capital and labor market clearing condition errors given
    particular values of r and w

    Inputs:
        rw_vec    = [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:
        solve_p
        firm.get_p_tilde
        get_cbess
        firm.get_C
        solve_X
        get_K
        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      = [M,] 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      = [I,S] matrix, optimal consumption of each good
                     given prices
        c_cstr    = [M,S] boolean matrix, =True if c_{m,s}<=0 for
                     given c_s
        euler_errors     = [S-1,] vector, Euler equations from optimal savings
        C      = [I,] vector, total consumption demand for each
                     industry
        X_params  = length 2 tuple, parameters for get_XK function:
                     (r, w)
        X      = [M,] vector, total output for each industry
        K      = [M,] vector, capital demand for each industry
        L_params  = length 2 tuple, parameters for get_Lvec function:
                     (r, w)
        Lvec      = [M,] vector, labor demand for each industry
        MCp_errs    = [2+M,] vector, capital and labor market clearing
                     errors and pricing equation errors given r, w, and p

    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 = rw_vec[0]
    w = rw_vec[1]
    if r <= 0 or w <=0 :
        MCKerr = 9999.
        MCLerr = 9999.
        MC_errs = np.array([MCKerr, MCLerr])
    elif r > 0 and w > 0:
        p_guess = np.ones(M)
        p_params =  A, gamma, epsilon, delta
        p = opt.fsolve(solve_p, p_guess, args=(p_params, r, w, xi), xtol=ss_tol, col_deriv=1)
        p= p/p[0] # need a normalization for prices
        p_k = np.dot(xi,p)
        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, p_k, A, gamma, epsilon, delta, xi, pi, I, M), xtol=ss_tol, col_deriv=1)
        K = get_K(r, w, X, p_k, A, gamma, epsilon, delta)
        L = get_L(r, w, K, p_k, gamma, epsilon, delta)

        # Calculate firm value
        V = p_k*K

        MCKerr = V.sum() - b.sum()
        MCLerr = L.sum() - n.sum()
        MC_errs = np.array([MCKerr, MCLerr])

    return MC_errs