One-Sample Z-Test
Calculator
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:
Where:
- = sample mean
- = hypothesized population mean
- = known population standard deviation
- = sample size
Confidence Interval:
Key Assumptions
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 = 10.05mm
- Known = 0.2mm
- mm
- mm
Calculating z-statistic:
Constructing confidence interval
We are 95% confident that the true mean lies between and .
Effect Size
Cohen's d for one-sample z-test:
Interpretation guidelines:
- Small effect:
- Medium effect:
- Large effect:
Power Analysis
Required sample size for desired power (1-β):
Where:
- = anticipated population mean
- = hypothesized mean
- = population standard deviation
- = significance level
- = probability of Type II error
Decision Rules
Reject if:
- Two-sided test:
- Left-tailed test:
- Right-tailed test:
- Or if
Reporting Results
Standard format:
Code Examples
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 )
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
Z Table
Normal Distribution Calculator
Help us improve
Found an error or have a suggestion? Let us know!