Newsletter

Sign up to our newsletter to receive the latest updates

Rajiv Gopinath

Z-Test: Definition, Formula, Applications & Python Implementation

Last updated:   April 05, 2025

Statistics and Data Science HubZ-TestHypothesis TestingStatistical AnalysisNormal DistributionTwo-Sample Z-TestOne-Sample Z-TestZ-Test FormulaMachine LearningData SciencePython Statistics
Z-Test: Definition, Formula, Applications & Python ImplementationZ-Test: Definition, Formula, Applications & Python Implementation

Z-Test

A Z-test is a statistical method used to analyze datasets that follow a normal distribution. It is primarily applied in hypothesis testing to determine whether the means of two large samples differ when the population variance is known. The test can be performed for a single sample, two samples, or proportions.

Depending on the dataset parameters, a Z-test can be classified into left-tailed, right-tailed, or two-tailed tests.

Table of Contents

  1. Introduction to Z-Test
  2. Applications of Z-Test
  3. Importance of Z-Test
  4. Z-Test Implementation in Python
  5. Conclusion

Introduction to Z-Test

A Z-test is employed to assess whether the means of two populations are statistically different, assuming that the data is normally distributed. The test involves formulating a null hypothesis and an alternative hypothesis, followed by calculating the Z-test statistic. The decision to reject or retain the null hypothesis depends on the Z critical value.

Definition of Z-Test

A Z-test is applicable when the population follows a normal distribution, the data points are independent, and the sample size is 30 or more. It helps determine if the means of two populations are equal when the population variance is known. If the Z-test statistic is significantly different from the critical value, the null hypothesis can be rejected.

Z-Test Formula

The Z-test formula is used to compare the Z-test statistic against the Z critical value to evaluate whether the means of two populations differ. The Z critical value divides the distribution into two regions:

  • Acceptance region (where the null hypothesis is not rejected)
  • Rejection region (where the null hypothesis is rejected)

If the test statistic falls within the rejection region, the null hypothesis is dismissed; otherwise, it is retained.

One-Sample Z-Test

A one-sample Z-test determines whether there is a statistically significant difference between the sample mean and the population mean, provided the population standard deviation is known. The formula for the Z-test statistic is given by:

Where:

  • Xˉ = Sample mean
  • μ = Population mean
  • σ\sigma = Population standard deviation
  • n = Sample size

One-Sample Z Test Algorithm

A one-sample Z test is conducted to determine whether a sample mean significantly differs from a known population mean. The test is based on the Z test statistic and follows different criteria depending on the hypothesis type.

Left-Tailed Test:

  • Null Hypothesis (H₀): μ=μ0\mu = \mu_0μ=μ0​
  • Alternative Hypothesis (H₁): μ<μ0\mu < \mu_0μ<μ0​
  • Decision Rule: If the Z test statistic is less than the critical Z value, reject the null hypothesis.

Right-Tailed Test:

  • Null Hypothesis (H₀): μ=μ0\mu = \mu_0μ=μ0​
  • Alternative Hypothesis (H₁): μ>μ0\mu > \mu_0μ>μ0​
  • Decision Rule: If the Z test statistic is greater than the critical Z value, reject the null hypothesis.

Two-Tailed Test:

  • Null Hypothesis (H₀): μ=μ0\mu = \mu_0μ=μ0​
  • Alternative Hypothesis (H₁): μ≠μ0\mu \neq \mu_0μ=μ0​
  • Decision Rule: If the absolute value of the Z test statistic is greater than the critical Z value, reject the null hypothesis.

 

Two-Sample Z Test

A two-sample Z test is used to compare the means of two independent samples to determine if there is a significant difference between them. The test statistic is calculated using the following formula:


Explanation of Variables:

  • Xˉ1, ​,Xˉ2​ = Sample means of the two groups
  • μ1,μ2 ​ = Population means of the two groups (typically assumed to be equal under H0​)
  • σ12,σ22 = Population variances of the two groups
  • n1,n2= Sample sizes of the two groups

Rejection Region for Null Hypothesis:
A diagram of a normal distribution

Description automatically generated

A Z Test for Proportions is employed to assess the difference between proportions. This test can be conducted for a single proportion or two proportions to determine statistical significance. Below are the respective formulas:

 

