-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmodels.py
288 lines (242 loc) · 10 KB
/
models.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from time import time
import numpy as np
from scipy import optimize as opt
from matplotlib import pyplot as plt
class NCGM:
"""
This is the (deterministic) NeoClassical Growth Model (NCGM). The model
is instantiated with a set of calibrated parameters. The 'solve' methods
(VFI, PFI and TI) will take the grid for the state variable(s) as input
arguments.
"""
def __init__(self, alpha=0.3, beta=0.95, gamma=1.5, delta=0.1):
"""
PARAMETERS
----------
alpha : float (default is 0.3)
The exponent in the production function, a.k.a. the intensity
of capital in production.
beta : float (default is 0.95)
The discount rate of the agent.
gamma : float (default is 1.5)
The coefficient of relative risk aversion of the agent.
delta : float (default is 0.1)
The depreciation rate of capital.
"""
self.alpha = alpha
self.beta = beta
self.gamma = gamma
self.delta = delta
self.u = lambda c: (c**(1-self.gamma)) / (1-self.gamma)
self.k_ss = ((1 - (1-delta) * beta) / (alpha * beta))**(1 / (alpha-1))
def _euler(self, c0, k):
"""
Implements the Euler Equation given a guess for the consumption level
c_t and for various levels of capital holdings k_t. It returns the
quantity resid = LHS - RHS.
"""
k1 = k**self.alpha - c0 + (1-self.delta) * k
pc = np.polyfit(k, c0, 1)
ctp1 = np.polyval(pc, k1)
opr = self.alpha * k1 ** (self.alpha-1) + 1 - self.delta
resid = c0 - ctp1 * (self.beta * opr) ** (-1/self.gamma)
return resid
def solve_vfi(self, k, tolerance=1e-6):
"""
This method takes a grid for the state variable and solves the Bellman
problem by Value Function Iteration. It returns the policy functions
and the computed value at the optimum. It also prints to display how
much time and how many iterations were necessary to converge to the
solution.
PARAMETERS
----------
k : numpy.array
The grid for the state variable over which the Value Function is
computed. The resulting policy functions will be computed at the
gridpoints in this array.
tolerance : float (optional, default is 10**(-6))
The value against which the sup-norm is compared to when
determining whether the algorithm converged or not.
RETURNS
-------
c_opt : numpy.array
The policy function for consumption, evaluated at the
gridpoints 'k'.
k_opt : numpy.array
The policy function for capital holdings, evaluated at the
gridpoints 'k'.
v_opt : numpy.array
The value function computed at the gridpoints 'k'.
"""
n = k.shape[0]
v_old = np.zeros((n,))
v = np.zeros((n,))
dr = np.zeros((n,), dtype=int)
criterion = 1
n_iter = 0
t0 = time()
while criterion > tolerance:
n_iter += 1
for i in range(n):
C = (k[i] ** self.alpha) + (1 - self.delta) * k[i] - k
negative_C = C < 0
C[negative_C] = np.nan
objective = self.u(C) + self.beta * v_old
v[i] = np.nanmax(objective)
dr[i] = np.nanargmax(objective)
criterion = np.max(np.abs(v - v_old))
v_old[:] = v # forcing a deep copy of the array
t1 = time()
k_opt = k[dr]
c_opt = k ** self.alpha + (1-self.delta) * k - k_opt
print('VFI took {} iterations and {:.3f} seconds to converge'.format(n_iter, t1 - t0))
return (c_opt, k_opt, v)
def solve_pfi(self, k, c0, tolerance=1e-6):
"""
This method takes a grid for the state variable and solves the Bellman
problem by Policy Function Iteration. As the convergence of PFI depends
on the initial condition, a guess must be provided by the user. The
method returns the policy functions. It also prints to display how much
time and how many iterations were necessary to converge to the
solution.
PARAMETERS
----------
k : numpy.array
The grid for the state variable over which the Value Function is
computed. The resulting policy functions will be computed at the
gridpoints in this array.
c0 : numpy.array
An initial condition for the guess on the policy function for
consumption.
tolerance : float (optional, default is 10**(-6))
The value against which the sup-norm is compared to when
determining whether the algorithm converged or not.
RETURNS
-------
c_opt : numpy.array
The policy function for consumption, evaluated at the
gridpoints 'k'.
k_opt : numpy.array
The policy function for capital holdings, evaluated at the
gridpoints 'k'.
"""
c_old = np.zeros(c0.shape)
c_old[:] = c0
n_iter = 0
criterion = 1
t0 = time()
while criterion > tolerance:
n_iter += 1
kp = (k ** self.alpha - c_old) + (1 - self.delta) * k
pc = np.polyfit(k, c_old, 5)
ctp1 = np.polyval(pc, kp)
opr = self.alpha * kp ** (self.alpha-1) + 1 - self.delta
c1 = ctp1 * (self.beta * opr) ** (-1 / self.gamma)
criterion = np.max(np.abs(c1 - c_old))
c_old[:] = c1
t1 = time()
c_opt = c1
k_opt = (k ** self.alpha - c_opt) + (1 - self.delta) * k
print('PFI took {} iterations and {:.3f} seconds to converge'.format(n_iter, t1 - t0))
return (c_opt, k_opt)
def solve_proj(self, k, c0, tolerance=1e-6):
"""
This method takes a grid for the state variable and solves the Bellman
problem by Policy Function Iteration. As the convergence of the
projection method depends on the initial condition, an initial
condition must be provided by the user. The method returns the policy
functions. It also prints to display how much time and how many
iterations were necessary to converge to the solution.
"""
t0 = time()
c_opt = opt.fsolve(self._euler, c0, args=k)
t1 = time()
k_opt = k ** self.alpha - c_opt + (1-self.delta) * k
print('Direct projection took {:.2f} seconds.'.format(t1-t0))
return [c_opt, k_opt]
def plot_solution(self, k, c_opt, k_opt, v=None, figSize=None):
"""
This method plots the policy functions of this model once they have
been obtained. It optionally plots the value function if this is
available. It essentially is a wrapper around matplotlib.pyplot.plot
with a (optionally custom) grid of plots.
PARAMETERS
----------
k : numpy.array
The grid of points over which the policy functions have been
computed.
c_opt : numpy.array
The policy function for consumption.
k_opt : numpy.array
The policy function for capital holdings.
v : numpy.array (optional)
The value function.
figSize : tuple
A tuple of floats representing the size of the resulting
figure in inches, formatted as (width, height).
RETURNS
-------
fig : matplotlib.figure
The figure object instantiated by this wrapper (mainly for later
saving to disk).
ax : list
The list of matplotlib.axes._subplots.AxesSubplot objects.
"""
if v is not None:
fig = plt.subplots(figsize=figSize)
ax = [None, None, None]
pltgrid = (2, 4)
ax[0] = plt.subplot2grid(pltgrid, (0, 0), rowspan=2, colspan=2)
ax[1] = plt.subplot2grid(pltgrid, (0, 2), colspan=2)
ax[2] = plt.subplot2grid(pltgrid, (1, 2), colspan=2)
ax[0].plot(k, v,
linewidth=2,
color='red',
label=r'$V(k)$')
ax[1].plot(k, k_opt,
linewidth=2,
color='red',
label=r"$k'(k)$",
zorder=2)
ax[2].plot(k, c_opt,
linewidth=2,
color='red',
label=r'$c(k)$')
ax[1].plot(k, k,
linewidth=1,
color='black',
linestyle='dashed',
zorder=1)
ax[0].set_title('Value function')
ax[1].set_title('Capital accumulation decision')
ax[2].set_title('Consumption decision')
else:
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=figSize)
ax[0].plot(k, k_opt,
color='red',
linewidth=2,
zorder=2,
label=r"$k'(k)$")
ax[1].plot(k, c_opt,
color='red',
linewidth=2,
zorder=2,
label=r'$c(k)$')
ax[0].plot(k, k,
color='black',
linewidth=1,
linestyle='dashed',
zorder=1)
ax[0].set_title('Capital accumulation decision')
ax[1].set_title('Consumption decision')
for a in range(len(ax)):
ax[a].axvline(self.k_ss,
linewidth=1,
color='black',
linestyle='dotted',
zorder=1)
ax[a].grid(alpha=0.3)
ax[a].set_xlabel('$k$')
ax[a].legend()
plt.tight_layout()
return [fig, ax]