Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

11.3 Hypothesis Testing for Proportion P (z-test)

Watch demo video

Suppose a random sample of nn (identical trials) is drawn from a population with probability of success pp. The sample proportion, p^\hat{p}, has approximately normal distribution with mean pp and standard deviation of p(1p)/n\sqrt{p(1-p)/n} for large nn.

To test a hypothesis of the form:

H 0 : p = p 0 H_0:p =p_0

Ha:p<p0H_a:p < p_0 smaller

OR:

H 0 : p = p 0 H_0:p =p_0

Ha:p>p0H_a:p > p_0 greater

OR:

H 0 : p = p 0 H_0:p =p_0

Ha:pp0H_a:p \ne p_0 two-sided

use the following test-statistics (z-distribution)

z = p ^ P o P o ( 1 P o ) n z=\frac{\hat p - P_o}{\sqrt{\frac{P_o(1-P_o)}{n}}}

Hypothesis Testing for Proportions pp (raw data given)

Hypothesis testing for the population proportion when raw data such as the sample size and number of success are given use the z_test_prop function below. This function needs the following arguments:null_prop the proportion from the null hypothesis, x, the number of "successes", n sample size, and alternative the direction of the alternative as "left", "right" or "two" for two sided test.

def z_test_prop(null_prop,x,n,alternative='two-sided'):  
    from scipy import stats
    p = x/n
    z = (p-null_prop)/(null_prop*(1-null_prop)/n)**.5
    if alternative == 'smaller':
        pv = stats.norm.cdf(z,0,1)
    elif alternative == 'greater':
        pv = 1-stats.norm.cdf(z,0,1)
    elif alternative == 'two-sided':
        if z< 0: 
            pv = 2*stats.norm.cdf(z,0,1)
        else: pv = 2*(1-stats.norm.cdf(z,0,1))
    else:
        print("altrnative option syntax error")
    return print("z test statistic = ", z, " p-value = ",pv)

Example: The president believes than 65% of voters approve his job performance. A pollster performs a hypothesis test to determine if the percentage is the same or different from 65%. She samples 100 voters and 61 reply that they approve the president's performance. Test the hypothesis at 1% level of significance.

Ho:p=0.65H_o:p=0.65 vs Ha:p0.65H_a:p \ne 0.65

Use the z_test_porp function above with arguments:0.65,61,100,'two'

z_test_prop(0.65,61,100,'two-sided')
Show expected output
z test statistic =  -0.8386278693775355  p-value =  0.4016781664697726

Interpretation: p-value > α=0.01\alpha=0.01, fail to reject the null

Example: The college president claims that the proportion of students registered to vote is more than 55%. A random sample of 20 students were asked if they are registered to vote. The results are listed below. Test the president's claim at 10% level of significance. NO, NO, YES, YES, NO, YES, NO, NO, YES, NO, YES, NO, NO, YES, YES, YES, YES, NO, YES, YES

Before we use the z_test_porp function, we need to find x (the number of success). We can use the `x_df.value_count' to find x.

x = ['NO', 'NO', 'YES', 'YES', 'NO', 'YES', 'NO', 'NO', 'YES', 'NO', 
             'YES', 'NO', 'NO', 'YES', 'YES', 'YES', 'YES', 'NO', 'YES', 'YES']
import pandas as pd
x_df = pd.DataFrame({'vote':x})
x_df.value_counts()
Show expected output
vote
YES     11
NO       9
dtype: int64

Ho:p=.55H_o:p=.55 vs Ha:p<0.55H_a:p<0.55

use the z_test_prop with arguments: .55, 11, 20, 'right'

z_test_prop(.55,11,20,'greater')
Show expected output
z test statistic =  0.0  p-value =  0.5

p-value = 0.5 is not < α=0.05\alpha=0.05. Do not reject the null.

Hypothesis Testing for Proportions pp (summary stats given)

Hypothesis testing when raw data such as sample size, proportion are given, use the z_test_stats function below with arguments: null_prop, p, n, alternative.

# z proportion test ,altenative can only be "left" or "right" or "two"

def z_test_prop_stats(null_prop,p,n,alternative):  
    from scipy import stats
    z = (p-null_prop)/(null_prop*(1-null_prop)/n)**.5
    if alternative == 'left':
        pv = stats.norm.cdf(z,0,1)
    elif alternative == 'right':
        pv = 1-stats.norm.cdf(z,0,1)
    elif alternative == 'two':
        if z< 0: 
            pv = 2*stats.norm.cdf(z,0,1)
        else: pv = 2*(1-stats.norm.cdf(z,0,1))
    else:
        print("altrnative option syntax error")
    return print("z test statistic = ", z, "and p-value = ",pv)

Example: The college president claims that the proportion of students registered to vote is more than 60%. A random sample of 50 students were asked if they are registered to vote. 62% were registered voters. Test the president's claim at 5% level of confidence.

Ho:p=0.60H_o:p=0.60 vs Ha:p>0.60H_a:p>0.60

Use the z_test_prop_stats function with arguments: null_prop=.60,p=.62,n=50,alternative='right'

z_test_prop_stats(.6,.62,50,'right')
Show expected output
z test statistic =  0.28867513459481314 and p-value =  0.38641499634222365

Interpretation: p-value is not less than α=0.05\alpha=0.05. Fail to reject the null hypothesis.

Example: The mayor of a city believes that more than 60% of the residents are fully vaccinated for Covid19. In a survey of 120 residents, 81 were fully vaccinated. Test the mayor's claim at α\alpha level of 0.05. Use the z_test_prop function below.

def z_test_prop(null_prop,x,n,alternative):  #altenative can only be "smaller" 
                                               # or "greater" or "two-sided"
    from scipy import stats
    p = x/n
    z = (p-null_prop)/(null_prop*(1-null_prop)/n)**.5
    if alternative == 'smaller':
        pv = stats.norm.cdf(z,0,1)
    elif alternative == 'greater':
        pv = 1-stats.norm.cdf(z,0,1)
    elif alternative == 'two-sided':
        if z< 0: 
            pv = 2*stats.norm.cdf(z,0,1)
        else: pv = 2*(1-stats.norm.cdf(z,0,1))
    else:
        print("altrnative option syntax error")
    return print("z test statistic = ", z, " p-value = ",pv)

z_test_prop(null_prop,x,n,alternative) with null_prop=.6,x=81,n=120,alternative='greater'

z_test_prop(.6,81,120,'greater')
Show expected output
z test statistic =  1.6770509831248437  p-value =  0.04676625634454645

Interpretation: Since the p-value 0.04676625634454645 is less than α\alpha of 0.05, reject the null hypothesis. There is enough evidence to support the mayor's claim.

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.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.