One-Proportion Z Test

A one-proportion Z test is utilized when comparing an observed proportion against a theoretical or expected proportion. The test statistic formula is as follows:

 

Explanation of Variables:

  • p^​ = Observed sample proportion
  • p0​ = Expected population proportion under the null hypothesis
  • n = Sample size


The null hypothesis is that the two proportions are the same while the alternative hypothesis is that they are not the same.

Two Proportion Z Test

In a Two-Proportion Z Test, the null hypothesis states that the two proportions are equal, whereas the alternative hypothesis suggests a difference between them. This test is used to compare two sample proportions and determine if their difference is statistically significant.

  • p1​^​ = Sample proportion from group 1
  • p2^ = Sample proportion from group 2
  • p^​ = Pooled sample proportion, calculated as:

 

  •  n1​ = Sample size of group 1
  • n2​ = Sample size of group 2
     

Comparison Between Z-Test and T-Test

CriteriaZ-TestT-Test
DefinitionA statistical test used to compare means when the population variance is known.A statistical test used to compare means when the population variance is unknown.
Sample SizeSuitable for samples with n ≥ 30.Used when the sample size is less than 30.
DistributionData follows a normal distribution.Data follows a Student's t-distribution.

Key Points About the Z-Test

  • The Z-test is a statistical method used for analyzing normally distributed data to determine whether there is a significant difference between the means of two datasets.
  • To apply a Z-test, the sample size should be at least 30, and the population variance must be known.
  • A one-sample Z-test evaluates whether the mean of a sample significantly differs from the population mean.
  • A two-sample Z-test assesses whether the means of two separate groups are statistically different from each other.

 

Applications of the Z-Test

Z-tests are widely utilized in various fields for hypothesis testing and mean comparisons, particularly when working with large sample sizes and a known population standard deviation. Some key applications include:

  1. Quality Control:
    • Manufacturing Assessment: Z-tests help determine whether the average product quality aligns with expected standards, identifying potential production inconsistencies.
  2. Biomedical Research:
    • Clinical Studies: Z-tests are used in medical trials to compare treatment and control groups, assessing whether a new drug or intervention has a statistically significant effect.
  3. Market Research:
    • Consumer Behavior Analysis: Businesses use Z-tests to compare consumer preferences and evaluate the effectiveness of marketing campaigns or product variations.

These examples illustrate the versatility of the Z-test in drawing meaningful conclusions. However, it is essential to consider its underlying assumptions—such as normality and known population standard deviation. If these conditions are not met, alternative methods like t-tests or non-parametric tests may be more appropriate.

 

Importance of the Z-Test

The Z-test plays a crucial role in statistical inference, allowing researchers to make data-driven conclusions about population parameters based on sample observations. Here’s why it is significant:

  1. Hypothesis Testing:
    • Z-tests serve as a fundamental tool in hypothesis testing, helping determine whether observed differences between sample means and population means—or between two sample means—are statistically meaningful.
  2. Use of Known Population Standard Deviation:
    • Unlike some other statistical tests, the Z-test is particularly beneficial when the population standard deviation is known, allowing for more precise calculations of standard error and facilitating the application of the Z-distribution.
  3. Effectiveness with Large Samples:
    • The accuracy of the Z-test increases with larger sample sizes, as the distribution of the sample mean approaches normality, making it well-suited for large-scale data analysis.

Implementation of Z-Test

 

# One-Sample Z-Test 
## Problem Statement:

A fitness tracker company claims that the average daily step count for its users is 30,000 steps. You decide to take a random sample of 10 users to check whether their average step count matches the company's claim. The step counts of the sample are:

[25, 30, 35, 28, 32, 31, 29, 27, 30, 34]

Assume the population standard deviation is known to be 5, and you want to test at a 5% significance level whether the sample mean is significantly different from the population mean of 30,000 steps.


import numpy as np
from scipy.stats import norm

# Sample data
sample_data = np.array([25, 30, 35, 28, 32, 31, 29, 27, 30, 34])

# Population parameters
population_mean = 30
population_stddev = 5

# Calculate sample mean and sample size
sample_mean = np.mean(sample_data)
n = len(sample_data)

# Compute Z-statistic
z_statistic = (sample_mean - population_mean) / (population_stddev / np.sqrt(n))

