How to do a hypothesis test for a population proportion (in Python, using SciPy)
Task
When we have qualitative data, we’re often interested in performing inference on population proportions. That is, the proportion (between 0.0 and 1.0) of the population that is in a certain category with respect to the qualitative variables. Given a sample proportion, $\bar{p}$, how can we test whether the population proportion is equal to, greater than, or less than some hypothesized value?
Related tasks:
- How to compute a confidence interval for the population proportion
- How to do a hypothesis test for a mean difference (matched pairs)
- How to do a hypothesis test for the difference between means when both population variances are known
- How to do a hypothesis test for the difference between two proportions
- How to do a hypothesis test for the mean with known standard deviation
- How to do a hypothesis test for the ratio of two population variances
- How to do a hypothesis test of a coefficient’s significance
- How to do a one-sided hypothesis test for two sample means
- How to do a two-sided hypothesis test for a sample mean
- How to do a two-sided hypothesis test for two sample means
Solution
We’re going to use fake data here for illustrative purposes, but you can replace our fake data with your real data in the code below.
Let’s say that we’ve hypothesized that about one-third of Bostonians are unhappy with the Red Sox’ performance. To test this hypothesis, we surveyed 460 Bostonians and found that 76 of them were unhappy with the Red Sox’ performance.
We summarize this situation with the following variables. We will do a test with a Type I error rate of $\alpha=0.05$.
1
2
3
4
n = 460 # Number of respondents in sample
x = 76 # Number of respondents in chosen subset
sample_prop = x/n # Proportion of sample in chosen subset
population_prop = 1/3 # Hypothesized population proportion
Two-tailed test
A two-tailed test is for the null hypothesis $H_0: p = \frac13$.
It can be done by directly computing the test statistic and $p$-value using
tools from SciPy’s stats
module.
1
2
3
4
5
import numpy as np
from scipy import stats
test_stat = ( (sample_prop - population_prop) /
np.sqrt(population_prop*(1 - population_prop)/n) )
stats.norm.sf(abs(test_stat))*2 # p-value
1
2.0284218907806657e-14
The $p$-value is less than $\alpha$, so we can reject the null hypothesis. The proportion of Bostonians unhappy with Red Sox performance is different from $\frac13$.
Right-tailed test
A right-tailed test is for the null hypothesis $H_0: p \le \frac13$. Most of the code is the same as above, but the $p$-value is computed differently for a one-sided test. We repeat the re-used code to make it easy to copy and paste.
1
2
3
4
5
import numpy as np
from scipy import stats
test_stat = ( (sample_prop - population_prop) /
np.sqrt(population_prop*(1 - population_prop)/n) )
stats.norm.sf(test_stat)
1
0.9999999999999899
The $p$-value is greater than $\alpha$, so we cannot reject the null hypothesis. We continue to assume that the proportion of Bostonians unhappy with Red Sox performance is less than or equal to $\frac13$.
Left-tailed test
A left-tailed test is for the null hypothesis $H_0: p\ge \frac13$. Most of the code is the same as above, but the $p$-value is computed differently yet again. We repeat the re-used code to make it easy to copy and paste.
1
2
3
4
5
import numpy as np
from scipy import stats
test_stat = ( (sample_prop - population_prop) /
np.sqrt(population_prop*(1 - population_prop)/n) )
stats.norm.sf(abs(test_stat))
1
1.0142109453903328e-14
The $p$-value is less than $\alpha$, so we can reject the null hypothesis. The proportion of Bostonians unhappy with Red Sox performance is less than $\frac13$.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)