From e29b7c3a955503e3bdcb6c9887fea2c7980e83fd Mon Sep 17 00:00:00 2001 From: hengchengzhang Date: Wed, 5 Jul 2023 13:32:43 +1000 Subject: [PATCH 01/11] Remove figure settings --- lectures/linear_equations.md | 1 - 1 file changed, 1 deletion(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index 84f43f0d..abf1e94a 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -48,7 +48,6 @@ We will use the following imports: ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) # set default figure size ``` From 6642ebc246121791c8f70d4639dcbd6abe728867 Mon Sep 17 00:00:00 2001 From: hengchengzhang Date: Wed, 5 Jul 2023 14:38:42 +1000 Subject: [PATCH 02/11] Minor updates --- lectures/linear_equations.md | 67 ++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index abf1e94a..f02fe007 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -26,14 +26,14 @@ Many problems in economics and finance require solving linear equations. In this lecture we discuss linear equations and their applications. -To illustrate the importance of linear equations, we begin with a two good +To illustrate the importance of linear equations, we begin with a two goods model of supply and demand. -The two good case is so simple that solutions can be calculated by hand. +The two goods case is so simple that solutions can be calculated by hand. But often we need to consider markets containing many goods. -In this case we face large systems of linear equations, with many equations +In the multiple goods case we face large systems of linear equations, with many equations and unknowns. To handle such systems we need two things: @@ -52,9 +52,9 @@ import matplotlib.pyplot as plt -## A two good example +## A two goods example -In this section we discuss a simple two good example and solve it by +In this section we discuss a simple two goods example and solve it by 1. pencil and paper 2. matrix algebra @@ -122,7 +122,7 @@ $$ ### Looking forward -Pencil and paper methods are easy in the two good case. +Pencil and paper methods are easy in the two goods case. But what if there are many goods? @@ -138,7 +138,7 @@ basics of vectors and matrices, in both theory and computation. ```{index} single: Linear Algebra; Vectors ``` -A **vector** of length $n$ is just a sequence (or array, or tuple) of $n$ numbers, which we write as $x = (x_1, \ldots, x_n)$. +A **vector** of length $n$ is just a sequence (or array, or tuple) of $n$ numbers, which we write as $x = (x_1, \ldots, x_n)$ or $x = \begin{bmatrix}x_1, \ldots, x_n\end{bmatrix}$. We can write these sequences either horizontally or vertically. @@ -157,7 +157,7 @@ Often vectors are represented visually as arrows from the origin to the point. Here's a visualization. ```{code-cell} ipython3 -:tags: [hide_input] +:tags: [hide-input] fig, ax = plt.subplots() # Set the axes through the origin @@ -242,7 +242,7 @@ $$ We can visualise vector addition in $\mathbb{R}^2$ as follows. ```{code-cell} ipython3 -:tags: [hide_input] +:tags: [hide-input] fig, ax = plt.subplots() # Set the axes through the origin @@ -314,7 +314,7 @@ $$ Scalar multiplication is illustrated in the next figure. ```{code-cell} ipython3 -:tags: [hide_input] +:tags: [hide-input] fig, ax = plt.subplots() # Set the axes through the origin @@ -376,7 +376,7 @@ x + y # Add (element-by-element) The **inner product** of vectors $x,y \in \mathbb R^n$ is defined as $$ -x' y = +x^\top y = \begin{bmatrix} \color{red}{x_1} & \color{blue}{x_2} & \cdots & x_n \end{bmatrix} @@ -394,7 +394,7 @@ The **norm** of a vector $x$ represents its "length" (i.e., its distance from the zero vector) and is defined as $$ - \| x \| := \sqrt{x' x} := \left( \sum_{i=1}^n x_i^2 \right)^{1/2} + \| x \| := \sqrt{x^\top x} := \left( \sum_{i=1}^n x_i^2 \right)^{1/2} $$ The expression $\| x - y\|$ can be thought of as the "distance" between $x$ and $y$. @@ -658,7 +658,7 @@ To get the transpose of `A`, use `A.transpose()` or, more simply, `A.T`. There are many convenient functions for creating common matrices (matrices of zeros, ones, etc.) --- see [here](https://python-programming.quantecon.org/numpy.html#creating-arrays). -Since operations are performed elementwise by default, scalar multiplication and addition have very natural syntax +Since operations are performed elementwise by default, scalar multiplication and addition have very natural syntaxes ```{code-cell} ipython3 A = np.identity(3) # 3 x 3 identity matrix @@ -677,9 +677,9 @@ To multiply matrices we use the `@` symbol. In particular, `A @ B` is matrix multiplication, whereas `A * B` is element-by-element multiplication. ``` -### Two good model in matrix form +### Two goods model in matrix form -We can now revisit the two good model and solve {eq}`two_equilibrium` +We can now revisit the two goods model and solve {eq}`two_equilibrium` numerically via matrix algebra. This involves some extra steps but the method is widely applicable --- as we @@ -710,6 +710,8 @@ First we rewrite {eq}`two_eq_demand` as \end{bmatrix} ``` +Recall that $p \in \mathbb{R}^{2}$ is the price of two goods. + (Please check that $q^d = D p + h$ represents the same equations as {eq}`two_eq_demand`.) We rewrite {eq}`two_eq_supply` as @@ -768,21 +770,21 @@ including crude oil, gasoline, coal, natural gas, ethanol and uranium. The prices of these goods are related, so it makes sense to study them together. -Pencil and paper solutions become very time consuming with large systems. +Pencil and paper method become very time consuming with large systems. But fortunately the matrix methods described above are essentially unchanged. In general, we can write the demand equation as $q^d = Dp + h$, where -* $q^d$ is an $n \times 1$ vector of demand quantities for $n$ different goods -* $D$ is an $n \times n$ "coefficient" matrix -* $h$ is an $n \times 1$ vector of constant values +* $q^d$ is an $n \times 1$ vector of demand quantities for $n$ different goods. +* $D$ is an $n \times n$ "coefficient" matrix. +* $h$ is an $n \times 1$ vector of constant values. Similarly, we can write the supply equation as $q^s = Cp + e$, where -* $q^d$ is an $n \times 1$ vector of supply quantities for the same goods -* $C$ is an $n \times n$ "coefficient" matrix -* $e$ is an $n \times 1$ vector of constant values +* $q^d$ is an $n \times 1$ vector of supply quantities for the same goods. +* $C$ is an $n \times n$ "coefficient" matrix. +* $e$ is an $n \times 1$ vector of constant values. To find an equilibrium, we solve $Dp + h = Cp + e$, or @@ -1001,8 +1003,10 @@ If the determinant of $A$ is not zero, then we say that $A$ is *nonsingular*. A square matrix $A$ is nonsingular if and only if the rows and columns of $A$ are linearly independent. +A more detailed explanation of matrix inverse can be found [here](https://www.mathsisfun.com/algebra/matrix-inverse.html). + You can check yourself that the in {eq}`no_soln` and {eq}`many_solns` with -linearly dependent rows are singular matrices as well. +linearly dependent rows are singular matrices. This gives us a useful one-number summary of whether or not a square matrix can be inverted. @@ -1028,7 +1032,7 @@ This is the solution to $Ax = b$ --- the solution we are looking for. ```{index} single: Linear Algebra; SciPy ``` -In the two good example we obtained the matrix equation, +In the two goods example we obtained the matrix equation, $$ p = (C-D)^{-1} h @@ -1334,8 +1338,13 @@ x ```{code-cell} ipython3 #using numpy.linalg.lstsq -x = np.linalg.lstsq(A, b, rcond = None) -x +x, res, _, _ = np.linalg.lstsq(A, b, rcond=None) +``` + +```{code-cell} ipython3 +:tags: [hide-input] +print(f"x\u0302 = {x}") +print(f"\u2016Ax\u0302 - b\u2016\u00B2 = {res[0]}") ``` Here is a visualization of how the least squares method approximates the equation of a line connecting a set of points. @@ -1347,10 +1356,10 @@ fig, ax = plt.subplots() p = np.array((1, 3, 8)) q = np.array((9, 7 ,3)) -a, b = x[0] +a, b = x -plt.plot(q, p, 'o', label='observations', markersize=5) -plt.plot(q, a - b*q, 'r', label='Fitted line') +ax.plot(q, p, 'o', label='observations', markersize=5) +ax.plot(q, a - b*q, 'r', label='Fitted line') plt.xlabel('quantity demanded') plt.ylabel('price') plt.legend() From 1473277f8c85f6d48d4a8166773bb2aaabfab928 Mon Sep 17 00:00:00 2001 From: hengchengzhang Date: Wed, 5 Jul 2023 14:47:57 +1000 Subject: [PATCH 03/11] Adjust to PEP8 --- lectures/linear_equations.md | 84 +++++++++++++++++------------------- 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index f02fe007..0b6fb412 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -50,8 +50,6 @@ import numpy as np import matplotlib.pyplot as plt ``` - - ## A two goods example In this section we discuss a simple two goods example and solve it by @@ -252,13 +250,13 @@ for spine in ['right', 'top']: ax.spines[spine].set_color('none') ax.set(xlim=(-2, 10), ylim=(-4, 4)) -#ax.grid() -vecs = ((4, -2), (3, 3),(7,1)) -tags = ('(x1, x2)','(y1, y2)','(x1+x2, y1+y2)') -colors = ('blue','green','red') +# ax.grid() +vecs = ((4, -2), (3, 3), (7, 1)) +tags = ('(x1, x2)', '(y1, y2)', '(x1+x2, y1+y2)') +colors = ('blue', 'green', 'red') for i, v in enumerate(vecs): ax.annotate('', xy=v, xytext=(0, 0), - arrowprops=dict(color = colors[i], + arrowprops=dict(color=colors[i], shrink=0, alpha=0.7, width=0.5, @@ -266,9 +264,9 @@ for i, v in enumerate(vecs): headlength=15)) ax.text(v[0] + 0.2, v[1] + 0.1, tags[i]) -for i,v in enumerate(vecs): - ax.annotate('', xy=(7,1), xytext=v, - arrowprops=dict(color = 'gray', +for i, v in enumerate(vecs): + ax.annotate('', xy=(7, 1), xytext=v, + arrowprops=dict(color='gray', shrink=0, alpha=0.3, width=0.5, @@ -410,15 +408,13 @@ x @ y # Another way to compute the inner product ``` ```{code-cell} ipython3 -np.sqrt(np.sum(x**2)) # Norm of x, take one +np.sqrt(np.sum(x**2)) # Norm of x, method one ``` ```{code-cell} ipython3 -np.linalg.norm(x) # Norm of x, take two +np.linalg.norm(x) # Norm of x, method two ``` - - ## Matrix operations ```{index} single: Matrix; Operations @@ -902,7 +898,7 @@ To illustrate why this situation arises let's plot the two lines. ```{code-cell} ipython3 fig, ax = plt.subplots() -x = np.linspace(-10,10) +x = np.linspace(-10, 10) plt.plot(x, (3-x)/3, label=f'$x + 3y = 3$') plt.plot(x, (-8-2*x)/6, label=f'$2x + 6y = -8$') plt.legend() @@ -1047,7 +1043,7 @@ We can now solve for equilibrium prices with NumPy's `linalg` submodule. All of these routines are Python front ends to time-tested and highly optimized FORTRAN code. ```{code-cell} ipython3 -C = ((10, 5), #matrix C +C = ((10, 5), # Matrix C (5, 10)) ``` @@ -1058,34 +1054,35 @@ C = np.array(C) ``` ```{code-cell} ipython3 -D = ((-10, -5), #matrix D +D = ((-10, -5), # Matrix D (-1, -10)) D = np.array(D) ``` ```{code-cell} ipython3 -h = np.array((100, 50)) #vector h -h.shape = 2,1 #transforming h to a column vector +h = np.array((100, 50)) # Vector h +h.shape = 2,1 # Transforming h to a column vector ``` ```{code-cell} ipython3 from numpy.linalg import det, inv A = C - D -det(A) # check that A is nonsingular (non-zero determinant), and hence invertible +# Check that A is nonsingular (non-zero determinant), and hence invertible +det(A) ``` ```{code-cell} ipython3 -A_inv = inv(A) # Compute the inverse +A_inv = inv(A) # compute the inverse A_inv ``` ```{code-cell} ipython3 -p = A_inv @ h # equilibrium prices +p = A_inv @ h # equilibrium prices p ``` ```{code-cell} ipython3 -q = C @ p # equilibrium quantities +q = C @ p # equilibrium quantities q ``` @@ -1095,12 +1092,12 @@ We can also solve for $p$ using `solve(A, h)` as follows. ```{code-cell} ipython3 from numpy.linalg import solve -p = solve(A, h) # equilibrium prices +p = solve(A, h) # equilibrium prices p ``` ```{code-cell} ipython3 -q = C @ p # equilibrium quantities +q = C @ p # equilibrium quantities q ``` @@ -1188,22 +1185,22 @@ b = \end{bmatrix} $$ -```{code-cell} ipython3 +```{code-cell} import numpy as np from numpy.linalg import det -A = np.array([[35, -5, -5], #matrix A +A = np.array([[35, -5, -5], # matrix A [-5, 25, -10], [-5, -5, 15]]) -b = np.array((100, 75, 55)) #column vector b -b.shape = (3,1) +b = np.array((100, 75, 55)) # column vector b +b.shape = (3, 1) -det(A) #check if A is nonsingular +det(A) # check if A is nonsingular ``` -```{code-cell} ipython3 -#using inverse +```{code-cell} +# Using inverse from numpy.linalg import det A_inv = inv(A) @@ -1212,10 +1209,10 @@ p = A_inv @ b p ``` -```{code-cell} ipython3 -#using numpy.linalg.solve +```{code-cell} +# Using numpy.linalg.solve from numpy.linalg import solve -p = solve(A,b) +p = solve(A, b) p ``` @@ -1322,27 +1319,28 @@ from numpy.linalg import inv ``` ```{code-cell} ipython3 -#using matrix algebra -A = np.array([[1, -9], #matrix A +# Using matrix algebra +A = np.array([[1, -9], # matrix A [1, -7], [1, -3]]) -A_T = np.transpose(A) #transpose of matrix A +A_T = np.transpose(A) # transpose of matrix A -b = np.array((1, 3, 8)) #column vector b -b.shape = (3,1) +b = np.array((1, 3, 8)) # column vector b +b.shape = (3, 1) x = inv(A_T @ A) @ A_T @ b x ``` ```{code-cell} ipython3 -#using numpy.linalg.lstsq +# Using numpy.linalg.lstsq x, res, _, _ = np.linalg.lstsq(A, b, rcond=None) ``` ```{code-cell} ipython3 :tags: [hide-input] + print(f"x\u0302 = {x}") print(f"\u2016Ax\u0302 - b\u2016\u00B2 = {res[0]}") ``` @@ -1354,7 +1352,7 @@ We can also describe this as "fitting" a line between a set of points. ```{code-cell} ipython3 fig, ax = plt.subplots() p = np.array((1, 3, 8)) -q = np.array((9, 7 ,3)) +q = np.array((9, 7, 3)) a, b = x @@ -1375,5 +1373,3 @@ plt.show() The documentation of the `numpy.linalg` submodule can be found [here](https://numpy.org/devdocs/reference/routines.linalg.html). More advanced topics in linear algebra can be found [here](https://python.quantecon.org/linear_algebra.html#id5). - - From 3a1b78ff9ddae1d8b3aee0444d89c76e4d84c8a1 Mon Sep 17 00:00:00 2001 From: hengchengzhang Date: Wed, 5 Jul 2023 15:03:33 +1000 Subject: [PATCH 04/11] Minor updates --- lectures/linear_equations.md | 74 +++++++++++++++++------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index 0b6fb412..22bc3f9c 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -16,10 +16,6 @@ kernelspec: ```{index} single: Linear Equations and Matrix Algebra ``` -```{contents} Contents -:depth: 2 -``` - ## Overview Many problems in economics and finance require solving linear equations. @@ -64,7 +60,7 @@ The second method is more general, as we will see. Suppose that we have two related goods, such as -* propane and ethanol +* propane and ethanol. * rice and wheat, etc. To keep things simple, we label them as good 0 and good 1. @@ -147,8 +143,8 @@ The set of all $n$-vectors is denoted by $\mathbb R^n$. For example, -* $\mathbb R^2$ is the plane --- the set of pairs $(x_1, x_2)$ -* $\mathbb R^3$ is 3 dimensional space --- the set of vectors $(x_1, x_2, x_3)$ +* $\mathbb R^2$ is the plane --- the set of pairs $(x_1, x_2)$. +* $\mathbb R^3$ is 3 dimensional space --- the set of vectors $(x_1, x_2, x_3)$. Often vectors are represented visually as arrows from the origin to the point. @@ -210,7 +206,7 @@ $$ \begin{bmatrix} 7 \\ 1 -\end{bmatrix} +\end{bmatrix}. $$ In general, @@ -234,7 +230,7 @@ x + y = x_2 + y_2 \\ \vdots \\ x_n + y_n -\end{bmatrix} +\end{bmatrix}. $$ We can visualise vector addition in $\mathbb{R}^2$ as follows. @@ -294,7 +290,7 @@ $$ \begin{bmatrix} -6 \\ 14 -\end{bmatrix} +\end{bmatrix}. $$ More generally, it takes a number $\gamma$ and a vector $x$ and produces @@ -306,7 +302,7 @@ $$ \gamma x_2 \\ \vdots \\ \gamma x_n -\end{bmatrix} +\end{bmatrix}. $$ Scalar multiplication is illustrated in the next figure. @@ -385,14 +381,14 @@ x^\top y = y_n \end{bmatrix} = {\color{red}{x_1 y_1}} + {\color{blue}{x_2 y_2}} + \cdots + x_n y_n -:= \sum_{i=1}^n x_i y_i +:= \sum_{i=1}^n x_i y_i. $$ The **norm** of a vector $x$ represents its "length" (i.e., its distance from the zero vector) and is defined as $$ - \| x \| := \sqrt{x^\top x} := \left( \sum_{i=1}^n x_i^2 \right)^{1/2} + \| x \| := \sqrt{x^\top x} := \left( \sum_{i=1}^n x_i^2 \right)^{1/2}. $$ The expression $\| x - y\|$ can be thought of as the "distance" between $x$ and $y$. @@ -445,7 +441,7 @@ $$ \begin{bmatrix} 6 & -39 \\ 0 & 15 -\end{bmatrix} +\end{bmatrix}. $$ In general for a number $\gamma$ and any matrix $A$, @@ -462,7 +458,7 @@ $$ \gamma a_{11} & \cdots & \gamma a_{1k} \\ \vdots & \vdots & \vdots \\ \gamma a_{n1} & \cdots & \gamma a_{nk} -\end{bmatrix} +\end{bmatrix}. $$ Consider this example of matrix addition, @@ -481,7 +477,7 @@ $$ \begin{bmatrix} 13 & 4 \\ 7 & 12 -\end{bmatrix} +\end{bmatrix}. $$ In general, @@ -502,7 +498,7 @@ A + B = a_{11} + b_{11} & \cdots & a_{1k} + b_{1k} \\ \vdots & \vdots & \vdots \\ a_{n1} + b_{n1} & \cdots & a_{nk} + b_{nk} -\end{bmatrix} +\end{bmatrix}. $$ In the latter case, the matrices must have the same shape in order for the @@ -593,7 +589,7 @@ $$ There are many tutorials to help you further visualize this operation, such as -* [this one](http://www.mathsisfun.com/algebra/matrix-multiplying.html), or +* [this one](http://www.mathsisfun.com/algebra/matrix-multiplying.html), or * the discussion on the [Wikipedia page](https://en.wikipedia.org/wiki/Matrix_multiplication). @@ -614,8 +610,8 @@ $$ It is a useful exercise to check the following: -* If $A$ is $n \times k$ and $I$ is the $k \times k$ identity matrix, then $AI = A$. -* If $I$ is the $n \times n$ identity matrix, then $IA = A$. +* if $A$ is $n \times k$ and $I$ is the $k \times k$ identity matrix, then $AI = A$. +* if $I$ is the $n \times n$ identity matrix, then $IA = A$. @@ -703,7 +699,7 @@ First we rewrite {eq}`two_eq_demand` as \begin{bmatrix} 100 \\ 50 - \end{bmatrix} + \end{bmatrix}. ``` Recall that $p \in \mathbb{R}^{2}$ is the price of two goods. @@ -726,19 +722,19 @@ We rewrite {eq}`two_eq_supply` as \begin{bmatrix} 10 & 5 \\ 5 & 10 - \end{bmatrix} + \end{bmatrix}. ``` Now equality of supply and demand can be expressed as $q^s = q^d$, or $$ - C p = D p + h + C p = D p + h. $$ We can rearrange the terms to get $$ - (C - D) p = h + (C - D) p = h. $$ If all of the terms were numbers, we could solve for prices as $p = h / @@ -749,7 +745,7 @@ prices using the inverse of $C - D$: ```{math} :label: two_matrix - p = (C - D)^{-1} h + p = (C - D)^{-1} h. ``` Before we implement the solution let us consider a more general setting. @@ -786,13 +782,13 @@ To find an equilibrium, we solve $Dp + h = Cp + e$, or ```{math} :label: n_eq_sys_la - (D- C)p = e - h + (D- C)p = e - h. ``` The solution is $$ - p = (D- C)^{-1}(e - h) + p = (D- C)^{-1}(e - h). $$ @@ -810,7 +806,7 @@ A more general version of the problem described above looks as follows. \end{matrix} ``` -The objective here is to solve for the "unknowns" $x_1, \ldots, x_n$ +The objective here is to solve for the "unknowns" $x_1, \ldots, x_n$. We take as given the coefficients $a_{11}, \ldots, a_{nn}$ and constants $b_1, \ldots, b_n$. @@ -840,7 +836,7 @@ In matrix form, the system {eq}`la_se` becomes b_1 \\ \vdots \\ b_n - \end{bmatrix} + \end{bmatrix}. ``` For example, {eq}`n_eq_sys_la` has this form with @@ -850,7 +846,7 @@ $$ \quad b = e - h \quad \text{and} \quad - x = p + x = p. $$ @@ -871,7 +867,7 @@ Recall again the system of equations {eq}`la_se`, which we write here again as ```{math} :label: la_se2 - A x = b + A x = b. ``` The problem we face is to find a vector $x \in \mathbb R^n$ that solves @@ -887,8 +883,8 @@ Consider the system of equations given by, $$ \begin{aligned} - x + 3y = 3 \\ - 2x + 6y = -8 + x + 3y &= 3 \\ + 2x + 6y &= -8. \end{aligned} $$ @@ -928,7 +924,7 @@ We can rewrite this system in matrix form as \begin{bmatrix} 3 \\ -8 - \end{bmatrix} + \end{bmatrix}. ``` It can be noted that the $2^{nd}$ row of matrix $A = (2, 6)$ is just a scalar multiple of the $1^{st}$ row of matrix $A = (1, 3)$. @@ -953,8 +949,8 @@ Now consider, $$ \begin{aligned} - x - 2y = -4 \\ - -2x + 4y = 8 + x - 2y &= -4 \\ + -2x + 4y &= 8. \end{aligned} $$ @@ -970,7 +966,7 @@ This is because the rows of the corresponding matrix \begin{bmatrix} 1 & -2 \\ -2 & 4 - \end{bmatrix} + \end{bmatrix}. ``` are linearly dependent --- can you see why? @@ -991,7 +987,7 @@ $$ \color{blue}{c} & \color{red}{d} \end{bmatrix} = -{\color{red}{ad}} - {\color{blue}{bc}} +{\color{red}{ad}} - {\color{blue}{bc}}. $$ If the determinant of $A$ is not zero, then we say that $A$ is *nonsingular*. @@ -1031,7 +1027,7 @@ This is the solution to $Ax = b$ --- the solution we are looking for. In the two goods example we obtained the matrix equation, $$ -p = (C-D)^{-1} h +p = (C-D)^{-1} h. $$ where $C$, $D$ and $h$ are given by {eq}`two_eq_demand_mat` and {eq}`two_eq_supply_mat`. From dbd81ecbb36c76f43f9a44e58564503793fd6134 Mon Sep 17 00:00:00 2001 From: porntipapoonpolsub Date: Thu, 6 Jul 2023 15:00:25 +1000 Subject: [PATCH 05/11] Minor update linear_equations.,d --- lectures/linear_equations.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index 22bc3f9c..d3ae0cf5 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -71,7 +71,7 @@ The demand for each good depends on the price of both goods: :label: two_eq_demand \begin{aligned} q_0^d = 100 - 10 p_0 - 5 p_1 \\ - q_1^d = 50 - p_0 - 10 p_1 + q_1^d = 50 - p_0 - 10 p_1. \end{aligned} ``` @@ -84,7 +84,7 @@ Let's suppose that supply is given by :label: two_eq_supply \begin{aligned} q_0^s = 10 p_0 + 5 p_1 \\ - q_1^s = 5 p_0 + 10 p_1 + q_1^s = 5 p_0 + 10 p_1. \end{aligned} ``` @@ -96,21 +96,21 @@ This yields the linear system :label: two_equilibrium \begin{aligned} 100 - 10 p_0 - 5 p_1 = 10 p_0 + 5 p_1 \\ - 50 - p_0 - 10 p_1 = 5 p_0 + 10 p_1 + 50 - p_0 - 10 p_1 = 5 p_0 + 10 p_1. \end{aligned} ``` We can solve this with pencil and paper to get $$ - p_0 = 4.41 \quad \text{and} \quad p_1 = 1.18 + p_0 = 4.41 \quad \text{and} \quad p_1 = 1.18. $$ Inserting these results into either {eq}`two_eq_demand` or {eq}`two_eq_supply` yields the equilibrium quantities $$ - q_0 = 50 \quad \text{and} \quad q_1 = 33.82 + q_0 = 50 \quad \text{and} \quad q_1 = 33.82. $$ @@ -508,7 +508,7 @@ definition to make sense. We also have a convention for *multiplying* two matrices. -The rule for matrix multiplication generalizes the idea of inner products +The rule for matrix multiplication generalizes the idea of the inner products discussed above. If $A$ and $B$ are two matrices, then their product $A B$ is formed by taking @@ -597,7 +597,7 @@ There are many tutorials to help you further visualize this operation, such as Unlike number products, $A B$ and $B A$ are not generally the same thing. ``` -ONe important special case is the [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix), which has ones on the principal diagonal and zero elsewhere: +One important special case is the [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix), which has ones on the principal diagonal and zero elsewhere: $$ I = @@ -650,7 +650,7 @@ To get the transpose of `A`, use `A.transpose()` or, more simply, `A.T`. There are many convenient functions for creating common matrices (matrices of zeros, ones, etc.) --- see [here](https://python-programming.quantecon.org/numpy.html#creating-arrays). -Since operations are performed elementwise by default, scalar multiplication and addition have very natural syntaxes +Since operations are performed elementwise by default, scalar multiplication and addition have very natural syntaxes. ```{code-cell} ipython3 A = np.identity(3) # 3 x 3 identity matrix @@ -785,7 +785,7 @@ To find an equilibrium, we solve $Dp + h = Cp + e$, or (D- C)p = e - h. ``` -The solution is +The solution $n \times 1$ vector of prices $p$ is $$ p = (D- C)^{-1}(e - h). From ebdb973b6ed5f13361dff49a37e2f294a16c145c Mon Sep 17 00:00:00 2001 From: hengchengzhang Date: Fri, 7 Jul 2023 14:18:22 +1000 Subject: [PATCH 06/11] Applied review chanegs --- lectures/linear_equations.md | 50 ++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index d3ae0cf5..70a328f7 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -22,10 +22,10 @@ Many problems in economics and finance require solving linear equations. In this lecture we discuss linear equations and their applications. -To illustrate the importance of linear equations, we begin with a two goods +To illustrate the importance of linear equations, we begin with a two good model of supply and demand. -The two goods case is so simple that solutions can be calculated by hand. +The two good case is so simple that solutions can be calculated by hand. But often we need to consider markets containing many goods. @@ -46,9 +46,9 @@ import numpy as np import matplotlib.pyplot as plt ``` -## A two goods example +## A two good example -In this section we discuss a simple two goods example and solve it by +In this section we discuss a simple two good example and solve it by 1. pencil and paper 2. matrix algebra @@ -60,7 +60,7 @@ The second method is more general, as we will see. Suppose that we have two related goods, such as -* propane and ethanol. +* propane and ethanol, and * rice and wheat, etc. To keep things simple, we label them as good 0 and good 1. @@ -71,7 +71,7 @@ The demand for each good depends on the price of both goods: :label: two_eq_demand \begin{aligned} q_0^d = 100 - 10 p_0 - 5 p_1 \\ - q_1^d = 50 - p_0 - 10 p_1. + q_1^d = 50 - p_0 - 10 p_1 \end{aligned} ``` @@ -84,7 +84,7 @@ Let's suppose that supply is given by :label: two_eq_supply \begin{aligned} q_0^s = 10 p_0 + 5 p_1 \\ - q_1^s = 5 p_0 + 10 p_1. + q_1^s = 5 p_0 + 10 p_1 \end{aligned} ``` @@ -96,7 +96,7 @@ This yields the linear system :label: two_equilibrium \begin{aligned} 100 - 10 p_0 - 5 p_1 = 10 p_0 + 5 p_1 \\ - 50 - p_0 - 10 p_1 = 5 p_0 + 10 p_1. + 50 - p_0 - 10 p_1 = 5 p_0 + 10 p_1 \end{aligned} ``` @@ -116,7 +116,7 @@ $$ ### Looking forward -Pencil and paper methods are easy in the two goods case. +Pencil and paper methods are easy in the two good case. But what if there are many goods? @@ -508,7 +508,7 @@ definition to make sense. We also have a convention for *multiplying* two matrices. -The rule for matrix multiplication generalizes the idea of the inner products +The rule for matrix multiplication generalizes the idea of inner products discussed above. If $A$ and $B$ are two matrices, then their product $A B$ is formed by taking @@ -610,7 +610,7 @@ $$ It is a useful exercise to check the following: -* if $A$ is $n \times k$ and $I$ is the $k \times k$ identity matrix, then $AI = A$. +* if $A$ is $n \times k$ and $I$ is the $k \times k$ identity matrix, then $AI = A$, and * if $I$ is the $n \times n$ identity matrix, then $IA = A$. @@ -650,7 +650,7 @@ To get the transpose of `A`, use `A.transpose()` or, more simply, `A.T`. There are many convenient functions for creating common matrices (matrices of zeros, ones, etc.) --- see [here](https://python-programming.quantecon.org/numpy.html#creating-arrays). -Since operations are performed elementwise by default, scalar multiplication and addition have very natural syntaxes. +Since operations are performed elementwise by default, scalar multiplication and addition have very natural syntax. ```{code-cell} ipython3 A = np.identity(3) # 3 x 3 identity matrix @@ -669,9 +669,9 @@ To multiply matrices we use the `@` symbol. In particular, `A @ B` is matrix multiplication, whereas `A * B` is element-by-element multiplication. ``` -### Two goods model in matrix form +### Two good model in matrix form -We can now revisit the two goods model and solve {eq}`two_equilibrium` +We can now revisit the two good model and solve {eq}`two_equilibrium` numerically via matrix algebra. This involves some extra steps but the method is widely applicable --- as we @@ -762,7 +762,7 @@ including crude oil, gasoline, coal, natural gas, ethanol and uranium. The prices of these goods are related, so it makes sense to study them together. -Pencil and paper method become very time consuming with large systems. +Pencil and paper methods become very time consuming with large systems. But fortunately the matrix methods described above are essentially unchanged. @@ -785,7 +785,7 @@ To find an equilibrium, we solve $Dp + h = Cp + e$, or (D- C)p = e - h. ``` -The solution $n \times 1$ vector of prices $p$ is +Then the price vector of the n different goods is $$ p = (D- C)^{-1}(e - h). @@ -1024,7 +1024,7 @@ This is the solution to $Ax = b$ --- the solution we are looking for. ```{index} single: Linear Algebra; SciPy ``` -In the two goods example we obtained the matrix equation, +In the two good example we obtained the matrix equation, $$ p = (C-D)^{-1} h. @@ -1259,15 +1259,15 @@ solution $\hat{x}$ is Now consider the general equation of a linear demand curve of a good given by: $$ - p = a - bq + p = m - nq $$ where $p$ is the price of the good and $q$ is the quantity demanded. -Suppose we are trying to *estimate* the values of $a$ and $b$. +Suppose we are trying to *estimate* the values of $m$ and $n$. We do this by repeatedly observing the price and quantity (for example, each -month) and then choosing $a$ and $b$ to fit the relationship between $p$ and +month) and then choosing $m$ and $n$ to fit the relationship between $p$ and $q$. We have the following observations: @@ -1279,19 +1279,19 @@ We have the following observations: | 8 | 3 | -Requiring the demand curve $p = a - b q$ to pass through all these points leads to the +Requiring the demand curve $p = m - nq$ to pass through all these points leads to the following three equations: $$ \begin{aligned} - 1 = a - 9b \\ - 3 = a - 7b \\ - 8 = a - 3b + 1 = m - 9n \\ + 3 = m - 7n \\ + 8 = m - 3n \end{aligned} $$ Thus we obtain a system of equations $Ax = b$ where $A = \begin{bmatrix} 1 & -9 \\ 1 & -7 \\ 1 & -3 \end{bmatrix}$, -$x = \begin{bmatrix} a \\ b \end{bmatrix}$ and $b = \begin{bmatrix} 1 \\ 3 \\ 8 \end{bmatrix}$. +$x = \begin{bmatrix} m \\ n \end{bmatrix}$ and $b = \begin{bmatrix} 1 \\ 3 \\ 8 \end{bmatrix}$. It can be verified that this system has no solutions. From 703877b6cad7829671a2fdee6c686795a0c5d0f3 Mon Sep 17 00:00:00 2001 From: Sreehari Pulickamadhom Sreedhar Date: Sun, 9 Jul 2023 16:11:11 +1000 Subject: [PATCH 07/11] Update long_run_growth.md Change "GPD" to "GDP" --- lectures/long_run_growth.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lectures/long_run_growth.md b/lectures/long_run_growth.md index 6622bf3e..eef4477e 100644 --- a/lectures/long_run_growth.md +++ b/lectures/long_run_growth.md @@ -130,7 +130,7 @@ colors = cm.Dark2(np.linspace(0, 0.8, len(country_names))) color_mapping = {country: color for country, color in zip(country_names, colors)} ``` -## GPD plots +## GDP plots Looking at the United Kingdom we can first confirm we are using the correct country code From c0e932f2e3b24d059a56661e568b09fe2d1661f2 Mon Sep 17 00:00:00 2001 From: Sreehari Pulickamadhom Sreedhar Date: Mon, 17 Jul 2023 11:10:24 +1000 Subject: [PATCH 08/11] Update linear_equations.md --- lectures/linear_equations.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index 70a328f7..daaa5932 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -39,7 +39,7 @@ To handle such systems we need two things: This lecture covers these steps. -We will use the following imports: +We will use the following packages: ```{code-cell} ipython3 import numpy as np @@ -757,7 +757,7 @@ Before we implement the solution let us consider a more general setting. It is natural to think about demand systems with more goods. For example, even within energy commodities there are many different goods, -including crude oil, gasoline, coal, natural gas, ethanol and uranium. +including crude oil, gasoline, coal, natural gas, ethanol, and uranium. The prices of these goods are related, so it makes sense to study them together. From afb2e6853d4f8b8389713d9e53d3090b7be9c074 Mon Sep 17 00:00:00 2001 From: hengchengzhang Date: Tue, 1 Aug 2023 09:52:46 +1000 Subject: [PATCH 09/11] Minor updates --- lectures/linear_equations.md | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index daaa5932..94f29941 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -13,9 +13,6 @@ kernelspec: # Linear Equations and Matrix Algebra -```{index} single: Linear Equations and Matrix Algebra -``` - ## Overview Many problems in economics and finance require solving linear equations. @@ -127,10 +124,7 @@ basics of vectors and matrices, in both theory and computation. -## {index}`Vectors ` - -```{index} single: Linear Algebra; Vectors -``` +## Vectors A **vector** of length $n$ is just a sequence (or array, or tuple) of $n$ numbers, which we write as $x = (x_1, \ldots, x_n)$ or $x = \begin{bmatrix}x_1, \ldots, x_n\end{bmatrix}$. @@ -175,9 +169,6 @@ plt.show() ### Vector operations -```{index} single: Vectors; Operations -``` - Sometimes we want to modify vectors. The two most common operators on vectors are addition and scalar @@ -355,18 +346,12 @@ y = np.array((2, 4, 6)) # Converts tuple (2, 4, 6) into a NumPy array x + y # Add (element-by-element) ``` -```{code-cell} ipython3 +```{code-cell} 4 * x # Scalar multiply ``` ### Inner product and norm -```{index} single: Vectors; Inner Product -``` - -```{index} single: Vectors; Norm -``` - The **inner product** of vectors $x,y \in \mathbb R^n$ is defined as $$ @@ -413,9 +398,6 @@ np.linalg.norm(x) # Norm of x, method two ## Matrix operations -```{index} single: Matrix; Operations -``` - When we discussed linear price systems, we mentioned using matrix algebra. Matrix algebra is similar to algebra for numbers. @@ -617,9 +599,6 @@ It is a useful exercise to check the following: ### Matrices in NumPy -```{index} single: Matrix; Numpy -``` - NumPy arrays are also used as matrices, and have fast, efficient functions and methods for all the standard matrix operations. You can create them manually from tuples of tuples (or lists of lists) as follows @@ -860,9 +839,6 @@ the following questions ## Solving systems of equations -```{index} single: Matrix; Solving Systems of Equations -``` - Recall again the system of equations {eq}`la_se`, which we write here again as ```{math} @@ -1021,9 +997,6 @@ This is the solution to $Ax = b$ --- the solution we are looking for. ### Linear equations with NumPy -```{index} single: Linear Algebra; SciPy -``` - In the two good example we obtained the matrix equation, $$ From 46cbf2871c3875e43cb4e9ca1c7c5624a1b0e126 Mon Sep 17 00:00:00 2001 From: Sreehari Pulickamadhom Sreedhar Date: Sun, 9 Jul 2023 16:11:11 +1000 Subject: [PATCH 10/11] Update long_run_growth.md Change "GPD" to "GDP" --- lectures/long_run_growth.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lectures/long_run_growth.md b/lectures/long_run_growth.md index e4b9169e..91e20357 100644 --- a/lectures/long_run_growth.md +++ b/lectures/long_run_growth.md @@ -164,8 +164,7 @@ colors = cm.tab20(np.linspace(0, 0.95, len(country_names))) color_mapping = {country: color for country, color in zip(country_names, colors)} ``` - -## GPD plots +## GDP plots Looking at the United Kingdom we can first confirm we are using the correct country code From 206ff646ee4d76ef247dcefd1ea3093e27200df7 Mon Sep 17 00:00:00 2001 From: hengchengzhang Date: Tue, 15 Aug 2023 13:21:05 +1000 Subject: [PATCH 11/11] Minor updates --- lectures/linear_equations.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index 94f29941..5f364682 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -13,6 +13,9 @@ kernelspec: # Linear Equations and Matrix Algebra +```{index} single: Linear Equations and Matrix Algebra +``` + ## Overview Many problems in economics and finance require solving linear equations. @@ -124,7 +127,10 @@ basics of vectors and matrices, in both theory and computation. -## Vectors +## {index}`Vectors ` + + ```{index} single: Linear Algebra; Vectors + ``` A **vector** of length $n$ is just a sequence (or array, or tuple) of $n$ numbers, which we write as $x = (x_1, \ldots, x_n)$ or $x = \begin{bmatrix}x_1, \ldots, x_n\end{bmatrix}$. @@ -169,6 +175,9 @@ plt.show() ### Vector operations +```{index} single: Vectors; Operations +``` + Sometimes we want to modify vectors. The two most common operators on vectors are addition and scalar @@ -352,6 +361,12 @@ x + y # Add (element-by-element) ### Inner product and norm +```{index} single: Vectors; Inner Product +``` + +```{index} single: Vectors; Norm +``` + The **inner product** of vectors $x,y \in \mathbb R^n$ is defined as $$ @@ -398,6 +413,9 @@ np.linalg.norm(x) # Norm of x, method two ## Matrix operations +```{index} single: Matrix; Operations +``` + When we discussed linear price systems, we mentioned using matrix algebra. Matrix algebra is similar to algebra for numbers. @@ -599,6 +617,9 @@ It is a useful exercise to check the following: ### Matrices in NumPy +```{index} single: Matrix; Numpy +``` + NumPy arrays are also used as matrices, and have fast, efficient functions and methods for all the standard matrix operations. You can create them manually from tuples of tuples (or lists of lists) as follows @@ -839,6 +860,9 @@ the following questions ## Solving systems of equations +```{index} single: Matrix; Solving Systems of Equations +``` + Recall again the system of equations {eq}`la_se`, which we write here again as ```{math} @@ -997,6 +1021,9 @@ This is the solution to $Ax = b$ --- the solution we are looking for. ### Linear equations with NumPy +```{index} single: Linear Algebra; SciPy +``` + In the two good example we obtained the matrix equation, $$