3 (Bonus)
NOTE: This is more a test of your statistical intuition, rather than coding ability in $\texttt{R}$.
When Paul M was a child he sold newspapers at a kiosk. In the morning he bought newspapers at 25 Brazilian Reals each, but after the first 200 newspapers the price falls to 5 Reals each. He sells the newspapers at 100 Reals each. If any remain unsold at the end of the day, he gets no refund. From previous experiences, he infers the demand for newspapers each day is distributed according to a $N(178,21^2)$ distribution. He would like to know what the optimal number of newspapers to buy each day was.
Let $n$ be the number of papers Paul M buys each day. Write R functions for the following functions:
cost(n)
to give the cost of buying $n$ papers,
profit(n,d)
to give the profit from sales when there is demand for $d$ papers and Paul M bought $n$ papers, and
average.profit(n,nreps)
to simulate nreps
values of demands $d\sim N(178,21^2)$ and return the average profit for these cases.
Use average.profit
to work out the optimal number of newspapers Paul M should buy each day.
You may find it helpful to use a small number of simulations initially, then increase this number to get accurate reuslts as your search closes in on the optimal value. Careful- a local maximum may not be a global maximum! Make sure to let Paul M know what you discover.
Can you think of a more efficient way to get the answer? Hint: investigate the set.seed
function.