⚙️ Setup¶
import numpy as np
from scipy import stats
rng = np.random.default_rng(401)
print("ready")
ready
sigma = 20
for n in [100, 400]:
print(f"n={n}: SE = 20/sqrt({n}) = {sigma/np.sqrt(n):.2f}")
n=100: SE = 20/sqrt(100) = 2.00 n=400: SE = 20/sqrt(400) = 1.00
Answer: SE = σ/√n, so for n = 100, SE = 20/10 = 2.0, and for n = 400, SE = 20/20 = 1.0. Quadrupling the sample halved the standard error, the square-root law in action.
mu, sd, n = 500, 120, 36
SE = sd/np.sqrt(n)
z = (540 - mu)/SE
print(f"SE = {SE:.2f}, z = (540-500)/SE = {z:.2f}")
print(f"P(sample mean > 540) = {1 - stats.norm.cdf(z):.4f}")
SE = 20.00, z = (540-500)/SE = 2.00 P(sample mean > 540) = 0.0228
Answer: the CLT makes the sample mean approximately normal with SE = 120/6 = 20. Then z = (540 − 500)/20 = 2.0, so P(mean > 540) = 1 − Φ(2) ≈ 0.0228. Even though daily sales are skewed, the mean of 36 days is nearly normal.
# SE scales like 1/sqrt(n); to divide SE by 4, multiply n by 4^2
factor = (4/1)**2
print(f"to reduce SE by a factor of 4, increase n by {factor:.0f}x")
to reduce SE by a factor of 4, increase n by 16x
Answer: since SE ∝ 1/√n, dividing the SE by 4 requires multiplying n by 4² = 16. Precision improves only with the square root of sample size, so each extra digit of accuracy is dramatically more expensive.
xbar, sigma, n = 50, 16, 64
SE = sigma/np.sqrt(n)
lo, hi = xbar - 1.96*SE, xbar + 1.96*SE
print(f"SE = 16/8 = {SE:.1f}")
print(f"95% CI = 50 +/- 1.96*{SE:.1f} = [{lo:.2f}, {hi:.2f}]")
SE = 16/8 = 2.0 95% CI = 50 +/- 1.96*2.0 = [46.08, 53.92]
Answer: SE = 16/√64 = 2.0, so the 95% interval is 50 ± 1.96(2) = [46.08, 53.92]. We are about 95% confident the true mean lies in this range, an interval built directly on the CLT.
p, n = 0.60, 1000
SE = np.sqrt(p*(1-p)/n)
print(f"SE = sqrt(0.6*0.4/1000) = {SE:.4f}")
print(f"95% margin of error = 1.96*SE = +/- {1.96*SE:.4f} (about +/- 3 points)")
SE = sqrt(0.6*0.4/1000) = 0.0155 95% margin of error = 1.96*SE = +/- 0.0304 (about +/- 3 points)
Answer: SE = √(0.6·0.4/1000) ≈ 0.0155, so the 95% margin of error is 1.96 × 0.0155 ≈ ±0.030, the familiar "±3 percentage points" of political polls. A proportion is just a mean of 0s and 1s, so the same CLT machinery applies.