There is a discrepancy in your sample. The challenge lies in determining whether the discrepancy exists or it is simply one of those statistical fluctuations between any two samples chosen from a population. There are five steps to determine if this discrepancy is real through a Z-test using one formula.
But, there is one caveat in almost all tutorials that do not mention that this test can only give you a statistically sound outcome under three particular circumstances. If you conduct this test without following the required prerequisites, it will give you an apparently accurate result with a printed value and be entirely false. This article explains the formula, types, methodology, and Python code for a Z-test.
What Will I Learn?
What Is a Z-Test?
The Z-test is a statistical test used to determine the difference between the sample mean (proportion) and a known population statistic measured in terms of number of standard deviations. The Z-test will provide the Z-statistic which will tell us whether our observation is unusual assuming that the null hypothesis is true.
Ironically, this is one of the most simple statistical tests to conduct but one of the easiest to misuse. Big sample size? Known standard deviation? Simple formula. Except, that makes it impossible for use with actual data sets.
Reject the null hypothesis if your Z-statistic is large enough; otherwise do not reject the null hypothesis.
The Z-Test Formula
For a one-sample Z-test comparing a sample mean to a population mean:
Where:
- x̄ = your sample mean
- μ = the population mean you’re testing against
- σ = the population standard deviation (must be known — not estimated)
- n = your sample size
In reality, the actual formula itself is far less relevant than the actual meaning behind it. Here, you’re taking the observed gap between the sample and the population and dividing it by the standard error (σ/√n), which tends to get smaller when the sample size increases.
Quick intuition: A logistics firm has data that indicate historically, it takes them 4.2 days to deliver goods, with a population standard deviation of 0.8 days (across thousands of shipments over five years). Following a test of its new routing technology on 64 shipments, the average delivery time decreased to 4.0 days. Does this mean anything?
A Z-score of −2.0 clears the ±1.96 threshold for α = 0.05. The routing improvement isn’t noise.
Assumptions of the Z-Test
There are three assumptions that must be met before conducting this test. Should even one of the assumptions be breached, then you are guaranteed an inaccurate result without being aware of it.
- Approximate normality or a large sample size. This means that the population should be normally distributed, or the sample should be large enough for the CLT to kick in. By the Central Limit Theorem, a sampling distribution approaches normality as the sample size grows, regardless of the shape of the original population. The widely accepted convention is that of a sample size greater than 30.
- Known population standard deviation. This is the assumption that is often overlooked and breached. Should you find yourself using the standard deviation of your sample, rather than that of the population (σ), then you should know that you have done so wrongly. Conduct a T-test instead.
- Independent observations. This assumption is often violated when data is collected sequentially or repeatedly.
How to Perform a Z-Test (Step-by-Step)
Five steps. Every Z-test, every type.
Step 1: State your hypotheses. Your null hypothesis (H₀) says that there’s no effect; e.g., “the mean delivery time is equal to 4.2 days.” Your alternative hypothesis (H₁) says what you’re looking for, e.g., “the mean delivery time is less than 4.2 days.” State both hypotheses before you do anything with the data.
Step 2: Choose a significance level (α). In almost all cases, you choose the common value α = 0.05, meaning that you’re willing to risk a 5% probability of wrongly rejecting the true null hypothesis (this is known as a Type I error). In matters related to health or safety, you use α = 0.01.
Step 3: Calculate the Z statistic. Put your numbers into the formula. It’s as simple as that — no trickery.
Step 4: Determine your critical value. If your test is two-tailed and α = 0.05, then the critical value is ±1.96. If your test is one-tailed and α = 0.05, then it’s ±1.645. If your test is two-tailed and α = 0.01, then it’s ±2.576.
Step 5: State your decision clearly. “Since |Z| = 2.0 > 1.96, we reject H₀ at the 5% significance level. There is sufficient evidence that the routing algorithm reduces average delivery time.” Write the conclusion in the language of the problem — not in formula symbols.
How to Read the Z-Table
The Z-table gives the cumulative probability for any Z-score — specifically, the area under the standard normal curve to the left of that value.
Here’s how to look up Z = 1.96:
- Find row 1.9 on the left side (the tenths digit)
- Find column 0.06 across the top (the hundredths digit)
- The cell gives 0.9750 — meaning 97.5% of the distribution falls below Z = 1.96
For a two-tailed test at α = 0.05, you need 2.5% in each tail. So you’re looking for the Z-score where cumulative probability = 0.975. That’s 1.96.
Key critical values — memorize these:
| Significance Level | Test Type | Critical Z-Value |
| α = 0.10 | Two-tailed | ±1.645 |
| α = 0.05 | Two-tailed | ±1.96 |
| α = 0.05 | One-tailed | ±1.645 |
| α = 0.01 | Two-tailed | ±2.576 |
| α = 0.001 | Two-tailed | ±3.29 |
Types of Z-Test
1. One-Sample Z-Test
Applicable when you want to test one sample mean against a known population mean with a known σ.
Example: Industry statistics reveal that the mean time for resolving a customer support ticket is 6.5 hours, and the population standard deviation is known to be 1.2 hours. Now, if an IT firm wants to test its new triaging system, it tests it using a random sample of 49 support tickets, and finds out the mean resolution time is 6.1 hours.
H₀: μ = 6.5 | H₁: μ < 6.5 (one-tailed — we’re testing for reduction) α = 0.05 | Critical Z = −1.645
|Z| = 2.34 > 1.645 → reject H₀. The new triage system produces a statistically significant reduction in resolution time.
Python implementation:
python
import numpy as np
from statsmodels.stats.weightstats import ztest
# Simulate 49 resolution times with sample mean ~6.1
np.random.seed(42)
resolution_times = np.random.normal(loc=6.1, scale=1.2, size=49)
population_mean = 6.5
# One-tailed test: alternative='smaller' tests for a reduction
z_stat, p_value = ztest(resolution_times, value=population_mean, alternative='smaller')
print(f"Z-Statistic: {z_stat:.4f}")
print(f"P-Value (one-tailed): {p_value:.4f}")
alpha = 0.05
if p_value < alpha:
print("Reject H₀: The triage system significantly reduces resolution time.")
else:
print("Fail to reject H₀: No significant reduction detected.")
Reading your output: The Z-statistic tells you the direction and magnitude of the difference. The p-value tells you how likely that Z-score (or more extreme) would be if H₀ were true. A p-value of 0.009 means: if the triage system had zero effect, you’d only see a result this extreme 0.9% of the time.
2. Two-Sample Z-Test
Used when comparing the means of two independent groups, and both population standard deviations are known.
Formula:
Worked example: Two factories produce the same steel rod. Historical records show Factory A has a population std dev of σ₁ = 0.3mm; Factory B has σ₂ = 0.4mm. Engineers sample 50 rods from Factory A (mean length: 100.3mm) and 60 rods from Factory B (mean length: 100.6mm). Is there a significant difference at α = 0.05?
H₀: μ₁ − μ₂ = 0 | H₁: μ₁ − μ₂ ≠ 0 (two-tailed)
|Z| = 4.57 >> 1.96 → reject H₀. The difference in mean rod length between factories is statistically significant.
python
import numpy as np
import scipy.stats as stats
x1_mean, x2_mean = 100.3, 100.6
sigma1, sigma2 = 0.3, 0.4
n1, n2 = 50, 60
z_score = (x1_mean - x2_mean) / np.sqrt((sigma1**2 / n1) + (sigma2**2 / n2))
p_value = 2 * (1 - stats.norm.cdf(abs(z_score))) # two-tailed
print(f"Z-Score: {z_score:.4f}")
print(f"P-Value: {p_value:.4f}")
if abs(z_score) > 1.96:
print("Reject H₀: Significant difference in mean rod length.")
else:
print("Fail to reject H₀: No significant difference detected.")
3. Z-Test for Proportions
The short answer is: this is the Z-test most practitioners actually need — and the one most statistics tutorials leave out entirely.
When you’re comparing proportions (conversion rates, defect rates, survey response rates) rather than means, the formula changes because proportions follow a binomial distribution, not a normal one. But with large enough samples, the CLT applies, and the Z-test becomes valid.
One-sample proportion formula:
Two-sample proportion formula:
Worked example — A/B test: An e-commerce team tests two checkout page designs. Version A (control, n = 1,200) converts 5.2% of visitors (62 conversions). Version B (variant, n = 1,200) converts 6.4% (77 conversions). Is the lift real?
H₀: p₁ = p₂ | H₁: p₁ ≠ p₂ | α = 0.05
|Z| = 1.78 < 1.96 → fail to reject H₀. The observed lift is not statistically significant. You’d need a larger sample — or a larger real effect — to be confident.
python
from statsmodels.stats.proportion import proportions_ztest
import numpy as np
# Conversion counts and sample sizes
conversions = np.array([62, 77])
nobs = np.array([1200, 1200])
z_stat, p_value = proportions_ztest(conversions, nobs)
print(f"Z-Statistic: {z_stat:.4f}")
print(f"P-Value: {p_value:.4f}")
if p_value < 0.05:
print("Reject H₀: Conversion rate difference is statistically significant.")
else:
print("Fail to reject H₀: Insufficient evidence to conclude a real difference.")
The result of “fail to reject H₀” is itself a useful finding here — it tells you not to ship the variant yet. Calling a lift significant when it isn’t costs real money.
One-Tailed vs. Two-Tailed Z-Test
That much should be obvious until it isn’t. Your hypothesis decides which critical value you will have, and thus whether you will or will not reject the null hypothesis.
Two-tailed test: In case you are interested in whether or not there is a difference between the sample statistic and the population parameter irrespective of whether it is positive or negative. Use this test by default. The rejection region will include both sides of the curve. Critical value at α = 0.05: ±1.96.
One-tailed test: In case you are interested whether the difference between your sample statistic and population parameter is positive or negative. This test can only be used when your hypothesis involves the direction of an effect, based on theoretical reasoning or previous studies’ results, in such a way that the opposite cannot influence your decision. Critical value at α = 0.05: ±1.645.
The principle is clear: in case of doubt use a two-tailed test. Selecting the type of test just to have a smaller critical value is p-hacking.
Z-Test vs. T-Test — Which Should You Use?
Honestly, this is where most people get stuck — and the confusion is understandable, because the tests are structurally almost identical.
The core difference is one condition: the Z-test requires a known population standard deviation. The T-test does not — it estimates variance from the sample and uses a t-distribution, which has heavier tails than the normal distribution to account for that extra uncertainty.
| Z-Test | T-Test | |
| σ known? | Yes — required | No — estimated from sample |
| Sample size | n > 30 recommended | Works for any n |
| Distribution used | Standard normal (Z) | t-distribution |
| Best for | Large samples with known σ | Small samples OR unknown σ |
The edge case that trips people up: n = 500, σ unknown. A large sample doesn’t save you. You still don’t have σ; you’re estimating it from your data. Use a T-test. With large n, the T-test converges toward the Z-test anyway — the practical difference in your output is tiny — but the conceptual choice still matters, especially when your work is reviewed or published.
Right. The decision is straightforward once you ask one question first: “Do I actually know the population standard deviation?” If yes, Z-test. If no — even if your sample is enormous — T-test.
Real-World Applications of the Z-Test
A/B testing in product analytics. Growth and product teams use two-sample proportion Z-tests to determine whether a new feature, design change, or pricing experiment produces a statistically significant change in conversion rate or click-through rate. The proportion Z-test is the statistical engine behind most A/B testing tools.
Clinical trials. Researchers use proportion Z-tests to compare response rates between treatment and control groups — for example, the share of patients achieving a target LDL reduction. When outcomes are continuous and σ is known from prior trials, the one-sample or two-sample Z-test for means is used instead.
Manufacturing and Six Sigma. Quality engineers test whether a production batch’s mean measurement (diameter, weight, voltage) falls within acceptable deviation from a specification. High-volume manufacturing processes often have well-characterized historical variance, making them natural candidates for the Z-test. A process running at 3-sigma or 6-sigma capability is evaluated partly through Z-score thresholds.
Finance and portfolio analysis. Portfolio managers compare a fund’s average monthly return against a benchmark index mean. When returns over a long history provide a reliable σ estimate, the Z-test is used to assess whether outperformance is statistically significant or within the range of random variation.
Common Mistakes When Using the Z-Test
Most statistics tutorials teach you how to compute the Z-test. The part they skip — and it matters more — is knowing when your Z-test result is wrong before you even interpret it.
Mistake 1: Using sample standard deviation where σ belongs.
Wrong answer.
You don’t know σ. You have s — your sample’s standard deviation. Many practitioners substitute s for σ because it’s the number their data produces. But that substitution invalidates the Z-test. You’ve introduced estimation error into the denominator without accounting for it. The fix: use a T-test, which adjusts for this uncertainty via degrees of freedom. For large samples, the practical difference in the p-value is negligible; the conceptual correctness is not.
Mistake 2: Misreading the p-value.
A p-value of 0.03 does not mean there is a 97% probability that your alternative hypothesis is true. It means: if the null hypothesis were true, you’d observe a result at least this extreme 3% of the time by chance alone. The p-value says nothing about the probability that H₀ is false. Decisions have been made — and papers have been published — based on this misreading.
Mistake 3: Treating statistical significance as practical significance.
A two-sample Z-test on n = 500,000 users will flag a 0.001 percentage point difference in conversion rate as statistically significant at p < 0.001. That doesn’t mean you should act on it. Always pair your Z-test result with an effect size — Cohen’s d for means, or absolute and relative lift for proportions. Statistical significance tells you the signal is real; effect size tells you whether it’s worth caring about.
Frequently Asked Questions
Q1. What is the difference between a Z-test and a Z-score?
Ans. A Z score standardizes an individual observation compared to the distribution from which it came using Z = (x − μ) / σ. On the other hand, a Z statistic (employed in Z test) standardizes the sample means or proportions against the sampling distribution. A Z score indicates where a particular datum lies, while a Z test applies the Z statistic to establish the statistical significance of a sample finding.
Q2. When is the Z-test not appropriate?
Ans. The following scenarios do not permit the usage of the Z test: (1) When σ is unknown and sample standard deviation is being used – use T test; (2) When n is small and it is not known whether the population is normally distributed or not – use T test; (3) When observations are not independent – the whole parametric approach must be reconsidered. In scenarios (1) and (2), T-test is nearly always the best substitute.
Q3. What Z-score is considered statistically significant?
Ans. It all depends on the value of your significance level. If you have a significance level of α = 0.05, any Z score outside of ±1.96 will be statistically significant. The cutoff for significance when α = 0.01 is ±2.576. If your significance level is set at α = 0.001, the requirement is ±3.29. There is no such thing as an excellent Z-score.
Q4. Can I use a Z-test for small samples?
Ans. This statement holds only when the distribution of the population is normally distributed, and where sigma is known. In most cases, if n < 30, then the t-test should be applied. It is for this reason that the t-distribution is preferred over the Z-distribution since the normality assumption used in the latter is no longer valid in small samples.
Q5. How do I interpret the p-value in a Z-test?
Ans. The p-value refers to the likelihood of obtaining a test statistic that is as extreme as, or more extreme than, the obtained one, assuming that the null hypothesis is true. The p-value is not the probability of H₀ being true or false. When p < α, we reject the null hypothesis. When p ≥ α, we do not reject the null hypothesis, which does not imply that the null hypothesis is accepted.
Closing
The Z-test only applies under a very specific set of circumstances. Specifically, when your sample size is large, your population standard deviation is known, and your observations are independent. Under any other circumstances, the test will still be calculated.
It will still provide you with output. And that output will be incorrect.
Remember this… as statistical procedures rarely fail noisily. Instead, they fail with the p-value to back it up. Understand your conditions. Test for them. Then run your test – then confirm you were supposed to