# How to compute a confidence interval for the population proportion (in Python, using SciPy)

See all solutions.

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?

## 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]$.