Skip to content

Commit dae56de

Browse files
committed
add exercise
1 parent 969c7b5 commit dae56de

File tree

1 file changed

+110
-15
lines changed

1 file changed

+110
-15
lines changed

lectures/olg.md

Lines changed: 110 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ kernelspec:
1111
name: python3
1212
---
1313

14+
+++ {"tags": []}
1415

1516
# The Overlapping Generations Model
1617

@@ -56,6 +57,7 @@ import matplotlib.pyplot as plt
5657
plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
5758
```
5859

60+
+++ {"tags": []}
5961

6062
## The Model
6163

@@ -73,7 +75,7 @@ aversion (CRRA) form,
7375
```{math}
7476
:label: eq_crra
7577
76-
U_t = u(c^1_t) + \beta u(c^2_{t+1})
78+
U_t = u(c^1_t) + \beta u(c^2_{t+1})
7779
```
7880

7981

@@ -86,7 +88,7 @@ Here
8688

8789
### Production
8890

89-
For each integer $t \geq 0$, output $Y_t$ in period $t$ is given by
91+
For each integer $t \geq 0$, output $Y_t$ in period $t$ is given by
9092

9193
$$
9294
Y_t = F(K_t, L_t) = K_t^{\alpha} L_t^{1-\alpha}
@@ -121,23 +123,25 @@ The wage rate is given by
121123
Savings by an individual of generation $t$, $s_t$, is determined as a
122124
solution to:
123125

124-
$$
126+
```{math}
127+
:label: max_sav_olg
125128
\begin{aligned}
126129
\max_{c^1_t, c^2_{t+1}, s_t} \ & u(c^1_t) + \beta u(c^2_{t+1}) \\
127130
\mbox{subject to } \ & c^1_t + s_t \le w_t \\
128131
& c^2_{t+1} \le R_{t+1}s_t\\
129132
\end{aligned}
130-
$$
133+
```
131134

132135
The second constraint incorporates notion that individuals only spend
133136
money on their own end of life consumption. Also, Since $u(.)$ is strictly increasing, both constraints will hold as equalities.
134137

135138

136-
Substituting $s_t$ we get from the first constraint into the second constraint we get $c^2_{t+1}$ in terms of $c^1_t$, i.e.,
139+
Substituting $s_t$ we get from the first constraint into the second constraint we get $c^2_{t+1}$ in terms of $c^1_t$, i.e.,
137140

138-
$$
141+
```{math}
142+
:label: c_2_olg
139143
c^2_{t+1} = R_{t+1}(w_t - c^1_t)
140-
$$
144+
```
141145
Thus first-order condition for a maximum can be written in the
142146
familiar form of the consumption Euler equation.
143147
Plugging $c^2_{t+1}$ into the objective function and taking derivative with respect to $c^1_t$ yield the Euler equation,
@@ -240,7 +244,7 @@ def plot45(olg, kstar=None):
240244
m = 1000
241245
k_grid = np.linspace(kmin, kmax, m)
242246
k_grid_next = np.empty_like(k_grid)
243-
247+
244248
for i in range(m):
245249
k_grid_next[i] = k_next(olg, k_grid[i])
246250
@@ -293,8 +297,7 @@ there is a unique steady state in $(0, \infty)$.
293297

294298
Let's find the value of $k^*$.
295299

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).
300+
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).
298301

299302
```{code-cell} ipython3
300303
def solve_for_k_star(x, model):
@@ -329,14 +332,14 @@ Let's define the constants and three distinct intital conditions
329332

330333
```{code-cell} ipython3
331334
ts_length = 10
332-
x0 = np.array([0.001, 0.5, 1.8, 3.5])
335+
x0 = np.array([0.001, 1.2, 2.6])
333336
```
334337

335338
```{code-cell} ipython3
336339
def simulate_ts(olg, x0_values, ts_length):
337340
338341
k_star = optimize.newton(solve_for_k_star, 0.2, args=(olg,))
339-
fig, ax = plt.subplots()
342+
fig, ax = plt.subplots(figsize=(10, 5))
340343
341344
ts = np.zeros(ts_length)
342345
@@ -348,7 +351,7 @@ def simulate_ts(olg, x0_values, ts_length):
348351
ax.plot(np.arange(ts_length), ts, '-o', ms=4, alpha=0.6,
349352
label=r'$k_0=%g$' %x_init)
350353
ax.plot(np.arange(ts_length), np.full(ts_length,k_star),
351-
alpha=0.6, color='red', label=r'$k_*$')
354+
alpha=0.6, color='red', label=r'$k^*$')
352355
ax.legend(fontsize=10)
353356
354357
ax.set_xlabel(r'$t$', fontsize=14)
@@ -361,19 +364,111 @@ def simulate_ts(olg, x0_values, ts_length):
361364
simulate_ts(olg, x0, ts_length)
362365
```
363366

