P-values and Hypothesis Test Setup › The Test Statistic: Standardizing Evidence
Machine Learning from ScratchAll chapters P-values and Hypothesis Test Setup › The Test Statistic: Standardizing EvidenceOne section · Sections in this chapter5 / 9 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Raw differences are meaningless without context. If I tell you my sample mean is 5 units above the hypothesized value, you can't assess the evidence without knowing how much variability to expect.
The test statistic solves this by standardizing the deviation. It answers: "How many standard errors away from the null hypothesis is my observation?"
The General Formula
For most tests involving means, the test statistic takes this form:
Let's break this down:
Numerator: How far is my observation from what the null predicts?
If claims the population mean is 100 and I observe a sample mean of 105, the numerator is .
Denominator: How much random variation should I expect?
The standard error measures the typical sampling variation. For a sample mean, it's (if we know the population standard deviation) or (if we estimate it from the sample).
The ratio: How surprising is my observation?
A test statistic of 0.5 means my observation is half a standard error from the null. That's not surprising. A test statistic of 3 means my observation is 3 standard errors from the null. That's quite unusual.
Example: Step-by-Step Calculation
Let's work through a complete example. Suppose you want to test whether the average weight of packages from a production line differs from the target of 500g. You sample 25 packages and measure their weights.
Step 1: State the hypotheses
- (the machine is calibrated correctly)
- (the machine is miscalibrated)
Step 2: Collect data and compute summary statistics
import numpy as np
# Sample data: weights of 25 packages (in grams)
weights = np.array(
[
502,
498,
505,
501,
497,
503,
499,
506,
500,
504,
501,
498,
502,
505,
499,
503,
500,
497,
504,
501,
506,
499,
502,
498,
503,
]
)
# Summary statistics
n = len(weights)
sample_mean = np.mean(weights)
sample_std = np.std(weights, ddof=1) # ddof=1 for sample standard deviation
standard_error = sample_std / np.sqrt(n)
print(f"Sample size: n = {n}")
print(f"Sample mean: x̄ = {sample_mean:.2f}g")
print(f"Sample standard deviation: s = {sample_std:.2f}g")
print(f"Standard error: SE = {standard_error:.2f}g")Sample size: n = 25 Sample mean: x̄ = 501.32g Sample standard deviation: s = 2.78g Standard error: SE = 0.56g
Step 3: Calculate the test statistic
Since we don't know the population standard deviation, we use the sample standard deviation and the t-statistic:
# Calculate t-statistic
hypothesized_mean = 500
t_stat = (sample_mean - hypothesized_mean) / standard_error
print(f"t-statistic: t = {t_stat:.3f}")t-statistic: t = 2.374
Step 4: Find the p-value
The t-statistic follows a t-distribution with degrees of freedom. For a two-sided test, we need the probability of being as extreme or more extreme than our observed t-statistic in either direction:
# Calculate two-sided p-value
df = n - 1
p_value = 2 * (1 - stats.t.cdf(abs(t_stat), df=df))
print(f"Degrees of freedom: df = {df}")
print(f"Two-sided p-value: p = {p_value:.4f}")Degrees of freedom: df = 24 Two-sided p-value: p = 0.0259
Step 5: Make a decision
At the conventional α = 0.05 significance level, our p-value of 0.023 is less than 0.05. We reject the null hypothesis and conclude there's statistically significant evidence that the mean weight differs from 500g.

Why Sample Size Matters
The standard error shrinks as sample size grows, but not linearly. It shrinks proportionally to :
- 4× the sample size → 2× smaller standard error
- 100× the sample size → 10× smaller standard error
This has important implications:
-
Larger samples detect smaller effects: With enough data, even tiny differences become "statistically significant."
-
Diminishing returns: Each additional observation contributes less than the previous one. Quadrupling your sample only halves your uncertainty.
-
Statistical vs practical significance: A huge sample might find a "significant" effect that's too small to matter in practice.
