3/17
Part 1 of 7 · From z to tLearn and do

Estimating the spread makes the tails heavier

The z-test in the A/B testing lesson treats the standard error as known. NIST's handbook notes that the more typical case is where the standard deviation must be estimated from the data, and that estimate is itself noisy.

William Sealy Gosset worked this out as a brewer at Guinness in Dublin. His contract barred him from publishing under his own name, so his 1908 paper went out under the pseudonym Student. He warned that with fewer experiments the estimated standard deviation carries more error, until judgments made with it can become altogether misleading.

His answer was the t distribution: shaped like the normal, NIST says, but with heavier tails. It has degrees of freedom, n − 1 for one sample, and approaches the normal as they grow, quite good past 30.

The t curve against the normalInteractive
Degrees of freedom
-4-2024t
t curve, 5 degrees of freedom normal curve t's 5% cutoffs
p from the t curve
0.102
p from the normal
0.046
t's 5% cutoff
±2.57
Normal's cutoff
±1.96
With 5 degrees of freedom, t = 2.00 leaves 10.2% in the two shaded tails, against 4.6% under the normal curve. Significance at 5% needs |t| past 2.57 here, not 1.96.
import math
from statistics import NormalDist


def t_pdf(x: float, df: float) -> float:
    """Student's t density with df degrees of freedom."""
    c = math.exp(math.lgamma((df + 1) / 2) - math.lgamma(df / 2)) / math.sqrt(df * math.pi)
    return c * (1 + x * x / df) ** (-(df + 1) / 2)


def t_two_sided_p(t: float, df: float, steps: int = 2_000) -> float:
    """The area beyond -|t| and +|t|: 1 minus Simpson's rule on -|t| to |t|."""
    h = abs(t) / steps
    inner = t_pdf(0, df) + t_pdf(abs(t), df)
    inner += sum((4 if i % 2 else 2) * t_pdf(i * h, df) for i in range(1, steps))
    return 1 - 2 * inner * h / 3


# 25 deliveries: average 4.4 days, standard deviation 1.0
n, mean, s = 25, 4.4, 1.0
# against the carrier's promised 4.0 days
t = (mean - 4.0) / (s / math.sqrt(n))
print(f"t = {t:.2f} with {n - 1} degrees of freedom")
print(f"p from the t curve:      {t_two_sided_p(t, n - 1):.4f}")
print(f"p from the normal curve: {2 * (1 - NormalDist().cdf(t)):.4f}")
Predict

Is the 0.4-day gap significant at the 5% level?

Pick one to see what it prints.
Pick an answer above to see the rest