The T-Test › The One-Sample T-Test
Machine Learning from ScratchAll chapters 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:
- (population mean equals the hypothesized value)
- (two-sided), or / (one-sided)
where:
- = sample mean
- = hypothesized population mean
- = sample standard deviation (with Bessel's correction: divide by )
- = sample size
- = standard error of the mean
Distribution under : (t-distribution with degrees of freedom)
P-value calculation:
- Two-sided: where
- One-sided (greater):
- One-sided (less):
Decision rule: Reject if , or equivalently, if 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.
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 * seLet's walk through the calculation step by step:
Step 1: Sample Statistics
Step 2: Standard Error
Step 3: T-Statistic
Step 4: P-Value
With df = 14, we find the probability of observing under :
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.
