The T-Test › The One-Sample T-Test

Michael BrenndoerferJanuary 3, 20265 min read
Machine Learning from ScratchAll chapters
Part 2 of 9Hypothesis Testing
Chapter 15The T-Test › The One-Sample T-Test
Reading section 4 of 11 · The One-Sample T-Test · 5 minSection 4 of 11 · 5 min

Book contents

Section 4 of 11 · 5 min read

The T-Test › The One-Sample T-TestOne section · Sections in this chapter4 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The one-sample t-test compares a sample mean to a hypothesized population value when the population variance is unknown. This is the simplest and most direct application of the t-distribution.

The Complete Procedure

Hypotheses:

  • H0:μ=μ0H_0: \mu = \mu_0 (population mean equals the hypothesized value)
  • Ha:μ≠μ0H_a: \mu \neq \mu_0 (two-sided), or μ>μ0\mu > \mu_0 / μ<μ0\mu < \mu_0 (one-sided)

Test statistic:

t=xˉ−μ0s/nt = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}

where:

  • xˉ\bar{x} = sample mean
  • μ0\mu_0 = hypothesized population mean
  • ss = sample standard deviation (with Bessel's correction: divide by n−1n-1)
  • nn = sample size
  • s/ns / \sqrt{n} = standard error of the mean

Distribution under H0H_0: t∼tn−1t \sim t_{n-1} (t-distribution with n−1n-1 degrees of freedom)

P-value calculation:

  • Two-sided: p=2×P(T>∣t∣)p = 2 \times P(T > |t|) where T∼tn−1T \sim t_{n-1}
  • One-sided (greater): p=P(T>t)p = P(T > t)
  • One-sided (less): p=P(T<t)p = P(T < t)

Decision rule: Reject H0H_0 if p<αp < \alpha, or equivalently, if ∣t∣>tα/2,n−1|t| > t_{\alpha/2, n-1} for two-sided tests.

Worked Example: Quality Control

A coffee shop claims their large drinks contain 16 ounces. A quality inspector measures 15 randomly selected drinks to verify this claim.

In[4]:
Code
import numpy as np
from scipy import stats

# Measurements from 15 randomly selected large drinks (in ounces)
drinks = [
    15.2,
    15.8,
    16.1,
    15.4,
    15.9,
    15.3,
    15.7,
    16.0,
    15.5,
    15.2,
    15.8,
    15.6,
    15.9,
    15.4,
    16.2,
]

# Hypothesized mean (the claimed amount)
mu_0 = 16.0

# Step 1: Calculate sample statistics
n = len(drinks)
x_bar = np.mean(drinks)
s = np.std(drinks, ddof=1)  # ddof=1 for Bessel's correction

# Step 2: Calculate standard error
se = s / np.sqrt(n)

# Step 3: Calculate t-statistic
t_stat = (x_bar - mu_0) / se

# Step 4: Determine degrees of freedom
df = n - 1

# Step 5: Calculate p-value (two-sided)
p_value = 2 * stats.t.sf(abs(t_stat), df=df)

# Step 6: Get critical value for comparison
t_crit = stats.t.ppf(0.975, df=df)

# Step 7: Calculate 95% confidence interval
ci_lower = x_bar - t_crit * se
ci_upper = x_bar + t_crit * se

Let's walk through the calculation step by step:

Step 1: Sample Statistics

xˉ=115∑i=115xi=15.2+15.8+⋯+16.215=15.667 oz\bar{x} = \frac{1}{15}\sum_{i=1}^{15} x_i = \frac{15.2 + 15.8 + \cdots + 16.2}{15} = 15.667 \text{ oz} s=∑i=115(xi−xˉ)215−1=0.309 ozs = \sqrt{\frac{\sum_{i=1}^{15}(x_i - \bar{x})^2}{15-1}} = 0.309 \text{ oz}

Step 2: Standard Error

SE=sn=0.30915=0.080 ozSE = \frac{s}{\sqrt{n}} = \frac{0.309}{\sqrt{15}} = 0.080 \text{ oz}

Step 3: T-Statistic

t=xˉ−μ0SE=15.667−16.00.080=−0.3330.080=−4.18t = \frac{\bar{x} - \mu_0}{SE} = \frac{15.667 - 16.0}{0.080} = \frac{-0.333}{0.080} = -4.18

Step 4: P-Value

With df = 14, we find the probability of observing ∣t∣≥4.18|t| \geq 4.18 under H0H_0:

p=2×P(T14>4.18)=0.0009p = 2 \times P(T_{14} > 4.18) = 0.0009
Out[5]:
Console
One-Sample T-Test: Coffee Shop Drink Size
=======================================================

Data: 15 drink measurements (ounces)
Hypothesized mean: mu_0 = 16.0 oz

Step-by-step calculation:
-------------------------------------------------------
1. Sample mean:       x_bar = 15.667 oz
2. Sample std dev:    s = 0.324 oz
3. Standard error:    SE = s/sqrt(n) = 0.324/sqrt(15) = 0.084 oz
4. T-statistic:       t = (x_bar - mu_0)/SE = (15.667 - 16.0)/0.084 = -3.980
5. Degrees of freedom: df = n - 1 = 15 - 1 = 14
6. P-value (two-sided): p = 0.0014

Decision (alpha = 0.05):
-------------------------------------------------------
Critical value: t_crit = +/-2.145
|t| = 3.980 > 2.145 = t_crit

95% Confidence Interval: [15.487, 15.846] oz
Note: CI does not contain 16.0, consistent with rejecting H_0

Conclusion:
-------------------------------------------------------
Reject H_0. There is significant evidence (p = 0.0009) that
the true mean drink size differs from the claimed 16 oz.
The drinks appear to be underfilled by about 0.33 oz on average.
Out[6]:
Visualization
T-distribution with observed test statistic and rejection regions highlighted.
Visualization of the one-sample t-test for the coffee shop example. The t-distribution shows the sampling distribution under the null hypothesis. The observed t-statistic of -4.18 falls far into the left tail, well beyond the critical value, leading to rejection of H_0.