{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "147d8186-107b-475c-9a89-6657197f4238",
   "metadata": {},
   "source": [
    "3.  A retired engineer has \\\\$250,000 to invest and is willing to spend\n",
    "five hours per week managing her investments.  Municipal bonds earn 6%\n",
    "per year and require no management. Real estate investments are expected\n",
    "to appreciate at 8% per year and require one hour of management per\n",
    "\\\\$100,000 invested. Blue chip stocks earn 10% per year and require 1.5\n",
    "hours of management. Junk bonds earn 12% and require 2.5 hours, while\n",
    "grain futures earn 15% and require five hours per \\\\$100,000 invested.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49a1a0b8-2f96-444a-8271-6d4794568733",
   "metadata": {},
   "source": [
    "(i) How should the retiree invest her money in order to maximize her\n",
    "expected earnings?  Solve as a linear programming problem."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1995b4c1-c3d3-47e3-84d5-7c16527f1eb8",
   "metadata": {},
   "source": [
    "The decision variables will be $x_i$ with the meanings\n",
    "\n",
    "$x_1 = {}$amount invested in municipal bonds.\\\n",
    "$x_2 = {}$amount invested in real estate.\\\n",
    "$x_3 = {}$amount invested in blue-chip stocks.\\\n",
    "$x_4 = {}$amount invested in junk bonds.\\\n",
    "$x_5 = {}$amount invested in grain futures.\n",
    "\n",
    "Let $h_i$ be the hours of time needed per \\\\$100,000 invested\n",
    "and $r_i$ be the return rates for each of the respective catagories.  Thus,\n",
    "$$\n",
    "    h = (0, 1, 1.5, 2.5, 5)\n",
    "    \\qquad\\hbox{and}\\qquad\n",
    "    r = (0.06, 0.08, 0.10, 0.12, 0.15).\n",
    "$$\n",
    "The corresponding constraints are on the hours available to manage the investments\n",
    "\n",
    "$$H(x) = h\\cdot x/100000 \\le 5$$\n",
    "\n",
    "and the total amount of money available\n",
    "\n",
    "$$T(x) = \\sum_{i=1}^5 x_i\\le 250000.$$\n",
    "\n",
    "The objective function is the earnings\n",
    "\n",
    "$$R(x) = r\\cdot x$$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9a61fd6b-ade8-4c40-be0a-1abee3ae9348",
   "metadata": {},
   "outputs": [],
   "source": [
    "using JuMP, HiGHS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7ec7e796-9cba-4f84-a9ee-986d12915be0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A JuMP Model\n",
       "├ solver: HiGHS\n",
       "├ objective_sense: FEASIBILITY_SENSE\n",
       "├ num_variables: 0\n",
       "├ num_constraints: 0\n",
       "└ Names registered in the model: none"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model=Model(HiGHS.Optimizer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "81299d71-53a8-42bb-b70e-ce5304f0a0f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Vector{VariableRef}:\n",
       " x[1]\n",
       " x[2]\n",
       " x[3]\n",
       " x[4]\n",
       " x[5]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@variable(model,x[1:5].>=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c14121aa-5a46-42af-9656-368575bfce73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Vector{Float64}:\n",
       " 0.0\n",
       " 1.0\n",
       " 1.5\n",
       " 2.5\n",
       " 5.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h=[0,1,1.5,2.5,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b92d10c6-8be1-4268-9640-be0aab1d77b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Vector{Float64}:\n",
       " 0.06\n",
       " 0.08\n",
       " 0.1\n",
       " 0.12\n",
       " 0.15"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r=[0.06,0.08,0.10,0.12,0.15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "38e8d322-e8b3-47e9-9125-0903151b7d73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$ 1.0 \\times 10^{-5} x_{2} + 1.5 \\times 10^{-5} x_{3} + 2.5 \\times 10^{-5} x_{4} + 5.0 \\times 10^{-5} x_{5} $"
      ],
      "text/plain": [
       "1.0e-5 x[2] + 1.5e-5 x[3] + 2.5e-5 x[4] + 5.0e-5 x[5]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "H(x)=h'*x/100000\n",
    "H(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eb3d0ba5-cd56-4c62-9643-051c60bd79da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$ x_{1} + x_{2} + x_{3} + x_{4} + x_{5} $"
      ],
      "text/plain": [
       "x[1] + x[2] + x[3] + x[4] + x[5]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "T(x)=ones(5)'*x\n",
    "T(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "aa2bfa8a-a829-471b-a1e9-48f3f27b9185",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$ 0.06 x_{1} + 0.08 x_{2} + 0.1 x_{3} + 0.12 x_{4} + 0.15 x_{5} $"
      ],
      "text/plain": [
       "0.06 x[1] + 0.08 x[2] + 0.1 x[3] + 0.12 x[4] + 0.15 x[5]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R(x)=r'*x\n",
    "R(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d7e8a213-c139-4871-b227-e39e010e4f38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$ 0.06 x_{1} + 0.08 x_{2} + 0.1 x_{3} + 0.12 x_{4} + 0.15 x_{5} $"
      ],
      "text/plain": [
       "0.06 x[1] + 0.08 x[2] + 0.1 x[3] + 0.12 x[4] + 0.15 x[5]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@objective(model,Max,R(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "82cafc9f-4aed-438f-8694-f59a743a5ed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$ 1.0 \\times 10^{-5} x_{2} + 1.5 \\times 10^{-5} x_{3} + 2.5 \\times 10^{-5} x_{4} + 5.0 \\times 10^{-5} x_{5} \\leq 5 $$"
      ],
      "text/plain": [
       "1.0e-5 x[2] + 1.5e-5 x[3] + 2.5e-5 x[4] + 5.0e-5 x[5] ≤ 5"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1=@constraint(model,H(x)<=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "41124b60-856e-4f46-b62f-9a2f478df538",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$ x_{1} + x_{2} + x_{3} + x_{4} + x_{5} \\leq 250000 $$"
      ],
      "text/plain": [
       "x[1] + x[2] + x[3] + x[4] + x[5] ≤ 250000"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c2=@constraint(model,T(x)<=250000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d0ae32c5-6c6e-4b6c-b034-ab88c7403146",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$ \\begin{aligned}\n",
       "\\max\\quad & 0.06 x_{1} + 0.08 x_{2} + 0.1 x_{3} + 0.12 x_{4} + 0.15 x_{5}\\\\\n",
       "\\text{Subject to} \\quad & 1.0 \\times 10^{-5} x_{2} + 1.5 \\times 10^{-5} x_{3} + 2.5 \\times 10^{-5} x_{4} + 5.0 \\times 10^{-5} x_{5} \\leq 5\\\\\n",
       " & x_{1} + x_{2} + x_{3} + x_{4} + x_{5} \\leq 250000\\\\\n",
       " & x_{1} \\geq 0\\\\\n",
       " & x_{2} \\geq 0\\\\\n",
       " & x_{3} \\geq 0\\\\\n",
       " & x_{4} \\geq 0\\\\\n",
       " & x_{5} \\geq 0\\\\\n",
       "\\end{aligned} $$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bd7ffac0-ffee-4b92-94f0-4aebbe987cc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running HiGHS 1.13.1 (git hash: 1d267d97c): Copyright (c) 2026 under Apache 2.0 license terms\n",
      "Using BLAS: blastrampoline \n",
      "LP has 2 rows; 5 cols; 9 nonzeros\n",
      "Coefficient ranges:\n",
      "  Matrix  [1e-05, 1e+00]\n",
      "  Cost    [6e-02, 1e-01]\n",
      "  Bound   [0e+00, 0e+00]\n",
      "  RHS     [5e+00, 2e+05]\n",
      "Presolving model\n",
      "2 rows, 5 cols, 9 nonzeros  0s\n",
      "2 rows, 5 cols, 9 nonzeros  0s\n",
      "Presolve reductions: rows 2(-0); columns 5(-0); nonzeros 9(-0) - Not reduced\n",
      "Problem not reduced by presolve: solving the LP\n",
      "Using dual simplex solver\n",
      "  Iteration        Objective     Infeasibilities num(sum)\n",
      "          0    -6.9599912765e+00 Ph1: 2(9.4152); Du: 5(6.95999) 0.0s\n",
      "          3     2.7500000000e+04 Pr: 0(0) 0.0s\n",
      "\n",
      "Model status        : Optimal\n",
      "Simplex   iterations: 3\n",
      "Objective value     :  2.7500000000e+04\n",
      "P-D objective error :  1.3228773321e-16\n",
      "HiGHS run time      :          0.00\n"
     ]
    }
   ],
   "source": [
    "optimize!(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4a821057-6cb8-4ae3-9070-4da11752441b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27500.000000000007"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "objective_value(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a4430aeb-3bff-4004-8236-7f1deb69463c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Vector{Float64}:\n",
       "      0.0\n",
       "      0.0\n",
       " 125000.00000000016\n",
       " 124999.99999999993\n",
       "      0.0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6f3f022-3eb6-4353-b849-9825a8e9d510",
   "metadata": {},
   "source": [
    "The retiree should invest \\\\$125000 in blue-chip stocks and \\\\$125000 in junk bonds.\n",
    "\n",
    "The earnings will be \\\\$27500 per year."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d19edcb-53ee-4bf0-b42d-b6053ab67601",
   "metadata": {},
   "source": [
    "(ii) Determine the shadow prices for each constraint. Interpret each\n",
    "shadow price in the context of this problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ef4eee38-b5d2-45e8-8f39-b0ec6637fdde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2000.0000000000045"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# shadow price for the H(x)<=5 constraint on time\n",
    "dual(c1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f86d5dc-1bf5-4e99-b124-759871daeb94",
   "metadata": {},
   "source": [
    "This means if the available time to manage the investments\n",
    "were increased by $1$ hour/week then earnings would increase by \\\\$2000 per year."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3eb826d4-9bfd-49c5-8dd9-112714b5cf34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.06999999999999997"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# shadow price for the T(x)<=250000 constraint on money\n",
    "dual(c2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5ded3ae-4101-442d-8436-b67c9a1338a2",
   "metadata": {},
   "source": [
    "This means if the amount invested were increased by \\\\$1\n",
    "the earnings would increase by \\\\$0.07 per year."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a095b583-2c53-43aa-ae67-0e32991ee8be",
   "metadata": {},
   "source": [
    "(iii) The retiree downloads software from the internet that allows her\n",
    "to effectively manage her grain futures in three hours per week per\n",
    "\\\\$100,000. How does this change the results in parts (i) and (ii)?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "14672a47-07b3-4c8d-be44-fb202f95fbee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Vector{Float64}:\n",
       " 0.0\n",
       " 1.0\n",
       " 1.5\n",
       " 2.5\n",
       " 3.0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h=[0,1,1.5,2.5,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5c01b827-dea9-4d1a-9274-747b5231ffb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$ 1.0 \\times 10^{-5} x_{2} + 1.5 \\times 10^{-5} x_{3} + 2.5 \\times 10^{-5} x_{4} + 3.0 \\times 10^{-5} x_{5} \\leq 5 $$"
      ],
      "text/plain": [
       "1.0e-5 x[2] + 1.5e-5 x[3] + 2.5e-5 x[4] + 3.0e-5 x[5] ≤ 5"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delete(model,c1)\n",
    "c1=@constraint(model,H(x)<=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "772539a5-de4b-4a1a-83e5-68c8cf29aa64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$ \\begin{aligned}\n",
       "\\max\\quad & 0.06 x_{1} + 0.08 x_{2} + 0.1 x_{3} + 0.12 x_{4} + 0.15 x_{5}\\\\\n",
       "\\text{Subject to} \\quad & x_{1} + x_{2} + x_{3} + x_{4} + x_{5} \\leq 250000\\\\\n",
       " & 1.0 \\times 10^{-5} x_{2} + 1.5 \\times 10^{-5} x_{3} + 2.5 \\times 10^{-5} x_{4} + 3.0 \\times 10^{-5} x_{5} \\leq 5\\\\\n",
       " & x_{1} \\geq 0\\\\\n",
       " & x_{2} \\geq 0\\\\\n",
       " & x_{3} \\geq 0\\\\\n",
       " & x_{4} \\geq 0\\\\\n",
       " & x_{5} \\geq 0\\\\\n",
       "\\end{aligned} $$"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2427ec80-23c2-4faa-81ec-ab4a1f48edb3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LP has 2 rows; 5 cols; 9 nonzeros\n",
      "Coefficient ranges:\n",
      "  Matrix  [1e-05, 1e+00]\n",
      "  Cost    [6e-02, 1e-01]\n",
      "  Bound   [0e+00, 0e+00]\n",
      "  RHS     [5e+00, 2e+05]\n",
      "Solving LP with useful basis so presolve not used\n",
      "Using dual simplex solver\n",
      "  Iteration        Objective     Infeasibilities num(sum)\n",
      "          0    -7.1999795117e-01 Ph1: 2(2.07344); Du: 2(0.719998) 0.0s\n",
      "          2     3.0000000000e+04 Pr: 0(0) 0.0s\n",
      "\n",
      "Model status        : Optimal\n",
      "Simplex   iterations: 2\n",
      "Objective value     :  3.0000000000e+04\n",
      "P-D objective error :  6.0631969585e-17\n",
      "HiGHS run time      :          0.00\n"
     ]
    }
   ],
   "source": [
    "optimize!(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5af28d55-e7ab-47ce-a1c9-2b48245e2b0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30000.000000000004"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "objective_value(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "da8b7bc8-2c1c-4bd8-8af7-6ae6b12af13c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2500.0000000000036"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "objective_value(model)-27500"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ad3a3edb-73d4-46d5-bba9-51402e7e2e18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Vector{Float64}:\n",
       "  83333.33333333333\n",
       "      0.0\n",
       "      0.0\n",
       "      0.0\n",
       " 166666.6666666667"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fa7a4fc-6d49-43f7-8904-a4435134aee4",
   "metadata": {},
   "source": [
    "The optimal investment strategy is now \\\\$83333.33 in municipal bonds and \\\\$166666.67 in grain futures.\n",
    "\n",
    "The earnings have increased by \\\\$2500 and are now \\\\$30000 per/year."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d087e81d-18b7-4bcb-ba0d-398a04c202ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-3000.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dual(c1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e7708b71-172a-46fa-a421-d81fbdd4a614",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.06"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dual(c2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b322841-54c0-4c10-a81d-59edd69f51ed",
   "metadata": {},
   "source": [
    "The contraint on time is even more costly so an additional hour/week would result in a \\\\$3000 increase in yearly earnings.\n",
    "\n",
    "On the other hand, increasing the amount available to invest by \\\\$1 only increases the return by \\\\$0.06.  Presumably,\n",
    "because the time constraint is so costly."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fc96a0b-cafd-4679-a1bf-bba65b81f73e",
   "metadata": {},
   "source": [
    "(iv) After a few disasters in the futures market, the engineer decides that\n",
    "risk is a significant factor in her investment strategy. An investment\n",
    "self-help book ranks municipal bonds, real estate, blue chip stocks,\n",
    "junk bonds, and grain futures as risk level 1, 4, 3, 6, and 10\n",
    "respectively. The engineer decides that her investment portfolio should\n",
    "have an average risk level of no more than 4. How does this change\n",
    "the results in parts (i) and (ii)?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4f080fa-e2df-4d0c-ab22-4406949002e2",
   "metadata": {},
   "source": [
    "Let $v=(1,4,3,6,10)$ and introduct the new constraint\n",
    "$$\n",
    "        V(x)=v\\cdot x/250000 \\le 4.\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "43b43f97-fab2-4a07-8ac1-1b294e5eb247",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$ 4.0 \\times 10^{-6} x_{1} + 1.6 \\times 10^{-5} x_{2} + 1.2 \\times 10^{-5} x_{3} + 2.4 \\times 10^{-5} x_{4} + 4.0 \\times 10^{-5} x_{5} $"
      ],
      "text/plain": [
       "4.0e-6 x[1] + 1.6e-5 x[2] + 1.2e-5 x[3] + 2.4e-5 x[4] + 4.0e-5 x[5]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v=[1,4,3,6,10]\n",
    "V(x)=v'*x/250000\n",
    "V(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3e00f500-3af5-487b-a233-611a0f98266f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$ 4.0 \\times 10^{-6} x_{1} + 1.6 \\times 10^{-5} x_{2} + 1.2 \\times 10^{-5} x_{3} + 2.4 \\times 10^{-5} x_{4} + 4.0 \\times 10^{-5} x_{5} \\leq 4 $$"
      ],
      "text/plain": [
       "4.0e-6 x[1] + 1.6e-5 x[2] + 1.2e-5 x[3] + 2.4e-5 x[4] + 4.0e-5 x[5] ≤ 4"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c3=@constraint(model,V(x)<=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ff13ec9b-00a8-46c3-a07a-61d83b8fa577",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LP has 3 rows; 5 cols; 14 nonzeros\n",
      "Coefficient ranges:\n",
      "  Matrix  [4e-06, 1e+00]\n",
      "  Cost    [6e-02, 1e-01]\n",
      "  Bound   [0e+00, 0e+00]\n",
      "  RHS     [4e+00, 2e+05]\n",
      "Solving LP with useful basis so presolve not used\n",
      "Using dual simplex solver\n",
      "  Iteration        Objective     Infeasibilities num(sum)\n",
      "          0     2.9999949658e+04 Pr: 1(6144) 0.0s\n",
      "          2     2.6785714286e+04 Pr: 0(0) 0.0s\n",
      "\n",
      "Model status        : Optimal\n",
      "Simplex   iterations: 2\n",
      "Objective value     :  2.6785714286e+04\n",
      "P-D objective error :  0.0000000000e+00\n",
      "HiGHS run time      :          0.00\n"
     ]
    }
   ],
   "source": [
    "optimize!(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bd61cac2-d27b-47da-8996-f3fd2d5b4a2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26785.714285714283"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "objective_value(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5e2883ad-2ee9-484a-bbbf-9d772a3cce17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5-element Vector{Float64}:\n",
       "      0.0\n",
       "      0.0\n",
       " 214285.71428571423\n",
       "      0.0\n",
       "  35714.28571428573"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "408d46a1-e9e8-4b64-a18d-01cd7d159c12",
   "metadata": {},
   "source": [
    "To reduce risk level the new strategy invests \\\\$214285.71 in blue-chip stocks and \\\\$35714.28 in grain futures.\n",
    "\n",
    "The yearly earnings on the investment is reduced to \\\\$26785.71 per year."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0341e0af-8025-4b33-bd67-d8ebf628101c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dual(c1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3a4a6a77-32d8-4428-9846-2e4f63abdd8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.07857142857142857"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dual(c2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b8b6d111-bd73-4be7-a7a8-30ed11c9a017",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1785.7142857142849"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dual(c3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06081515-de8b-4708-824c-3ea94ed27336",
   "metadata": {},
   "source": [
    "The fact that the shadow price for the time constraint is 0 implies that time needed\n",
    "to manage the investments is no longer a factor that limits the earnings.\n",
    "\n",
    "Every increase of \\\\$1 in the investment results in a \\\\$0.07857 increase in yearly earnings.\n",
    "\n",
    "Allowing 1 unit greater risk factor would increase yearly earnings by \\\\$1785.71.  But note\n",
    "these are average yearly earnings and the primary reason for the risk factor was to reduce\n",
    "the risk of the earnings being less than expected."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38d26d57-7d6d-4cc6-88dc-e6a4a58dc024",
   "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
}
