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
np.random.seed(42)
values = np.random.normal(loc=50, scale=5, size=1000) # mean 50, SD 5
np.random.binomial(n=10, p=0.5, size=100): heads in 10 flips, 100 timesnp.random.multinomial(20, [0.3, 0.5, 0.2], size=1000): small, medium, large among 20 coffee orders, 1,000 timesnp.random.seed(42)
flips = np.random.choice([0, 1], size=5000) # 0 = tails, 1 = heads
running = flips.cumsum() / (np.arange(5000) + 1)
cumsum(): heads so far · np.arange(5000) + 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.