{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f45981b3-2010-4772-a113-dd3b6f03638a",
   "metadata": {},
   "source": [
    "**Example 7.1.** An electronics manufacturer produces a variety of diodes. Qual-\n",
    "ity control engineers attempt to insure that faulty diodes will be detected in the\n",
    "factory before they are shipped. It is estimated that 0.3% of the diodes pro-\n",
    "duced will be faulty. It is possible to test each diode individually. It is also\n",
    "possible to place a number of diodes in series and test the entire group. If this\n",
    "test fails, it means that one or more of the diodes in that group are faulty. The\n",
    "estimated testing cost is 5 cents for a single diode, and $4 + n$ cents for a group of\n",
    "$n > 1$ diodes. If a group test fails, then each diode in the group must be retested\n",
    "individually to ﬁnd the bad one(s). Find the most cost–eﬀective quality control\n",
    "procedure for detecting bad diodes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e187783-23c0-4613-ad7b-a3f46c9dc7b7",
   "metadata": {},
   "source": [
    "Let $A$ be the expected average cost per diode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cd190836-3b55-4b95-99d7-61fc02154973",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A (generic function with 1 method)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A(n)=4/n+6-5*p^n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2b274f77-fb31-444e-b3e2-70d2edabcdfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.997"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q=0.003\n",
    "p=1-q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "85c3c57b-64f5-4a17-b3e7-e294dc66b7ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19×2 Matrix{Float64}:\n",
       "  2.0  3.02996\n",
       "  3.0  2.3782\n",
       "  4.0  2.05973\n",
       "  5.0  1.87455\n",
       "  6.0  1.75599\n",
       "  7.0  1.67549\n",
       "  8.0  1.61875\n",
       "  9.0  1.57784\n",
       " 10.0  1.54799\n",
       " 11.0  1.52618\n",
       " 12.0  1.51039\n",
       " 13.0  1.49922\n",
       " 14.0  1.49167\n",
       " 15.0  1.487\n",
       " 16.0  1.48467\n",
       " 17.0  1.48426\n",
       " 18.0  1.48545\n",
       " 19.0  1.48796\n",
       " 20.0  1.4916"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T=[2:20 A.(2:20)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3dd8301f-b6fd-4da6-a09e-5bbd8cd6da6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i=argmin(A.(2:20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1ce2cdfd-b687-44b1-9500-bb2ffae2a61c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Vector{Float64}:\n",
       " 17.0\n",
       "  1.484264961220581"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T[i,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd593176-9094-4368-8786-a52843d4f42f",
   "metadata": {},
   "source": [
    "The optimial batch size is 17 and the per-diode testing cost is\n",
    "1.484264961220581"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "01788e20-2cdf-4ba5-aaae-0820a0bac5c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nopt=Int(T[i,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2e8cb448-c9a6-485f-b81d-92296f3d8607",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.484264961220581"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Aopt=T[i,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "226de0ff-6d9c-4496-b022-9da624130163",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Vector{Num}:\n",
       " q\n",
       " p\n",
       " n"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Symbolics\n",
    "D(f,x)=expand_derivatives(Differential(x)(f))\n",
    "@variables q,p,n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8af084fa-8b26-4145-8fda-0df6a3f1f2a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[96m6\u001b[39m - \u001b[96m5\u001b[39m(p^n) + \u001b[96m4\u001b[39m / n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9de3c557-30fa-404e-9a54-319516b35d47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[96m6\u001b[39m - \u001b[96m5\u001b[39m((\u001b[96m1\u001b[39m - q)^n) + \u001b[96m4\u001b[39m / n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Anq=substitute(A(n),p=>1-q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "110a7469-a1e4-49b3-a7ae-0d125d35fc2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[96m5\u001b[39mn*((\u001b[96m1\u001b[39m - q)^(-\u001b[96m1\u001b[39m + n))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dAdq=D(Anq,q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "dbd14d37-24f7-4b84-a047-1c0319913962",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.16373867744143392"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SAq=substitute(q/Aopt*dAdq,[n=>nopt,q=>0.003])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "558ac3e4-5c48-43e4-89c4-89fa3d30ef8f",
   "metadata": {},
   "source": [
    "The relative sensitivity $S(A,q)=0.16373867744143392$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23dd221c-14e4-42bf-9e59-4b5e7d214cb0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.10",
   "language": "julia",
   "name": "julia-1.10"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
