Sub topic 9
Comparing One Sample with a Target
This chapter assumes you can write null and alternative hypotheses, you know what alpha and a p-value mean, and you can read a histogram and a normal probability plot. Here you apply those ideas to the simplest test setup, where one set of measurements is compared with a fixed number.
When a one-sample test applies
A one-sample test compares a statistic from a single sample with a fixed reference value. The reference value comes from outside the data. It might be a specification, a contract, a regulatory limit, or a design target.
Typical Analyze-phase questions look like this:
- Does the average fill weight of this line equal the labeled 500 g?
- Is the mean cycle time at or below the 48-hour promise to customers?
- Is the defect rate on this line above the 5% the process owner believes it is?
In each case there is one Y, one group, and one target. If you are comparing two groups with each other, or the same units before and after a change, you need the two-sample or paired tests covered in the next chapter.
The target must not be estimated from the same data. If you take 500 g from last month's sample mean, you are comparing two samples, not one sample with a target.
Hypotheses for a target
Let mu be the true process mean and mu0 the target. You choose one of three forms before looking at the data.
| Question | H0 | Ha | Tail |
|---|---|---|---|
| Is the mean different from target? | mu = mu0 | mu != mu0 | Two-sided |
| Is the mean above target? | mu <= mu0 | mu > mu0 | Upper one-sided |
| Is the mean below target? | mu >= mu0 | mu < mu0 | Lower one-sided |
Use a two-sided test by default. Use a one-sided test only when a difference in the other direction would lead to the same action as no difference. Choosing the tail after you see which way the data lean doubles your real Type I error rate.
The one-sample t-test
For continuous Y, the standard tool is the one-sample t-test. It converts the gap between the sample mean and the target into standard-error units:
t = (x_bar - mu0) / (s / sqrt(n))
The statistic follows a t distribution with n - 1 degrees of freedom when H0 is true. The t distribution is used instead of the normal distribution because s is estimated from the same sample. The extra uncertainty matters most for small n.
The test assumes:
- Observations are independent. Consecutive readings from a drifting process, or several parts from one batch, violate this.
- Y is continuous and measured with adequate resolution.
- The data come from an approximately normal population. This matters mostly for small samples. For moderate to large n, the sampling distribution of the mean is close to normal even for mildly skewed data, so the test is fairly tolerant.
Worked example
A filling line is labeled 500 g. You weigh 25 randomly chosen containers over a representative period. The code checks normality, runs the test, and computes a 95% confidence interval for the mean.
import numpy as np
from scipy import stats
rng = np.random.default_rng(11)
target = 500.0
fill = rng.normal(loc=498.8, scale=3.0, size=25)
n = len(fill)
print("n =", n, "mean =", fill.mean(), "sd =", fill.std(ddof=1))
# Assumption check
print("Shapiro-Wilk p =", stats.shapiro(fill).pvalue)
# Two-sided one-sample t-test against the target
result = stats.ttest_1samp(fill, popmean=target, alternative="two-sided")
print("t =", result.statistic, "p =", result.pvalue)
# 95% confidence interval for the true mean
half_width = stats.t.ppf(0.975, df=n - 1) * stats.sem(fill)
print("95% CI:", fill.mean() - half_width, "to", fill.mean() + half_width)Read the output in this order:
- Confirm the Shapiro-Wilk p-value does not give strong evidence against normality, and look at a histogram as well.
- Compare the t-test p-value with your alpha, usually 0.05. If p is below alpha, reject H0 and conclude the mean differs from 500 g.
- Look at the confidence interval. It shows the size and direction of the gap, which the p-value does not. If the interval excludes 500, a two-sided test at the matching alpha rejects H0. This is the same conclusion, expressed in grams.
For a one-sided question, pass alternative="less" or alternative="greater". In SciPy, the alternative describes Ha, so "less" tests whether the true mean is below the target.
When normality fails
Skewed measurements such as cycle times, wait times, and particle counts often fail the normality check. Your options depend on the sample size and on what you want to say about the data.
- Large sample (roughly 30 or more) with mild skew. The t-test is usually still reasonable. Check for strong outliers first.
- Small sample with clear skew or outliers. Use a rank-based test.
- Strictly positive, right-skewed data. A log transform often makes the data close to normal. Test the mean of the logged values against log(target). This then compares the geometric mean, not the arithmetic mean.
The usual non-parametric option is the Wilcoxon signed-rank test. It ranks the absolute differences from the target and tests whether positive and negative differences balance.
import numpy as np
from scipy import stats
rng = np.random.default_rng(5)
target_hours = 48.0
cycle_time = rng.lognormal(mean=3.6, sigma=0.5, size=20)
print("Shapiro-Wilk p =", stats.shapiro(cycle_time).pvalue)
# Wilcoxon signed-rank on the differences from the target
w = stats.wilcoxon(cycle_time - target_hours, alternative="two-sided")
print("W =", w.statistic, "p =", w.pvalue)The signed-rank test assumes the distribution of differences is roughly symmetric, so it is a test of the median only under that condition. For strongly skewed data, the sign test is the safer rank-free option. It counts how many values fall above the target and uses a binomial test on that count. It has less power but needs almost no assumptions.
One sample against a target proportion
When Y is discrete (each unit is defective or not), the reference value is a proportion. The exact binomial test compares the observed count with the count expected under the target rate.
from scipy import stats
# 14 defective units in 200 inspected; the process owner believes the rate is 5%
result = stats.binomtest(k=14, n=200, p=0.05, alternative="greater")
print("p =", result.pvalue)
print("Observed rate =", 14 / 200)The exact test is preferable to a normal approximation when n times p0 or n times (1 - p0) is small, roughly under 10. Here the expected defective count under H0 is 10, which is borderline, so the exact test is the right choice.
Choosing the right one-sample test
Common errors
- Using the sample mean as the target. This compares the sample with itself and always gives a meaningless result.
- Ignoring independence. Autocorrelated data, such as readings taken seconds apart, make the effective sample size smaller than n. The p-value comes out too small.
- Switching tails after seeing the data. State the tail in the problem statement before analysis.
- Testing a spec limit with a mean test. A mean of 500 g can meet the target while many individual containers fall below the limit. If the requirement applies to individual units, you need a capability or tolerance analysis, not just a test of the mean.
- Reporting only the p-value. Always give the estimate, the confidence interval, and n.
Exercise
A team measures 16 shipments and finds a mean lead time of 12.4 days with a standard deviation of 0.8 days. The promised lead time is 12.0 days. Calculate the t statistic and its degrees of freedom. The two-sided critical value at alpha = 0.05 with 15 degrees of freedom is 2.131. Do you reject H0: mu = 12.0?
Show answer
The standard error is 0.8 / sqrt(16) = 0.2, so t = (12.4 - 12.0) / 0.2 = 2.0 with 15 degrees of freedom. Since 2.0 is less than 2.131, you fail to reject H0 at alpha = 0.05 (the two-sided p-value is about 0.06). The data do not give enough evidence of a difference from 12.0 days. They also do not prove the mean is 12.0, so report the confidence interval as well.
Exercise
Cycle-time data from 12 orders are strongly right-skewed with two extreme values, and the target is 48 hours. Which test would you choose, and what do you check before relying on its result?
Show answer
With a small, strongly skewed sample, the t-test is risky, so use a rank-based test. Wilcoxon signed-rank is appropriate if the differences from 48 are roughly symmetric. Check that with a histogram of the differences. If they are clearly asymmetric, use the sign test or log-transform the data and test against log(48).