Book contents

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 H0H_0, which represents the status quo or no effect, and an alternative hypothesis HaH_a, which represents a difference or effect of interest. For example, in a one-sample test of a mean:

Test Statistic and p-Value

A test statistic is a function of the data that summarises evidence against H0H_0. In a one-sample tt-test, t=X‾−μ0s/n,t = \frac{\bar{X} - \mu_0}{s/\sqrt{n}}, which follows a tt distribution with n−1n-1 degrees of freedom under H0H_0 if the data are normal.

The p-value is the probability, under H0H_0, of observing a test statistic as extreme as or more extreme than the one observed. A small p-value indicates evidence against H0H_0.

Errors and Significance Levels

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 n=25n=25 samples and compute x‾=5.4\bar{x}=5.4, s=1.2s=1.2. We test H0:μ=5H_0: \mu=5 vs Ha:μ>5H_a: \mu>5.

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 H0H_0 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 (XX) and control (YY), we test H0:μX=μYH_0: \mu_X = \mu_Y. If equal variances can be assumed, the pooled t-statistic is t=X‾−Y‾sp1/nX+1/nY,sp2=(nX−1)sX2+(nY−1)sY2nX+nY−2.t = \frac{\bar{X} - \bar{Y}}{s_p \sqrt{1/n_X + 1/n_Y}}, \quad s_p^2 = \frac{(n_X-1)s_X^2 + (n_Y-1)s_Y^2}{n_X + n_Y - 2}.

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 UU test (two independent samples) provide robust alternatives.

Exercises

Practice

  1. 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.

  2. 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.

  3. 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?

  4. Use R, Python or Julia to simulate a one-sample t-test under H0H_0 (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