The Z-Test › One-Sample Z-Test: Complete Worked Example
Machine Learning from ScratchAll chapters The Z-Test › One-Sample Z-Test: Complete Worked ExampleOne section · Sections in this chapter5 / 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
Let's work through a complete example with every calculation shown explicitly.
The Problem
A coffee machine manufacturer claims their machines dispense exactly 200 mL per cup, with a known standard deviation of 5 mL (established through extensive quality testing). A consumer group tests 49 cups from a randomly selected machine and finds a mean of 197.5 mL. Is there evidence that this machine dispenses less than the claimed amount?
Step 1: State the Hypotheses
Since we're specifically asking whether the machine dispenses less than claimed, this is a one-sided test:
- (The machine dispenses the claimed amount)
- (The machine dispenses less than claimed)
Step 2: Identify the Known Values
- Sample mean: mL
- Hypothesized mean: mL
- Population standard deviation: mL
- Sample size:
- Significance level:
Step 3: Calculate the Standard Error
This tells us that sample means of 49 cups typically vary by about 0.714 mL from the true population mean.
Step 4: Calculate the Z-Statistic
The sample mean is 3.50 standard errors below the hypothesized value. This is quite extreme!
Step 5: Calculate the P-Value
For a left-tailed test:
Step 6: Make a Decision
Since , we reject the null hypothesis. There is strong evidence that the machine dispenses less than 200 mL per cup.
import numpy as np
from scipy import stats
# Given values
x_bar = 197.5 # Sample mean
mu_0 = 200 # Hypothesized mean
sigma = 5 # Population standard deviation
n = 49 # Sample size
alpha = 0.05 # Significance level
# Step 3: Standard error
se = sigma / np.sqrt(n)
# Step 4: Z-statistic
z = (x_bar - mu_0) / se
# Step 5: P-value (left-tailed)
p_value = stats.norm.cdf(z)
# Critical value for left-tailed test
z_critical = stats.norm.ppf(alpha)
print("One-Sample Z-Test: Coffee Machine")
print("=" * 50)
print(f"Sample mean: x̄ = {x_bar} mL")
print(f"Hypothesized mean: μ₀ = {mu_0} mL")
print(f"Population std: σ = {sigma} mL")
print(f"Sample size: n = {n}")
print()
print(f"Standard error: SE = {se:.4f} mL")
print(f"Z-statistic: z = {z:.3f}")
print(f"P-value (left-tailed): p = {p_value:.6f}")
print(f"Critical value: z_α = {z_critical:.3f}")
print()
print(f"Decision: {'Reject H₀' if p_value < alpha else 'Fail to reject H₀'}")
print()
print("Interpretation: There is strong evidence that the machine")
print("dispenses less than the claimed 200 mL per cup.")One-Sample Z-Test: Coffee Machine ================================================== Sample mean: x̄ = 197.5 mL Hypothesized mean: μ₀ = 200 mL Population std: σ = 5 mL Sample size: n = 49 Standard error: SE = 0.7143 mL Z-statistic: z = -3.500 P-value (left-tailed): p = 0.000233 Critical value: z_α = -1.645 Decision: Reject H₀ Interpretation: There is strong evidence that the machine dispenses less than the claimed 200 mL per cup.

Confidence Interval Interpretation
We can also construct a 95% confidence interval for the true mean:
# 95% CI for the population mean
z_crit_ci = stats.norm.ppf(0.975) # For two-sided CI
margin = z_crit_ci * se
ci_lower = x_bar - margin
ci_upper = x_bar + margin
print(f"95% Confidence Interval: [{ci_lower:.2f}, {ci_upper:.2f}] mL")
print()
print("The claimed value of 200 mL is outside this interval,")
print("which is consistent with rejecting the null hypothesis.")95% Confidence Interval: [196.10, 198.90] mL The claimed value of 200 mL is outside this interval, which is consistent with rejecting the null hypothesis.