How to compute a confidence interval for the population proportion (in Python, using SciPy)
Task
If we have a sample of qualitative data from a normally distributed population, then how do we compute a confidence interval for a population proportion?
Related tasks:
- How to compute a confidence interval for a mean difference (matched pairs)
- How to compute a confidence interval for a regression coefficient
- How to compute a confidence interval for a population mean
- How to compute a confidence interval for a single population variance
- How to compute a confidence interval for the difference between two means when both population variances are known
- How to compute a confidence interval for the difference between two means when population variances are unknown
- How to compute a confidence interval for the difference between two proportions
- How to compute a confidence interval for the expected value of a response variable
- How to compute a confidence interval for the ratio of two population variances
Solution
We’re going to use some fake data here for illustrative purposes, but you can replace our fake data with your real data in the code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Replace the next two lines of code with your real data
sample_size = 30
sample_proportion = .39
# Find the margin of error
from scipy import stats
import numpy as np
alpha = 0.05 # replace with your chosen alpha (here, a 95% confidence level)
moe = stats.norm.ppf(1-alpha/2) * np.sqrt(sample_proportion*(1-sample_proportion)/sample_size)
# Find the confidence interval
lower_bound = sample_proportion - moe
upper_bound = sample_proportion + moe
lower_bound, upper_bound
1
(0.21546413420702207, 0.564535865792978)
Our 95% confidence interval is $[0.2155, 0.5645]$.
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)