# Calculate p-value from Z-statistic
p_value = 2 * (1 - norm.cdf(abs(z_statistic)))

# Output
print(f"One-Sample Z-Test:")
print(f"Z-statistic: {z_statistic}")
print(f"P-value: {p_value}")

# Hypothesis test at 5% significance level
alpha = 0.05
if p_value < alpha:
    print("The sample mean is significantly different from the population mean.")
else:
    print("There is no significant difference between the sample mean and the population mean.")

One-Sample Z-Test:
Z-statistic: 0.06324555320336848
P-value: 0.9495709711511044
There is no significant difference between the sample mean and the population mean.
Explanation:

* Null Hypothesis (H₀): The average step count of the users is equal to 30,000.
* Alternative Hypothesis (H₁): The average step count of the users is different from 30,000.
* We conduct a one-sample Z-test, comparing the sample mean with the population mean.
* Based on the p-value, if it's less than 0.05 (alpha level), we reject the null hypothesis.


# Two-Sample Z-Test Example
## Problem Statement:

A company is testing two different marketing strategies to increase sales. Group 1 received Marketing Strategy A, while Group 2 received Marketing Strategy B. After the campaign, the number of items purchased by a random sample of customers in each group is recorded. The data are as follows:

* Group 1: [23, 25, 28, 30, 32]
* Group 2: [18, 20, 22, 25, 28]

Assume the population standard deviations are known to be 4 for Group 1 and 3 for Group 2. Test whether there is a significant difference in the means of the two groups at a 5% significance level.
import numpy as np
from scipy.stats import norm

# Sample data
group1 = np.array([23, 25, 28, 30, 32])
group2 = np.array([18, 20, 22, 25, 28])


# Population standard deviations
population_stddev1 = 4
population_stddev2 = 3

# Calculate means and sample sizes
mean1 = np.mean(group1)
mean2 = np.mean(group2)
n1 = len(group1)
n2 = len(group2)

# Compute Z-statistic
z_statistic = (mean1 - mean2) / np.sqrt((population_stddev1**2 / n1) + (population_stddev2**2 / n2))

# Calculate p-value from Z-statistic
p_value = 2 * (1 - norm.cdf(abs(z_statistic)))

# Output
print(f"Two-Sample Z-Test:")
print(f"Z-statistic: {z_statistic}")
print(f"P-value: {p_value}")

# Hypothesis test at 5% significance level
alpha = 0.05
if p_value < alpha:
    print("The means of the two groups are significantly different.")
else:
    print("There is no significant difference between the means of the two groups.")

Two-Sample Z-Test:

Z-statistic: 2.23606797749979

P-value: 0.0253473186774682

The means of the two groups are significantly different.

Explanation:

* Null Hypothesis (H₀): There is no difference between the means of the two groups (Strategy A and Strategy B).
* Alternative Hypothesis (H₁): There is a difference between the means of the two groups.
* A two-sample Z-test compares the means of the two groups to see if they are significantly different.
* We analyze the p-value, and if it's below 0.05, we conclude there is a significant difference between the two marketing strategies.

Google Colab Code

Conclusion

The Z-test serves as an essential statistical method for drawing conclusions about population parameters, particularly in cases involving large sample sizes and a known population standard deviation. Below are the key takeaways:

  1. High Accuracy with Known Population Standard Deviation:

    • The Z-test is especially effective when the population standard deviation is known, as it allows for precise calculation of the standard error, leading to more reliable statistical conclusions.
  2. Best Suited for Large Samples:

    • The test is most effective when applied to large sample sizes. As the sample size grows, the sample mean distribution becomes approximately normal, which aligns with the fundamental assumptions of the Z-test.
  3. Application in Mean Comparisons:

    • The Z-test is widely employed to compare means—either between a sample and a known population or between two separate samples. This makes it a valuable tool in fields such as manufacturing quality control, medical studies, finance, and consumer research.

Final Thoughts

The Z-test is a robust and reliable statistical approach for hypothesis testing and mean comparison. When the necessary conditions are met, it provides an accurate framework for analyzing data and making informed decisions. However, it is important to assess whether the assumptions of the test hold true before application, ensuring that the chosen method aligns with the dataset and research objectives.