A one proportion z-test is used to compare an observed proportion to a theoretical one.
This test uses the following null hypotheses:
- H0: p = p0 (population proportion is equal to hypothesized proportion p0)
The alternative hypothesis can be either two-tailed, left-tailed, or right-tailed:
- H1 (two-tailed): p ≠ p0 (population proportion is not equal to some hypothesized value p0)
- H1 (left-tailed): p < p0 (population proportion is less than some hypothesized value p0)
- H1 (right-tailed): p > p0 (population proportion is greater than some hypothesized value p0)
The test statistic is calculated as:
z = (p-p0) / √
- p: observed sample proportion
- p0: hypothesized population proportion
- n: sample size
If the p-value that corresponds to the test statistic z is less than your chosen significance level (common choices are 0.10, 0.05, and 0.01) then you can reject the null hypothesis.
One Proportion Z-Test in Python
To perform a one proportion z-test in Python, we can use the proportions_ztest() function from the statsmodels library, which uses the following syntax:
proportions_ztest(count, nobs, value=None, alternative=’two-sided’)
- count: The number of successes
- nobs: The number of trials
- value: The hypothesized population proportion
- alternative: The alternative hypothesis
This function returns a z test-statistic and a corresponding p-value.
The following example shows how to use this function to perform a one proportion z-test in Python.
Example: One Proportion Z-Test in Python
Suppose we want to know whether or not the proportion of residents in a certain county who support a certain law is equal to 60%. To test this, we collect the following data on a random sample:
- p0: hypothesized population proportion = 0.60
- x: residents who support law: 64
- n: sample size = 100
The following code shows how to use the proportions_ztest function to perform a one sample z-test:
#import proportions_ztest function from statsmodels.stats.proportion import proportions_ztest #perform one proportion z-test proportions_ztest(count=60, nobs=100, value=0.64) (-0.8164965809277268, 0.41421617824252466)
From the output we can see that the z test-statistic is -0.8165 and the corresponding p-value is 0.4142. Since this value is not less than α = 0.05, we fail to reject the null hypothesis. We do not have sufficient evidence to say that the proportion of residents who support the law is different from 0.60.