One Proportion Z-Test Calculator

One-Proportion Z-Test Calculator

Choose data input type, then enter values to calculate the t-score, degrees of freedom, and p-value.

Z-Score:

P-Value:

Understanding the One-Proportion Z-Test

The one-proportion z-test is used to determine whether the proportion of a sample significantly differs from a hypothesized population proportion. This test is commonly used in situations where we want to know if the observed proportion in a sample matches a known or assumed proportion in the population.

Key Components of the One-Proportion Z-Test

  • Sample Proportion (\( \hat{p} \)): The proportion observed in the sample data.
  • Hypothesized Proportion (\( p_0 \)): The assumed proportion in the population we are testing against.
  • Z-Score: A standardized value derived from the sample data to compare the sample proportion to the hypothesized proportion.

Formula for the One-Proportion Z-Test

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

\[ z = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0 (1 - p_0)}{n}}} \]

where:

  • \( \hat{p} \): Sample proportion (e.g., number of successes divided by the sample size)
  • \( p_0 \): Hypothesized population proportion
  • \( n \): Sample size

Programmatically Calculating the One-Proportion Z-Test

Below are examples for calculating the one-proportion 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-proportion z-test:

// Define inputs
const sampleProportion = 0.45;  // Sample proportion
const hypothesizedProportion = 0.5;  // Hypothesized proportion
const sampleSize = 100;  // Sample size

// Calculate z-score
const zScore = (sampleProportion - hypothesizedProportion) / Math.sqrt((hypothesizedProportion * (1 - hypothesizedProportion)) / 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-proportion z-test:

from scipy.stats import norm

# Define inputs
sample_proportion = 0.45  # Sample proportion
hypothesized_proportion = 0.5  # Hypothesized proportion
sample_size = 100  # Sample size

# Calculate z-score
z_score = (sample_proportion - hypothesized_proportion) / ((hypothesized_proportion * (1 - hypothesized_proportion)) / 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-proportion z-test:

# Define inputs
sample_proportion <- 0.45  # Sample proportion
hypothesized_proportion <- 0.5  # Hypothesized proportion
sample_size <- 100  # Sample size

# Calculate z-score
z_score <- (sample_proportion - hypothesized_proportion) / sqrt((hypothesized_proportion * (1 - hypothesized_proportion)) / 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.