STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 9
Hypothesis Testing
Hypothesis testing provides a formal mechanism for deciding whether data provide evidence against a default claim (the null hypothesis). It is widely used in science to test whether a treatment has an effect, whether a coin is fair or whether a difference between groups is significant.
Null and Alternative Hypotheses
We begin with a null hypothesis , which represents the status quo or no effect, and an alternative hypothesis , which represents a difference or effect of interest. For example, in a one-sample test of a mean:
(the population mean equals )
(two-sided), or (one-sided)
Test Statistic and p-Value
A test statistic is a function of the data that summarises evidence against . In a one-sample -test, which follows a distribution with degrees of freedom under if the data are normal.
The p-value is the probability, under , of observing a test statistic as extreme as or more extreme than the one observed. A small p-value indicates evidence against .
Errors and Significance Levels
A Type I error occurs when we reject a true . Its probability is the significance level (commonly 0.05).
A Type II error occurs when we fail to reject a false . The probability of not making a Type II error is the power of the test (1–).
There is a trade-off between and power: lowering decreases the probability of Type I error but can decrease power if the sample size is fixed.
One-Sample t-Test Example
Suppose we want to test whether the mean concentration of a pollutant exceeds the regulatory limit of 5 units. We collect samples and compute , . We test vs .
Worked example
t-test in R and Python.
n <- 25
xbar <- 5.4
s <- 1.2
tstat <- (xbar - 5) / (s / sqrt(n))
p_val <- pt(tstat, df = n - 1, lower.tail = FALSE)
import scipy.stats as stats
n = 25; xbar = 5.4; s = 1.2
tstat = (xbar - 5) / (s / (n**0.5))
p_val = 1 - stats.t.cdf(tstat, df=n-1)
If the p-value is below 0.05, we reject and conclude that the mean exceeds 5. Otherwise, we do not have sufficient evidence.
Two-Sample t-Test
When comparing two independent samples, e.g. treatment () and control (), we test . If equal variances can be assumed, the pooled t-statistic is
If variances differ, use Welch’s t-test, which does not pool variances and adjusts degrees of freedom.
Non-Parametric Alternatives
When data are not normal or contain outliers, non-parametric tests such as the Wilcoxon signed-rank test (paired samples) or Mann–Whitney test (two independent samples) provide robust alternatives.
Exercises
Practice
A manufacturer claims that the mean lifetime of a battery is 500 hours. A sample of 16 batteries has mean 480 hours and standard deviation 50 hours. Test the claim at the 5% significance level. State the null and alternative hypotheses, compute the test statistic and p-value, and interpret the result.
Two competing pain relievers are tested on 20 patients each. Treatment A yields a mean reduction in pain score of 3.2 (SD 1.0) while Treatment B yields 2.8 (SD 0.8). Test at the 1% level whether the treatments differ. Conduct both equal-variance and unequal-variance t-tests.
Explain the difference between a p-value and the significance level. Why doesn’t a small p-value prove that the alternative hypothesis is true?
Use R, Python or Julia to simulate a one-sample t-test under (mean 0, variance 1). Generate 10 000 samples of size 30 and compute p-values. Estimate the proportion of p-values less than 0.05. Does this match the nominal Type I error rate?
References
Lehmann EL, Romano JP. Testing Statistical Hypotheses. 3rd ed. Springer; 2005.
Schervish MJ. Theory of Statistics. Springer; 1995.
Gibbons JD, Chakraborti S. Nonparametric Statistical Inference. 5th ed. Boca Raton: CRC Press; 2011.