The Z-Test › The Z-Test Formula: A Deep Dive

Michael BrenndoerferUpdated January 12, 20265 min read
Machine Learning from ScratchAll chapters
Part 2 of 9Hypothesis Testing
Chapter 14The Z-Test › The Z-Test Formula: A Deep Dive
Reading section 3 of 11 · The Z-Test Formula: A Deep Dive · 5 minSection 3 of 11 · 5 min

Book contents

Section 3 of 11 · 5 min read

The Z-Test › The Z-Test Formula: A Deep DiveOne section · Sections in this chapter3 / 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 Core Formula

The z-test statistic is:

z=xˉ−μ0σ/nz = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}

This formula looks simple, but each component has deep meaning. Let's understand why this particular combination of quantities gives us exactly what we need for hypothesis testing.

Step 1: The Numerator Measures the Discrepancy

xˉ−μ0\bar{x} - \mu_0

This is the difference between what you observed (sample mean xˉ\bar{x}) and what the null hypothesis claims (population mean μ0\mu_0).

Example: If the null hypothesis claims the population mean is 100 and you observe a sample mean of 95, the numerator is 95−100=−595 - 100 = -5.

A difference of 5 could be huge or trivial depending on context. If you're measuring heights in millimeters, 5 mm is negligible. If you're measuring earthquake magnitudes, 5 points is catastrophic. We need to standardize.

Step 2: The Denominator Provides Context

σn\frac{\sigma}{\sqrt{n}}

This is the standard error, and it answers: "How much do sample means typically vary from sample to sample?"

The standard error depends on two factors:

Factor 1: Population variability (σ)

More variable populations produce more variable sample means. If individual observations vary a lot, the averages of groups of observations will also vary.

Factor 2: Sample size (n)

Larger samples produce more stable estimates. The n\sqrt{n} in the denominator reflects a fundamental principle: averaging reduces noise. When you average n independent observations, random fluctuations tend to cancel out.

Out[3]:
Visualization
Two normal distributions showing different spreads due to different σ values.
Effect of population variability (σ) on the sampling distribution. When individual observations are more variable, sample means are also more variable. Both distributions are for n=25.
Two normal distributions showing different spreads due to different sample sizes.
Effect of sample size (n) on the sampling distribution. Larger samples produce more precise estimates of the population mean. Both distributions have σ=20.

Step 3: The Ratio Gives Standardized Evidence

When you divide the observed discrepancy by the standard error:

z=xˉ−μ0σ/nz = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}

You get a standardized score that tells you: "How many standard errors away from the null hypothesis is my observation?"

  • z=1z = 1 means your sample mean is 1 standard error away from μ0\mu_0
  • z=2z = 2 means your sample mean is 2 standard errors away
  • z=−1.5z = -1.5 means your sample mean is 1.5 standard errors below μ0\mu_0

The beauty of standardization is that it puts all tests on the same scale. Whether you're measuring heights, weights, temperatures, or dollars, a z-score of 2 always has the same interpretation: your observation is 2 standard errors from what the null predicts.

Why the Standard Normal Distribution?

Under the null hypothesis H0:μ=μ0H_0: \mu = \mu_0, if either:

  1. The population is normally distributed, OR
  2. The sample size is large enough for the Central Limit Theorem to apply

Then the z-statistic follows a standard normal distribution N(0,1)N(0, 1).

This is the key insight that makes z-tests work. We know everything about the standard normal distribution. We can calculate exact probabilities for any z-value. So once we compute z, we can immediately determine how surprising our result is under the null hypothesis.

Out[4]:
Visualization
Standard normal distribution with shaded regions showing common probability areas.
The standard normal distribution and its relationship to z-values. Under the null hypothesis, the z-statistic follows this distribution. About 68% of z-values fall within ±1, 95% within ±2, and 99.7% within ±3. Extreme z-values (beyond ±2 or so) provide evidence against the null hypothesis.