One Sample Z-Test Calculator

One-Sample Z-Test Calculator

Choose data input type, then enter values to calculate the z-score and p-value.

Z-Score:

P-Value:

Understanding the One-Sample Z-Test

The one-sample z-test is used to determine whether the mean of a sample significantly differs from a known population mean. This test is especially useful when the population standard deviation is known and the sample size is large (typically \( n > 30 \)).

Key Components of the One-Sample Z-Test

  • Sample Mean (\( \bar{X} \)): The average value of the sample data.
  • Population Mean (\( \mu \)): The population mean we are testing against.
  • Population Standard Deviation (\( \sigma \)): The standard deviation of the population (known).
  • Z-Score: A standardized value derived from the sample data to compare the sample mean to the population mean.

Formula for the One-Sample Z-Test

The z-score for a one-sample z-test is calculated as:

\[ z = \frac{\bar{X} - \mu}{\sigma / \sqrt{n}} \]

where:

  • \( \bar{X} \): Sample mean
  • \( \mu \): Population mean
  • \( \sigma \): Population standard deviation
  • \( n \): Sample size

Programmatically Calculating the One-Sample Z-Test

Below are examples for calculating the one-sample z-test in JavaScript, Python, and R, including left-tailed, right-tailed, and two-tailed versions.

1. Using JavaScript (with jStat)

In JavaScript, the jStat library can be used to calculate the one-sample z-test:

// Define inputs
const sampleMean = 20;  // Sample mean
const populationMean = 23;  // Population mean
const populationStdDev = 4;  // Population standard deviation
const sampleSize = 30;  // Sample size

// Calculate z-score
const zScore = (sampleMean - populationMean) / (populationStdDev / Math.sqrt(sampleSize));
const alpha = 0.05;  // Significance level

// Calculate p-values
const pValueTwoTailed = 2 * (1 - jStat.normal.cdf(Math.abs(zScore), 0, 1));
const pValueRightTailed = 1 - jStat.normal.cdf(zScore, 0, 1);
const pValueLeftTailed = jStat.normal.cdf(zScore, 0, 1);

console.log(`Two-tailed p-value: ${pValueTwoTailed.toFixed(5)}`);
console.log(`Right-tailed p-value: ${pValueRightTailed.toFixed(5)}`);
console.log(`Left-tailed p-value: ${pValueLeftTailed.toFixed(5)}`);

2. Using Python (with SciPy)

In Python, the SciPy library can be used to calculate the one-sample z-test:

from scipy.stats import norm

# Define inputs
sample_mean = 20  # Sample mean
population_mean = 23  # Population mean
population_stddev = 4  # Population standard deviation
sample_size = 30  # Sample size

# Calculate z-score
z_score = (sample_mean - population_mean) / (population_stddev / (sample_size ** 0.5))
alpha = 0.05  # Significance level

# Calculate p-values
p_value_two_tailed = 2 * (1 - norm.cdf(abs(z_score)))
p_value_right_tailed = 1 - norm.cdf(z_score)
p_value_left_tailed = norm.cdf(z_score)

print(f"Two-tailed p-value: {p_value_two_tailed:.5f}")
print(f"Right-tailed p-value: {p_value_right_tailed:.5f}")
print(f"Left-tailed p-value: {p_value_left_tailed:.5f}")

3. Using R

In R, the stats package includes functions to calculate the one-sample z-test:

# Define inputs
sample_mean <- 20  # Sample mean
population_mean <- 23  # Population mean
population_stddev <- 4  # Population standard deviation
sample_size <- 30  # Sample size

# Calculate z-score
z_score <- (sample_mean - population_mean) / (population_stddev / sqrt(sample_size))
alpha <- 0.05  # Significance level

# Calculate p-values
p_value_two_tailed <- 2 * (1 - pnorm(abs(z_score)))
p_value_right_tailed <- 1 - pnorm(z_score)
p_value_left_tailed <- pnorm(z_score)

cat("Two-tailed p-value:", round(p_value_two_tailed, 5), "\n")
cat("Right-tailed p-value:", round(p_value_right_tailed, 5), "\n")
cat("Left-tailed p-value:", round(p_value_left_tailed, 5), "\n")

Further Reading

Implementations

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.