Imagine you run a newsstand. In the morning, you buy a certain number n of newspapers at price c0. Over the course of the day, you try to sell this inventory at price c1; anything that isn’t sold in the evening is discarded (no salvage value). If you knew how many papers you would actually sell during the course of the day (the demand m), then it would be easy: you would buy exactly m papers in the morning. However, the demand is not known exactly, although we know the probability p(k) of selling exactly k copies. The question is: how many papers should you buy in the morning in order to maximize your net earnings (the revenue)?
A first guess might be to use the average number of papers that we expect to sell—that is, the mean of p(k). However, this approach may not be good enough: suppose that c1 is much larger than c0 (so that your markup is high). In that case, it makes sense to purchase more papers in the hope of selling them, because the gain from selling an additional paper outweighs the loss from having purchased too many. (In other words, the opportunity cost that we incur if we have too few papers to satisfy all demand is greater than the cost of purchasing the inventory.) The converse also holds: if the markup is small, then each unsold paper significantly reduces our overall revenue.
This problem lends itself nicely to simulations. The listing that follows shows a minimal program for simulating the newsvendor problem. We fix the purchase price c0 at $1 and read the projected sales price c1 from the command line. For the demand, we assume a Gaussian distribution with mean μ = 100 and standard deviation σ = 10. Now, for each possible initial level of inventory n, we make 1,000 random trials. Each trial corresponds to a single “day”; we randomly generate a level of demand m and calculate the resulting revenue for that day. The revenue consists of the sales price for the number of units that were actually sold less the purchase price for the inventory. You should convince yourself that the number of units sold is the lesser of the inventory and the demand: in the first case, we sold out; in the second case, we ended up discarding inventory. Finally, we average all trials for the current level of starting inventory and print the average revenue generated. The results are shown in Figure 17-2 for several different sales prices c1:
from sys import argv from random import gauss c0, c1 = 1.0, float( argv ) mu, sigma = 100, 10 maxtrials = 1000 for n in range( mu-5*sigma, mu+5*sigma ): avg = 0 for trial in range( maxtrials ): m = int( 0.5 + gauss( mu, sigma ) ) r = c1*min( n, m ) - c0*n avg += r print c1, n, avg/maxtrials
Of course, the total revenue depends on the actual sales price—the higher the price, the more we take home. But we can also see that, for each value of the sales price, the revenue curve has a maximum at a different horizontal location. The corresponding value of n gives us the optimal initial inventory level for that sales price. Thus we have achieved our objective: we have found the optimal number of newspapers to buy at the beginning of the day to maximize our earnings.
This simple idea can be extended in different ways. More complicated situations may involve different types of items, each with its own demand distribution. How much of each item should we hold in inventory now? Alternatively, we can turn the problem around by asking: given a fixed inventory, what would be the optimal price to maximize earnings? To answer this question, we need to know how the demand varies as we change the price—that is, we need to know the demand curve, which takes the role of the demand distribution in our example.
Learn more about this topic from Data Analysis with Open Source Tools.
Turning raw data into something useful requires that you know how to extract precisely what you need. With this insightful book, intermediate to experienced programmers interested in data analysis will learn techniques for working with data in a business environment. You'll learn how to look at data to discover what it contains, how to capture those ideas in conceptual models, and then feed your understanding back into the organization through business plans, metrics dashboards, and other applications.