Skip to content

Commit c33b392

Browse files
authored
Merge pull request #400 from QuantEcon/update_cagan_adaptive_code
[cagan_adaptive] Update the code suggestions
2 parents 16f959e + d2b251b commit c33b392

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

lectures/cagan_adaptive.md

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ kernelspec:
1616
## Overview
1717

1818

19-
This lecture is a sequel or prequel to the lecture {doc}`cagan_ree`.
19+
This lecture is a sequel or prequel to {doc}`cagan_ree`.
2020

2121
We'll use linear algebra to do some experiments with an alternative "monetarist" or "fiscal" theory of price levels.
2222

2323
Like the model in {doc}`cagan_ree`, the model asserts that when a government persistently spends more than it collects in taxes and prints money to finance the shortfall, it puts upward pressure on the price level and generates persistent inflation.
2424

25-
Instead of the "perfect foresight" or "rational expectations" version of the model in {doc}`cagan_ree`, our model in the present lecture is an "adaptive expectations" version of a model that {cite}`Cagan` used to study the monetary dynamics of hyperinflations.
25+
Instead of the "perfect foresight" or "rational expectations" version of the model in {doc}`cagan_ree`, our model in the present lecture is an "adaptive expectations" version of a model that {cite}`Cagan` used to study the monetary dynamics of hyperinflations.
2626

2727
It combines these components:
2828

@@ -36,7 +36,7 @@ It combines these components:
3636

3737
Our model stays quite close to Cagan's original specification.
3838

39-
As in the lectures {doc}`pv` and {doc}`cons_smooth`, the only linear algebra operations that we'll be using are matrix multiplication and matrix inversion.
39+
As in {doc}`pv` and {doc}`cons_smooth`, the only linear algebra operations that we'll be using are matrix multiplication and matrix inversion.
4040

