TExcelTAD
Home › Statistics › Testing a Mean

Testing a Mean

This lesson tests a claim about a population mean μ with a right-tailed test: we look for evidence that the true mean is greater than a reference value μ₀. Because σ is unknown, we use the one-sample t-test.

The scenario

The previous model of a wireless speaker ran for 10 hours on a full charge on average. Engineers claim the new battery lasts longer. They test 16 randomly selected speakers from the first production run (hours of playback):

text
10.4  9.8 11.2 10.9 10.1 10.6 11.5  9.9
10.8 10.3 11.0 10.7  9.6 10.5 11.3 10.2

Step 1–2: Hypotheses and α

text
H₀: μ = 10      (no improvement)
H₁: μ > 10      (longer battery life)   ← right-tailed
α = 0.05

Step 3: Conditions

  1. Random sample of independent speakers.
  2. The population of battery lives is approximately normal, or n is large. With n = 16 we should check the data for strong skew or outliers:
python · try it yourself
import numpy as np
from scipy import stats

hours = np.array([10.4, 9.8, 11.2, 10.9, 10.1, 10.6, 11.5, 9.9,
                  10.8, 10.3, 11.0, 10.7, 9.6, 10.5, 11.3, 10.2])

print(f"n = {hours.size}, mean = {hours.mean():.3f}, median = {np.median(hours):.3f}")
print(f"skewness = {round(stats.skew(hours), 3) + 0.0:.3f}")   # + 0.0 turns -0.0 into 0.0
q1, q3 = np.percentile(hours, [25, 75])
iqr = q3 - q1
print("outliers (1.5 IQR rule):", hours[(hours < q1 - 1.5 * iqr) | (hours > q3 + 1.5 * iqr)])
n = 16, mean = 10.550, median = 10.550
skewness = 0.000
outliers (1.5 IQR rule): []

Mean ≈ median, almost no skew and no outliers: the t-test is appropriate.

Step 4: Test statistic

text
t = (x̄ − μ₀) / (s / √n)        df = n − 1
python · try it yourself
import math
import statistics

hours = [10.4, 9.8, 11.2, 10.9, 10.1, 10.6, 11.5, 9.9,
         10.8, 10.3, 11.0, 10.7, 9.6, 10.5, 11.3, 10.2]
mu0 = 10
n = len(hours)
xbar = statistics.mean(hours)
s = statistics.stdev(hours)
t = (xbar - mu0) / (s / math.sqrt(n))

print(f"x-bar = {xbar:.3f}, s = {s:.4f}, n = {n}")
print(f"t = ({xbar:.3f} - {mu0}) / ({s:.4f} / sqrt({n})) = {t:.3f}")
print("df =", n - 1)
x-bar = 10.550, s = 0.5538, n = 16
t = (10.550 - 10) / (0.5538 / sqrt(16)) = 3.973
df = 15

The sample mean is almost 4 standard errors above 10 hours.

Step 5: p-value

For a right-tailed test, the p-value is the area to the right of t under the t-distribution with 15 degrees of freedom:

python · try it yourself
from scipy import stats

t, df = 3.9727, 15
print(f"p-value = P(T > {t}) = {stats.t.sf(t, df):.5f}")
print(f"critical value t* = {stats.t.ppf(0.95, df):.3f}")
p-value = P(T > 3.9727) = 0.00061
critical value t* = 1.753

Step 6: Conclusion

p ≈ 0.0006, far below α = 0.05, and t = 3.97 > 1.753. We reject H₀.

In context: there is strong evidence that the mean battery life of the new speakers is greater than 10 hours.

All in one call

scipy.stats.ttest_1samp performs the test. The alternative argument chooses the tail: "greater", "less" or "two-sided" (the default):

python · try it yourself
import numpy as np
from scipy import stats

hours = np.array([10.4, 9.8, 11.2, 10.9, 10.1, 10.6, 11.5, 9.9,
                  10.8, 10.3, 11.0, 10.7, 9.6, 10.5, 11.3, 10.2])

res = stats.ttest_1samp(hours, popmean=10, alternative="greater")
print(f"t = {res.statistic:.3f}, df = {res.df}, p-value = {res.pvalue:.5f}")
ci = res.confidence_interval(confidence_level=0.95)
print(f"one-sided 95% lower bound for μ: {ci.low:.3f} hours")
t = 3.973, df = 15, p-value = 0.00061
one-sided 95% lower bound for μ: 10.307 hours

The one-sided confidence bound tells the engineers more than the p-value alone: the mean battery life is at least about 10.3 hours, with 95% confidence.

Effect size

A tiny p-value says the effect is real, not that it is large. A common effect size for a mean difference is Cohen's d, the difference expressed in standard deviations:

text
d = (x̄ − μ₀) / s
python · try it yourself
import statistics

hours = [10.4, 9.8, 11.2, 10.9, 10.1, 10.6, 11.5, 9.9,
         10.8, 10.3, 11.0, 10.7, 9.6, 10.5, 11.3, 10.2]
d = (statistics.mean(hours) - 10) / statistics.stdev(hours)
print(f"Cohen's d = {d:.2f}")
print(f"improvement = {statistics.mean(hours) - 10:.2f} hours ({(statistics.mean(hours) - 10) / 10:.1%})")
Cohen's d = 0.99
improvement = 0.55 hours (5.5%)

By Cohen's rough guidelines (0.2 small, 0.5 medium, 0.8 large) this is a large effect: about half an hour, or 5.5% more playback.

Test yourself

Which distribution gives the p-value of a one-sample t-test with n = 16?

  1. Standard normal
  2. t with 16 df
  3. t with 15 df
  4. Chi-square with 15 df

What does alternative="greater" mean in ttest_1samp(x, 10, alternative="greater")?

  1. H₁: μ > 10
  2. H₁: μ < 10
  3. H₁: μ ≠ 10
  4. H₀: μ > 10

A right-tailed t-test has p = 0.0001 but Cohen's d = 0.05. What is the best interpretation?

  1. A huge and important effect
  2. A real but very small effect, probably from a large sample
  3. The test is wrong
  4. The null hypothesis is true