⚙️ Setup¶
import numpy as np
from scipy import stats
print("ready")
ready
mu, sd = 170, 8
print(f"162 to 178 is mu +/- 1 sigma -> about 68% (exact {(stats.norm.cdf(178,mu,sd)-stats.norm.cdf(162,mu,sd))*100:.1f}%)")
print(f"154 to 186 is mu +/- 2 sigma -> about 95% (exact {(stats.norm.cdf(186,mu,sd)-stats.norm.cdf(154,mu,sd))*100:.1f}%)")
162 to 178 is mu +/- 1 sigma -> about 68% (exact 68.3%) 154 to 186 is mu +/- 2 sigma -> about 95% (exact 95.4%)
Answer: 162 to 178 is μ ± 1σ, so about 68%; 154 to 186 is μ ± 2σ, so about 95%. The empirical rule lets you answer these in your head, no z-table required.
z = (85 - 75) / 5
print(f"z = (85 - 75)/5 = {z:.1f}")
print(f"percentile = P(Z < 2) = {stats.norm.cdf(z)*100:.1f}%")
z = (85 - 75)/5 = 2.0 percentile = P(Z < 2) = 97.7%
Answer: z = (85 − 75)/5 = 2.0, so the score is two standard deviations above the mean. That places the student at the 97.7th percentile, higher than about 98% of test-takers.
zA = (88 - 80) / 4
zB = (90 - 85) / 10
print(f"Ana: z = {zA:.2f}")
print(f"Ben: z = {zB:.2f}")
print("Ana is stronger relative to her exam" if zA>zB else "Ben is stronger")
Ana: z = 2.00 Ben: z = 0.50 Ana is stronger relative to her exam
Answer: Ana's z = 2.0 versus Ben's z = 0.5. Although Ben's raw score is higher, Ana is far more exceptional relative to her exam (two standard deviations above the mean versus half of one). The z-score, not the raw number, is the fair comparison.
mu, sd = 100, 15
z = (130 - mu)/sd
print(f"z = (130-100)/15 = {z:.1f}")
print(f"P(X > 130) = 1 - P(Z < 2) = {(1-stats.norm.cdf(z))*100:.2f}%")
z = (130-100)/15 = 2.0 P(X > 130) = 1 - P(Z < 2) = 2.28%
Answer: 130 is z = 2 above the mean, so P(X > 130) = 1 − Φ(2) ≈ 2.28%. By the empirical rule, about 2.5% lie beyond +2σ, matching the exact 2.28%.
x = np.array([10, 12, 14, 16, 18])
z = (x - x.mean()) / x.std()
print(f"mean = {x.mean()}, std = {x.std():.3f}")
print(f"z-scores = {z.round(3)}")
print(f"check: mean {z.mean():.3f}, std {z.std():.3f}")
mean = 14.0, std = 2.828 z-scores = [-1.414 -0.707 0. 0.707 1.414] check: mean 0.000, std 1.000
Answer: the mean is 14 and the standard deviation is √8 ≈ 2.83, giving z-scores [−1.41, −0.71, 0, 0.71, 1.41]. The standardized data has mean 0 and std 1, exactly what StandardScaler produces, and the most common feature-preprocessing step in machine learning.