@@ -3,8 +3,10 @@ jupytext:
33 text_representation :
44 extension : .md
55 format_name : myst
6+ format_version : 0.13
7+ jupytext_version : 1.16.7
68kernelspec :
7- display_name : Python 3
9+ display_name : Python 3 (ipykernel)
810 language : python
911 name : python3
1012---
@@ -29,10 +31,9 @@ kernelspec:
2931
3032In addition to what's in Anaconda, this lecture will need the following libraries:
3133
32- ``` {code-cell} ipython
33- ---
34- tags: [hide-output]
35- ---
34+ ``` {code-cell} ipython3
35+ :tags: [hide-output]
36+
3637!pip install quantecon
3738```
3839
@@ -74,9 +75,8 @@ The model will prove useful for illustrating concepts such as
7475
7576Let's start with some imports:
7677
77- ``` {code-cell} ipython
78+ ``` {code-cell} ipython3
7879import matplotlib.pyplot as plt
79- plt.rcParams["figure.figsize"] = (11, 5) #set default figure size
8080import quantecon as qe
8181import numpy as np
8282import scipy.linalg as la
@@ -329,7 +329,7 @@ In what follows we set it equal to unity.
329329
330330First, we create the objects for the optimal linear regulator
331331
332- ``` {code-cell} python3
332+ ``` {code-cell} ipython3
333333# Set parameters
334334α, β, ρ1, ρ2, σ = 10.0, 0.95, 0.9, 0.0, 1.0
335335
@@ -364,7 +364,7 @@ sxbewley = sxo
364364
365365The next step is to create the matrices for the LQ system
366366
367- ``` {code-cell} python3
367+ ``` {code-cell} ipython3
368368A12 = np.zeros((3,1))
369369ALQ_l = np.hstack([A, A12])
370370ALQ_r = np.array([[0, -R, 0, R]])
@@ -383,7 +383,7 @@ CLQ = np.array([0., σ, 0., 0.]).reshape(4,1)
383383
384384Let's print these out and have a look at them
385385
386- ``` {code-cell} python3
386+ ``` {code-cell} ipython3
387387print(f"A = \n {ALQ}")
388388print(f"B = \n {BLQ}")
389389print(f"R = \n {RLQ}")
@@ -392,14 +392,14 @@ print(f"Q = \n {QLQ}")
392392
393393Now create the appropriate instance of an LQ model
394394
395- ``` {code-cell} python3
395+ ``` {code-cell} ipython3
396396lqpi = qe.LQ(QLQ, RLQ, ALQ, BLQ, C=CLQ, beta=β_LQ)
397397```
398398
399399We'll save the implied optimal policy function soon compare them with what we get by
400400employing an alternative solution method
401401
402- ``` {code-cell} python3
402+ ``` {code-cell} ipython3
403403P, F, d = lqpi.stationary_values() # Compute value function and decision rule
404404ABF = ALQ - BLQ @ F # Form closed loop system
405405```
428428
429429Now we'll apply the formulas in this system
430430
431- ``` {code-cell} python3
431+ ``` {code-cell} ipython3
432432# Use the above formulas to create the optimal policies for b_{t+1} and c_t
433433b_pol = G @ la.inv(np.eye(3, 3) - β * A) @ (A - np.eye(3, 3))
434434c_pol = (1 - β) * G @ la.inv(np.eye(3, 3) - β * A)
@@ -453,13 +453,13 @@ G_LSS = np.hstack([G_LSS1, G_LSS2])
453453
454454` A_LSS ` calculated as we have here should equal ` ABF ` calculated above using the LQ model
455455
456- ``` {code-cell} python3
456+ ``` {code-cell} ipython3
457457ABF - A_LSS
458458```
459459
460460Now compare pertinent elements of ` c_pol ` and ` F `
461461
462- ``` {code-cell} python3
462+ ``` {code-cell} ipython3
463463print(c_pol, "\n", -F)
464464```
465465
@@ -501,7 +501,7 @@ A second graph plots a collection of simulations against the population distrib
501501
502502Comparing sample paths with population distributions at each date $t$ is a useful exercise---see {ref}` our discussion <lln_mr> ` of the laws of large numbers
503503
504- ``` {code-cell} python3
504+ ``` {code-cell} ipython3
505505lss = qe.LinearStateSpace(A_LSS, C_LSS, G_LSS, mu_0=μ_0, Sigma_0=Σ_0)
506506```
507507
@@ -514,7 +514,7 @@ In the code below, we use the [LinearStateSpace](https://github.com/QuantEcon/Qu
514514- simulate a group of 25 consumers and plot sample paths on the same
515515 graph as the population distribution.
516516
517- ``` {code-cell} python3
517+ ``` {code-cell} ipython3
518518def income_consumption_debt_series(A, C, G, μ_0, Σ_0, T=150, npaths=25):
519519 """
520520 This function takes initial conditions (μ_0, Σ_0) and uses the
@@ -545,8 +545,8 @@ def income_consumption_debt_series(A, C, G, μ_0, Σ_0, T=150, npaths=25):
545545 debt_var = np.empty(T)
546546 for t in range(T):
547547 μ_x, μ_y, Σ_x, Σ_y = next(moment_generator)
548- cons_mean[t], cons_var[t] = μ_y[1], Σ_y[1, 1]
549- debt_mean[t], debt_var[t] = μ_x[3], Σ_x[3, 3]
548+ cons_mean[t], cons_var[t] = μ_y[1,0 ], Σ_y[1, 1]
549+ debt_mean[t], debt_var[t] = μ_x[3,0 ], Σ_x[3, 3]
550550
551551 return bsim, csim, ysim, cons_mean, cons_var, debt_mean, debt_var
552552
@@ -622,7 +622,7 @@ def consumption_debt_fanchart(csim, cons_mean, cons_var,
622622
623623Now let's create figures with initial conditions of zero for $y_0$ and $b_0$
624624
625- ``` {code-cell} python3
625+ ``` {code-cell} ipython3
626626out = income_consumption_debt_series(A_LSS, C_LSS, G_LSS, μ_0, Σ_0)
627627bsim0, csim0, ysim0 = out[:3]
628628cons_mean0, cons_var0, debt_mean0, debt_var0 = out[3:]
@@ -632,7 +632,7 @@ consumption_income_debt_figure(bsim0, csim0, ysim0)
632632plt.show()
633633```
634634
635- ``` {code-cell} python3
635+ ``` {code-cell} ipython3
636636consumption_debt_fanchart(csim0, cons_mean0, cons_var0,
637637 bsim0, debt_mean0, debt_var0)
638638
@@ -698,7 +698,7 @@ behavior early in the sample.
698698
699699By altering initial conditions, we shall remove this transient in our second example to be presented below
700700
701- ``` {code-cell} python3
701+ ``` {code-cell} ipython3
702702def cointegration_figure(bsim, csim):
703703 """
704704 Plots the cointegration
@@ -713,7 +713,7 @@ def cointegration_figure(bsim, csim):
713713 return fig
714714```
715715
716- ``` {code-cell} python3
716+ ``` {code-cell} ipython3
717717cointegration_figure(bsim0, csim0)
718718plt.show()
719719```
@@ -756,7 +756,7 @@ There is no need for foreigners to lend to our group.
756756
757757Let's have a look at the corresponding figures
758758
759- ``` {code-cell} python3
759+ ``` {code-cell} ipython3
760760out = income_consumption_debt_series(A_LSS, C_LSS, G_LSS, mxbewley, sxbewley)
761761bsimb, csimb, ysimb = out[:3]
762762cons_meanb, cons_varb, debt_meanb, debt_varb = out[3:]
@@ -766,7 +766,7 @@ consumption_income_debt_figure(bsimb, csimb, ysimb)
766766plt.show()
767767```
768768
769- ``` {code-cell} python3
769+ ``` {code-cell} ipython3
770770consumption_debt_fanchart(csimb, cons_meanb, cons_varb,
771771 bsimb, debt_meanb, debt_varb)
772772
@@ -785,7 +785,7 @@ But now there is some initial dispersion because there is *ex-ante* heterogeneit
785785
786786Let's have a look at the cointegration figure
787787
788- ``` {code-cell} python3
788+ ``` {code-cell} ipython3
789789cointegration_figure(bsimb, csimb)
790790plt.show()
791791```
0 commit comments