{ "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": [ "
| Row | type | area | cost | price | need |
|---|---|---|---|---|---|
| Int64 | Int64 | Int64 | Int64 | Int64 | |
| 1 | 1 | 2 | 122 | 5 | 20 |
| 2 | 2 | 3 | 130 | 7 | 15 |
| 3 | 3 | 4 | 150 | 9 | 10 |