← How Rewards Guide Actions
Interactive

The newsvendor

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.

01

Step through expected value

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).

Buy cost c2
Sell price p5
Salvage s0
Critical fractile(p−c)/(p−s) = 0.60
profit(q, d) = p min(q, d) + s max(q − d, 0) − c q
Order quantity q = 3
Running expected value
0.00
Demand d P(d) Sold Left Short Profit P(d) · profit Running EV
02

Write it in Python

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.

Loading Python runtime…
Waiting for the runtime.