Skip to content

Commit 969c7b5

Browse files
committed
Add k simulation
1 parent 99822c1 commit 969c7b5

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

lectures/olg.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ kernelspec:
1212
---
1313

1414

15-
1615
# The Overlapping Generations Model
1716

1817
In this lecture we study the overlapping generations (OLG) model.
@@ -58,7 +57,6 @@ plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
5857
```
5958

6059

61-
6260
## The Model
6361

6462
We assume that
@@ -213,6 +211,9 @@ $$
213211

214212
From the above equation, we see that in order to find $k_{t+1}$ we need some root-finding algorithm that solves for $k_{t+1}$ given that we have $k_{t}$.
215213

214+
And suppose, $k_{t+1} = g(k_t)$
215+
216+
216217
So for that we will use [scipy.optimize.newton](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html#scipy.optimize.newton).
217218

218219
```{code-cell} ipython3
@@ -231,6 +232,8 @@ def k_next(model, k):
231232
return optimize.newton(solve_for_k, k, args=(model, k))
232233
```
233234

235+
Let's plot the 45 degree diagram of $k$
236+
234237
```{code-cell} ipython3
235238
def plot45(olg, kstar=None):
236239
kmin, kmax = 0, 0.3
@@ -245,7 +248,7 @@ def plot45(olg, kstar=None):
245248
246249
ymin, ymax = np.min(k_grid_next), np.max(k_grid_next)
247250
248-
ax.plot(k_grid, k_grid_next, lw=2, alpha=0.6)
251+
ax.plot(k_grid, k_grid_next, lw=2, alpha=0.6, label='$g$')
249252
ax.plot(k_grid, k_grid, 'k-', lw=1, alpha=0.7, label='45')
250253
251254
if kstar:
@@ -256,13 +259,12 @@ def plot45(olg, kstar=None):
256259
ax.annotate(r'$k^*$',
257260
xy=(kstar, kstar),
258261
xycoords='data',
259-
xytext=(-40, -60),
262+
xytext=(0, -60),
260263
textcoords='offset points',
261264
fontsize=14,
262265
arrowprops=dict(arrowstyle="->"))
263266
264267
ax.legend(loc='upper left', frameon=False, fontsize=12)
265-
266268
ax.set_xlabel('$k_t$', fontsize=12)
267269
ax.set_ylabel('$k_{t+1}$', fontsize=12)
268270
@@ -274,9 +276,25 @@ olg = create_olg_model()
274276
plot45(olg)
275277
```
276278

279+
Suppose, at some $k_t$, the value $g(k_t)$ lies strictly above the 45 degree line.
280+
281+
Then we have $k_{t+1} = g(k_t) > k_t$ and capital per worker rises.
282+
283+
If $g(k_t) < k_t$ then capital per worker falls.
284+
285+
If $g(k_t) = k_t$, then we are at a **steady state** and $k_t$ remains constant.
286+
287+
(A steady state of the model is a [fixed point](https://en.wikipedia.org/wiki/Fixed_point_(mathematics)) of the mapping $g$.)
288+
289+
From the shape of the function $g$ in the figure, we see that
290+
there is a unique steady state in $(0, \infty)$.
291+
292+
+++
293+
277294
Let's find the value of $k^*$.
278295

279-
By observing the above graph, we can see that the value of $k^*$ roughly falls between $(0.15, 0.2)$. Using this information, we will again use [scipy.optimize.newton](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html#scipy.optimize.newton).
296+
By observing the above graph, we can see that the value of $k^*$ roughly falls between $(0.15, 0.2)$.
297+
Using this information, we will again use [scipy.optimize.newton](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html#scipy.optimize.newton).
280298

281299
```{code-cell} ipython3
282300
def solve_for_k_star(x, model):
@@ -289,12 +307,59 @@ def solve_for_k_star(x, model):
289307

290308
```{code-cell} ipython3
291309
k_star = optimize.newton(solve_for_k_star, 0.2, args=(olg,))
310+
print(f"k_star = {k_star}")
292311
```
293312

294313
```{code-cell} ipython3
295314
plot45(olg, k_star)
296315
```
297316

317+
From our graphical analysis, it appears that $(k_t)$ converges to $k^*$, regardless of initial capital
318+
$k_0$.
319+
320+
This is a form of global stability.
321+
322+
323+
The next figure shows three time paths for capital, from
324+
three distinct initial conditions, under the parameterization listed above.
325+
326+
At this parameterization, $k^* \approx 0.161$.
327+
328+
Let's define the constants and three distinct intital conditions
329+
330+
```{code-cell} ipython3
331+
ts_length = 10
332+
x0 = np.array([0.001, 0.5, 1.8, 3.5])
333+
```
334+
335+
```{code-cell} ipython3
336+
def simulate_ts(olg, x0_values, ts_length):
337+
338+
k_star = optimize.newton(solve_for_k_star, 0.2, args=(olg,))
339+
fig, ax = plt.subplots()
340+
341+
ts = np.zeros(ts_length)
342+
343+
# simulate and plot time series
344+
for x_init in x0_values:
345+
ts[0] = x_init
346+
for t in range(1, ts_length):
347+
ts[t] = k_next(olg, ts[t-1])
348+
ax.plot(np.arange(ts_length), ts, '-o', ms=4, alpha=0.6,
349+
label=r'$k_0=%g$' %x_init)
350+
ax.plot(np.arange(ts_length), np.full(ts_length,k_star),
351+
alpha=0.6, color='red', label=r'$k_*$')
352+
ax.legend(fontsize=10)
353+
354+
ax.set_xlabel(r'$t$', fontsize=14)
355+
ax.set_ylabel(r'$k_t$', fontsize=14)
356+
357+
plt.show()
358+
```
359+
360+
```{code-cell} ipython3
361+
simulate_ts(olg, x0, ts_length)
362+
```
298363

299364

300365
## Exercises

lectures/solow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def simulate_ts(x0_values, ts_length):
247247
ax.plot(np.arange(ts_length), ts, '-o', ms=4, alpha=0.6,
248248
label=r'$k_0=%g$' %x_init)
249249
ax.plot(np.arange(ts_length), np.full(ts_length,k_star),
250-
alpha=0.6, color='red', label=r'$k_*$')
250+
alpha=0.6, color='red', label=r'$k^*$')
251251
ax.legend(fontsize=10)
252252
253253
ax.set_xlabel(r'$t$', fontsize=14)

0 commit comments

Comments
 (0)