The Z-Test › One-Sample Z-Test: Complete Worked Example

Michael BrenndoerferUpdated January 12, 20264 min read
Machine Learning from ScratchAll chapters
Part 2 of 9Hypothesis Testing
Chapter 14The Z-Test › One-Sample Z-Test: Complete Worked Example
Reading section 5 of 11 · One-Sample Z-Test: Complete Worked Example · 4 minSection 5 of 11 · 4 min

Book contents

Section 5 of 11 · 4 min read

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:

  • H0:μ=200H_0: \mu = 200 (The machine dispenses the claimed amount)
  • H1:μ<200H_1: \mu < 200 (The machine dispenses less than claimed)

Step 2: Identify the Known Values

  • Sample mean: xˉ=197.5\bar{x} = 197.5 mL
  • Hypothesized mean: μ0=200\mu_0 = 200 mL
  • Population standard deviation: σ=5\sigma = 5 mL
  • Sample size: n=49n = 49
  • Significance level: α=0.05\alpha = 0.05

Step 3: Calculate the Standard Error

SE=σn=549=57=0.714 mLSE = \frac{\sigma}{\sqrt{n}} = \frac{5}{\sqrt{49}} = \frac{5}{7} = 0.714 \text{ mL}

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

z=xˉ−μ0SE=197.5−2000.714=−2.50.714=−3.50z = \frac{\bar{x} - \mu_0}{SE} = \frac{197.5 - 200}{0.714} = \frac{-2.5}{0.714} = -3.50

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:

p-value=P(Z<−3.50)=0.00023\text{p-value} = P(Z < -3.50) = 0.00023

Step 6: Make a Decision

Since p=0.00023<α=0.05p = 0.00023 < \alpha = 0.05, we reject the null hypothesis. There is strong evidence that the machine dispenses less than 200 mL per cup.

In[6]:
Code
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.")
Out[6]:
Console
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.
Out[7]:
Visualization
Standard normal distribution with rejection region and observed z-statistic marked.
Visualization of the coffee machine z-test. The observed z-statistic of -3.50 falls deep in the left tail, well beyond the critical value of -1.645. This provides strong evidence against the null hypothesis.

Confidence Interval Interpretation

We can also construct a 95% confidence interval for the true mean:

In[8]:
Code
# 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.")
Out[8]:
Console
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.