Sub topic 6
Testing a single mean with z and t
This chapter assumes you know the hypothesis setup (null, alternative, one- or two-tailed), the general form of a test statistic, and the critical-value and p-value decision rules from the earlier chapters. It also assumes you are comfortable with the standard error of the mean. Here you apply those pieces to one question: is the population mean equal to a stated value?
The working model
A test of a single mean compares the sample mean with the hypothesized mean, measured in standard errors:
test statistic = (sample mean - hypothesized mean) / standard error of the sample meanThe only choices left are how to compute the standard error and which distribution supplies the critical value. Both depend on whether you know the population standard deviation, σ.
- z-test. If σ is known, the standard error is σ / √n and the statistic follows the standard normal distribution.
- t-test. If σ is unknown, you replace it with the sample standard deviation s. The standard error becomes s / √n, and the statistic follows a t-distribution with n − 1 degrees of freedom.
In practice σ is almost never known, so the t-test is the default.
The t-distribution is symmetric and bell-shaped, like the normal, but it has fatter tails. Using s adds estimation noise, and the fatter tails account for it. The t-distribution gets closer to the normal as the degrees of freedom increase.
| Situation | Sample size | Use |
|---|---|---|
| Normal population, σ known | Any | z |
| Normal population, σ unknown | Any | t, df = n − 1 |
| Non-normal population, σ known | Large (about 30 or more) | z, by the central limit theorem |
| Non-normal population, σ unknown | Large | t (z is an acceptable approximation) |
| Non-normal population, σ unknown | Small | Neither is reliable |
The last row is a real gap. With a small sample from a non-normal population, the sampling distribution of the mean is not guaranteed to be normal, so neither test is justified. Consider a nonparametric test instead, which a later chapter covers.
Choosing the test
Degrees of freedom and critical values
The t-statistic uses s, which is computed from n deviations around the sample mean. Those deviations must sum to zero, so only n − 1 of them can vary freely. That count is the degrees of freedom.
Critical values for a 5% test show how the choice matters.
| Test | Two-tailed critical value | One-tailed critical value |
|---|---|---|
| z | ±1.960 | 1.645 |
| t, df = 9 | ±2.262 | 1.833 |
| t, df = 35 | ±2.030 | 1.690 |
| t, df = 120 | ±1.980 | 1.658 |
At small n the t critical value is noticeably larger, so rejecting the null is harder. This guards against being overconfident when s is a rough estimate of σ.
Worked example in code
A fund reports ten monthly excess returns (in %). Test H0: μ = 0 against Ha: μ ≠ 0 at the 5% level. The sample mean is 0.61 and s is about 0.967, so the standard error is 0.967 / √10, or about 0.306. The statistic is 0.61 / 0.306, or about 1.996, with 9 degrees of freedom.
import numpy as np
from scipy import stats
returns = np.array([1.2, -0.4, 0.8, 2.1, -1.0, 0.5, 1.7, 0.3, -0.2, 1.1])
mu0 = 0.0
alpha = 0.05
n = len(returns)
df = n - 1
mean = returns.mean()
s = returns.std(ddof=1) # sample standard deviation, divides by n - 1
se = s / np.sqrt(n)
t_stat = (mean - mu0) / se
# Two-tailed test
t_crit_two = stats.t.ppf(1 - alpha / 2, df)
p_two = 2 * stats.t.sf(abs(t_stat), df)
# One-tailed test, Ha: mu > 0 (the direction must be chosen before seeing data)
t_crit_one = stats.t.ppf(1 - alpha, df)
p_one = stats.t.sf(t_stat, df)
# Hypothetical z-test: pretend sigma = 1.0 is known
z_stat = (mean - mu0) / (1.0 / np.sqrt(n))
# 95% confidence interval for the mean
ci = (mean - t_crit_two * se, mean + t_crit_two * se)
print(f"mean={mean:.3f} s={s:.3f} se={se:.3f} t={t_stat:.3f} df={df}")
print(f"two-tailed: crit=+/-{t_crit_two:.3f} p={p_two:.3f} reject={abs(t_stat) > t_crit_two}")
print(f"one-tailed: crit={t_crit_one:.3f} p={p_one:.3f} reject={t_stat > t_crit_one}")
print(f"z with known sigma=1.0: z={z_stat:.3f}")
print(f"95% CI: ({ci[0]:.3f}, {ci[1]:.3f})")The result is instructive:
- The two-tailed test fails to reject, because 1.996 is below 2.262 and the p-value is about 0.08.
- The one-tailed test rejects, because 1.996 is above 1.833.
- The confidence interval includes 0, which agrees with the two-tailed result. A two-tailed test at level α rejects exactly when the hypothesized value falls outside the (1 − α) confidence interval.
Common errors
- Using z when σ is unknown. If the problem gives only a sample standard deviation, use t, even when n is large. At large n the two answers are close, but t is the correct one.
- Wrong degrees of freedom. For a single mean, df = n − 1, not n. With n = 36 you read the t table at 35.
- Forgetting the square root. The standard error is s / √n. Dividing by n, or using s alone, inflates or deflates the statistic.
- Using the population formula for s. The sample standard deviation divides by n − 1. In NumPy,
stddefaults to dividing by n, so you must passddof=1. - Mixing tails. A two-tailed test at 5% puts 2.5% in each tail. Reading a one-tailed critical value (for example 1.645) for a two-tailed test makes you reject too easily.
- Treating "fail to reject" as "accept". A failure to reject says the sample did not provide enough evidence against H0. It does not show that μ equals the hypothesized value.
- Confusing statistical and economic significance. With a large n, a trivially small difference can produce a large t-statistic. Always look at the size of the estimated mean as well.
Using the SciPy shortcut
stats.ttest_1samp(returns, popmean=0.0) returns the same t-statistic and a two-tailed p-value. To get a one-tailed p-value, pass alternative="greater" or alternative="less". SciPy has no built-in z-test for a single mean with known σ, so compute that statistic directly and use stats.norm.sf for the p-value.
Exercise
An analyst tests whether a portfolio's mean annual return differs from 6%. A sample of 36 annual returns has a mean of 8.2% and a sample standard deviation of 6.0%. The population standard deviation is unknown. Compute the test statistic, state the degrees of freedom, and decide at the 5% level using a two-tailed test (critical value about ±2.030 for df = 35).
Show answer
The standard error is 6.0 / √36 = 1.0. The t-statistic is (8.2 − 6.0) / 1.0 = 2.2, with df = 36 − 1 = 35. Since 2.2 exceeds 2.030, reject H0: the mean return differs significantly from 6% at the 5% level. A t-test is used because σ is unknown.
Exercise
A researcher has 12 observations from a population that is clearly skewed, and σ is unknown. Which test should she use for a single mean, and why?
Show answer
Neither the z-test nor the t-test is reliable. The sample is small and the population is non-normal, so the sampling distribution of the mean cannot be assumed normal. A nonparametric test, such as a sign test or Wilcoxon signed-rank test, is more appropriate.