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:
statistical reasoning and Monte Carlo methods
Taguchi quality control
analyzing data
z-transforms, linear recursions
properties of the discrete Fourier transform and the FFT
filter design and image processing with the FFT
linear programming and regression
cost-benefit analysis, present value of future sums
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 \(80\%\) after the first dosis and costs \(\$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 \(\$10\). Clinical tests for a new fewer reducing medicine reported a success rate of \(95\%\) after taking the first dosis.
Under the same circumstances as above, is a cost of \(\$2\) for the new medicine justified? (Hint: compute the expected loss of having a fever for both medicines.)
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.
The expected loss for the first medicine:
\[0.80 ~\cdot~ \$1 + 0.2 \cdot ( \$1 + \$10 ) = \$3.0.\]For the new medicine:
\[0.95 ~\cdot~ \$2 + 0.05 \cdot ( \$2 + \$10 ) = \$2.5.\]The patient is better off with the new medicine.
For a fair price, we assume the same expected loss.
Let \(x\) be the break even price, with both medicines the same loss.
\[\begin{split}\begin{array}{rcl} 3.0 & = & 0.95 x + 0.05(x + 10) \\ & = & x + 0.5 \\ & \Rightarrow & x = \$2.5. \end{array}\end{split}\]If \(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?
Set up a simulation to decide the odds of to finish the race.
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 \(\$400\) is deposited into the account. After the first deposit, \(20\%\) of the balance of the savings account is withdrawn at the first day of every month.
Set up the recursion relation for the balance \(B(n)\) at month \(n\), after \(n\) deposits.
Solve the recursion for \(B(n)\).
In doing so, will we ever get rich? Is there a limit to \(B(n)\)?
The recursion is
\[B(n) = 0.8 B(n-1) + 400, \quad B(0) = 0.\]
We can solve this recursion by substitution.
Now use \(B(0) = 0\) and the explicit formula for the geometric sum.
To answer the original question if we will ever get rich, we observe that we will never get more than \(\$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
\(f_1=2 \mbox{ ft}^2\)
\(c_1=\$122\)
\(p_1=\$5\)
\(n_1=20\)
2
\(f_2=3 \mbox{ ft}^2\)
\(c_2=\$130\)
\(p_2=\$7\)
\(n_2=15\)
3
\(f_3=4 \mbox{ ft}^2\)
\(c_3=\$150\)
\(p_3=\$9\)
\(n_3=10\)
Boat \(i\) requires \(f_i\) square feet to store, costs \(c_i\) dollars to purchase, and can be rented at \(p_i\) dollars a trip. Our constraints are as follows:
We need at least \(n_i\) boats of type \(i\),
but have only 400 square feet to store the boats, and
our budget is limited to \(\$10,000\).
Determine how many boats of each type we should order.
The mathematical formulation as a linear programming problem is as follows:
Let \(x_i\) be the number of boats of type \(i\).
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:
Now we add constraints to the model.
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)
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)
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 \(\$10,000\) will save us \(\$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 \(\$10,000\), the investment is not worthwhile.
Benefit of a Subsidy to the Consumer¶
Consider a market with demand \(q = 5 + 10/p\) and supply \(q = p^2 - 3\).
Compute the equilibrium price and the revenue.
Suppose the government gives the producer a subsidy of \(\$1\) per item. What is the effect of this subsidy? Compute the new supply, equilibrium price, and revenue function.
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
Then the revenue is
For the effect of the subsidy, we compute the new supply, equilibrium price, and revenue function. The new supply function is
We apply NLsolve
again, and find as
the new equilibrium price: $2.47.
The new supply is 9.05 and
the new revenue is \(\$22.35\).
The benefit for the consumer is the difference between the old and the new equilibrium price, computed as follows:
so the consumer gets 85 cents for the \(\$1\) subsidy. Of the \(\$1\) subsidy, 15 cents goes to the producer.