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 af1774af4..000000000 Binary files a/lectures/.DS_Store and /dev/null differ 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: diff --git a/lectures/lp_intro.md b/lectures/lp_intro.md index d46ca75d3..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,18 +294,18 @@ $$ 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$. 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,36 +319,36 @@ 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: $$ -\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 +### Example 2: Investment Problem 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*. diff --git a/lectures/opt_transport.md b/lectures/opt_transport.md index 6b01ac382..143544a36 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 @@ -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.