# How to compute a confidence interval for the population proportion

## Description

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?

## Using SciPy, in Python

View this solution alone.

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

See a problem? Tell us or edit the source.

## Solution, in R

View this solution alone.

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
# Replace the next two lines of code with your real data
sample_size = 30
sample_proportion = 0.39

# Find the margin of error
alpha <- 0.05       # replace with your chosen alpha (here, a 95% confidence level)
moe <- qnorm(1-alpha/2, 0, 1) * sqrt(sample_proportion*(1-sample_proportion)/sample_size)

# Find the confidence interval
upper_bound <- sample_proportion + moe
lower_bound <- sample_proportion - moe
lower_bound
upper_bound

1
2
3
4
5
 0.2154641

 0.5645359


Our 95% confidence interval is $[0.2155, 0.5645]$.