4141
To facilitate using linear matrix algebra as our principal mathematical tool, we'll use a finite horizon version of
4242
the model.
@@ -261,7 +261,7 @@ $$
261261
262262
which is just $\pi^*$ with the last element dropped.
263263
264-
## Forecast errors
264+
## Forecast errors and model computation
265265
266266
Our computations will verify that
267267
@@ -278,10 +278,9 @@ $$ (eq:notre)
278278
This outcome is typical in models in which adaptive expectations hypothesis like equation {eq}`eq:adaptexpn` appear as a
279279
component.
280280
281-
In {doc}`cagan_ree` we studied a version of the model that replaces hypothesis {eq}`eq:adaptexpn` with
281+
In {doc}`cagan_ree`, we studied a version of the model that replaces hypothesis {eq}`eq:adaptexpn` with
282282
a "perfect foresight" or "rational expectations" hypothesis.
283283
284-
285284
But now, let's dive in and do some computations with the adaptive expectations version of the model.
286285
287286
As usual, we'll start by importing some Python modules.
@@ -296,53 +295,36 @@ import matplotlib.pyplot as plt
296295
Cagan_Adaptive = namedtuple("Cagan_Adaptive",
297296
["α", "m0", "Eπ0", "T", "λ"])
298297
299-
def create_cagan_adaptive_model(α, m0, Eπ0, T, λ):
298+
def create_cagan_adaptive_model(α = 5, m0 = 1, Eπ0 = 0.5, T=80, λ = 0.9):
300299
return Cagan_Adaptive(α, m0, Eπ0, T, λ)
301-
```
302-
+++ {"user_expressions": []}
303-
304-
Here we define the parameters.
305-
306-
```{code-cell} ipython3
307-
# parameters
308-
T = 80
309-
T1 = 60
310-
α = 5
311-
λ = 0.9 # 0.7
312-
m0 = 1
313-
314-
μ0 = 0.5
315-
μ_star = 0
316300
317-
md = create_cagan_adaptive_model(α=α, m0=m0, Eπ0=μ0, T=T, λ=λ)
301+
md = create_cagan_adaptive_model()
318302
```
319303
+++ {"user_expressions": []}
320304
321305
We solve the model and plot variables of interests using the following functions.
322306
323307
```{code-cell} ipython3
324-
def solve(model, μ_seq):
308+
def solve_cagan_adaptive(model, μ_seq):
325309
" Solve the Cagan model in finite time. "
326-
327-
model_params = model.α, model.m0, model.Eπ0, model.T, model.λ
328-
α, m0, Eπ0, T, λ = model_params
310+
α, m0, Eπ0, T, λ = model
329311
330312
A = np.eye(T+2, T+2) - λ*np.eye(T+2, T+2, k=-1)
331313
B = np.eye(T+2, T+1, k=-1)
332314
C = -α*np.eye(T+1, T+2) + α*np.eye(T+1, T+2, k=1)
333315
Eπ0_seq = np.append(Eπ0, np.zeros(T+1))
334316
335317
# Eπ_seq is of length T+2
336-
Eπ_seq = np.linalg.inv(A - (1-λ)*B @ C) @ ((1-λ) * B @ μ_seq + Eπ0_seq)
318+
Eπ_seq = np.linalg.solve(A - (1-λ)*B @ C, (1-λ) * B @ μ_seq + Eπ0_seq)
337319
338320
# π_seq is of length T+1
339321
π_seq = μ_seq + C @ Eπ_seq
340322
341-
D = np.eye(T+1, T+1) - np.eye(T+1, T+1, k=-1)
323+
D = np.eye(T+1, T+1) - np.eye(T+1, T+1, k=-1) # D is the coefficient matrix in Equation (14.8)
342324
m0_seq = np.append(m0, np.zeros(T))
343325
344326
# m_seq is of length T+2
345-
m_seq = np.linalg.inv(D) @ (μ_seq + m0_seq)
327+
m_seq = np.linalg.solve(D, μ_seq + m0_seq)
346328
m_seq = np.append(m0, m_seq)
347329
348330
# p_seq is of length T+2
@@ -356,7 +338,7 @@ def solve(model, μ_seq):
356338
```{code-cell} ipython3
357339
def solve_and_plot(model, μ_seq):
358340
359-
π_seq, Eπ_seq, m_seq, p_seq = solve(model, μ_seq)
341+
π_seq, Eπ_seq, m_seq, p_seq = solve_cagan_adaptive(model, μ_seq)
360342
361343
T_seq = range(model.T+2)
362344
@@ -369,10 +351,12 @@ def solve_and_plot(model, μ_seq):
369351
ax[4].plot(T_seq, p_seq)
370352
371353
y_labs = [r'$\mu$', r'$\pi$', r'$m - p$', r'$m$', r'$p$']
354+
subplot_title = [r'Money supply growth', r'Inflation', r'Real balances', r'Money supply', r'Price level']
372355
373356
for i in range(5):
374357
ax[i].set_xlabel(r'$t$')
375358
ax[i].set_ylabel(y_labs[i])
359+
ax[i].set_title(subplot_title[i])
376360
377361
ax[1].legend()
378362
plt.tight_layout()
@@ -406,12 +390,10 @@ By assuring that the coefficient on $\pi_t$ is less than one in absolute value,
406390
The reader is free to study outcomes in examples that violate condition {eq}`eq:suffcond`.
407391
408392
```{code-cell} ipython3
409-
print(np.abs((λ - α*(1-λ))/(1 - α*(1-λ))))
393+
print(np.abs((md.λ - md.α*(1-md.λ))/(1 - md.α*(1-md.λ))))
410394
```
411395
412-
```{code-cell} ipython3
413-
print(λ - α*(1-λ))
414-
```
396+
## Experiments
415397
416398
Now we'll turn to some experiments.
417399
@@ -425,7 +407,7 @@ Thus, let $T_1 \in (0, T)$.
425407
So where $\mu_0 > \mu^*$, we assume that
426408
427409
$$
428-
\mu_{t+1} = \begin{cases}
410+
\mu_{t} = \begin{cases}
429411
\mu_0 , & t = 0, \ldots, T_1 -1 \\
430412
\mu^* , & t \geq T_1
431413
\end{cases}
@@ -436,7 +418,12 @@ Notice that we studied exactly this experiment in a rational expectations vers
436418
So by comparing outcomes across the two lectures, we can learn about consequences of assuming adaptive expectations, as we do here, instead of rational expectations as we assumed in that other lecture.
437419
438420
```{code-cell} ipython3
439-
μ_seq_1 = np.append(μ0*np.ones(T1), μ_star*np.ones(T+1-T1))
421+
# Parameters for the experiment 1
422+
T1 = 60
423+
μ0 = 0.5
424+
μ_star = 0
425+
426+
μ_seq_1 = np.append(μ0*np.ones(T1), μ_star*np.ones(md.T+1-T1))
440427
441428
# solve and plot
442429
π_seq_1, Eπ_seq_1, m_seq_1, p_seq_1 = solve_and_plot(md, μ_seq_1)
@@ -460,7 +447,7 @@ The sluggish fall in inflation is explained by how anticipated inflation $\pi_t
460447
```{code-cell} ipython3
461448
# parameters
462449
ϕ = 0.9
463-
μ_seq_2 = np.array([ϕ**t * μ0 + (1-ϕ**t)*μ_star for t in range(T)])
450+
μ_seq_2 = np.array([ϕ**t * μ0 + (1-ϕ**t)*μ_star for t in range(md.T)])
464451
μ_seq_2 = np.append(μ_seq_2, μ_star)
465452
466453

0 commit comments

Comments
 (0)