Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 5 Solutions: Simulating Distributions with NumPy

Open In Colab

Lab 5 Solutions: Simulating Distributions with NumPy

These are the worked solutions, with outputs. Try the lab yourself first.

In the reading, Olaya’s manager planned her mornings with three probability models. Now you do the same for two other branches, then judge a trial with a confidence interval.

The question. The regional manager asks you for a morning plan for the University branch (baristas and breakfast boxes) and the Corniche branch (croissants), and one decision: should Corniche open at 6:30 for good?

How this lab works

  • Run the cells from top to bottom with Shift + Enter. Replace every ... with your code, then run the check cell under it.

  • Extended track: after Part D, try the ★ challenges.

  • Submit before the next lab session: File → Download → Download .ipynb.

You may use an AI assistant for code. If you do, add a short note at the end: what you asked, what you kept and what you changed.

Loading...
ColumnMeaning
date, weekdaythe day
day_typeworkday (Sunday to Thursday) or weekend (Friday and Saturday)
ramadan1 during Ramadan (1 to 29 March 2025), when mornings are much quieter
branch, citywhere
peak_customerscustomers who arrived between 8:00 and 9:00
croissant_demandcroissants customers asked for: sales, plus requests logged after a sell-out
box_preorders, box_pickupsbreakfast boxes pre-ordered in the app for that morning (paid at pickup), and boxes collected
early_customerscustomers between 6:30 and 7:00, counted only during the early-opening trial (Olaya and Corniche, 5 October to 9 November); empty otherwise

normal_days keeps the workdays outside Ramadan, when mornings are alike.

Part A: Croissants at Corniche (normal distribution)

A1. The distribution

Select the Corniche rows of normal_days into corn. Store the mean and the standard deviation of its croissant_demand in mu and sigma.

mean 97.2, standard deviation 16.4
✅ A1 241 normal workdays at Corniche
✅ A1 mean and standard deviation

A2. Baking the mean

If Corniche bakes round(mu) croissants every day, on what share of the 241 days would it have sold out (demand above the quantity baked)? Store the share in soldout.

sold out on 49% of days
✅ A2 baking the mean sells out on about half the days

A3. A service level

Use stats.norm.ppf to find how many croissants to bake to sell out on at most 10% of days (bake90), and at most 5% (bake95). Round up with int(np.ceil(...)).

119 125
✅ A3 90% service level
✅ A3 95% service level

Part B: Baristas at University (Poisson distribution)

B1. Is the Poisson model plausible?

Select the University rows of normal_days into uni. Store the mean of peak_customers in lam and the ratio of its variance to its mean in ratio.

mean 55.57, variance / mean = 0.88
✅ B1 the University peak mean
✅ B1 variance close to the mean (ratio between 0.7 and 1.3)

B2. How many baristas?

One barista serves 25 orders an hour. For 1, 2 and 3 baristas, compute the probability that more customers arrive than they can serve, with stats.poisson.sf(capacity, lam). Store the three probabilities in a list overload, in that order.

1 barista(s): overloaded with probability 1.0000
2 barista(s): overloaded with probability 0.7479
3 barista(s): overloaded with probability 0.0053
✅ B2 three probabilities, falling
✅ B2 two baristas are overloaded on most workdays
✅ B2 three baristas almost never

B3. Weekends

Students are away at weekends. Compute lam_weekend, the mean peak_customers at University on weekend days outside Ramadan, and risk_one, the probability that one barista is overloaded.

weekend mean 13.9; one barista overloaded with probability 0.0024
✅ B3 a quiet weekend
✅ B3 one barista is enough

Part C: Breakfast boxes at University (binomial distribution)

C1. The pickup rate

Over all University rows of arrivals, what share of pre-ordered boxes were collected? Store it in p_uni.

0.755
✅ C1 the pickup rate

C2. How many boxes for 30 pre-orders?

