…
…
Choose N for the promise "90% of orders at your door in N days", measure with a simulation how often the promise looks broken by chance in weekly, monthly, and quarterly reports, then recommend with one chart and two sentences.
The Bazaar marketing team wants to put "90% of your orders at your door in 4 days" on the home page; in the January–June data, the share delivered within 4 days is 90.5%. The customer service manager warns: "Every Monday we look at last week's report; if one week comes out at 85%, social media will say 'the promise was not kept'." You are asked for the answer to a single question: Which N should the promise use?
This unit task teaches nothing new. It combines the four lessons of the probability and sampling unit (simulation, conditional share, sampling variability, the normal model) in one business decision. The one thing you do for the first time is turn a business promise into a simulation question yourself: which share, which sample size, which threshold?
Data: orders.csv (fictional sample data, the Bazaar case), one row for each order. The columns used in this task:
| Column | Description |
|---|---|
delivery_days | How many days the order took to be delivered (empty for canceled orders) |
carrier | The carrier: Arrow Express, Gull Post, or Caravan Freight |
Assumption: We use the 851 delivery times as the population that future orders come from; next week's orders behave like a sample drawn with replacement from this population. This is a reasonable assumption only if the carriers' shares and the season do not change.
Product: One chart that shows how much the "90% in N days" promise varies with sample size, and a two-sentence recommendation to the marketing team (step 7).
Lessons used: missing values (19), grouping and the mean as a share (7), percentiles (15), choosing a chart and titles that state the result (17), simulation (23), conditional probability (24), sampling variability (25), the normal model and standard error (26, only in the optional step 6).
Cells share one session; each step uses the variables from the one before. Simulations are seeded with np.random.default_rng(42), so when you run a cell again you see the same numbers.
What you'll do
Required topics
orders.csvDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Before giving a number for the promise, look at the delivery times we have. orders.csv has 885 orders; for canceled orders the delivery_days cell is empty, because they were never delivered. Those rows have no place in a question about delivery time (lesson 19: dropna(subset=...), which looks only at the column you care about).
Keep the delivered orders under the name t, then print their count and the frequency table of delivery time, in day order.
Assumption (for the whole task): We will treat the 851 delivery times we have as the population that future orders come from. Next week's 30 orders behave like a sample of 30 drawn with replacement from this population (the note on sampling with replacement in lesson 25). This is reasonable only if the share of each carrier and the season stay the same; you will mention this in your report.
This step's code cell is for members.Sign up free
Marketing wants to write "90% of orders at your door in N days". For each candidate N, compute the share "delivered within N days": t["delivery_days"] <= N gives a True
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
FalseN_candidateYou can ask the same question with the percentile from lesson 15: quantile(0.9) tells you the day at or below which 90% of orders were delivered. The two ways should give the same N.
Look at the shares carefully: how far above 0.9 is the share for the candidate N?
This step's code cell is for members.Sign up free
In the population the share is 0.905. But what does the weekly report that customer service checks every Monday show? About 30 orders are delivered in a week (the weekly median in the data is 33). To imitate a week, we pick 30 delivery times from the population with replacement and compute the share of those 30 orders (lessons 23 and 25: rng.choice).
Write the function week_share(data, n, N, rng): it should pick n values with replacement from the data array and return the share of that sample delivered within N days. The cell simulates three separate weeks; the generator is created once before the loop (lesson 23).
Look at the shares of the three weeks: are they all 0.905? Are they all above 0.9?
This step's code cell is for members.Sign up free
Three weeks prove nothing; let's repeat thousands of times, as in lesson 25. The reports come in three sizes: weekly (n = 30), monthly (n = 130), and quarterly (n = 400). For each we will simulate 2,000 reports (instead of the 10,000 in lesson 25: the result gives the same picture, and the cell runs fast).
The function simulate(data, n, N, rng, reps=2000) uses the pattern from lesson 25: rng.choice(data, size=(reps, n)) draws a table where each row is one report, then it returns the share delivered within N days for each row (axis=1). The loop computes two things for each (N, n) pair:
np.percentile, lesson 25),The calculation is done for both the 4-day and the 5-day promise.
As you read the table, ask yourself: as n grows the band gets narrower, but where is its center? Why does the "below" column not drop to 0 in the N = 4 rows?
This step's code cell is for members.Sign up free
The overall share is the average over all orders. But Bazaar works with three carriers, and customers do not choose which one they get. To see whether the promise holds for each carrier, compute the "within 4 days" and "within 5 days" shares for each carrier: this is the conditional share from lesson 24 ("the probability of delivery within 4 days given the carrier is Caravan Freight"); it is the mean of a 0/1 column with groupby (lessons 7 and 24).
Then find the carrier whose 4-day share is below 0.9 even in the population, and call it problem_carrier.
This step's code cell is for members.Sign up free
This step is optional; you can skip it and go to step 7 (step 7 does not use its result).
In lesson 26 you found the typical variation of a sample mean with a formula: standard error = σ / √n. A share is also a mean: the mean of a True/False array. So the same formula fits a share; σ is the standard deviation of the 0/1 indicator in the population (with NumPy's default, as in lesson 26). For this array σ comes out at about 0.293.
Run the simulation again for the 4-day promise (with the same seed, the same as the N = 4 rows in step 4) and compare the standard deviation of the shares with the formula. If the difference is smaller than 0.002, the formula says in one line what 2,000 simulations say.
This step's code cell is for members.Sign up free
Now put the findings into a single chart that the marketing team can read at a glance. On the horizontal axis put the three reports (week, month, quarter), and on the vertical axis the "share delivered within N days"; in each report draw two bands: the 4-day promise (orange) and the 5-day promise (blue). Each band is the 95% range (from low to high) of that row in the rows list from step 4. You saw line charts in lesson 17; a band is a line with two points. The gray dashed line is the 90% threshold: ax.axhline(0.9) draws a horizontal line from one end of the chart to the other, and it is given. Make the title a sentence that states the result, as in lesson 17: not a label like "Delivery share", but a sentence that tells the reader what they see.
If you draw it correctly, all three orange bands cross the gray line, and none of the blue bands do.
The cell then prints six sentences. For each one, ask: did our data and simulation really show this? Write the letters of the ones they showed into the says list, and the letters of the ones they did not show into the does_not_say list.
Finally, write your own two sentences to the marketing team in the recommendation variable (for the team in the fictional case, in your own words). Before you finish, tick these five items yourself:
1. Is the N you recommend written out clearly?
2. Is at least one report size given, with how often it falls "below 90%", as a number?
3. Does it say that the 4-day promise is borderline in the population (90.5%)?
4. Does it say that one low week does not mean things have broken on its own, or does it include the Caravan Freight warning?
5. Is the assumption (if the carriers and the season stay the same) mentioned?
A sample answer is in the solution. This one, however, is weak:
"According to our data, 90.5% of deliveries are made in 4 days, so a '4 days' promise can safely be made." Why is this recommendation weak?
Four questions without points. First think of your own answer, then choose.
In quarterly reports (n = 400) the band got much narrower, but the fraction of reports where the 4-day promise falls below 90% is still about 34%. Why did it not go down?
One week's 4-day share came out at 83% (30 orders). Should customer service raise the alarm?
If Caravan Freight's share of orders grows (and the other carriers stay at the same speed), what happens to the overall 4-day share?
A pizza shop wants to say "90% of orders at your door in 30 minutes". Which of the steps in this task also work there?
This step's code cell is for members.Sign up free