From 88d4d21e11c765bfd07630a411a4a1bc9ae0e81c Mon Sep 17 00:00:00 2001 From: Humphrey Yang Date: Tue, 23 Jul 2024 23:22:18 +1000 Subject: [PATCH 1/3] address feedbacks from reading group --- lectures/time_series_with_matrices.md | 63 ++++++++++++++------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/lectures/time_series_with_matrices.md b/lectures/time_series_with_matrices.md index 1e88336d..23d413f2 100644 --- a/lectures/time_series_with_matrices.md +++ b/lectures/time_series_with_matrices.md @@ -4,7 +4,7 @@ jupytext: extension: .md format_name: myst format_version: 0.13 - jupytext_version: 1.16.2 + jupytext_version: 1.16.1 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -49,7 +49,9 @@ We will use the following imports: import numpy as np import matplotlib.pyplot as plt from matplotlib import cm -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size +plt.rcParams["figure.figsize"] = (11, 5) # Set default figure size +# Set decimal printing to 3 decimal places +np.set_printoptions(precision=3, suppress=True) ``` ## Samuelson's model @@ -142,8 +144,8 @@ T = 80 α_1 = 1.53 α_2 = -.9 -y_neg1 = 28. # y_{-1} -y_0 = 24. +y_neg1 = 28.0 # y_{-1} +y_0 = 24.0 ``` Now we construct $A$ and $b$. @@ -197,6 +199,13 @@ point precision: np.allclose(y, y_second_method) ``` +$A$ is invertible as it is lower triangular and [its diagonal entries are non-zero](https://www.statlect.com/matrix-algebra/triangular-matrix) + +```{code-cell} ipython3 +# Check if A is lower triangular +np.allclose(A, np.tril(A)) +``` + ```{note} In general, `np.linalg.solve` is more numerically stable than using `np.linalg.inv` directly. @@ -392,7 +401,13 @@ class population_moments: Parameters: α_0, α_1, α_2, T, y_neg1, y_0 """ - def __init__(self, α_0, α_1, α_2, T, y_neg1, y_0, σ_u): + def __init__(self, α_0=10.0, + α_1=1.53, + α_2=-.9, + T=80, + y_neg1=28.0, + y_0=24.0, + σ_u=1): # compute A A = np.identity(T) @@ -437,8 +452,7 @@ class population_moments: return self.μ_y, self.Σ_y -series_process = population_moments( - α_0=10.0, α_1=1.53, α_2=-.9, T=80, y_neg1=28., y_0=24., σ_u=1) +series_process = population_moments() μ_y, Σ_y = series_process.get_moments() A_inv = series_process.A_inv @@ -483,12 +497,17 @@ Notice how the population variance increases and asymptotes. Let's print out the covariance matrix $\Sigma_y$ for a time series $y$. ```{code-cell} ipython3 -series_process = population_moments( - α_0=0, α_1=.8, α_2=0, T=6, y_neg1=0., y_0=0., σ_u=1) +series_process = population_moments(α_0=0, + α_1=.8, + α_2=0, + T=6, + y_neg1=0., + y_0=0., + σ_u=1) μ_y, Σ_y = series_process.get_moments() print("μ_y = ", μ_y) -print("Σ_y = ", Σ_y) +print("Σ_y = \n", Σ_y) ``` Notice that the covariance between $y_t$ and $y_{t-1}$ -- the elements on the superdiagonal -- are *not* identical. @@ -502,9 +521,9 @@ We describe how to do that in [Linear State Space Models](https://python.quantec But just to set the stage for that analysis, let's print out the bottom right corner of $\Sigma_y$. ```{code-cell} ipython3 -series_process = population_moments( - α_0=10.0, α_1=1.53, α_2=-.9, T=80, y_neg1=28., y_0=24., σ_u=1) +series_process = population_moments() μ_y, Σ_y = series_process.get_moments() + print("bottom right corner of Σ_y = \n", Σ_y[72:,72:]) ``` @@ -529,26 +548,13 @@ To study the structure of $A^{-1}$, we shall print just up to $3$ decimals. Let's begin by printing out just the upper left hand corner of $A^{-1}$. ```{code-cell} ipython3 -with np.printoptions(precision=3, suppress=True): - print(A_inv[0:7,0:7]) +print(A_inv[0:7,0:7]) ``` Evidently, $A^{-1}$ is a lower triangular matrix. - -Let's print out the lower right hand corner of $A^{-1}$ and stare at it. - -```{code-cell} ipython3 -with np.printoptions(precision=3, suppress=True): - print(A_inv[72:,72:]) -``` - Notice how every row ends with the previous row's pre-diagonal entries. - - - - Since $A^{-1}$ is lower triangular, each row represents $ y_t$ for a particular $t$ as the sum of - a time-dependent function $A^{-1} b$ of the initial conditions incorporated in $b$, and - a weighted sum of current and past values of the IID shocks $\{u_t\}$. @@ -566,9 +572,6 @@ This is a **moving average** representation with time-varying coefficients. Just as system {eq}`eq:eqma` constitutes a **moving average** representation for $y$, system {eq}`eq:eqar` constitutes an **autoregressive** representation for $y$. - - - ## A forward looking model Samuelson’s model is *backward looking* in the sense that we give it *initial conditions* and let it @@ -638,7 +641,7 @@ for i in range(T): ``` ```{code-cell} ipython3 -B +print(B) ``` ```{code-cell} ipython3 From 1a0a299322eec5e4c42274cf3448acb1ff5af969 Mon Sep 17 00:00:00 2001 From: Humphrey Yang Date: Fri, 26 Jul 2024 16:09:02 +1000 Subject: [PATCH 2/3] update based on feedback --- lectures/time_series_with_matrices.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lectures/time_series_with_matrices.md b/lectures/time_series_with_matrices.md index 23d413f2..285affef 100644 --- a/lectures/time_series_with_matrices.md +++ b/lectures/time_series_with_matrices.md @@ -49,7 +49,10 @@ We will use the following imports: import numpy as np import matplotlib.pyplot as plt from matplotlib import cm -plt.rcParams["figure.figsize"] = (11, 5) # Set default figure size + +# Custom figsize required for this lecture +plt.rcParams["figure.figsize"] = (11, 5) + # Set decimal printing to 3 decimal places np.set_printoptions(precision=3, suppress=True) ``` From 0c33b84b836a4270d691c9cdc64e73588bf1803f Mon Sep 17 00:00:00 2001 From: Humphrey Yang Date: Fri, 26 Jul 2024 16:56:03 +1000 Subject: [PATCH 3/3] remove "for" --- lectures/time_series_with_matrices.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lectures/time_series_with_matrices.md b/lectures/time_series_with_matrices.md index 285affef..e73566ce 100644 --- a/lectures/time_series_with_matrices.md +++ b/lectures/time_series_with_matrices.md @@ -50,7 +50,7 @@ import numpy as np import matplotlib.pyplot as plt from matplotlib import cm -# Custom figsize required for this lecture +# Custom figsize for this lecture plt.rcParams["figure.figsize"] = (11, 5) # Set decimal printing to 3 decimal places