Login
📚 Python for Introductory Statistics
Chapters ▾

11.3 Hypothesis Testing for Proportion P (z-test)

Watch demo video

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

To test a hypothesis of the form:

H 0 : p = p 0

Ha:p<p0 smaller

OR:

H 0 : p = p 0

Ha:p>p0 greater

OR:

H 0 : p = p 0

Ha:pp0 two-sided

use the following test-statistics (z-distribution)

z = p ^ P o P o ( 1 P o ) n

Hypothesis Testing for Proportions p (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.65 vs Ha:p0.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, 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']
x_df = pd.DataFrame({'vote':x})
x_df.value_counts()
Show expected output
vote
YES     11
NO       9
dtype: int64

Ho:p=.55 vs Ha: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. Do not reject the null.

Hypothesis Testing for Proportions p (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.60 vs Ha: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. 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 α 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 α 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.