Testing a Mean (Two-Tailed)
A two-tailed t-test asks whether the true mean is different from μ₀, in either direction. It is the standard choice when both "too high" and "too low" matter, as in quality control, where a part that is too long is as bad as one that is too short.
The scenario
A lathe is set to cut steel pins with a diameter of 12.00 mm. Each morning, a technician measures 12 pins to check whether the machine has drifted:
12.03 11.97 12.08 12.05 11.99 12.06 12.02 12.10 11.98 12.04 12.07 12.01Step 1–3
H₀: μ = 12.00 mm (machine on target)
H₁: μ ≠ 12.00 mm (machine drifted) ← two-tailed
α = 0.05Pins are measured at random times during the shift; machining errors are usually close to normal.
Step 4–5: Statistic and p-value
The p-value adds both tails: p = 2 × P(T ≥ |t|). The critical values are ±t_(α/2, df).
import math
import statistics
from scipy import stats
pins = [12.03, 11.97, 12.08, 12.05, 11.99, 12.06, 12.02, 12.10, 11.98, 12.04, 12.07, 12.01]
mu0, n = 12.00, len(pins)
xbar, s = statistics.mean(pins), statistics.stdev(pins)
t = (xbar - mu0) / (s / math.sqrt(n))
p_value = 2 * stats.t.sf(abs(t), df=n - 1)
t_crit = stats.t.ppf(1 - 0.05 / 2, df=n - 1)
print(f"x-bar = {xbar:.4f} mm, s = {s:.4f} mm")
print(f"t = {t:.3f}, df = {n - 1}")
print(f"p-value = {p_value:.4f}")
print(f"critical values = ±{t_crit:.3f}")x-bar = 12.0333 mm, s = 0.0410 mm
t = 2.818, df = 11
p-value = 0.0167
critical values = ±2.201
Step 6: Conclusion
p ≈ 0.017 < 0.05 and |t| = 2.82 > 2.201, so we reject H₀.
In context: the data show that the mean pin diameter differs from 12.00 mm; the machine appears to have drifted upwards and should be recalibrated.
One call, plus the confidence interval
import numpy as np
from scipy import stats
pins = np.array([12.03, 11.97, 12.08, 12.05, 11.99, 12.06, 12.02, 12.10, 11.98, 12.04, 12.07, 12.01])
res = stats.ttest_1samp(pins, popmean=12.00) # two-sided is the default
ci = res.confidence_interval(0.95)
print(f"t = {res.statistic:.3f}, p = {res.pvalue:.4f}")
print(f"95% CI for μ: ({ci.low:.4f}, {ci.high:.4f}) mm")t = 2.818, p = 0.0167
95% CI for μ: (12.0073, 12.0594) mm
For means, the duality is exact: the two-tailed test at α = 0.05 rejects H₀: μ = μ₀ exactly when μ₀ lies outside the 95% t-interval. Here 12.00 is below the interval, which tells us both that the machine drifted and in which direction.
A day without drift
On another day the measurements look like this. The sample mean is again slightly above 12, but the test cannot tell that apart from random noise:
import numpy as np
from scipy import stats
pins = np.array([12.02, 11.96, 12.04, 11.99, 12.01, 12.05, 11.97, 12.03, 12.00, 11.98, 12.04, 12.01])
res = stats.ttest_1samp(pins, popmean=12.00)
ci = res.confidence_interval(0.95)
print(f"mean = {pins.mean():.4f}, t = {res.statistic:.3f}, p = {res.pvalue:.3f}")
print(f"95% CI: ({ci.low:.4f}, {ci.high:.4f})")mean = 12.0083, t = 0.989, p = 0.344
95% CI: (11.9898, 12.0269)
p > 0.05 and 12.00 is inside the interval: we fail to reject H₀. That does not prove the machine is perfect. It means any drift is too small to detect with 12 pins.
Comparing two groups: the two-sample t-test
Two-tailed tests are most often used to compare two means, for example the pins from two different machines. The Welch t-test (equal_var=False) does not assume the groups have equal variances and is the safe default:
H₀: μ₁ = μ₂ H₁: μ₁ ≠ μ₂
t = (x̄₁ − x̄₂) / √(s₁²/n₁ + s₂²/n₂)import numpy as np
from scipy import stats
machine_a = np.array([12.03, 11.97, 12.08, 12.05, 11.99, 12.06, 12.02, 12.10, 11.98, 12.04, 12.07, 12.01])
machine_b = np.array([11.98, 12.01, 11.96, 12.00, 11.99, 12.02, 11.97, 12.00, 11.98, 12.01])
res = stats.ttest_ind(machine_a, machine_b, equal_var=False)
print(f"means: A = {machine_a.mean():.4f}, B = {machine_b.mean():.4f}")
print(f"Welch t = {res.statistic:.3f}, df = {res.df:.1f}, p = {res.pvalue:.4f}")means: A = 12.0333, B = 11.9920
Welch t = 3.105, df = 16.2, p = 0.0067
Machine A's pins are significantly wider on average than machine B's. When the same units are measured twice (before/after, left/right), use the paired t-test, stats.ttest_rel, which tests whether the mean of the differences is zero.
Test yourself
A two-tailed t-test gives t = −2.5 with df = 20. Which code gives the p-value?
stats.t.cdf(-2.5, 20)2 * stats.t.sf(2.5, 20)stats.t.sf(-2.5, 20)2 * stats.t.cdf(2.5, 20)
A 95% t-interval for μ is (4.1, 5.3). Will a two-tailed t-test of H₀: μ = 5 at α = 0.05 reject?
- Yes
- No, because 5 lies inside the interval
- Only if n > 30
- It is impossible to tell
You measure the reaction time of the same 15 people before and after drinking coffee. Which test fits?
- Welch two-sample t-test
- Paired t-test (
ttest_rel) - One-proportion z-test
- Chi-square test