367+
+++ {"tags": []}
364368

365369
## Exercises
366370

367371
```{exercise}
368372
:label: olg_ex1
369373
370-
Replace the utility function $u(c)$ in equation {eq}`eq_crra` with a quasilinear form $u(c)=c + c^{\alpha}$.
374+
Replace the utility function $u(c)$ in equation {eq}`eq_crra` with a quasilinear form $u(c)=c + c^{\theta}$.
371375
372-
Now we don't have an analytical solution.
376+
Now we don't have an analytical solution.
373377
374378
Try to compute the time path capital $\{k_t\}$ in this case.
375379
```
376380

381+
+++
382+
383+
From {eq}`max_sav_olg`, the first-order condition for a maximum can be written in the
384+
familiar form of the consumption Euler equation given by
385+
386+
$$
387+
u'(c^1_t) = \beta R_{t+1} u'(c^2_{t+1})
388+
$$
389+
390+
Using {eq}`c_2_olg`, we have
391+
392+
$$
393+
u'(c^1_t) = \beta R_{t+1} u'(R_{t+1}(w_t - c^1_t))
394+
$$
395+
396+
Substituting using {eq}`R_func`, and {eq}`w_func`
397+
398+
$$
399+
u'(c^1_t) = \beta \alpha k^{\alpha-1}_{t+1} u'(\alpha k^{\alpha-1}_{t+1}((1 - \alpha)k^{\alpha}_t - c^1_t))
400+
$$
401+
402+
Since $u$ is quasilinear, in order to solve for $c^1_t$, we will need to use [scipy.optimize.newton](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html#scipy.optimize.newton).
403+
404+
From {eq}`k_dyms_crra`,
405+
406+
$$
407+
k_{t+1} = \frac{s(w_t, R_{t+1})}{1 + n} = \frac{w_t - c^1_t}{1+n}
408+
$$
409+
410+
+++
411+
412+
Let's define the functions to solve the above system.
413+
414+
```{code-cell} ipython3
415+
def u_prime(c, θ):
416+
return 1 + θ * c ** (θ - 1)
417+
418+
def solve_for_c(x, θ, model, k_t, k_t_1):
419+
l = u_prime(x, θ)
420+
R = model.α * k_t_1**(model.α-1)
421+
r = model.β * R
422+
y = R * ((1 - model.α) * k_t**model.α - x)
423+
r = r * u_prime(y, θ)
424+
return l - r
425+
```
426+
427+
```{code-cell} ipython3
428+
def solve_for_k(x, θ, model, k_t):
429+
c = optimize.newton(solve_for_c, k_t, args=(θ, model, k_t, x))
430+
num = (1 - model.α) * k_t**model.α - c
431+
den = (1 + model.n)
432+
return x - num/den
433+
434+
def k_next(model, θ, k_t):
435+
return optimize.newton(solve_for_k, k_t, args=(θ, model, k_t), maxiter=200)
436+
```
437+
438+
```{code-cell} ipython3
439+
ts_length = 10
440+
θ = 1.5
441+
x0 = np.array([1.2, 2.6])
442+
```
443+
444+
```{code-cell} ipython3
445+
def simulate_ts(olg, θ, x0_values, ts_length):
446+
447+
fig, ax = plt.subplots(figsize=(10, 5))
448+
449+
ts = np.zeros(ts_length)
450+
451+
# simulate and plot time series
452+
for x_init in x0_values:
453+
ts[0] = x_init
454+
for t in range(1, ts_length):
455+
ts[t] = k_next(olg, θ, ts[t-1])
456+
ax.plot(np.arange(ts_length), ts, '-o', ms=4, alpha=0.6,
457+
label=r'$k_0=%g$' %x_init)
458+
ax.plot(np.arange(ts_length), np.full(ts_length,k_star),
459+
alpha=0.6, color='red', label=r'$k^*$')
460+
ax.legend(fontsize=10)
461+
462+
ax.set_xlabel(r'$t$', fontsize=14)
463+
ax.set_ylabel(r'$k_t$', fontsize=14)
464+
465+
plt.show()
466+
```
467+
468+
```{code-cell} ipython3
469+
simulate_ts(olg, θ, x0, ts_length)
470+
```
471+
377472
```{code-cell} ipython3
378473
379474
```

0 commit comments

Comments
 (0)