-
Notifications
You must be signed in to change notification settings - Fork 3
/
sim_steady_state.py
executable file
·211 lines (146 loc) · 6.18 KB
/
sim_steady_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Simple code for standard incomplete markets model.
Built up in "Lecture 1, Standard Incomplete Markets Steady State.ipynb".
"""
import numpy as np
import numba
"""Part 0: example calibration from notebook"""
def example_calibration():
y, _, Pi = discretize_income(0.975, 0.7, 7)
return dict(a_grid = discretize_assets(0, 10_000, 500),
y=y, Pi=Pi,
r = 0.01/4, beta=1-0.08/4, eis=1)
"""Part 1: discretization tools"""
def discretize_assets(amin, amax, n_a):
# find maximum ubar of uniform grid corresponding to desired maximum amax of asset grid
ubar = np.log(1 + np.log(1 + amax - amin))
# make uniform grid
u_grid = np.linspace(0, ubar, n_a)
# double-exponentiate uniform grid and add amin to get grid from amin to amax
return amin + np.exp(np.exp(u_grid) - 1) - 1
def rouwenhorst_Pi(N, p):
# base case Pi_2
Pi = np.array([[p, 1 - p],
[1 - p, p]])
# recursion to build up from Pi_2 to Pi_N
for n in range(3, N + 1):
Pi_old = Pi
Pi = np.zeros((n, n))
Pi[:-1, :-1] += p * Pi_old
Pi[:-1, 1:] += (1 - p) * Pi_old
Pi[1:, :-1] += (1 - p) * Pi_old
Pi[1:, 1:] += p * Pi_old
Pi[1:-1, :] /= 2
return Pi
def stationary_markov(Pi, tol=1E-14):
# start with uniform distribution over all states
n = Pi.shape[0]
pi = np.full(n, 1/n)
# update distribution using Pi until successive iterations differ by less than tol
for _ in range(10_000):
pi_new = Pi.T @ pi
if np.max(np.abs(pi_new - pi)) < tol:
return pi_new
pi = pi_new
def discretize_income(rho, sigma, n_e):
# choose inner-switching probability p to match persistence rho
p = (1+rho)/2
# start with states from 0 to n_e-1, scale by alpha to match standard deviation sigma
e = np.arange(n_e)
alpha = 2*sigma/np.sqrt(n_e-1)
e = alpha*e
# obtain Markov transition matrix Pi and its stationary distribution
Pi = rouwenhorst_Pi(n_e, p)
pi = stationary_markov(Pi)
# e is log income, get income y and scale so that mean is 1
y = np.exp(e)
y /= np.vdot(pi, y)
return y, pi, Pi
"""Part 2: Backward iteration for policy"""
def backward_iteration(Va, Pi, a_grid, y, r, beta, eis):
# step 1: discounting and expectations
Wa = beta * Pi @ Va
# step 2: solving for asset policy using the first-order condition
c_endog = Wa**(-eis)
coh = y[:, np.newaxis] + (1+r)*a_grid
a = np.empty_like(coh)
for e in range(len(y)):
a[e, :] = np.interp(coh[e, :], c_endog[e, :] + a_grid, a_grid)
# step 3: enforcing the borrowing constraint and backing out consumption
a = np.maximum(a, a_grid[0])
c = coh - a
# step 4: using the envelope condition to recover the derivative of the value function
Va = (1+r) * c**(-1/eis)
return Va, a, c
def policy_ss(Pi, a_grid, y, r, beta, eis, tol=1E-9):
# initial guess for Va: assume consumption 5% of cash-on-hand, then get Va from envelope condition
coh = y[:, np.newaxis] + (1+r)*a_grid
c = 0.05 * coh
Va = (1+r) * c**(-1/eis)
# iterate until maximum distance between two iterations falls below tol, fail-safe max of 10,000 iterations
for it in range(10_000):
Va, a, c = backward_iteration(Va, Pi, a_grid, y, r, beta, eis)
# after iteration 0, can compare new policy function to old one
if it > 0 and np.max(np.abs(a - a_old)) < tol:
return Va, a, c
a_old = a
"""Part 3: forward iteration for distribution"""
def get_lottery(a, a_grid):
# step 1: find the i such that a' lies between gridpoints a_i and a_(i+1)
a_i = np.searchsorted(a_grid, a, side='right') - 1
# step 2: implement (8) to obtain lottery probabilities pi
a_pi = (a_grid[a_i+1] - a)/(a_grid[a_i+1] - a_grid[a_i])
return a_i, a_pi
@numba.njit
def forward_policy(D, a_i, a_pi):
Dend = np.zeros_like(D)
for e in range(a_i.shape[0]):
for a in range(a_i.shape[1]):
# send pi(e,a) of the mass to gridpoint i(e,a)
Dend[e, a_i[e,a]] += a_pi[e,a]*D[e,a]
# send 1-pi(e,a) of the mass to gridpoint i(e,a)+1
Dend[e, a_i[e,a]+1] += (1-a_pi[e,a])*D[e,a]
return Dend
def forward_iteration(D, Pi, a_i, a_pi):
Dend = forward_policy(D, a_i, a_pi)
return Pi.T @ Dend
def distribution_ss(Pi, a, a_grid, tol=1E-10):
a_i, a_pi = get_lottery(a, a_grid)
# as initial D, use stationary distribution for s, plus uniform over a
pi = stationary_markov(Pi)
D = pi[:, np.newaxis] * np.ones_like(a_grid) / len(a_grid)
# now iterate until convergence to acceptable threshold
for _ in range(10_000):
D_new = forward_iteration(D, Pi, a_i, a_pi)
if np.max(np.abs(D_new - D)) < tol:
return D_new
D = D_new
"""Part 4: solving for steady state, including aggregates"""
def steady_state(Pi, a_grid, y, r, beta, eis):
Va, a, c = policy_ss(Pi, a_grid, y, r, beta, eis)
a_i, a_pi = get_lottery(a, a_grid)
D = distribution_ss(Pi, a, a_grid)
return dict(D=D, Va=Va,
a=a, c=c, a_i=a_i, a_pi=a_pi,
A=np.vdot(a, D), C=np.vdot(c, D),
Pi=Pi, a_grid=a_grid, y=y, r=r, beta=beta, eis=eis)
"""Part 5: expectation iterations"""
@numba.njit
def expectation_policy(Xend, a_i, a_pi):
X = np.zeros_like(Xend)
for e in range(a_i.shape[0]):
for a in range(a_i.shape[1]):
# expectation is pi(e,a)*Xend(e,i(e,a)) + (1-pi(e,a))*Xend(e,i(e,a)+1)
X[e, a] = a_pi[e, a]*Xend[e, a_i[e, a]] + (1-a_pi[e, a])*Xend[e, a_i[e, a]+1]
return X
def expectation_iteration(X, Pi, a_i, a_pi):
Xend = Pi @ X
return expectation_policy(Xend, a_i, a_pi)
def expectation_functions(X, Pi, a_i, a_pi, T):
# set up array of curlyEs and fill in first row with base case
curlyE = np.empty((T, ) + X.shape)
curlyE[0] = X
# recursively apply law of iterated expectations
for j in range(1, T):
curlyE[j] = expectation_iteration(curlyE[j-1], Pi, a_i, a_pi)
return curlyE