@@ -11,6 +11,8 @@ kernelspec:
1111 name : python3
1212---
1313
14+ +++ {"user_expressions": [ ] }
15+
1416# Consumption smoothing
1517
1618## Overview
@@ -25,6 +27,8 @@ import matplotlib.pyplot as plt
2527from collections import namedtuple
2628```
2729
30+ +++ {"user_expressions": [ ] }
31+
2832Let
2933
3034 * $T \geq 2$ be a positive integer that constitutes a time-horizon
@@ -92,6 +96,8 @@ def creat_cs_model(R=1.05, g1=1, g2=1/2, T=65):
9296 β_seq=β_seq, T=65)
9397```
9498
99+ +++ {"user_expressions": [ ] }
100+
95101## Difference equations with linear algebra
96102
97103As a warmup, we'll describe a useful way of representing and "solving" linear difference equations.
132138
133139Multiplying both sides by inverse of the matrix on the left provides the solution
134140
135- $$
141+ ``` {math}
142+ :label: fst_ord_inverse
143+
136144\begin{bmatrix}
137145y_1 \cr y_2 \cr y_3 \cr \vdots \cr y_T
138146\end{bmatrix}
@@ -142,22 +150,49 @@ y_1 \cr y_2 \cr y_3 \cr \vdots \cr y_T
142150\lambda & 1 & 0 & \cdots & 0 & 0 \cr
143151\lambda^2 & \lambda & 1 & \cdots & 0 & 0 \cr
144152 \vdots & \vdots & \vdots & \cdots & \vdots & \vdots \cr
145- \lambda^{T-1} & \lambda^{T-2} & \lambda^{T-3} & \cdots & - \lambda & 1
153+ \lambda^{T-1} & \lambda^{T-2} & \lambda^{T-3} & \cdots & \lambda & 1
146154\end{bmatrix}
147155\begin{bmatrix}
148156\lambda y_0 \cr 0 \cr 0 \cr \vdots \cr 0
149157\end{bmatrix}
158+ ```
159+
160+ ``` {exercise}
161+ :label: consmooth_ex1
162+
163+ In the {numref}`fst_ord_inverse`, we multiply the inverse of the matrix on the left ($A$). In this exercise, please confirm that
164+
165+ $$
166+ \begin{bmatrix}
167+ 1 & 0 & 0 & \cdots & 0 & 0 \cr
168+ \lambda & 1 & 0 & \cdots & 0 & 0 \cr
169+ \lambda^2 & \lambda & 1 & \cdots & 0 & 0 \cr
170+ \vdots & \vdots & \vdots & \cdots & \vdots & \vdots \cr
171+ \lambda^{T-1} & \lambda^{T-2} & \lambda^{T-3} & \cdots & \lambda & 1
172+ \end{bmatrix}
150173$$
151174
175+ is indeed the inverse of $A$ and check that $A A^{-1} = I$
176+
177+ ```
178+
152179### Second order difference equation
153180
181+ The second-order linear difference equation for $\{ y_t\} _ {t=0}^T$ is
182+
183+ $$
184+ y_{t} = \lambda_1 y_{t-1} + \lambda_2 y_{t-2}, \quad t = 1, 2, \ldots, T
185+ $$
186+
187+ Similarly, we can cast this set of $T$ equations as a single matrix equation
188+
154189$$
155190\begin{bmatrix}
1561911 & 0 & 0 & \cdots & 0 & 0 & 0 \cr
157192-\lambda_1 & 1 & 0 & \cdots & 0 & 0 & 0 \cr
158193-\lambda_2 & -\lambda_1 & 1 & \cdots & 0 & 0 & 0 \cr
159194 \vdots & \vdots & \vdots & \cdots & \vdots & \vdots \cr
160- 0 & 0 & 0 & \cdots & \lambda_2 & -\lambda_1 & 1
195+ 0 & 0 & 0 & \cdots & - \lambda_2 & -\lambda_1 & 1
161196\end{bmatrix}
162197\begin{bmatrix}
163198y_1 \cr y_2 \cr y_3 \cr \vdots \cr y_T
170205
171206Multiplying both sides by inverse of the matrix on the left again provides the solution.
172207
173- ### Extensions
208+ ``` {exercise}
209+ :label: consmooth_ex2
174210
175211As an exercise, we ask you to represent and solve a **third order linear difference equation**.
176212How many initial conditions must you specify?
213+ ```
177214
178215## Friedman-Hall consumption-smoothing model
179216
@@ -238,6 +275,7 @@ def compute_optimal(model, a0, y_seq):
238275 return c_seq, a_seq
239276```
240277
278+ +++ {"user_expressions": [ ] }
241279
242280## Permanent income model of consumption
243281
@@ -313,7 +351,8 @@ y_seq = np.concatenate([np.ones(46), np.zeros(20)])
313351cs_model = creat_cs_model()
314352c_seq, a_seq = compute_optimal(cs_model, a0, y_seq)
315353
316- print('check a_T+1=0:', np.abs(a_seq[-1] - 0) <= 1e-8)
354+ print('check a_T+1=0:',
355+ np.abs(a_seq[-1] - 0) <= 1e-8)
317356```
318357
319358``` {code-cell} ipython3
@@ -331,6 +370,8 @@ plt.ylabel(r'$c_t,y_t,a_t$')
331370plt.show()
332371```
333372
373+ +++ {"user_expressions": [ ] }
374+
334375We can evaluate the welfare using the formula {eq}` welfare `
335376
336377``` {code-cell} ipython3
@@ -343,7 +384,7 @@ def welfare(model, c_seq):
343384print('Welfare:', welfare(cs_model, c_seq))
344385```
345386
346-
387+ +++ {"user_expressions": [ ] }
347388
348389### Feasible consumption variations ###
349390
@@ -415,6 +456,7 @@ def compute_variation(model, ξ1, ϕ, a0, y_seq, verbose=1):
415456 return cvar_seq
416457```
417458
459+ +++ {"user_expressions": [ ] }
418460
419461We visualize variations with $\xi_1 \in \{ .01, .05\} $ and $\phi \in \{ .95, 1.02\} $
420462
@@ -439,17 +481,20 @@ for i, param in enumerate(params):
439481 ls = '-.'
440482 else:
441483 ls = '-'
442- ax.plot(range(T+1), cvar_seq, ls=ls, color=colors[ξ1], label=fr'$\xi_1 = {ξ1}, \phi = {ϕ}$')
484+ ax.plot(range(T+1), cvar_seq, ls=ls,
485+ color=colors[ξ1],
486+ label=fr'$\xi_1 = {ξ1}, \phi = {ϕ}$')
443487
444- plt.plot(range(T+1), c_seq, color='orange', label=r'Optimal $\vec{c}$ ')
488+ plt.plot(range(T+1), c_seq,
489+ color='orange', label=r'Optimal $\vec{c}$ ')
445490
446491plt.legend()
447492plt.xlabel(r'$t$')
448493plt.ylabel(r'$c_t$')
449494plt.show()
450495```
451496
452-
497+ +++ {"user_expressions": [ ] }
453498
454499We can even use the Python ` np.gradient ` command to compute derivatives of welfare with respect to our two parameters.
455500
@@ -459,16 +504,22 @@ First, we define the welfare with respect to $\xi_1$ and $\phi$
459504
460505``` {code-cell} ipython3
461506def welfare_rel(ξ1, ϕ):
462- "Compute welfare of variation sequence for given ϕ, ξ1 with a consumption smoothing model"
463- cvar_seq = compute_variation(cs_model, ξ1=ξ1, ϕ=ϕ, a0=a0,
464- y_seq=y_seq, verbose=0)
507+ """
508+ Compute welfare of variation sequence
509+ for given ϕ, ξ1 with a consumption smoothing model
510+ """
511+
512+ cvar_seq = compute_variation(cs_model, ξ1=ξ1,
513+ ϕ=ϕ, a0=a0,
514+ y_seq=y_seq,
515+ verbose=0)
465516 return welfare(cs_model, cvar_seq)
466517
467518# Vectorize the function to allow array input
468519welfare_vec = np.vectorize(welfare_rel)
469520```
470521
471-
522+ +++ {"user_expressions": [ ] }
472523
473524Then we can visualize the relationship between welfare and $\xi_1$ and compute its derivatives
474525
@@ -488,6 +539,8 @@ plt.xlabel(r'$\xi_1$')
488539plt.show()
489540```
490541
542+ +++ {"user_expressions": [ ] }
543+
491544The same can be done on $\phi$
492545
493546``` {code-cell} ipython3
0 commit comments