{ "cells": [ { "cell_type": "markdown", "id": "fd7a16db-81e6-4c8f-8f96-503efd467988", "metadata": {}, "source": [ "This notebook contains the review questions for the midterm exam." ] }, { "cell_type": "code", "execution_count": 1, "id": "ba877857-aab8-4f82-84c7-5e9292673da6", "metadata": {}, "outputs": [], "source": [ "using SymPy" ] }, { "cell_type": "code", "execution_count": 2, "id": "787ce4d0", "metadata": {}, "outputs": [], "source": [ "using JuMP\n", "import DataFrames\n", "import GLPK" ] }, { "cell_type": "code", "execution_count": 3, "id": "53ccfb7a-fbf5-4784-8ea4-d49e4a6ed9e3", "metadata": {}, "outputs": [], "source": [ "using NLsolve" ] }, { "cell_type": "markdown", "id": "9e2a342b-66ff-45d0-8c2a-57a93cd92b65", "metadata": {}, "source": [ "# 1. Taguchi quality control" ] }, { "cell_type": "markdown", "id": "9755723c-a528-47e0-871c-acb5887decfd", "metadata": {}, "source": [ "Suppose a medicine to reduce fever is effective in 80% of the cases\n", "after the first dosis and costs $1.\n", "\n", "A temperature reading of more than two degrees above normal \n", "six hours after taking the first dosis requires a visit \n", "to the doctor which costs $10.\n", "\n", "Clinical tests for a new fewer reducing medicine reported a success\n", "rate of 95% after taking the first dosis. \n", "\n", "1. Under the same circumstances as above, is a cost of $2 for the\n", " new medicine justified?\n", " \n", " (*Hint:* compute the expected loss of having a fever for both medicines.)\n", "\n", "2. What is a fair price for the new medicine, taking into account its higher quality?\n", "\n", "At which price would you buy the new medicine?" ] }, { "cell_type": "markdown", "id": "953c7470-8244-40bc-8dc3-836280a0eec1", "metadata": {}, "source": [ "## computing the expected loss" ] }, { "cell_type": "markdown", "id": "60d11325-1e98-4646-a1a2-5b09f26ebe23", "metadata": {}, "source": [ "The expected loss for the first medicine:\n", "\n", "$$\n", " 0.80 ~\\cdot~ \\$1 + 0.2 \\cdot ( \\$1 + \\$10 ) = \\$3.0.\n", "$$\n", "\n", "For the new medicine:\n", "\n", "$$\n", " 0.95 ~\\cdot~ \\$2 + 0.05 \\cdot ( \\$2 + \\$10 ) = \\$2.5.\n", "$$\n", "\n", "The patient is better off with the new medicine." ] }, { "cell_type": "markdown", "id": "d0c5234d-254e-4918-aa7b-913826f1853e", "metadata": {}, "source": [ "## the fair price for the new medicine" ] }, { "cell_type": "markdown", "id": "0dbdfd32-b297-4739-a9ae-bc1c091935f2", "metadata": {}, "source": [ "For a fair price, we assume the same expected loss.\n", "\n", "Let $x$ be the break even price, with both medicines the same loss.\n", "\n", "$$\n", "\\begin{eqnarray*}\n", " 3.0 & = & 0.95 x + 0.05(x + 10) \\\\\n", " & = & x + 0.5 \\\\\n", " & \\Rightarrow & x = \\$2.5.\n", "\\end{eqnarray*}\n", "$$\n", "If $x \\leq \\$2.5$, then we buy the medicine.\n", "Otherwise not." ] }, { "cell_type": "markdown", "id": "63a082e8-3c0e-4e7b-87d2-a34b10212296", "metadata": {}, "source": [ "# 2. running a simulation" ] }, { "cell_type": "markdown", "id": "baee44bd-8434-41bf-b7af-425ffb878d8a", "metadata": {}, "source": [ "A man decides to participate in a 1,000 mile car race.\n", "\n", "Because of the rough terrain and excessive speed,\n", "tires have a life time normally distributed with an\n", "average of 600 miles and standard deviation of 200 miles.\n", "\n", "The man starts the race with 4 new tires and 4 spare tires.\n", "\n", "Given the expected life span of the tires,\n", "what are the odds to finish the 1,000 mile race?\n", "\n", "1. Set up a simulation to decide the odds of to finish the race.\n", " \n", "2. Run the simulation 10,000 times and give the probability of reaching the finish." ] }, { "cell_type": "markdown", "id": "6583fb57-b93f-4137-a0b6-9d70e63aca51", "metadata": {}, "source": [ "## a Julia function" ] }, { "cell_type": "code", "execution_count": 4, "id": "e7540e7b-d0e7-4ab9-ae75-9f8d87c14001", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "run" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", " Runs N simulations and returns the\n", "number of times the finish was reached.\n", "\"\"\"\n", "function run(N::Int64)\n", " successes = 0\n", " for k=1:N\n", " tires = 600 .+ randn(8)*200\n", " life = tires[1:4]\n", " for j=1:4\n", " life = sort(life)\n", " life[1] = life[1] + tires[4+j]\n", " end\n", " if minimum(life) >= 1000\n", " successes = successes + 1\n", " end\n", " end\n", " return successes\n", "end" ] }, { "cell_type": "code", "execution_count": 5, "id": "7608d3bc-eba2-4506-9232-2a287bf2c3c2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3739\n" ] } ], "source": [ "println(run(10000)/10000)" ] }, { "cell_type": "markdown", "id": "d4859951-7321-4644-b97d-aca7790e6fa0", "metadata": {}, "source": [ "So, the probability of finishing the race is about .37" ] }, { "cell_type": "markdown", "id": "514e4d35-d649-48ac-b380-17bc753830e1", "metadata": {}, "source": [ "# 3. a recursion" ] }, { "cell_type": "markdown", "id": "8f55bcc2-9d06-4fc6-8c78-e3e93644138a", "metadata": {}, "source": [ "Suppose we start with an empty savings account.\n", "\n", "At the last day of every month $400 is deposited into the account.\n", "\n", "After the first deposit, 20% of the balance of the savings account\n", "is withdrawn at the first day of every month.\n", "\n", "1. Set up the recursion relation for the balance $B(n)$\n", " at month $n$, after $n$ deposits.\n", " \n", "2. Solve the recursion for $B(n)$.\n", " \n", "3. In doing so, will we ever get rich? Is there a limit to $B(n)$?" ] }, { "cell_type": "markdown", "id": "1ed9a61c-3d1e-4124-8ceb-c2460c504dc8", "metadata": {}, "source": [ "## defining and solving the recursion" ] }, { "cell_type": "markdown", "id": "bab2720f-f01b-4ab5-989e-a5cc5b4025e5", "metadata": {}, "source": [ "$$\n", " B(n) = 0.8 B(n-1) + 400, B(0) = 0.\n", "$$" ] }, { "cell_type": "markdown", "id": "51e6bf32-345a-4978-add5-7586676db691", "metadata": {}, "source": [ "We can solve this recursion by substitution.\n", "\n", "$$\n", "\\begin{eqnarray*}\n", " B(n) & = & {\\displaystyle \\frac{4}{5} ~ B(n-1) + 400} \\\\\n", " & = & {\\displaystyle \\frac{4}{5} \n", " \\left( \\frac{4}{5} ~ B(n-2) + 400 \\right) + 400} \\\\\n", " & \\vdots & \\\\\n", " & = & {\\displaystyle \\left( \\frac{4}{5} \\right)^n B(0)\n", " + 400 \\sum_{k=0}^{n-1} \\left( \\frac{4}{5} \\right)^k} \\\\\n", " B(n) & = & {\\displaystyle \\left( \\frac{4}{5} \\right)^n B(0)\n", " + 400 \\sum_{k=0}^{n-1} \\left( \\frac{4}{5} \\right)^k}\n", "\\end{eqnarray*}\n", "$$" ] }, { "cell_type": "markdown", "id": "de1ce320-38f2-43bc-b11c-ac2bcec1508d", "metadata": {}, "source": [ "Now use $B(0) = 0$ and the explicit formula for the geometric sum.\n", "\n", "$$\n", " 400 \\sum_{k=0}^{n-1} \\left( \\frac{4}{5} \\right)^k\n", " = 400 \\frac{\\left( \\frac{4}{5} \\right)^n - 1}{\\frac{4}{5} - 1}\n", " = 2000 \\left( 1 - \\left( \\frac{4}{5} \\right)^n \\right)\n", "$$" ] }, { "cell_type": "markdown", "id": "780242c8-ec0b-482c-9131-f5b88eaffa4e", "metadata": {}, "source": [ "Alternatively, or as a verification, we can use the ``rsolve`` of ``SymPy``." ] }, { "cell_type": "code", "execution_count": 6, "id": "69193e2b-73b6-4ec6-bb59-8304a8a104f1", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$n$" ], "text/plain": [ "n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = Sym(\"n\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "0090fb3c-0e01-45e6-b168-6f3b4ad4ac47", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$B$" ], "text/plain": [ "B" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = SymFunction(\"B\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "6c50c3e3-adcd-45c7-9c0d-709c46005f47", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$B{\\left(n \\right)} = 0.8 B{\\left(n - 1 \\right)} + 400$" ], "text/plain": [ "B(n) = 0.8*B(n - 1) + 400" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equ = Eq(B(n), 0.8*B(n-1) + 400)" ] }, { "cell_type": "code", "execution_count": 9, "id": "93c785e6-b3ad-47c3-a92c-0deabeab2648", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation*}\\begin{cases}B{\\left(0 \\right)} & \\text{=>} &0\\\\\\end{cases}\\end{equation*}" ], "text/plain": [ "Dict{Sym{PyCall.PyObject}, Int64} with 1 entry:\n", " B(0) => 0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "init = Dict(B(0) => 0)" ] }, { "cell_type": "code", "execution_count": 10, "id": "b45a687c-2191-47dc-8c09-7df52f6c5712", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$2000.0 - 2000.0 \\cdot 0.8^{n}$" ], "text/plain": [ " n\n", "2000.0 - 2000.0*0.8 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = sympy.rsolve(equ, B(n), init)" ] }, { "cell_type": "markdown", "id": "cb211624-b0cd-4f8b-921f-482206501f31", "metadata": {}, "source": [ "In doing so, will we ever get rich? Is there a limit to $B(n)$?\n", "\n", "We see that we will never get more than $2000." ] }, { "cell_type": "markdown", "id": "755f7a27-fdb8-4dac-8a6a-2d935fa1324e", "metadata": {}, "source": [ "# 4. linear programming" ] }, { "cell_type": "markdown", "id": "6f769dac", "metadata": {}, "source": [ "A boat rental company must order new boats for next summer. \n", "\n", "There are three types of boats:" ] }, { "cell_type": "markdown", "id": "edaacb7b", "metadata": {}, "source": [ "|boat type | storage area | purchase cost | rental price | need for boats|\n", "| :----: | :----: | :----: | :----: | :----: |\n", "| 1 | $f_1=2~{\\rm ft}^2$ | $c_1=\\$122$ | $p_1=\\$5$ | $n_1=20$ |\n", "| 2 | $f_2=3~{\\rm ft}^2$ | $c_2=\\$130$ | $p_2=\\$7$ | $n_2=15$ |\n", "| 3 | $f_3=4~{\\rm ft}^2$ | $c_3=\\$150$ | $p_3=\\$9$ | $n_3=10$ |" ] }, { "cell_type": "markdown", "id": "285428aa", "metadata": {}, "source": [ "Boat $i$ requires $f_i$ square feet to store, costs $c_i$ dollars\n", "to purchase, and can be rented at $p_i$ dollars a trip.\n", "\n", "Our constraints are as follows:\n", "\n", "* We need at least $n_i$ boats of type $i$,\n", "* but have only $400$ square feet to store the boats, and\n", "* our budget is limited to $\\$10,000$.\n", "\n", "Determine how many boats of each type we should order." ] }, { "cell_type": "markdown", "id": "4df0f776", "metadata": {}, "source": [ "## defining the problem" ] }, { "cell_type": "code", "execution_count": 11, "id": "4de71168", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
3×5 DataFrame
Rowtypeareacostpriceneed
Int64Int64Int64Int64Int64
112122520
223130715
334150910
" ], "text/latex": [ "\\begin{tabular}{r|ccccc}\n", "\t& type & area & cost & price & need\\\\\n", "\t\\hline\n", "\t& Int64 & Int64 & Int64 & Int64 & Int64\\\\\n", "\t\\hline\n", "\t1 & 1 & 2 & 122 & 5 & 20 \\\\\n", "\t2 & 2 & 3 & 130 & 7 & 15 \\\\\n", "\t3 & 3 & 4 & 150 & 9 & 10 \\\\\n", "\\end{tabular}\n" ], "text/plain": [ "\u001b[1m3×5 DataFrame\u001b[0m\n", "\u001b[1m Row \u001b[0m│\u001b[1m type \u001b[0m\u001b[1m area \u001b[0m\u001b[1m cost \u001b[0m\u001b[1m price \u001b[0m\u001b[1m need \u001b[0m\n", "\u001b[1m \u001b[0m│\u001b[90m Int64 \u001b[0m\u001b[90m Int64 \u001b[0m\u001b[90m Int64 \u001b[0m\u001b[90m Int64 \u001b[0m\u001b[90m Int64 \u001b[0m\n", "─────┼───────────────────────────────────\n", " 1 │ 1 2 122 5 20\n", " 2 │ 2 3 130 7 15\n", " 3 │ 3 4 150 9 10" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "boats = DataFrames.DataFrame(\n", " [\n", " 1 2 122 5 20\n", " 2 3 130 7 15\n", " 3 4 150 9 10\n", " ],\n", " [\"type\", \"area\", \"cost\", \"price\", \"need\"],\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "id": "06cdd556", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "A JuMP Model\n", "├ solver: GLPK\n", "├ objective_sense: FEASIBILITY_SENSE\n", "├ num_variables: 0\n", "├ num_constraints: 0\n", "└ Names registered in the model: none" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = Model(GLPK.Optimizer)" ] }, { "cell_type": "code", "execution_count": 13, "id": "3e26e17d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:\n", " Dimension 1, [1, 2, 3]\n", "And data, a 3-element Vector{VariableRef}:\n", " x[1]\n", " x[2]\n", " x[3]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@variable(model, x[boats.type] >= 0)" ] }, { "cell_type": "markdown", "id": "a5a6d5db", "metadata": {}, "source": [ "The objective is to maximize the rental price." ] }, { "cell_type": "code", "execution_count": 14, "id": "8ecae3bf", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$ 5 x_{1} + 7 x_{2} + 9 x_{3} $" ], "text/plain": [ "5 x[1] + 7 x[2] + 9 x[3]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@objective(\n", " model,\n", " Max,\n", " sum(boat[\"price\"] * x[boat[\"type\"]] for boat in eachrow(boats)),\n", ")" ] }, { "cell_type": "markdown", "id": "b1135d10", "metadata": {}, "source": [ "Let us add the constraint on the storage." ] }, { "cell_type": "code", "execution_count": 15, "id": "a3f8c6e9", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ 2 x_{1} + 3 x_{2} + 4 x_{3} \\leq 400 $$" ], "text/plain": [ "2 x[1] + 3 x[2] + 4 x[3] <= 400" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "storage = @expression(\n", " model,\n", " sum(boat[\"area\"] * x[boat[\"type\"]] for boat in eachrow(boats)),\n", ")\n", "@constraint(model, storage <= 400)" ] }, { "cell_type": "markdown", "id": "faaf6cdd", "metadata": {}, "source": [ "The second constraint is on the budget." ] }, { "cell_type": "code", "execution_count": 16, "id": "9780160f", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ 122 x_{1} + 130 x_{2} + 150 x_{3} \\leq 10000 $$" ], "text/plain": [ "122 x[1] + 130 x[2] + 150 x[3] <= 10000" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "budget = @expression(\n", " model,\n", " sum(boat[\"cost\"] * x[boat[\"type\"]] for boat in eachrow(boats)),\n", ")\n", "@constraint(model, budget <= 10000)" ] }, { "cell_type": "markdown", "id": "579f1197", "metadata": {}, "source": [ "And we have a need for each type of boat." ] }, { "cell_type": "code", "execution_count": 17, "id": "c869c6eb", "metadata": {}, "outputs": [], "source": [ "for boat in eachrow(boats)\n", " @constraint(model, x[boat[\"type\"]] >= boat[\"need\"])\n", "end" ] }, { "cell_type": "markdown", "id": "b8760b0b", "metadata": {}, "source": [ "To verify whether we have the model defined correctly, we print it." ] }, { "cell_type": "code", "execution_count": 18, "id": "93e53d65", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\begin{aligned}\n", "\\max\\quad & 5 x_{1} + 7 x_{2} + 9 x_{3}\\\\\n", "\\text{Subject to} \\quad & x_{1} \\geq 20\\\\\n", " & x_{2} \\geq 15\\\\\n", " & x_{3} \\geq 10\\\\\n", " & 2 x_{1} + 3 x_{2} + 4 x_{3} \\leq 400\\\\\n", " & 122 x_{1} + 130 x_{2} + 150 x_{3} \\leq 10000\\\\\n", " & x_{1} \\geq 0\\\\\n", " & x_{2} \\geq 0\\\\\n", " & x_{3} \\geq 0\\\\\n", "\\end{aligned} $$" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(model)" ] }, { "cell_type": "markdown", "id": "7e3bc19b", "metadata": {}, "source": [ "## solving the problem" ] }, { "cell_type": "code", "execution_count": 19, "id": "a06a0528", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "solution_summary(; result = 1, verbose = false)\n", "├ solver_name : GLPK\n", "├ Termination\n", "│ ├ termination_status : OPTIMAL\n", "│ ├ result_count : 1\n", "│ ├ raw_status : Solution is optimal\n", "│ └ objective_bound : Inf\n", "├ Solution (result = 1)\n", "│ ├ primal_status : FEASIBLE_POINT\n", "│ ├ dual_status : FEASIBLE_POINT\n", "│ ├ objective_value : 5.41600e+02\n", "│ └ dual_objective_value : 5.41600e+02\n", "└ Work counters\n", " └ solve_time (sec) : 9.99928e-04" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "optimize!(model)\n", "solution_summary(model)" ] }, { "cell_type": "code", "execution_count": 20, "id": "6cc24415", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x[1] = 20.0\n", "x[2] = 15.0\n", "x[3] = 37.4\n" ] } ], "source": [ "for boat in boats.type\n", " println(\"x[\",boat, \"] = \", value(x[boat]))\n", "end" ] }, { "cell_type": "markdown", "id": "1486f52e", "metadata": {}, "source": [ "The above values lead to the value 541.6 for the objective." ] }, { "cell_type": "markdown", "id": "05a97f39-e4ae-48f0-bb92-8287b33ac33c", "metadata": {}, "source": [ "# 5. cost benefit analysis" ] }, { "cell_type": "markdown", "id": "a040a316-c284-40e9-b256-667d672f36a7", "metadata": {}, "source": [ "An investment of $10,000 will save us $1,500 each year \n", "for the coming eight years.\n", "\n", "* Use continuous compounding and a discount rate of 6% \n", " to compute the present worth of the savings.\n", "\n", "* Is the investment worthwhile?" ] }, { "cell_type": "markdown", "id": "55269fec-6fc2-47b3-970a-37425fa2436b", "metadata": {}, "source": [ "## the present value of the future savings" ] }, { "cell_type": "markdown", "id": "7c9625bd-169e-48aa-9dba-d4141db3f934", "metadata": {}, "source": [ "We compute the present value of the future savings:" ] }, { "cell_type": "code", "execution_count": 21, "id": "ab381d68-2ffb-4d32-b4ce-2f6cce0fe0e8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1500" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 1500" ] }, { "cell_type": "code", "execution_count": 22, "id": "3379c8dc-cdad-44fd-9627-18c630806108", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.06" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = 0.06" ] }, { "cell_type": "code", "execution_count": 23, "id": "13613046-af97-43ee-b502-f66369633696", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9247.361701729767" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum([s*exp(-r*t) for t=1:8])" ] }, { "cell_type": "markdown", "id": "62ac6e48-14da-4a48-bf58-c8a1858b1c83", "metadata": {}, "source": [ "Since the present value is less than $10,000,\n", "the investment is not worthwhile." ] }, { "cell_type": "markdown", "id": "aba88e3c-f7ae-4c8d-b815-f488ecb7ac84", "metadata": {}, "source": [ "# 6. supply and demand" ] }, { "cell_type": "markdown", "id": "c8ebc2a7-0942-4040-b642-d413d629f7e9", "metadata": {}, "source": [ "Consider a market with demand $q = 5 + 10/p$ and supply $q = p^2 - 3$.\n", "\n", "1. Compute the equilibrium price and the revenue.\n", "\n", "2. Suppose the government gives the producer a subsidy\n", " of $1 per item. What is the effect of this subsidy?\n", "\n", " Compute the new supply, equilibrium price, and revenue function.\n", "\n", "3. Of every dollar the government spends on subsidy,\n", " how much goes to producer, and how much to the consumer?" ] }, { "cell_type": "markdown", "id": "75664bab-a255-4ccf-aad7-c68d0b77f6c4", "metadata": {}, "source": [ "## equilibrium and revenue" ] }, { "cell_type": "markdown", "id": "ebdd46e3-7747-491a-b455-d31c6465a7b1", "metadata": {}, "source": [ "At the equilibrium, supply equals demand:" ] }, { "cell_type": "markdown", "id": "e3a77291-994a-46b2-93fa-eb99c08103a0", "metadata": {}, "source": [ "$$\n", " 5 + 10/p = p^2 - 3 \n", "$$" ] }, { "cell_type": "markdown", "id": "02023fad-5506-47d0-a010-15c44ba33c27", "metadata": {}, "source": [ "Let us use ``NLsolve`` to compute the equilibrium." ] }, { "cell_type": "code", "execution_count": 24, "id": "b0fc0ae2-4748-4448-bfa0-659b85bb3592", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f!" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", " function f!(y, x)\n", "\n", "defines y[1] as 5+10/p - p^2 + 3,\n", "where p equals x[1].\n", "\"\"\"\n", "function f!(y, x)\n", " p = x[1]\n", " y[1] = 5+10/p - p^2 + 3\n", "end" ] }, { "cell_type": "code", "execution_count": 25, "id": "33e7dd42-8a02-424e-abb2-850e0b806276", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Results of Nonlinear Solver Algorithm\n", " * Algorithm: Trust-region with dogleg and autoscaling\n", " * Starting Point: [1.0]\n", " * Zero: [3.318628217750186]\n", " * Inf-norm of residuals: 0.000000\n", " * Iterations: 5\n", " * Convergence: true\n", " * |x - x'| < 0.0e+00: false\n", " * |f(x)| < 1.0e-08: true\n", " * Function Calls (f): 6\n", " * Jacobian Calls (df/dx): 6" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equilibrium = nlsolve(f!, [1.0])" ] }, { "cell_type": "code", "execution_count": 26, "id": "06ef3f54-37d6-4fbf-a337-2d1325128759", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Vector{Float64}:\n", " 3.318628217750186" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equilibrium.zero" ] }, { "cell_type": "markdown", "id": "2d68010f-e131-41fd-bf08-6eef79a24678", "metadata": {}, "source": [ "So the equilibrium price is $3.32." ] }, { "cell_type": "markdown", "id": "742833b1-1154-4a7a-87ff-820a19c62029", "metadata": {}, "source": [ "Then the revenue is\n", "$$\n", " R = p \\cdot S(p).\n", "$$" ] }, { "cell_type": "code", "execution_count": 27, "id": "f8eaa6a2-7695-4f3a-9f16-5d43221c4412", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "S (generic function with 1 method)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S(p) = p^2 - 3" ] }, { "cell_type": "code", "execution_count": 28, "id": "4a4ce0be-a5ac-4985-902c-c1c0e0d63143", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.318628217750186" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equ_p = equilibrium.zero[1]" ] }, { "cell_type": "code", "execution_count": 29, "id": "845498b5-4a6f-42e1-b390-ec21bf5891ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "26.593141088750933" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R = equ_p * S(equ_p)" ] }, { "cell_type": "markdown", "id": "d56e1561-2f30-4dc5-b270-2b27600a1fa2", "metadata": {}, "source": [ "The revenue is $26.59." ] }, { "cell_type": "markdown", "id": "d86b6ea0-0763-4ef1-aa9d-22995dddfbf6", "metadata": {}, "source": [ "The new supply function is $S(p+1)$, or\n", "$$\n", " S(p+1) = (p+1)^2 - 3.\n", "$$\n", "For the new equilibrium price, use ``NLsolve`` again. " ] }, { "cell_type": "code", "execution_count": 30, "id": "6fecb379-55b3-4cf8-8423-45c6cb6ca4e8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "g!" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", " function g!(y, x)\n", "\n", "defines y[1] as 5+10/p - (p+1)^2 + 3,\n", "where p equals x[1].\n", "\"\"\"\n", "function g!(y, x)\n", " p = x[1]\n", " y[1] = 5+10/p - (p+1)^2 + 3\n", "end" ] }, { "cell_type": "code", "execution_count": 31, "id": "9c77ecdc-1176-4c6e-ab4b-53c11dadbfb2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Results of Nonlinear Solver Algorithm\n", " * Algorithm: Trust-region with dogleg and autoscaling\n", " * Starting Point: [1.0]\n", " * Zero: [2.470895516291017]\n", " * Inf-norm of residuals: 0.000000\n", " * Iterations: 4\n", " * Convergence: true\n", " * |x - x'| < 0.0e+00: false\n", " * |f(x)| < 1.0e-08: true\n", " * Function Calls (f): 5\n", " * Jacobian Calls (df/dx): 5" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_equilibrium = nlsolve(g!, [1.0])" ] }, { "cell_type": "code", "execution_count": 32, "id": "609d40df-ae45-45bd-8365-b3fee3e36deb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Vector{Float64}:\n", " 2.470895516291017" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_equilibrium.zero" ] }, { "cell_type": "markdown", "id": "7b04a46b-d5dd-4fdd-bf1a-5706c8a6ce98", "metadata": {}, "source": [ "So the new equilibrium price is $2.47." ] }, { "cell_type": "code", "execution_count": 33, "id": "27d881bd-6e19-4b6d-83ff-fc3901ca6476", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.470895516291017" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_equ_p = new_equilibrium.zero[1]" ] }, { "cell_type": "markdown", "id": "0d187ed9-f7d5-4870-9abf-a5792ee2181b", "metadata": {}, "source": [ "To compute the new supply, evaluate the supply function at ``new_equ_p`` plus one:" ] }, { "cell_type": "code", "execution_count": 34, "id": "abb3dc97-1362-461b-b61b-c613081dac08", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.047115685009086" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S(new_equ_p+1)" ] }, { "cell_type": "markdown", "id": "ad462abf-448f-45a4-b0dc-88fbb0d8be54", "metadata": {}, "source": [ "The new supply: 9.05." ] }, { "cell_type": "code", "execution_count": 35, "id": "0cf3d6e4-9c3e-4527-b96a-ff30f747cecb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22.354477581455082" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_R = new_equ_p*S(new_equ_p+1)" ] }, { "cell_type": "markdown", "id": "a1abb72c-f5fe-4808-b9b6-9bb3c80254a6", "metadata": {}, "source": [ "The new revenue: $22.35." ] }, { "cell_type": "markdown", "id": "6ebcc7f3-b16a-4329-8646-7a5700187696", "metadata": {}, "source": [ "The benefit of the consumer:" ] }, { "cell_type": "code", "execution_count": 36, "id": "e6f10337-1980-4423-8584-7d23f89eb401", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.8499999999999996" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.32 - 2.47" ] }, { "cell_type": "markdown", "id": "906ca9ab-1b31-4b5b-8cb2-0e6199f88444", "metadata": {}, "source": [ "so the consumer gets 85 cents for the $1 subsidy.\n", "\n", "Of the $1 subsidy, 15 cents goes to the producer." ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.12", "language": "julia", "name": "julia-1.12" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }