You buy newspapers in the morning before you know demand. Each leftover copy is a loss; each unmet customer is a missed sale. Expected value is the sum, over every possible demand, of profit in that world times how likely that world is.
Pick an order quantity, then walk demand one outcome at a time. The running expected value starts at zero and accumulates each contribution P(d) · profit(q, d).
| Demand d | P(d) | Sold | Left | Short | Profit | P(d) · profit | Running EV |
|---|
A real CPython runtime runs in this page. Run executes whatever is in the editor. Submit tests checks that expected_profit matches the newsvendor above.