Tomorrow University has 30 pre-orders. For every number of boxes b from 20 to 30, compute the probability that more than b customers come to collect one (stats.binom.sf(b, 30, p_uni)). Store the smallest b with a probability of at most 10% in boxes.

26 0.042
✅ C2 the smallest safe number of boxes

C3. Preparing all 30 boxes wastes 30 × (1 − 0.755) ≈ 7.4 boxes on an average morning. In one sentence: is preparing fewer boxes than pre-orders a fair deal for the customers who pre-ordered? What could the branch do for the customer who finds no box ready?

With 26 boxes, a customer has to wait for a box made on the spot on about 4% of mornings (25 boxes would just miss the 10% limit, at 10.9%), while the waste falls from about 7 boxes to about 3.4. Since boxes are paid at pickup, nobody loses money; the branch can keep the wait short by preparing the missing box first, or offer a free coffee when it happens.

Part D: Simulation and a trial

D1. Simulate the University peak

Use a NumPy generator with seed 42 to simulate 100,000 workday peaks at University (rng.poisson). Store the simulated share of mornings with more than 75 customers in sim_share, and compare it with overload[2].

simulated 0.0051   formula 0.0053
✅ D1 100,000 simulated mornings
✅ D1 the simulation agrees with the formula

D2. Should Corniche open at 6:30?

Corniche ran the same 36-day early-opening trial as Olaya. Its break-even is also 17 customers per morning. Compute the mean of its early_customers (mean_early) and a 95% confidence interval for the long-run mean (lo, hi), with stats.t.ppf(0.975, n - 1).

n = 36, mean 22.36, 95% CI [19.90, 24.82]
✅ D2 36 trial days
✅ D2 the interval

D3. Your recommendation

Write three sentences for the regional manager: how many baristas University needs on workdays and at weekends, and how many breakfast boxes to prepare for 30 pre-orders; how many croissants Corniche should bake, with the service level you chose; and whether Corniche should open at 6:30, explaining why your answer differs from Olaya’s (Olaya: mean 18.3, 95% CI [16.05, 20.56]).

University needs 3 baristas on workday mornings (with 2 the peak overloads them on about 75% of days, with 3 on 0.5%) but only 1 at weekends, and should prepare 26 boxes for 30 pre-orders (a wait on about 4% of mornings, about 3 boxes wasted instead of 7). Corniche should bake 119 croissants on workdays, which sells out on about 1 day in 10; baking the mean (97) sells out on about half the days. Corniche should open at 6:30: its 95% interval for early customers, [19.9, 24.8], lies entirely above the break-even of 17, whereas Olaya’s interval still includes 17, so Olaya needs a longer trial before deciding.

★ Extended challenges

E1. One distribution for the whole year? Compute the mean and the variance of peak_customers at University over all 365 days. Is a single Poisson model plausible? Explain the result in two sentences.

E2. Do 95% intervals catch the truth 95% of the time? Treat Corniche’s 241 workday croissant demands as the population. Draw 1,000 samples of 36 days (without replacement), build a 95% interval from each, and report the share that contains the population mean.

E3. How long a trial at Olaya? If Olaya’s early-morning mean (18.31) and standard deviation (6.66) stayed the same, how many trial days would make the lower end of the 95% interval reach 17? Use 1.96 for tt.

mean 41.4, variance 431.5, ratio 10.4

The variance is about ten times the mean, so a single Poisson model does not fit: the year mixes busy workdays (about 56 customers), quiet weekends (about 14) and Ramadan mornings. Each kind of day has its own rate, and a mixture of rates is far more variable than any one of them, so the branch should plan each kind of day separately.

95.5% of the intervals contain the population mean 97.2

Close to 95%, as the method promises (a little higher, because sampling without replacement from only 241 days varies less than sampling from an endless population).

about 100 days

About 100 days, nearly three times the original trial, and only if the mean really stays at 18.3. If the true mean is closer to 17, no realistic trial will settle it; the decision then rests on whether a small possible loss is acceptable.