EZ Statistics

One-Sample Z-Test

Calculator

2. Select Columns & Options

Learn More

One-Sample Z-Test

Definition

One-Sample Z-Test is a statistical test used to determine whether a sample mean significantly differs from a known population mean when the population standard deviation is known. It's particularly useful for large samples and when working with known population parameters.

Formula

Test Statistic:

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

Where:

  • xˉ\bar x = sample mean
  • μ0\mu_0 = hypothesized population mean
  • σ\sigma = known population standard deviation
  • nn = sample size

Confidence Interval:

Two-sided: xˉ±zα/2σn\text{Two-sided: }\bar{x} \pm z_{\alpha/2} \cdot \frac{\sigma}{\sqrt{n}}One-sided: xˉ±zασn\text{One-sided: }\bar{x} \pm z_{\alpha} \cdot \frac{\sigma}{\sqrt{n}}
where zα/2z_{\alpha/2} and zαz_{\alpha} are the critical z-values

Key Assumptions

Known Population Standard Deviation: σ must be known
Random Sampling: Data should be randomly selected
Independence: Observations should be independent
Normality: Population should be normal or n>30n > 30

Practical Example

A manufacturing process is known to produce bolts with a population mean of 10mm and standard deviation of 0.2mm. To test if the process has shifted:

  • Sample of 100 bolts measured
  • Sample mean xˉ\bar x = 10.05mm
  • Known σ\sigma = 0.2mm
  • H0:μ=10H_0: \mu = 10mm
  • Ha:μ10H_a: \mu \neq 10mm

Calculating z-statistic:

z=10.05100.2/100=2.50z = \frac{10.05 - 10}{0.2/\sqrt{100}} = 2.50

Constructing confidence interval

xˉ±zα/2σn=10.05±1.960.2100=[10.011,10.089]\bar{x} \pm z_{\alpha/2}\frac{\sigma}{\sqrt{n}} = 10.05 \pm 1.96\frac{0.2}{\sqrt{100}} = [10.011, 10.089]

We are 95% confident that the true mean lies between 10.01110.011 and 10.08910.089 .

Effect Size

Cohen's d for one-sample z-test:

d=xˉμ0σd = \frac{|\bar{x} - \mu_0|}{\sigma}

Interpretation guidelines:

  • Small effect: d0.2|d| \approx 0.2
  • Medium effect: d0.5|d| \approx 0.5
  • Large effect: d0.8|d| \approx 0.8

Power Analysis

Required sample size for desired power (1-β):

n=((z1α/2+z1β)σμaμ0)2n = \left(\frac{(z_{1-\alpha/2} + z_{1-\beta})\sigma}{|\mu_a-\mu_0|}\right)^2

Where:

  • μa\mu_a = anticipated population mean
  • μ0\mu_0 = hypothesized mean
  • σ\sigma = population standard deviation
  • α\alpha = significance level
  • β\beta = probability of Type II error

Decision Rules

Reject H0H_0 if:

  • Two-sided test: z>zα/2|z| > z_{\alpha/2}
  • Left-tailed test: z<zαz < -z_{\alpha}
  • Right-tailed test: z>zαz > z_{\alpha}
  • Or if p-value<αp\text{-value} < \alpha

Reporting Results

Standard format:

"A one-sample z-test was conducted to compare [variable] to [hypothesized value]. Results indicated that [variable] (M = [mean], σ = [pop std dev]) was [not] significantly different from [hypothesized value], z = [z-value], p = [p-value], d = [Cohen's d]."

Code Examples

R
1library(tidyverse)
2set.seed(42)
3sample_data <- tibble(
4  value = rnorm(50, mean = 150, sd = 10) # 50 observations, mean=150, sd=10
5)
6
7# Known parameters
8pop_mean <- 145
9pop_sd <- 10  # Known population SD (this is key for z-test)
10
11# Basic summary statistics
12summary_stats <- sample_data %>%
13  summarise(
14    n = n(),
15    mean = mean(value),
16    sd = sd(value),
17    se = pop_sd/sqrt(n)  # Note: using pop_sd for z-test
18  )
19
20# Calculate z-statistic
21z_stat <- with(summary_stats, (mean - pop_mean)/(pop_sd/sqrt(n)))
22p_value <- 2 * pnorm(-abs(z_stat))  # two-sided
23
24# Effect size (Cohen's d)
25cohens_d <- with(summary_stats, (mean - pop_mean)/pop_sd)
26
27# Calculate confidence interval (95%)
28alpha <- 0.05
29ci <- summary_stats %>%
30  mutate(
31    lower = mean - qnorm(1-alpha/2) * se,
32    upper = mean + qnorm(1-alpha/2) * se
33  ) %>%
34  select(lower, upper)
35
36# Print results
37print(str_glue("Z-statistic: {round(z_stat, 3)}"))
38print(str_glue("P-value: {round(p_value, 4)}"))
39print(str_glue("Effect size (Cohen's d): {round(cohens_d, 3)}"))
40print(str_glue("95% CI: [{round(ci$lower, 2)}, {round(ci$upper, 2)}]"))
41
42# Visualize the data
43ggplot(sample_data, aes(x = value)) +
44  geom_histogram(aes(y = after_stat(density)), bins = 10, fill = "lightblue", color = "black") +
45  geom_density(color = "darkblue") +
46  geom_vline(xintercept = pop_mean, color = "red", linetype = "dashed") +
47  geom_vline(xintercept = summary_stats$mean, color = "blue") +
48  theme_minimal() +
49  labs(
50    title = "Sample Distribution with Population Mean",
51    subtitle = "Blue: Sample Mean, Red dashed: Population Mean",
52    x = "Value",
53    y = "Density"
54  )
Python
1import numpy as np
2from scipy import stats
3import matplotlib.pyplot as plt
4import seaborn as sns
5
6# Generate sample data
7np.random.seed(42)
8n = 50
9pop_mean = 145
10pop_sd = 10
11sample_data = np.random.normal(150, pop_sd, n)
12
13# Calculate z-statistic
14z_stat = (np.mean(sample_data) - pop_mean)/(pop_sd/np.sqrt(n))
15
16# Calculate p-value (two-sided)
17p_value = 2 * (1 - stats.norm.cdf(abs(z_stat)))
18
19# Calculate confidence interval
20alpha = 0.05
21ci = stats.norm.interval(1-alpha, 
22                        loc=np.mean(sample_data),
23                        scale=pop_sd/np.sqrt(n))
24
25# Calculate effect size
26cohens_d = abs(np.mean(sample_data) - pop_mean)/pop_sd
27
28# Visualization
29plt.figure(figsize=(10, 6))
30sns.histplot(sample_data, stat='density')
31plt.axvline(pop_mean, color='red', linestyle='--', label='Population Mean')
32plt.axvline(np.mean(sample_data), color='blue', label='Sample Mean')
33plt.legend()
34plt.show()
35
36# Print results
37print(f"Z-statistic: {z_stat:.4f}")
38print(f"P-value: {p_value:.4f}")
39print(f"95% CI: [{ci[0]:.2f}, {ci[1]:.2f}]")
40print(f"Cohen's d: {cohens_d:.4f}")

Related Calculators

One-Sample T-Test Calculator

Two-Sample Z-Test Calculator

Normal Distribution Calculator

Help us improve

Found an error or have a suggestion? Let us know!