Skip to content

Commit d6a9a03

Browse files
committed
[cagan_adaptive] Update the code suggestions
Update coding suggestion in issue #392, more specifically, this pull request is related to - Set the default values in `create_cagan_adaptive_model` instead of global parameters. (Some parameters are still global and move to the Experiment 1 section as they are not part of the `create_cagan_adaptive_model`) - change `inv` to `solve` (I also modified the name of `solve` function to `solve_cagan_adaptive` to avoid confusion with the `solve` function in `np`) - remove extra print when checking the stability of dynamics. - in the section of 14.6. Technical condition for stability and add a new section that focuses on experiments. - give names to matrices and equations so that they can be referred in the code comment (I add comment in code to suggest matrix D is the coefficient matrix of equation (14.8)) - Add comments in this function and give titles to subplots. - Change subtitle "Forcast error" -> "Forcast error and model computation" - Change the time notations for equations in the section "Experiment 1"
1 parent 3c7f340 commit d6a9a03

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

lectures/cagan_adaptive.md

+21-33
Original file line numberDiff line numberDiff line change
@@ -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
@@ -296,53 +296,36 @@ import matplotlib.pyplot as plt
296296
Cagan_Adaptive = namedtuple("Cagan_Adaptive",
297297
["α", "m0", "Eπ0", "T", "λ"])
298298
299-
def create_cagan_adaptive_model(α, m0, Eπ0, T, λ):
299+
def create_cagan_adaptive_model(α = 5, m0 = 1, Eπ0 = 0.5, T=80, λ = 0.9):
300300
return Cagan_Adaptive(α, m0, Eπ0, T, λ)
301-
```
302-
+++ {"user_expressions": []}
303-
304-
Here we define the parameters.
305301
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
316-
317-
md = create_cagan_adaptive_model(α=α, m0=m0, Eπ0=μ0, T=T, λ=λ)
302+
md = create_cagan_adaptive_model()
318303
```
319304
+++ {"user_expressions": []}
320305
321306
We solve the model and plot variables of interests using the following functions.
322307
323308
```{code-cell} ipython3
324-
def solve(model, μ_seq):
309+
def solve_cagan_adaptive(model, μ_seq):
325310
" 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
311+
α, m0, Eπ0, T, λ = model
329312
330313
A = np.eye(T+2, T+2) - λ*np.eye(T+2, T+2, k=-1)
331314
B = np.eye(T+2, T+1, k=-1)
332315
C = -α*np.eye(T+1, T+2) + α*np.eye(T+1, T+2, k=1)
333316
Eπ0_seq = np.append(Eπ0, np.zeros(T+1))
334317
335318
# Eπ_seq is of length T+2
336-
Eπ_seq = np.linalg.inv(A - (1-λ)*B @ C) @ ((1-λ) * B @ μ_seq + Eπ0_seq)
319+
Eπ_seq = np.linalg.solve(A - (1-λ)*B @ C, (1-λ) * B @ μ_seq + Eπ0_seq)
337320
338321
# π_seq is of length T+1
339322
π_seq = μ_seq + C @ Eπ_seq
340323
341-
D = np.eye(T+1, T+1) - np.eye(T+1, T+1, k=-1)
324+
D = np.eye(T+1, T+1) - np.eye(T+1, T+1, k=-1) # D is the coefficient matrix in Equation (14.8)
342325
m0_seq = np.append(m0, np.zeros(T))
343326
344327
# m_seq is of length T+2
345-
m_seq = np.linalg.inv(D) @ (μ_seq + m0_seq)
328+
m_seq = np.linalg.solve(D, μ_seq + m0_seq)
346329
m_seq = np.append(m0, m_seq)
347330
348331
# p_seq is of length T+2
@@ -356,7 +339,7 @@ def solve(model, μ_seq):
356339
```{code-cell} ipython3
357340
def solve_and_plot(model, μ_seq):
358341
359-
π_seq, Eπ_seq, m_seq, p_seq = solve(model, μ_seq)
342+
π_seq, Eπ_seq, m_seq, p_seq = solve_cagan_adaptive(model, μ_seq)
360343
361344
T_seq = range(model.T+2)
362345
@@ -369,10 +352,12 @@ def solve_and_plot(model, μ_seq):
369352
ax[4].plot(T_seq, p_seq)
370353
371354
y_labs = [r'$\mu$', r'$\pi$', r'$m - p$', r'$m$', r'$p$']
355+
subplot_title = [r'Money supply growth', r'Inflation', r'Real balances', r'Money supply', r'Price level']
372356
373357
for i in range(5):
374358
ax[i].set_xlabel(r'$t$')
375359
ax[i].set_ylabel(y_labs[i])
360+
ax[i].set_title(subplot_title[i])
376361
377362
ax[1].legend()
378363
plt.tight_layout()
@@ -406,12 +391,10 @@ By assuring that the coefficient on $\pi_t$ is less than one in absolute value,
406391
The reader is free to study outcomes in examples that violate condition {eq}`eq:suffcond`.
407392
408393
```{code-cell} ipython3
409-
print(np.abs((λ - α*(1-λ))/(1 - α*(1-λ))))
394+
print(np.abs((md.λ - md.α*(1-md.λ))/(1 - md.α*(1-md.λ))))
410395
```
411396
412-
```{code-cell} ipython3
413-
print(λ - α*(1-λ))
414-
```
397+
## Experiments
415398
416399
Now we'll turn to some experiments.
417400
@@ -425,7 +408,7 @@ Thus, let $T_1 \in (0, T)$.
425408
So where $\mu_0 > \mu^*$, we assume that
426409
427410
$$
428-
\mu_{t+1} = \begin{cases}
411+
\mu_{t} = \begin{cases}
429412
\mu_0 , & t = 0, \ldots, T_1 -1 \\
430413
\mu^* , & t \geq T_1
431414
\end{cases}
@@ -436,7 +419,12 @@ Notice that we studied exactly this experiment in a rational expectations vers
436419
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.
437420
438421
```{code-cell} ipython3
439-
μ_seq_1 = np.append(μ0*np.ones(T1), μ_star*np.ones(T+1-T1))
422+
# Parameters for the experiment 1
423+
T1 = 60
424+
μ0 = 0.5
425+
μ_star = 0
426+
427+
μ_seq_1 = np.append(μ0*np.ones(T1), μ_star*np.ones(md.T+1-T1))
440428
441429
# solve and plot
442430
π_seq_1, Eπ_seq_1, m_seq_1, p_seq_1 = solve_and_plot(md, μ_seq_1)
@@ -460,7 +448,7 @@ The sluggish fall in inflation is explained by how anticipated inflation $\pi_t
460448
```{code-cell} ipython3
461449
# parameters
462450
ϕ = 0.9
463-
μ_seq_2 = np.array([ϕ**t * μ0 + (1-ϕ**t)*μ_star for t in range(T)])
451+
μ_seq_2 = np.array([ϕ**t * μ0 + (1-ϕ**t)*μ_star for t in range(md.T)])
464452
μ_seq_2 = np.append(μ_seq_2, μ_star)
465453
466454

0 commit comments

Comments
 (0)