From 685a20e9f4d5f8fd5d1058a6d413e874f6e22f89 Mon Sep 17 00:00:00 2001 From: mmcky Date: Thu, 7 Oct 2021 16:45:44 +1100 Subject: [PATCH 1/5] tidy up and update environment --- .gitignore | 1 + environment.yml | 1 + lectures/.DS_Store | Bin 12292 -> 0 bytes 3 files changed, 2 insertions(+) delete mode 100644 lectures/.DS_Store diff --git a/.gitignore b/.gitignore index cd43ec824..98795e75d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.DS_Store _build/ lectures/_build/ diff --git a/environment.yml b/environment.yml index 0ebf5331b..f4a56876c 100644 --- a/environment.yml +++ b/environment.yml @@ -14,6 +14,7 @@ dependencies: - jupytext==1.11.2 - ghp-import==1.1.0 - jupinx==0.2.3 + - sphinxcontrib-youtube==1.0.1 # Temporary Fixes - tornado>=6.1 diff --git a/lectures/.DS_Store b/lectures/.DS_Store deleted file mode 100644 index af1774af439994ea542ed95640482f7c41e96327..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12292 zcmeHN&2k$>5bm`dVwsJI8enS z@CfiG+<1-LIl=e!tWeLYH*qc;%&bcqX+2+e&(}RO-Ln#r?D2ebEb>T1cJN}`-ox8Z zBHadBbMgOttzDbAli~FhUijP=xs(&>%a!DKfBm(AF2rx)bs&X|BYYuekT!s{z8uMg zjJ_-;!$C0_pf^-)oB{muZ@t1D`5f)hzjs8oVaGt`kXy*;`svkWF}VavbymiRyXi^{ zBnA=#iGjqx|APVUy|{MLUKof-?}>rLz={E`4_&<2_R97qw7PWQjVV0;&lFxSd`5Dv z_qt`uPt&dZA74 ziGjqxIs+f$R=ZMrU&us8sJ#n$?pEG^VtTDAZ;`jrvq0^h%Mk6%!>~5r_}bLw4fLD> z!J2=D8h=uU+WN(ycuMfBL|wx|Vkbb4@!7Riey`}=Qwc+$mU;_MbM}4(*TM+|Pd2^- z@){QOq3=1gx}(j0uArM0^c2V|IdwJvRl(i|c8I?|EH9K}j`#`^3f!Ba9eH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jqx-DDu!dh+ziV~j1U86Tguck%iLujPfl+8uBDC?2go ribt!D;?aI$`u6x4_ijRqPq_Z`9|1V4W!nEMRaDylLoIg4_y2zY($2di From 16c29484d280ab6de076df04002826bee4905654 Mon Sep 17 00:00:00 2001 From: mmcky Date: Thu, 7 Oct 2021 16:55:34 +1100 Subject: [PATCH 2/5] add additional extensions --- lectures/_config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lectures/_config.yml b/lectures/_config.yml index 749435459..9f4619a6d 100644 --- a/lectures/_config.yml +++ b/lectures/_config.yml @@ -19,7 +19,7 @@ latex: targetname: quantecon-python.tex sphinx: - extra_extensions: [sphinx_multitoc_numbering, sphinxext.rediraffe, sphinx_tojupyter] + extra_extensions: [sphinx_multitoc_numbering, sphinxext.rediraffe, sphinx_tojupyter, sphinxcontrib.youtube, sphinx.ext.todo] config: nb_render_priority: html: From fe6b9cf0862666f1f7522c77d3904ce4b953a1ce Mon Sep 17 00:00:00 2001 From: mmcky Date: Thu, 7 Oct 2021 17:21:21 +1100 Subject: [PATCH 3/5] minor changes to lectures --- lectures/lp_intro.md | 6 +++--- lectures/opt_transport.md | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lectures/lp_intro.md b/lectures/lp_intro.md index d46ca75d3..b739891a2 100644 --- a/lectures/lp_intro.md +++ b/lectures/lp_intro.md @@ -305,7 +305,7 @@ Here, $Ax = b$ means that the $i$-th entry of $Ax$ equals the $i$-th entry of Similarly, $x >= 0$ means that $x_j$ is greater than $0$ for every $j$. -#### Useful Transformations +### Useful Transformations It is useful to know how to transform a problem that initially is not stated in the standard form into one that is. @@ -319,7 +319,7 @@ By deploying the following steps, any linear programming problem can be transfor Let's apply the above steps to the two examples described above. -#### Example 1: Production Problem +### Example 1: Production Problem The original problem is: @@ -343,7 +343,7 @@ $$ \end{align*} $$ -#### Example 2: Investment Problem +### Example 2: Investment Problem The original problem is: diff --git a/lectures/opt_transport.md b/lectures/opt_transport.md index 6b01ac382..38215491f 100644 --- a/lectures/opt_transport.md +++ b/lectures/opt_transport.md @@ -26,13 +26,13 @@ The optimal transport problem was studied in early work about linear programming We shall solve our problems first by using the scipy function *linprog* and then the quantecon program *linprog_simplex*. -Let's start with some imports. - ```{code-cell} ipython3 -# !pip list outdated -# !pip install -- upgrade quantecon +:tags: [hide-output] +!pip install --upgrade quantecon ``` +Let's start with some imports. + ```{code-cell} ipython3 import numpy as np from scipy.optimize import linprog From 6d19e45bddcf93d3657a830a0ec37a01ec91b948 Mon Sep 17 00:00:00 2001 From: mmcky Date: Thu, 7 Oct 2021 17:43:45 +1100 Subject: [PATCH 4/5] adjust some math for lp_intro for aligned --- lectures/lp_intro.md | 68 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/lectures/lp_intro.md b/lectures/lp_intro.md index b739891a2..e2ba90f4a 100644 --- a/lectures/lp_intro.md +++ b/lectures/lp_intro.md @@ -118,12 +118,12 @@ Let $x_i$ denote the quantity of Product $i$ that the firm produces. This problem can be formulated as: $$ -\begin{align*} +\begin{aligned} \max_{x_1,x_2} \ & z = 3 x_1 + 4 x_2 \\ \mbox{subject to } \ & 2 x_1 + 5 x_2 \le 30 \\ & 4 x_1 + 2 x_2 \le 20 \\ & x_1, x_2 \ge 0 \\ -\end{align*} +\end{aligned} $$ The following graph illustrates the firm's constraints and iso-revenue lines. @@ -240,7 +240,7 @@ $$ Thus, the mutual fund confronts the linear program: $$ -\begin{align*} +\begin{aligned} \max_{x} \ & 1.30 \cdot 3x_1 + 1.06 x_4 + 1.30 x_5 \\ \mbox{subject to } \ & x_1 + x_2 = 100,000\\ & x_1 - 1.06 x_2 + x_3 + x_5 = 0\\ @@ -251,7 +251,7 @@ $$ & x_5 \le 50,000\\ & x_j \ge 0, \quad j = 1,5\\ & x_j \ \text{unrestricted}, \quad j = 2,3,4\\ -\end{align*} +\end{aligned} $$ ## Standard Form @@ -267,14 +267,14 @@ it is useful to devote some effort to describe a **standard form**. Our standard form is: $$ -\begin{align*} +\begin{aligned} \min_{x} \ & c_1 x_1 + c_2 x_2 + \dots + c_n x_n \\ \mbox{subject to } \ & a_{11} x_1 + a_{12} x_2 + \dots + a_{1n} x_n = b_1 \\ & a_{21} x_1 + a_{22} x_2 + \dots + a_{2n} x_n = b_2 \\ & \quad \vdots \\ & a_{m1} x_1 + a_{m2} x_2 + \dots + a_{mn} x_n = b_m \\ & x_1, x_2, \dots, x_n \ge 0 \\ -\end{align*} +\end{aligned} $$ Let @@ -294,11 +294,11 @@ $$ The standard form LP problem can be expressed concisely as: $$ -\begin{align*} +\begin{aligned} \min_{x} \ & c'x \\ \mbox{subject to } \ & Ax = b\\ & x >= 0\\ -\end{align*} +\end{aligned} $$ (lpproblem) Here, $Ax = b$ means that the $i$-th entry of $Ax$ equals the $i$-th entry of $b$ for every $i$. @@ -324,23 +324,23 @@ Let's apply the above steps to the two examples described above. The original problem is: $$ -\begin{align*} +\begin{aligned} \max_{x_1,x_2} \ & 3 x_1 + 4 x_2 \\ \mbox{subject to } \ & 2 x_1 + 5 x_2 \le 30 \\ & 4 x_1 + 2 x_2 \le 20 \\ & x_1, x_2 \ge 0 \\ -\end{align*} +\end{aligned} $$ This problem is equivalent to the following problem with a standard form: $$ -\begin{align*} +\begin{aligned} \min_{x_1,x_2} \ & -(3 x_1 + 4 x_2) \\ \mbox{subject to } \ & 2 x_1 + 5 x_2 + s_1 = 30 \\ & 4 x_1 + 2 x_2 + s_2 = 20 \\ & x_1, x_2, s_1, s_2 \ge 0 \\ -\end{align*} +\end{aligned} $$ ### Example 2: Investment Problem @@ -348,7 +348,7 @@ $$ The original problem is: $$ -\begin{align*} +\begin{aligned} \max_{x} \ & 1.30 \cdot 3x_1 + 1.06 x_4 + 1.30 x_5 \\ \mbox{subject to } \ & x_1 + x_2 = 100,000\\ & x_1 - 1.06 x_2 + x_3 + x_5 = 0\\ @@ -359,13 +359,13 @@ $$ & x_5 \le 50,000\\ & x_j \ge 0, \quad j = 1,5\\ & x_j \ \text{unrestricted}, \quad j = 2,3,4\\ -\end{align*} +\end{aligned} $$ This problem is equivalent to the following problem with a standard form: $$ -\begin{align*} +\begin{aligned} \min_{x} \ & -(1.30 \cdot 3x_1 + 1.06 x_4^+ - 1.06 x_4^- + 1.30 x_5) \\ \mbox{subject to } \ & x_1 + x_2^+ - x_2^- = 100,000\\ & x_1 - 1.06 (x_2^+ - x_2^-) + x_3^+ - x_3^- + x_5 = 0\\ @@ -377,7 +377,7 @@ $$ & x_j \ge 0, \quad j = 1,5\\ & x_j^+, x_j^- \ge 0, \quad j = 2,3,4\\ & s_j \ge 0, \quad j = 1,2,3,4\\ -\end{align*} +\end{aligned} $$ ## Computations @@ -385,12 +385,12 @@ $$ The package *scipy.optimize* provides a function ***linprog*** to solve linear programming problems with a form below: $$ -\begin{align*} +\begin{aligned} \min_{x} \ & c' x \\ \mbox{subject to } \ & A_{ub}x \le b_{ub} \\ & A_{eq}x = b_{eq} \\ & l \le x \le u \\ -\end{align*} +\end{aligned} $$ ```{note} @@ -404,12 +404,12 @@ Let's apply this great Python tool to solve our two example problems. The problem is: $$ -\begin{align*} +\begin{aligned} \max_{x_1,x_2} \ & 3 x_1 + 4 x_2 \\ \mbox{subject to } \ & 2 x_1 + 5 x_2 \le 30 \\ & 4 x_1 + 2 x_2 \le 20 \\ & x_1, x_2 \ge 0 \\ -\end{align*} +\end{aligned} $$ ```{code-cell} ipython3 @@ -449,7 +449,7 @@ This problem is to maximize the objective, so that we need to put a minus sign i The problem is: $$ -\begin{align*} +\begin{aligned} \max_{x} \ & 1.30 \cdot 3x_1 + 1.06 x_4 + 1.30 x_5 \\ \mbox{subject to } \ & x_1 + x_2 = 100,000\\ & x_1 - 1.06 x_2 + x_3 + x_5 = 0\\ @@ -460,7 +460,7 @@ $$ & x_5 \le 50,000\\ & x_j \ge 0, \quad j = 1,5\\ & x_j \ \text{unrestricted}, \quad j = 2,3,4\\ -\end{align*} +\end{aligned} $$ Let's solve this problem using *linprog*. @@ -508,7 +508,7 @@ Associated with a linear programming of form {eq}`linprog` with $m$ constraints there is an **dual** linear programming problem that takes the form (please see {cite}`bertsimas_tsitsiklis1997`) $$ -\begin{align*} +\begin{aligned} \max_{p} \ & b' p \\ \mbox{subject to } \ & p_i \ge 0, & i \in M_1 \\ & p_i \le 0, & i \in M_2 \\ @@ -516,7 +516,7 @@ $$ & A_j' p \le c_j, & j \in N_1 \\ & A_j' p \ge c_j, & j \in N_2 \\ & A_j' p = c_j, & j \in N_3 \\ -\end{align*} +\end{aligned} $$ Where $A_j$ is $j$-th column of the $m$ by $n$ matrix $A$. @@ -562,30 +562,30 @@ The following table summarizes relationships between objects in primal and dual As an example, the dual problem of the standard form {eq}`lpproblem` is: $$ -\begin{align*} +\begin{aligned} \max_{p} \ & b'p \\ \mbox{subject to } \ & A'p \le c\\ -\end{align*} +\end{aligned} $$ As another example, consider a linear programming problem with form: $$ -\begin{align*} +\begin{aligned} \max_{x} \ & c'x \\ \mbox{subject to } \ & A x \le b\\ & x \ge 0\\ -\end{align*} +\end{aligned} $$ (linprog2) Its dual problem is: $$ -\begin{align*} +\begin{aligned} \min_{p} \ & b'p \\ \mbox{subject to } \ & A' p \ge c\\ & p \ge 0\\ -\end{align*} +\end{aligned} $$ ## Duality Theorems @@ -690,12 +690,12 @@ This problem is one specific instance of the problem {eq}`linprog2`, whose econo Its dual problem is: $$ -\begin{align*} +\begin{aligned} \min_{x_1,x_2} \ & 30 p_1 + 20 p_2 \\ \mbox{subject to } \ & 2 p_1 + 4 p_2 \ge 3 \\ & 5 p_1 + 2 p_2 \ge 4 \\ & p_1, p_2 \ge 0 \\ -\end{align*} +\end{aligned} $$ We then solve this dual problem by function *linprog*. Since parameters used here are defined before when solving the primal problem, we don't need to define them here. @@ -714,7 +714,7 @@ The optimal of the dual problem is 27.5, which is the same as the primal problem The dual problem is: $$ -\begin{align*} +\begin{aligned} \min_{p} \ & 100,000 p_1 - 20,000 p_4 - 20,000 p_5 - 20,000 p_6 + 50,000 p_7 \\ \mbox{subject to } \ & p_1 + p_2 + p_3 \ge 1.30 \cdot 3 \\ & p_1 - 1.06 p_2 + p_4 = 0 \\ @@ -724,7 +724,7 @@ $$ & p_i \ \text{unrestricted}, \quad i = 1,2,3 \\ & p_i \le 0, \quad i = 4,5,6 \\ & p_7 \ge 0 \\ -\end{align*} +\end{aligned} $$ We then solve this dual problem by function *linprog*. From bb3e34cc795a4a3d85bb7b00a1babfc77f3ae936 Mon Sep 17 00:00:00 2001 From: mmcky Date: Thu, 7 Oct 2021 17:47:26 +1100 Subject: [PATCH 5/5] fix opt_transport align* to aligned --- lectures/opt_transport.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lectures/opt_transport.md b/lectures/opt_transport.md index 38215491f..143544a36 100644 --- a/lectures/opt_transport.md +++ b/lectures/opt_transport.md @@ -62,12 +62,12 @@ A planner wants to minimize total transportation costs subject to the following The planner's problem can be expressed as the following constrained minimization problem: $$ -\begin{align*} +\begin{aligned} \min_{x_{ij}} \ & \sum_{i=1}^m \sum_{j=1}^n c_{ij} x_{ij} \\ \mbox{subject to } \ & \sum_{j=1}^n x_{ij} = p_i, & i = 1, 2, \dots, m \\ & \sum_{i=1}^m x_{ij} = q_j, & j = 1, 2, \dots, n \\ & x_{ij} \ge 0 \\ -\end{align*} +\end{aligned} $$ (plannerproblem) This is an **optimal transport problem** with @@ -105,12 +105,12 @@ Let Where $\mathbf{1}_n$ denotes $n$-dimensional column vector $(1, 1, \dots, 1)'$, our problem can now be expressed compactly as: $$ -\begin{align*} +\begin{aligned} \min_{X} \ & \operatorname{tr} (C' X) \\ \mbox{subject to } \ & X \ \mathbf{1}_n = p \\ & X' \ \mathbf{1}_m = q \\ & X \ge 0 \\ -\end{align*} +\end{aligned} $$ We can convert the matrix $X$ into a vector by stacking all of its columns into a column vector. @@ -176,11 +176,11 @@ $$ Our problem can now be expressed in terms of an $mn$-dimensional vector of decision variables: $$ -\begin{align*} +\begin{aligned} \min_{z} \ & \operatorname{vec}(C)' z \\ \mbox{subject to } \ & A z = b \\ & z \ge 0 \\ -\end{align*} +\end{aligned} $$ (decisionvars) where @@ -451,10 +451,10 @@ Let $u, v$ denotes vectors of dual decision variables with entries $(u_i), (v_j) The **dual** to **minimization** problem {eq}`plannerproblem` is the **maximization** problem: $$ -\begin{align*} +\begin{aligned} \max_{u_i, v_j} \ & \sum_{i=1}^m p_i u_i + \sum_{j=1}^n q_j v_j \\ \mbox{subject to } \ & u_i + v_j \le c_{ij}, \ i = 1, 2, \dots, m;\ j = 1, 2, \dots, n \\ -\end{align*} +\end{aligned} $$ (dualproblem) The dual problem is also a linear programming problem. @@ -476,10 +476,10 @@ Components of the vectors $u$ and $v$ of **values** are **shadow prices** of th We can write the dual problem as $$ -\begin{align*} +\begin{aligned} \max_{u_i, v_j} \ & p u + q v \\ \mbox{subject to } \ & A' \begin{bmatrix} u \\ v \\ \end{bmatrix} = \operatorname{vec}(C) \\ -\end{align*} +\end{aligned} $$ (dualproblem2) For the same numerical example described above, let's solve the dual problem.