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: Simulating Distributions with NumPy

Open In Colab

Lab 5: Simulating Distributions with NumPy

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.

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.

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.

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

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.

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.

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.

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.

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.

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?

Your answer here (double-click to edit).

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

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

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

Write your recommendation here (double-click to edit).

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

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.