Exemplo n.º 1
0
def Qfunc(theta):
    
    '''
    This is a function that calculates the distance between simulated moments 
    and the actual moments for the setup used for table 2
    '''
    
    import kgrid
    import zgrid
    import z_markov
    import simulate as sim
    import VFI
    
    # getting the grid space for capital
    kgrid, sizek = kgrid.main(params,w,a_k)

    # getting the grid space for shocks
    z_grid, pi = zgrid.main(sigma, mu, rho, sizez)

    # getting firm's optimal decision rule
    V, PF = VFI.main(theta, params, z_grid, kgrid, sizek, sizez, 
                                               pi, a_k, w, psi)
    
    # simulate the data 
    k_sim, I_sim, V_sim, pi_sim = sim.main(theta, params, kgrid, 
                                               pi, V, PF, N, T, z_grid)

   
    # define variables of interest
    I_K = (I_sim/k_sim).reshape((N*T, 1))
    Qav = (V_sim/k_sim).reshape((N*T, 1))
    PI_K = (pi_sim/k_sim).reshape((N*T, 1))
    
    # get the necessary statistics 
    I_Kr = I_K.reshape((1, N*T))
    sc = np.corrcoef(I_Kr[0][1:], I_Kr[0][:N*T-1])[0,1]
    sd = np.std(PI_K)
    qbar = V_sim.sum() / k_sim.sum()

    # run the q-regression
    Y = np.matrix(I_K)
    X = np.matrix(np.concatenate((np.ones((N*T, 1)), Qav, PI_K), axis = 1))
    a0, a1, a2 = np.linalg.inv(X.T * X) * X.T * Y 

    # construct the vectors
    mu_sim = np.array((a1[0, 0], a2[0, 0], sc, sd, qbar))
    mu_real = np.array((.03, .24, .4, .25, 3))
    
    # identity matrix
    Wmat = np.eye(len(mu_sim))

    # get the distance between simulated statistics and the actual statistics
    dist = np.matrix((mu_real - mu_sim)) * np.linalg.inv(Wmat) * np.matrix((mu_real - mu_sim)).T

    return dist
Exemplo n.º 2
0
def get_sim(theta):
    
    '''
    This is a function that returns the simulated moments 
    to be used to obtain the standard errors
    '''
    
    import kgrid
    import zgrid
    import z_markov
    import simulate as sim
    import VFI
    
    # getting the grid space for capital
    kgrid, sizek = kgrid.main(params,w,a_k)

    # getting the grid space for shocks
    z_grid, pi = zgrid.main(sigma, mu, rho, sizez)

    # getting firm's optimal decision rule
    V, PF = VFI.main(theta, params, z_grid, kgrid, sizek, sizez, 
                                               pi, a_k, w, psi)
    
    # simulate the data 
    k_sim, I_sim, V_sim, pi_sim = sim.main(theta, params, kgrid, 
                                               pi, V, PF, N, T, z_grid)

   
    # define variables of interest
    I_K = (I_sim/k_sim).reshape((N*T, 1))
    Qav = (V_sim/k_sim).reshape((N*T, 1))
    PI_K = (pi_sim/k_sim).reshape((N*T, 1))
    
    # get the necessary statistics 
    I_Kr = I_K.reshape((1, N*T))
    sc = np.corrcoef(I_Kr[0][1:], I_Kr[0][:N*T-1])[0,1]
    sd = np.std(PI_K)
    qbar = V_sim.sum() / k_sim.sum()

    # run the q-regression
    Y = np.matrix(I_K)
    X = np.matrix(np.concatenate((np.ones((N*T, 1)), Qav, PI_K), axis = 1))
    a0, a1, a2 = np.linalg.inv(X.T * X) * X.T * Y 

    # construct the vectors
    mu_sim = np.array((a1[0, 0], a2[0, 0], sc, sd, qbar))
    
    return mu_sim
Exemplo n.º 3
0
def sim_moments(theta):

    import numpy as np
    import time
    import numba

    import zgrid
    import kgrid
    import zshocks
    import simul
    import VFI

    # params
    #sigma = 0.213  # struct param to be estimated
    mu = 0  # fixed param
    #rho = 0.7605  # struct param to be estimated
    sizez = 9  # fixed param
    #ak = .297  # struct param to be estimated
    al = .650  # fixed
    delta = 0.15  # fixed
    betaf = .95  # fixed
    #psi = 1.080  # struct param to be estimated

    params = (mu, sizez, al, delta, betaf)

    T = 250  # number of years simulated
    N = 1000  # number of firms simulated

    kgrid, sizek = kgrid.main(params)

    z_grid, pi = zgrid.main(sigma, mu, rho, sizez)

    # E = earnings.main(theta, params, kgrid, sizek, z_grid)

    Vnext, loc = VFI.main(theta, params, z_grid, kgrid, sizek, sizez, pi)

    k_sim, I_sim, V_sim, prof_sim = simul.main(theta, params, kgrid, pi, Vnext,
                                               loc, T, N, z_grid)

    def moments(k_sim, I_sim, V_sim, prof_sim):

        IoverK = (I_sim / k_sim).reshape((N * 50, 1))
        avgQ = (V_sim / k_sim).reshape((N * 50, 1))
        PIoverK = (prof_sim / k_sim).reshape((N * 50, 1))

        y = np.matrix(IoverK)
        X = np.matrix(
            np.concatenate((np.ones((N * 50, 1)), avgQ, PIoverK), axis=1))

        a0, a1, a2 = np.linalg.inv(X.T * X) * X.T * y  # reg coeffs

        ik = IoverK.reshape((1, N * 50))  # I over K reshaped for corrcoef()
        sc_ik = np.corrcoef(ik[0][1:], ik[0][:50000 - 1])[0, 1]  # sc_
        sd_piK = np.std(PIoverK)
        qbar = V_sim.sum() / k_sim.sum()

        U_sim = np.array((a1[0, 0], a2[0, 0], sc_ik, sd_piK, qbar))

        return U_sim

    U_sim = moments(k_sim, I_sim, V_sim, prof_sim)

    return U_sim
Exemplo n.º 4
0
delta = .154  # Depreciation
psi = 1.080  # Coeff on quadratic adj costs
wage = .7  # wage rate
r = .040  # Int rate
betaf = 1 / (1 + r)  # discount factor
sigma = .213  # Std. Deviation of z shockkgrid
mu = 0  # mean of z AR1
rho = .7605  # Persistance of z shock
sizez = 9  # size of z grid
h = 6.616

# parameters that occur in any state of the model (Gen or Partial equilibrium)
params = (ak, al, delta, psi, wage, r, betaf, sigma, mu, rho, sizez)

# calls the kgrid function to return the grid of k values and size of kgrid
kgrid, sizek = kgrid.main(params)

# calls the z_grid function to caluclate the zgrid given sizez
z_grid, pi = zgrid.main(params)

# free parameters in the Gen eq (not including wage since it is endog.)
eq_params = (ak, al, delta, psi, r, betaf, sigma, mu, rho, sizez, h)

wage_initial = .89  # initialize a wage guess.

# cal the optimizer routine of the equil function which determines equlibrium
# for a given wage and param values
OPTWAGE = opt.minimize(equil.main,
                       wage_initial, (eq_params, sizek, kgrid, z_grid, pi),
                       method='Nelder-Mead',
                       tol=1e-5,