Review for the First Midterm Exam ================================= The questions following in the overview are representative for the style of midterm exam questions, suitable for what can be covered in a 50-minute lecture. We have covered many more topics than the content of those five sample review questions. Homework problems must be reviewed as well. Overview -------- The midterm exam covers the following topics: 1. statistical reasoning and Monte Carlo methods 2. Taguchi quality control 3. analyzing data 4. z-transforms, linear recursions 5. properties of the discrete Fourier transform and the FFT 6. filter design and image processing with the FFT 7. linear programming and regression 8. cost-benefit analysis, present value of future sums 9. microeconomics and macroeconomics As the exam will require the use of Julia, in preparation for the exam, make sure that all packages are properly installed on your computer. In the list of all Julia packages we used, the lecture numbers indicates the first encounter: * IJulia, Plots, QuadGK (L-1) * Statistics, StatsKit (L-3) * DelimitedFiles, DataFrames (L-5) * SymPy (L-6) * FFTW (L-10) * Images (L-11) * JuMP, GLPK (L-15) * GLM (L-16) * FastGaussQuadrature (L-17) * NLsolve (L-18) Upgrading to a newer version of Julia may require at least a recompilation, possibly a download, of all packages. Fair Price for a New Medicine? ------------------------------ Suppose a medicine to reduce fever is effective in :math:`80\%` after the first dosis and costs :math:`\$1`. A temperature reading of more than two degrees above normal six hours after taking the first dosis requires a visit to the doctor which costs :math:`\$10`. Clinical tests for a new fewer reducing medicine reported a success rate of :math:`95\%` after taking the first dosis. 1. Under the same circumstances as above, is a cost of :math:`\$2` for the new medicine justified? (*Hint:* compute the expected loss of having a fever for both medicines.) 2. What is a fair price for the new medicine, taking into account its higher quality? At which price would you buy the new medicine? To solve this question, we start by computing the expected loss. 1. The expected loss for the first medicine: .. math:: 0.80 ~\cdot~ \$1 + 0.2 \cdot ( \$1 + \$10 ) = \$3.0. For the new medicine: .. math:: 0.95 ~\cdot~ \$2 + 0.05 \cdot ( \$2 + \$10 ) = \$2.5. The patient is better off with the new medicine. 2. For a fair price, we assume the same expected loss. Let :math:`x` be the break even price, with both medicines the same loss. .. math:: \begin{array}{rcl} 3.0 & = & 0.95 x + 0.05(x + 10) \\ & = & x + 0.5 \\ & \Rightarrow & x = \$2.5. \end{array} If :math:`x \leq \$2.5`, then we buy the medicine. Otherwise not. Odds of Finishing a Race ------------------------ A man decides to participate in a 1,000 mile car race. Because of the rough terrain and excessive speed, tires have a life time normally distributed with an average of 600 miles and standard deviation of 200 miles. The man starts the race with 4 new tires and 4 spare tires. Given the expected life span of the tires, what are the odds to finish the 1,000 mile race? 1. Set up a simulation to decide the odds of to finish the race. 2. Run the simulation 10,000 times and give the probability of reaching the finish. Code for the simulation is defined in the Julia function below: :: function run(N::Int64) successes = 0 for k=1:N tires = 600 .+ randn(8)*200 life = tires[1:4] for j=1:4 life = sort(life) life[1] = life[1] + tires[4+j] end if minimum(life) >= 1000 successes = successes + 1 end end return successes end Running the simulation, executing ``run(10000)/10000`` gives ``3729`` So the probability is about .37 of finishing the race. Will We Ever Get Rich? ---------------------- Suppose we start with an empty savings account. At the last day of every month :math:`\$400` is deposited into the account. After the first deposit, :math:`20\%` of the balance of the savings account is withdrawn at the first day of every month. 1. Set up the recursion relation for the balance :math:`B(n)` at month :math:`n`, after :math:`n` deposits. 2. Solve the recursion for :math:`B(n)`. 3. In doing so, will we ever get rich? Is there a limit to :math:`B(n)`? The recursion is .. math:: B(n) = 0.8 B(n-1) + 400, \quad B(0) = 0. We can solve this recursion by substitution. .. math:: \begin{array}{rcl} B(n) & = & {\displaystyle \frac{4}{5} ~ B(n-1) + 400} \\ & = & {\displaystyle \frac{4}{5} \left( \frac{4}{5} ~ B(n-2) + 400 \right) + 400} \\ & \vdots & \\ & = & {\displaystyle \left( \frac{4}{5} \right)^n B(0) + 400 \sum_{k=0}^{n-1} \left( \frac{4}{5} \right)^k} \end{array} Now use :math:`B(0) = 0` and the explicit formula for the geometric sum. .. math:: 400 \sum_{k=0}^{n-1} \left( \frac{4}{5} \right)^k = 400 \frac{\left( \frac{4}{5} \right)^n - 1}{\frac{4}{5} - 1} = 2000 \left( 1 - \left( \frac{4}{5} \right)^n \right) To answer the original question if we will ever get rich, we observe that we will never get more than :math:`\$2000`. Planning for a Boat Rental Company ---------------------------------- A boat rental company must order new boats for next summer. There are three types of boats as defined in the table below: +-----------+----------------------------+-------------------+-----------------+----------------+ | boat type | storage | purchase | rental | need | +-----------+----------------------------+-------------------+-----------------+----------------+ | #seats | area | cost | price | for boats | +===========+============================+===================+=================+================+ | 1 | :math:`f_1=2 \mbox{ ft}^2` | :math:`c_1=\$122` | :math:`p_1=\$5` | :math:`n_1=20` | +-----------+----------------------------+-------------------+-----------------+----------------+ | 2 | :math:`f_2=3 \mbox{ ft}^2` | :math:`c_2=\$130` | :math:`p_2=\$7` | :math:`n_2=15` | +-----------+----------------------------+-------------------+-----------------+----------------+ | 3 | :math:`f_3=4 \mbox{ ft}^2` | :math:`c_3=\$150` | :math:`p_3=\$9` | :math:`n_3=10` | +-----------+----------------------------+-------------------+-----------------+----------------+ Boat :math:`i` requires :math:`f_i` square feet to store, costs :math:`c_i` dollars to purchase, and can be rented at :math:`p_i` dollars a trip. Our constraints are as follows: 1. We need at least :math:`n_i` boats of type :math:`i`, 2. but have only 400 square feet to store the boats, and 3. our budget is limited to :math:`\$10,000`. Determine how many boats of each type we should order. The mathematical formulation as a linear programming problem is as follows: Let :math:`x_i` be the number of boats of type :math:`i`. .. math:: \max 5 x_1 + 7 x_2 + 9 x_3 .. math:: \begin{array}{rrcl} \mbox{subject to} & & & \\ & 2 x_1 + 3 x_2 + 4 x_3 & \leq & 400 \\ & 122 x_1 + 130 x_2 + 150 x_3 & \leq & 10000 \\ & x_1 & \geq & 20 \\ & x_2 & \geq & 15 \\ & x_3 & \geq & 10 \end{array} Let us now solve this problem with Julia. We start by defining the dataframe for the problem, using the Julia optimization package JuMP: :: using JuMP import DataFrames import GLPK and store the data in a dataframe: :: boats = DataFrames.DataFrame( [ 1 2 122 5 20 2 3 130 7 15 3 4 150 9 10 ], ["type", "area", "cost", "price", "need"] ) The code which defines the model follows: :: model = Model(GLPK.Optimizer) @variable(model, x[boats.type] >= 0) @objective( model, Max, sum(boat["price"] * x[boat["type"]] for boat in eachrow(boats)) ) defines the objective: .. math:: \max 5 x_1 + 7 x_2 + 9 x_3 Now we add constraints to the model. 1. The first contraint is on the storage: :: storage = @expression( model, sum(boat["area"] * x[boat["type"]] for boat in eachrow(boats)), ) @constraint(model, storage <= 400) 2. The second constraint is on the budget: :: budget = @expression( model, sum(boat["cost"] * x[boat["type"]] for boat in eachrow(boats)), ) @constraint(model, budget <= 10000) 3. adding the need for each type of boat: :: for boat in eachrow(boats) @constraint(model, x[boat["type"]] >= boat["need"]) end To verify whether we have the model defined correctly, do ``print(model)`` and then we can solve the problem: :: optimize!(model) solution_summary(model) prints :: * Solver : GLPK * Status Termination status : OPTIMAL Primal status : FEASIBLE_POINT Dual status : FEASIBLE_POINT Message from the solver: "Solution is optimal" * Candidate solution Objective value : 541.5999999999999 Objective bound : Inf Dual objective value : 541.6 * Work counters Solve time (sec) : 0.00600 To see the number of boats of each type we have to purchase we do :: for boat in boats.type println("x[",boat, "] = ", value(x[boat])) end which shows :: x[1] = 20.0 x[2] = 15.0 x[3] = 37.4 and this corresponds to the value of 541.6 of the objective. Is an Investment Worthwhile? ---------------------------- An investment of :math:`\$10,000` will save us :math:`\$1,500` each year for the coming eight years. * Use continuous compounding and a discount rate of 6\% to compute the present worth of the savings. * Is the investment worthwhile? In our cost benefit analysis, we compute the present value of the future savings, directly in the Julia shell: :: julia> s = 1500; r = 0.06; julia> sum([s*exp(-r*t) for t=1:8]) 9247.361701729767 Since the present value is less than :math:`\$10,000`, the investment is not worthwhile. Benefit of a Subsidy to the Consumer ------------------------------------ Consider a market with demand :math:`q = 5 + 10/p` and supply :math:`q = p^2 - 3`. 1. Compute the equilibrium price and the revenue. 2. Suppose the government gives the producer a subsidy of :math:`\$1` per item. What is the effect of this subsidy? Compute the new supply, equilibrium price, and revenue function. 3. Of every dollar the government spends on subsidy, how much goes to producer, and how much to the consumer? Using ``NLsolve``, to compute the equilbrium, we solve the equation .. math:: 5 + 10/p = p^2 - 3 \quad \Rightarrow \quad p = \$3.32. Then the revenue is .. math:: R = p \cdot S(p) = \$3.32 \cdot 8.01 = \$26.59. For the effect of the subsidy, we compute the new supply, equilibrium price, and revenue function. The new supply function is .. math:: S(p+1) = (p+1)^2 - 3. We apply ``NLsolve`` again, and find as the new equilibrium price: \$2.47. The new supply is 9.05 and the new revenue is :math:`\$22.35`. The benefit for the consumer is the difference between the old and the new equilibrium price, computed as follows: .. math:: \$3.32 - \$2.47 = \$0.85, so the consumer gets 85 cents for the :math:`\$1` subsidy. Of the :math:`\$1` subsidy, 15 cents goes to the producer.