I'm writing a simulation for a newspaper stand in Scala. Before I wrote the simulation, I tried to come up with the max value for the simulation analytically (to no avail). I should be able to calculate this value with ease, but it's been a few years since I've done any math.

Anyway, the simulation has the following constraints: sales are randomly generated by a function call to gen(1,10) * 100 where gen is a scala function from a simulation library assumed to be actually random. The orders must be constant(cannot vary day to day). Left over orders (as in orders in excess of sales on a given day), are sold at ten cents a paper. The formula is I came up with for calculating profit:

profit = $1.00 * sales -$0.50 * orders + $0.1(orders-sales)

From here though, I'm stumped. I tagged it as hw, but it's not for a grade or anything. HW is the most viewed tag.

  • $\begingroup$ You say that orders is constant; does that mean it is known? If so, then the best you can do is fifty cents per order. (I'm assuming your model is not going to let you sell newspapers you don't actually have.) $\endgroup$ – tabstop Feb 6 '14 at 21:53

TL;DR: You should order $60$ copies of the newspaper.

Let $P$ be the price of ordering a newspaper, $S$ be the price of selling it, and $L$ be the price at which you sell leftovers. In your case, $P = \$0.50, S = \$1.00, L = \$0.10$.

Also, let $x$ be the number of orders, and let $s$ be the number of sales. The formula you wrote, using these variables, is

$$ \text{Profit}(x,s) = S s - Px + L(x - s)$$

But your profit formula needs to take into account the scenario in which you run out of papers before the sales are complete. Therefore, you should modify your formula: $$ \text{Profit}(x,s) = \left\{ \begin{array}{ll} Ss - Px + L(x-s) &\text{ if } s \le x \\ Sx - Px &\text{ if } s \ge x \end{array} \right. $$ Now, $s$ is uniformly random from $10 \cdot 1$ to $10 \cdot 10$, let us say from $a$ to $b$. So $$ \text{Expected Profit} = \frac{1}{b-a} \int_a^b \text{Profit}(x, s) \; ds $$ It would never be good to order more papers than you know you are going to sell; nor would it ever be good to order less papers than you know are going to sell. Therefore, we set $x$ somewhere between $a$ and $b$. So \begin{align*} \int_a^b \text{Profit}(x, s) \; ds &= \int_a^x [Ss - Px + L(x-s)] \; ds + \int_x^b [Sx - Px] \; ds \\ &= \int_a^b (-Px ) \; ds + \int_a^x [Ss + L(x-s)] \; ds + \int_x^b [Sx] \; ds \\ &= -(b - a) Px + \left[\frac12 S s^2 - \frac12 L (x-s)^2 \right]_a^x + (b - x) Sx \\ &= (-Pb + Pa)x + \frac{S}{2} (x^2 - a^2) + \frac{L}{2} (x - a)^2 + bSx - Sx^2 \end{align*} Ignoring constants, the important terms are $$ Pax - Pbx + \frac12 S x^2 + \frac12 L x^2 - La x + bS x - Sx^2 $$ $$ = Pax - Pbx + \left( bSx - \frac12 S x^2 \right) + \left(\frac12 L x^2 - Lax \right) $$ At this point we may as well plug in actual values for $P, S, L, a, b$. I'm assuming that your gen function makes $a = 10, b = 100$. \begin{align*} &\quad (.5) (10) x - (.5) (100) x + (100)(1)(x) - \frac12 (1) x^2 + \frac12 (.1) x^2 - (.1) (10) x \\ &= 5x - 50x + 100x - \frac12 x^2 + \frac{1}{20} x^2 - x \\ &= 54x - \frac{9}{20} x^2 \\ \end{align*}

This function is quadratic and its maximum is at $$ x = \frac{-54}{2(-9/20)} = \frac{540}{9} = \boxed{60}. $$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.