Expected value is for planning across many cases, not predicting one.
rng = np.random.default_rng(8)
simulated = pd.Series(rng.choice(shipping['outcome'], size=10000,
p=shipping['probability']))
simulated.value_counts(normalize=True)
on time 0.8016
1 day late 0.1525
3+ days late 0.0459
Name: proportion, dtype: float64
tips dataWe see the sample; we want to learn about the population.
total_bill tip sex day time size
0 16.99 1.01 Female Sun Dinner 2
1 10.34 1.66 Male Sun Dinner 3
2 21.01 3.50 Male Sun Dinner 3
total_bill, tip · Categorical: sex, day, timedemand = [0, 1, 2, 3] # cakes ordered in a day
probabilities = [0.10, 0.30, 0.40, 0.20]
observed = [2, 1, 0, 3, 2, 2, 1, 0, 2, 3, 1, 2, 2, 1, 3, 2, 0, 2, 1, 2]
8.2 · Think Data Science & Management
tips[['total_bill', 'tip', 'size']].describe().round(2)
total_bill tip size
count 244.00 244.00 244.00
mean 19.79 3.00 2.57
std 8.90 1.38 0.95
min 3.07 1.00 1.00
25% 13.35 2.00 2.00
50% 17.80 2.90 2.00
75% 24.13 3.56 3.00
max 50.81 10.00 6.00
| Statistic | Value | Meaning |
|---|---|---|
| Mean | $19.79 | the average |
| Median | $17.80 | the middle bill |
| SD | $8.90 | typical distance from the mean |
| IQR | $10.78 | range of the middle 50% |
Mean > median: a few large bills pull the mean up.
col = tips['total_bill']
print(round(col.var(), 2), round(col.std(), 2), round(np.std(col), 2))
np.percentile(col, [25, 50, 75, 90])
79.25 8.9 8.88
array([13.3475, 17.795 , 24.1275, 32.235 ])
.std() divides by n − 1; np.std() divides by nupper = q75 + 1.5 * (q75 - q25)
(col > upper).sum() # 9
Center · spread · shape · outliers
rng = np.random.default_rng(42)
values = rng.normal(loc=50, scale=5, size=1000) # mean 50, SD 5
rng.binomial(n=100, p=0.5): heads in 100 coin flipsrng = np.random.default_rng(42)
flips = rng.integers(0, 2, size=1000) # 0 = tails, 1 = heads
running = flips.cumsum() / (np.arange(1000) + 1)
cumsum(): heads so far · np.arange(1000) + 1: flips so farvisits = np.array([410, 395, 430, 420, 405, 1250, 415,
400, 425, 390, 435, 410, 1180, 420])
np.std)8.3 · Think Data Science & Management
rng = np.random.default_rng(42)
for i in range(3):
print(rng.choice(bills, size=20).mean().round(2))
22.45
17.54
20.98
How much do those numbers bounce around?
rng = np.random.default_rng(seed=0)
p_true, n, n_sims = 0.6, 40, 5000
samples = rng.binomial(n=n, p=p_true, size=n_sims)
sample_props = samples / n
np.percentile(sample_props, [2.5, 97.5])
array([0.45, 0.75])
print(round(np.sqrt(0.6 * 0.4 / 40), 4),
round(sample_props.std(ddof=1), 4))
0.0775 0.0781
A survey's "margin of error" is usually 2 SE.
A survey of 40 customers finds 26 would renew.
| n | 10 | 40 | 200 |
|---|---|---|---|
| SE (formula) | 0.155 | 0.077 | 0.035 |
Precision costs more and more: halving the error needs four times the data.
About 30% of a bank's customers use mobile check deposit.
This deck holds the slides for all of Chapter 8, one part per class meeting: - 8.1 Probability Models (slide 2) - 8.2 Descriptive Distributions (slide 19) - 8.3 Sampling Variability (slide 37) Each part opens with its own title slide and runs about 50 minutes, with its own goals, quick checks, "Your turn" exercise, and recap. To start a class, jump to that part's slide number: in the HTML deck, type the number and press Enter, or add #number to the URL.
Welcome to Chapter 8. Up to now we have described data we already have. This chapter adds the other side: models of chance that describe how data could come out. Managers use them every day, whether or not they call them probability: delivery promises, return rates, budget reserves. Today: what a probability model is, events and complements, expected value, simulation, and the vocabulary of populations, samples, variables, and observations. Timing: about 50 minutes, three quick checks, one practice exercise.
Read the goals. They support chapter objectives 1 (describe a probability model and compute event probabilities, complements, and expected values) and 3 (interpret empirical distributions).
Our running case: an online store ships orders, and the operations team uses this model for one shipment. The table lists every possible outcome and its probability. The list of all outcomes is the sample space. The model does not say what will happen to the next shipment; it says how likely each outcome is. Ask: "Where do numbers like 0.80 come from?" Usually from past data: 80% of past shipments arrived on time.
These two rules are how you check any probability model. Exactly one outcome must happen, so the probabilities must add up to 1. Quick sanity habit for students: whenever you build a model in code, sum the probability column.
Give students 20 seconds. Answer: B. 0.80 + 0.15 + 0.10 = 1.05. Both rules must hold, and the second one fails. Why the others are wrong: A checks only the first rule. C is about the order of the probabilities, which is not a rule. D is not a rule; any number of outcomes works. This exact case is homework question 1.
An event groups outcomes we care about. "Late" contains two outcomes, so its probability is the sum: 0.15 + 0.05 = 0.20. The complement is "the event does not happen". Because everything adds up to 1, P(not late) = 1 - 0.20 = 0.80. The complement is often the shortcut: "at least one day late" is everything except "on time".
The store gives a $5 credit for a 1-day delay and $20 for 3 or more days. The credit is a random variable: a number that depends on chance. The expected value multiplies each value by its probability and adds them up: $1.75 per shipment.
This is the most commonly misunderstood idea in the section. Expected value is not the typical outcome. Most shipments cost $0. It is the long-run average, which is exactly what a manager needs for a budget.
Give students 20 seconds. Answer: B. Expected value is a long-run average. Why the others are wrong: A, a typical shipment costs $0. C confuses a dollar amount with a probability; P(any credit) is 0.20. D is a policy idea, not what the number means. This is homework question 2.
A model describes the process; data are what we observe. Simulation connects them: rng.choice draws 10,000 outcomes with the model's probabilities. The proportions in the simulated data, the empirical distribution, are close to the model's 0.80, 0.15, 0.05, but not exactly equal. A different seed gives slightly different numbers.
Side by side, the model and the simulation almost match. The simulated average credit is $1.68, close to the $1.75 expected value. With more shipments, the average tends to get closer; that is the law of large numbers, coming up in 8.2.
In practice we work the other way around: we observe a sample and want to learn about the population. That gap is what the rest of the course is about: how much can a sample tell us? Business examples: a customer survey (sample) and all customers (population); last month's orders and all future orders.
This connects back to tidy data in Chapter 7. One row per observation, one column per variable. The type of variable decides the summary: quantitative variables get means and spreads; categorical variables get counts and proportions. Party size is numeric but takes few values, a useful edge case to discuss.
Distribution is the word that ties this section together. A model's distribution says what should happen; an empirical distribution says what did happen in data. Comparing the two is the heart of statistics, and Chapter 9 does exactly that with hypothesis tests.
Give students 20 seconds. Answer: B. The complement is everything except the event. It includes next day, 2+ days, and never. Why the others are wrong: A and C are only parts of the complement. D is one possible outcome, not the whole complement.
This is the section 8.1 exercise. Give students 5 minutes. Answers: total probability 1.0; P(at least one) = 1 - 0.10 = 0.90; expected orders = 0*0.1 + 1*0.3 + 2*0.4 + 3*0.2 = 1.7. Observed days: 17 of 20 had an order (0.85) and the average was 1.6. Close to the model, not equal: 20 days is a small sample, so the empirical distribution varies by chance. Common mistakes: computing P(at least one) by adding only 0.30 + 0.40 and forgetting 3 cakes; reading the expected value 1.7 as "the bakery sells 1.7 cakes on a typical day"; floating-point output such as 0.8999999999999999, which round(…, 2) fixes.
Ask students to supply each line. Next time, 8.2: describing distributions of data with center, spread, and shape, and three distributions that come up again and again: normal, binomial, and uniform.
Last time we built models of chance. Today we describe what data actually look like: center, spread, and shape, plus three common distributions and the law of large numbers. Timing: about 50 minutes, three quick checks, one practice exercise.
Read the goals. They support chapter objectives 2 (summarize a distribution by its center, spread, and shape) and 3 (interpret empirical distributions).
describe gives count, mean, standard deviation, min, the three quartiles, and max for every numeric column. The 50% row is the median. Ask: "What do you notice about total_bill's mean and median?" The mean, $19.79, is above the median, $17.80. That gap is a clue about shape, next slide.
Two measures of center and two of spread. The mean is pulled toward large values; the median is not. When the mean is noticeably above the median, the data are skewed to the right: a long tail of large values. For a typical bill, report the median. The SD and IQR both measure spread. The IQR, like the median, resists outliers. Rule of thumb used in the exercises: if the mean is more than 20% above the median, treat the data as right-skewed and report the median. It is a practice convention, not a formal test. For tips the mean is about 11% above the median, so the skew is moderate.
Variance is the SD squared. Its units are squared dollars, which nobody can picture, so we report the SD. Two standard deviations: pandas .std() divides by n minus 1 (the sample SD, ddof=1) and np.std() divides by n (ddof=0). With 244 bills they differ by two cents, 8.90 vs 8.88, but on a dataset of 10 values the gap is noticeable. Lab question 4 asks for np.std, so use the function a question names. The p-th percentile is the value below which p percent of the values fall. The 25th, 50th, and 75th are the quartiles; the 50th is the median. Ask: "What does the 90th percentile tell a manager?" Nine bills in ten are $32.24 or less.
The 1.5 times IQR rule is the usual way to flag outliers, and it is exactly what a box plot uses: anything past the fences is drawn as a separate dot. All nine flagged bills are on the high side, which matches the right skew. Flagged does not mean wrong: these are real, large bills. A manager would look at them, not delete them.
The histogram shows the right skew: a peak around $15 to $20 and a long tail toward $50. The mean (dashed red) sits to the right of the median (dotted black). The box plot shows the same thing compactly: the box is the IQR, the line is the median, and the dots on the right are the nine bills above the $40.30 fence from the previous slide. Use the four-word checklist every time you describe a distribution.
Give students 20 seconds. Answer: B. The few very large salaries pull the mean up; the median is the middle value and is not affected by how large the top salaries are. Why the others are wrong: the maximum describes one person; the SD measures spread, not center. This is homework question 3.
The normal distribution appears whenever many small independent effects add up: measurement errors, heights, daily demand for a stable product. loc is the mean and scale is the standard deviation. The 68-95-99.7 rule is worth memorizing; we use it constantly in Chapters 9 to 11.
In these 1,000 simulated values, 68.8% are within 1 SD of the mean (45 to 55) and 95.7% are within 2 SD (40 to 60), close to the 68% and 95% the rule predicts. Simulation lets us check a rule instead of memorizing it.
Business examples of binomial: how many of 100 customers renew, how many of 50 parts are defective, how many emails get opened. With n = 100 and p = 0.5 the binomial looks almost exactly like a normal with mean 50 and SD 5. It is discrete, whole numbers only, but the shape is a bell. That is the Central Limit Theorem at work: totals of many independent trials tend toward normal.
A binomial count is a sum of n yes/no trials. With p = 0.2 and only 5 trials, most counts are 0 or 1 and the distribution is skewed right; the red normal curve with the same mean and SD fits poorly. At 30 trials it is close to a bell; at 300 the histogram follows the curve almost exactly. Stress what changes: not the number of draws, which is 5,000 everywhere, but n, the number of trials added up in each count. That is the Central Limit Theorem. In 8.3 we see the same thing for sample means.
A fair die is uniform: each face has a one-in-six chance. With 60 rolls the bars are uneven by chance. By 60,000 rolls they are nearly flat at the red line, 1/6. That pattern, noisy with little data, close to the model with a lot, is exactly the next idea.
Give students 30 seconds. Answer: C. A fixed number of independent yes/no trials with the same success probability is binomial. Why the others are wrong: A has the wrong mean; the expected count is 100 x 0.3 = 30. B would make 0 and 100 as likely as 30. D ignores chance: 30 is the expected value, not a guarantee.
Each flip is 0 or 1. The line shows the proportion of heads so far. Early on it swings widely; after 1,000 flips it is 0.509, close to the true 0.5. The law of large numbers says the sample average gets closer to the true average as the sample grows. It is why a casino can plan its revenue and why the shipping budget from 8.1 works across 10,000 shipments. Common misconception: it does not say a head is "due" after a run of tails. Each flip is still 50-50.
cumsum adds up the heads as we go; dividing by the number of flips so far gives the running proportion. This two-line pattern works for any running average.
This is the section 8.2 exercise. Give students 5 minutes. Answers: mean 527.5, median 417.5, SD 281.3. Two promotion days pull the mean far above the median, so the data are skewed to the right and the median describes a typical day better. Q1 = 406.25, Q3 = 428.75, IQR = 22.5, upper fence = 462.5, and 2 days (the promotion days) are outliers. Common mistake: reporting the mean because it is "the average". Ask what the 1,250 and 1,180 days were; a manager would want them explained separately.
Ask students to supply each line. Next time, 8.3: if we took another sample, how different would our numbers be? That is sampling variability, measured by the standard error.
Last time: averages settle down as samples grow. Today we measure how much a sample result would change if we drew a different sample. That amount, the standard error, is behind every margin of error in a survey and every confidence interval in Chapter 11. Timing: about 50 minutes, three quick checks, one practice exercise.
Read the goals. They support chapter objectives 4 (explain why samples vary, and use simulation to show it) and 5 (use the standard error to describe how sample size affects precision).
In real work we never see the population. Here we flip it around: pretend the 244 bills are every bill there is, so we know the true mean, 19.79. That number is a parameter. Each random sample of 20 bills gives its own mean, a statistic. Three samples, three different answers, none exactly 19.79. That is sampling variability, and it is the subject of today. rng.choice samples with replacement by default, as if each bill came from the restaurant's steady stream of customers.
Repeat the sampling 5,000 times and plot every sample mean on the same axis as the bills. Three things to point out: the means are centered on the population mean (the red line); they vary far less than individual bills, about $2 instead of about $9; and they form a bell even though the bills themselves are skewed right. That last point is the Central Limit Theorem from 8.2, now for sample means. The SD of the sample means has a name, the standard error. We come back to it after looking at proportions.
This is the core question. p is a fact about the population; we never see it directly. p-hat is what our survey gives. Every new random sample gives a new p-hat. Two surveys showing 72% and 68% satisfied does not mean anything changed; it may just be sampling variability. That is homework question 4.
We pretend we know p = 0.6 and run 5,000 surveys of 40 customers each. rng.binomial gives the number of renewals in each survey; dividing by 40 gives each p-hat. The middle 95% of the 5,000 p-hats run from 0.45 to 0.75. So one survey of 40 people could easily say 45% or 75% even though the truth is 60%.
This histogram is the sampling distribution: the distribution of p-hat across many repeated samples. It is centered on the truth, so the survey method is unbiased, but individual surveys land anywhere from about 0.45 to 0.75. Stress the difference: the distribution of the data (40 yes/no answers) is not the sampling distribution (5,000 p-hats).
Students often mix these up. Inside one survey there are 40 yes/no answers, summarized by one number, p-hat. The sampling distribution is one level up: the p-hats from many surveys. A company only ever runs one survey; the sampling distribution tells it how far that one number could be from the truth.
Give students 20 seconds. Answer: B. Each value in the histogram is one survey's p-hat. Why the others are wrong: A would be 40 zeros and ones. C is a single number, 0.6, the red line. D is not shown. If students pick A, redraw the two levels on the board: answers inside a survey, and surveys inside the simulation.
The standard error is the standard deviation of the sampling distribution: how far a typical p-hat lands from p. The formula gives 0.0775; the SD of the 5,000 simulated p-hats is 0.0781. They agree, which is the point: the formula summarizes what the simulation shows. In real work we do not know p, so we plug in the sample's p-hat. That approximation works well for reasonably large samples.
Combine the 68-95 rule from 8.2 with the standard error. About 95% of p-hats fall within 2 SE of the truth. Two SE here is 0.155, so 0.445 to 0.755, which matches the 2.5th and 97.5th percentiles of our 5,000 simulated surveys. When a news report says "plus or minus 3 points", that is about 2 standard errors. Chapter 11 turns this into confidence intervals.
In practice we do not know p, so we plug in p-hat. 26 of 40 is 0.65; the SE is about 0.075; two SE is about 0.15. So the truth could plausibly be anywhere from 0.50 to 0.80. Business reading: a survey of 40 cannot distinguish "half our customers" from "four out of five". If the decision depends on that difference, survey more people; the next slides show how many.
Back to the tips bills. The same square-root idea works for means: the standard error of a sample mean is the population SD divided by the square root of n. With sigma = 8.88 (np.std of the 244 bills, since they are the whole population) and n = 20, the formula gives 1.99, exactly what the simulation showed. As with proportions, we rarely know sigma, so we plug in the sample SD. This is homework question 10.
Same true p, three sample sizes. With 10 customers the p-hats are all over the place; with 200 they cluster tightly around 0.6. The table gives the formula SE for each n; the panel titles give the SD of the simulated p-hats, which match to within 0.001.
Because n is under a square root, doubling the sample does not halve the error. Quadrupling it does. Management angle: a survey vendor quotes a price per response. Cutting the margin of error in half costs four times as much. Often a moderate sample is the better business decision.
Give students 20 seconds. Answer: B. Four times the sample size, so the SE is divided by the square root of 4, which is 2. Why the others are wrong: C would require 16 times the sample. A and D ignore the formula. This is homework question 5.
This is the section 8.3 exercise. Give students 5 minutes. Answers: SE with n = 100 is sqrt(0.3 x 0.7 / 100) = 0.046; with n = 900 it is 0.015; the ratio is 3.0. Nine times the sample gives an SE three times smaller. The 27% is a statistic: it comes from a sample, and the parameter is the true share, about 30%. SE of the mean deposit: 400 / sqrt(100) = 40 and 400 / sqrt(400) = 20. Lab question 5 asks students to check the mean version by simulation. Question 3 has no single right answer; discuss what decision the survey supports and whether plus or minus 4.6 points is precise enough.
Ask students to supply each line. That completes Chapter 8. Before next class: the section exercises, then the Chapter 8 lab and homework. Next chapter: hypothesis testing. We will compare an observed result with what a chance model predicts, using exactly the simulation ideas from today.