Login
📚 Python for Introductory Statistics
Chapters ▾

10.3 Confidence Intervals for Proportion p (z-distribution)

Suppose a random sample of n (identical trials) is selected from a population with probability of success, p. The (1α)100 confidence interval is given by:

C=1α (Confidence level)

E=zα2p^q^n (Margin of Error)

p^E<P<p^+E (Confidence interval)

Confidence Interval and Margin of Error for p

Watch demo video

To find the confidence interval for the population proportion when summary statistics such as number of success in the sample and sample size, use the function conf_prop below with arguments: confidence level C, number of success and sample size n.

E = z α 2 p ^ q ^ n

p^=xn=p is the sample proportion

# confidence interval and margin of error given sample proportion p and n

def conf_prop(C,p,n):
  from scipy import stats
  eb1 = ((p*(1-p)/n)**.5)*stats.norm.ppf(C+(1-C)/2,0,1)
  return print(" Sample Proportion =",p,"\n Margin of Error = ", eb1,
               "\n Confidence Interval","(",p - eb1,",",p + eb1,")")

Or you can use the code below by substituting p = x/n for the sample proportion and n sample size.

error = ((p*(1-p)/n)**.5)*stats.norm.ppf(C+(1-C)/2,0,1)

Example: In a random sample of 35 students in a college, 20% are registered to vote. Construct the 90% confidence interval for the proportion of all students in the college registered to vote.

Use the conf_prop function with arguments: C=.9, p=.2 and n=35.

conf_prop(.90,.2,35)
Show expected output
 Sample Proportion = 0.2 
 Margin of Error =  0.11121240329789352 
 Confidence Interval ( 0.0887875967021065 , 0.3112124032978935 )

Example: In a random sample of 35 students in a college, 20 are registered to vote. Construct the 95% confidence interval for the proportion of all students in the college registered to vote.

Before we use the conf_prop we need to find the sample proportion p=x/n = 20/35

p = 20/35
p
Show expected output
0.5714285714285714

Now call the function with C =.95, p = 0.5714285714285714 and n = 35

conf_prop(.95,0.5714285714285714,35)
Show expected output
 Sample Proportion = 0.5714285714285714 
 Margin of Error =  0.16394819961335458 
 Confidence Interval ( 0.4074803718152168 , 0.735376771041926 )

Calculating the Sample Size n

Sample Size for Estimating Proportions, *p*

n = p ( 1 p ) * [ z α 2 E ] 2

To calculate the minimum sample size n for a given level of confidence C, estimated proportion p and margin of error E, use the function samp_pro below. p=.5 when no estimates are available.

def samp_pro(C,p,E):
  from scipy import stats
  sam = p*(1-p)*stats.norm.ppf(C+(1-C)/2,0,1)**2/E**2
  return print("sample size = ", sam)

Example: Suppose city officials want to determine the current percentage of residents vaccinated for Covid19. How many residents should the city officials survey in order to be 90% confident that the estimated (sample) proportion is within three percentage points of the true population proportion of residents who are vaccinated for Covid19?

use the samp_pro function above with arguments: C = 0.90, p = 0.5 (none is given), E = 0.03.

samp_pro(.90,.5,.03)
Show expected output
sample size =  751.5398483598369

Interpretation: 752 (always round up) residents are needed.

Sample Size for Estimating Mean, μ

n = [ σ × z α 2 E ] 2

To calculate the minimum sample size n for a given level of confidence C, standard devation σ as s and margin of error E, use the function samp_mean below.

def samp_mean(C,s,E):
  from scipy import stats
  sam = (s*stats.norm.ppf(C+(1-C)/2,0,1)/E)**2
  return print("sample size = ", sam)
samp_mean(.95,15,3)
Show expected output
sample size =  96.03647051735315

Example: A 4th grade elementary school teacher wants to estimate the mean IQ score of all 4h grade students. How many 4th grade students must be randomly selected if she wants a 90% confidence that the sample mean is within 5 IQ scores of the population mean? (Use σ=15)

use the samp_mean function above with arguments: C = 0.90, s = 15 and E = 3.

samp_mean(0.9,15,3)
Show expected output
sample size =  67.63858635238533

Interpretation: 68 (round up) students are needed in the random sample.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.