Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To Find A P-Value From A Z-Score In Python?
Obtaining a p-value from a z-score is a common statistical procedure. The z-score represents how many standard deviations a value is from the mean of a normal distribution. The p-value indicates the probability of observing a test statistic at least as extreme as the one observed, assuming the null hypothesis is true.
This article explains how to calculate p-values from z-scores in Python using the scipy.stats module.
What is a P-value?
A p-value is the probability that a test statistic will be at least as extreme as the observed one, assuming the null hypothesis is true. The null hypothesis typically states that there is no significant difference or effect.
In hypothesis testing, p-values help determine statistical significance. A small p-value (typically
What is a Z-score?
A z-score (or standard score) measures how many standard deviations a value is from the mean of a normal distribution. It's calculated as:
z = (x - ?) / ?
Where x is the value, ? is the mean, and ? is the standard deviation. Z-scores enable comparison of values from different normal distributions on a standardized scale.
One-tailed vs Two-tailed Tests
The method for calculating p-values depends on your hypothesis test type:
-
One-tailed (right): Use
norm.sf(z) -
One-tailed (left): Use
norm.cdf(z) -
Two-tailed: Use
2 * norm.sf(abs(z))
Method 1: Using norm.sf() for Right-tailed Tests
The norm.sf() function calculates the probability that a standard normal variable is greater than the given z-score.
from scipy.stats import norm
# Calculate p-value for z-score of 2.0 (right-tailed)
z_score = 2.0
p_value = norm.sf(z_score)
print(f"Z-score: {z_score}")
print(f"P-value (right-tailed): {p_value:.6f}")
Z-score: 2.0 P-value (right-tailed): 0.022750
Method 2: Using norm.cdf() for Left-tailed Tests
The norm.cdf() function calculates the probability that a standard normal variable is less than or equal to the given z-score.
from scipy.stats import norm
# Calculate p-value for z-score of -1.5 (left-tailed)
z_score = -1.5
p_value = norm.cdf(z_score)
print(f"Z-score: {z_score}")
print(f"P-value (left-tailed): {p_value:.6f}")
Z-score: -1.5 P-value (left-tailed): 0.066807
Method 3: Two-tailed Test
For two-tailed tests, multiply the one-tailed p-value by 2 since we consider both directions.
from scipy.stats import norm
# Calculate p-value for z-score of 1.96 (two-tailed)
z_score = 1.96
p_value_two_tailed = 2 * norm.sf(abs(z_score))
print(f"Z-score: {z_score}")
print(f"P-value (two-tailed): {p_value_two_tailed:.6f}")
# Compare with one-tailed
p_value_one_tailed = norm.sf(abs(z_score))
print(f"P-value (one-tailed): {p_value_one_tailed:.6f}")
Z-score: 1.96 P-value (two-tailed): 0.049958 P-value (one-tailed): 0.024979
Complete Example: Hypothesis Testing
Here's a practical example demonstrating the complete process of hypothesis testing with z-scores and p-values.
from scipy.stats import norm
def calculate_p_value(z_score, test_type="two-tailed"):
"""Calculate p-value from z-score based on test type"""
if test_type == "right":
return norm.sf(z_score)
elif test_type == "left":
return norm.cdf(z_score)
else: # two-tailed
return 2 * norm.sf(abs(z_score))
# Example: Testing if population mean differs from 100
z_scores = [1.64, 1.96, 2.58]
alpha = 0.05
print("Z-score\tP-value\t\tSignificant (?=0.05)")
print("-" * 40)
for z in z_scores:
p_val = calculate_p_value(z, "two-tailed")
significant = "Yes" if p_val < alpha else "No"
print(f"{z}\t{p_val:.6f}\t{significant}")
Z-score P-value Significant (?=0.05) ---------------------------------------- 1.64 0.101081 No 1.96 0.049958 Yes 2.58 0.009876 Yes
Conclusion
Use norm.sf() for right-tailed tests, norm.cdf() for left-tailed tests, and 2 * norm.sf(abs(z)) for two-tailed tests. The choice depends on your alternative hypothesis and whether you're testing for differences in one or both directions.
