def __init__(self, N_z, N_eps, N_x, upper_x, c_ubar=2663, T=31, N_h=2, r=0.02, nu=3.81, beta=0.97, rho=0.922, sig_z=np.sqrt(0.05), sig_eps=np.sqrt(0.665)): self.N_x, self.N_z, self.N_eps, self.N_h = N_x, N_z, N_eps, N_h self.upper_x, self.c_ubar = upper_x, c_ubar self.T, self.r, self.beta = T, r, beta # utility function def u(x): return x**(1-nu)/(1-nu) self.u = u # markov chain for medical expenses shocks z = qe.rouwenhorst(N_z, 0, sig_z, rho) eps = qe.rouwenhorst(N_eps, 0, sig_eps, 0) self.grid_med = (np.kron(z.state_values, np.ones(N_eps)) + np.kron(np.ones(N_z), eps.state_values)) self.Pi_med = np.kron(z.P, eps.P[1, :]) # grid on cash on hand (more points for lower values) self.grid_x = np.linspace(np.sqrt(c_ubar), np.sqrt(upper_x), N_x)**2 # tax function brackets = np.array([0, 6250, 40200, 68400, 93950, 148250, 284700, 2e7]) tau = np.array([0.0765, 0.2616, 0.4119, 0.3499, 0.3834, 0.4360, 0.4761]) tax = np.zeros(8) for i in range(7): tax[i+1] = tax[i] + (brackets[i+1]-brackets[i]) * tau[i] self.tax = tax self.brackets = brackets
def discretize(ssy, K, I, J, add_x_data=False): """ And here's the actual discretization process. """ ρ = ssy.ρ ϕ_z = ssy.ϕ_z σ_bar = ssy.σ_bar ϕ_c = ssy.ϕ_c ρ_hz = ssy.ρ_hz σ_hz = ssy.σ_hz ρ_hc = ssy.ρ_hc σ_hc = ssy.σ_hc hc_mc = rouwenhorst(K, 0, σ_hc, ρ_hc) #hc_mc = tauchen(ρ_hc, σ_hc, n=K) hz_mc = rouwenhorst(I, 0, σ_hz, ρ_hz) #hz_mc = tauchen(ρ_hz, σ_hz, n=I) σ_c_states = ϕ_c * σ_bar * np.exp(hc_mc.state_values) σ_z_states = ϕ_z * σ_bar * np.exp(hz_mc.state_values) M = I * J * K z_states = np.zeros((I, J)) z_Q = np.zeros((I, J, J)) for i, σ_z in enumerate(σ_z_states): mc_z = rouwenhorst(J, 0, np.sqrt(1 - ρ**2) * σ_z, ρ) #mc_z = tauchen(ρ, np.sqrt(1 - ρ**2) * σ_z, n=J) for j in range(J): z_states[i, j] = mc_z.state_values[j] z_Q[i, j, :] = mc_z.P[j, :] # Create an instance of SSYConsumptionDiscretized to store output ssyd = SSYConsumptionDiscretized( ssy, K, I, J, σ_c_states, hc_mc.P, # equals σ_c_P σ_z_states, hz_mc.P, # equals σ_z_P z_states, z_Q) if add_x_data: ssyd.x_states, ssyd.x_P = build_x_mc(ssyd) return ssyd
def stability_exp_discretized(by, D=10): """ Compute stability exponent by numerical linear algebra. * by is an instance of BYCV """ # Unpack parameters ρ, σ = by.ρ, by.σ σ_c, μ_c = by.σ_c, by.μ_c σ_d, μ_d = by.σ_d, by.μ_d β, γ = by.β, by.γ # Discretize the state process X_mc = qe.rouwenhorst(D, 0.0, σ, ρ) x_vals = X_mc.state_values P = X_mc.P # Build the matrix # # V(x, y) = β * exp(μ_d - γ*μ_c + (1-γ)*x + (σ_d**2 + (γ*σ_c)**2) / 2) Π(x, y) # V = np.empty((D, D)) for i, x in enumerate(x_vals): for j, y in enumerate(x_vals): a = β * exp(μ_d - γ*μ_c + (1-γ)*x + (σ_d**2 + (γ*σ_c)**2) / 2) V[i, j] = a * P[i, j] # Return the log of r(V) return np.log(np.max(np.abs(eigvals(V))))
def discretize(by, I, J, add_x_data=False): """ And here's the actual discretization process. """ # Unpack consumption parameters ρ, ϕ_z, v, d, ϕ_σ = by.ρ, by.ϕ_z, by.v, by.d, by.ϕ_σ # Discretize σ first σ_mc = rouwenhorst(I, d, ϕ_σ, v) σ_P = σ_mc.P σ_states = np.sqrt(np.maximum(σ_mc.state_values, 0)) # Allocate memory M = I * J z_states = np.empty((I, J)) z_Q = np.empty((I, J, J)) x_states = np.empty((2, M)) x_P = np.empty((M, M)) # Discretize z at each σ_i and record state values for z in z_states. # Also, record transition probability from z_states[i, j] to # z_states[i, jp] when σ = σ_i. Store it as q[i, j, jp]. for i, σ in enumerate(σ_states): mc_z = rouwenhorst(J, 0.0, ϕ_z * σ, ρ) for j in range(J): z_states[i, j] = mc_z.state_values[j] for jp in range(J): z_Q[i, j, jp] = mc_z.P[j, jp] # Create an instance of BYConsumptionDiscretized to store output byd = BYConsumptionDiscretized(by, I, J, σ_states, σ_P, z_states, z_Q) if add_x_data: byd.x_states, byd.x_P = build_x_mc(byd) return byd
def lrm_discretized(lg, D=10): # Unpack parameters γ, ρ, σ, σ_c, μ_c = lg.γ, lg.ρ, lg.σ, lg.σ_c, lg.μ_c # Discretize the state process X_mc = qe.rouwenhorst(D, 0.0, σ, ρ) x_vals = X_mc.state_values Q = X_mc.P # Build the matrix K(x, y) = exp((1-γ) y) Q(x, y) K = np.empty((D, D)) for i, x in enumerate(x_vals): for j, y in enumerate(x_vals): K[i, j] = np.exp((1 - γ) * (μ_c + x) + ((1 - γ)**2) * 0.5 * σ_c**2) * Q[i, j] # Return MC rK = np.max(np.abs(eigvals(K))) return rK**(1 / (1 - γ))
def __init__(self, β=0.96, γ=2.5, ρ=0.9, d=0.0, σ=0.1, r=0.04, w=1.0, z_grid_size=25, x_grid_size=200, x_grid_max=15): self.β, self.γ = β, γ self.R = 1 + r self.w = w self.z_grid_size, self.x_grid_size = z_grid_size, x_grid_size mc = qe.rouwenhorst(z_grid_size, d, σ, ρ) self.Q = mc.P self.z_grid = np.exp(mc.state_values) self.x_grid = np.linspace(0.0, x_grid_max, x_grid_size)