Login
📚 Python for Introductory Statistics
Chapters ▾

11.2 Hypothesis Testing for Mean μ (t-test)

Watch demo video

Suppose a random sample of size n is drawn from a population that is approximately normal with unknow mean μ and unknow standard deviation σ. To test a hypothesis about the mean μ of the form:

H 0 : μ = μ 0

Ha:μ<μ0 smaller

OR:

H 0 : μ = μ 0

Ha:μ>μ0 greater

OR:

H 0 : μ = μ 0

Ha:μμ0 two-sided

we use the following test-statistic (t-distribution)

t = x ¯ μ o s n

We use the t_test_mean function with arguments: null_mean = μo, sample mean = sample_mean, sample standard deviation = sample_std, sample size n and alternative:'smaller', 'greater' or 'two-sided' for two sided test.

# t test with alternative being 'smaller' or 'greater' or 'two-sided'
def t_test_mean(null_mean,sample_mean,sample_std,n,alternative='two-sided'):  
    from scipy import stats
    t = (sample_mean-null_mean)/(sample_std/n**.5)
    if alternative == 'smaller':
        pv = stats.t.cdf(t,n-1,0,1)
    elif alternative == 'greater':
        pv = 1-stats.t.cdf(t,n-1,0,1)
    elif alternative == 'two-sided':
        if t< 0: 
            pv = 2*stats.t.cdf(t,n-1,0,1)
        else: pv = 2*(1-stats.t.cdf(t,n-1,0,1))
    else:
        print("alternative option syntax error")
    return print("t test statistic = ", t, " p-value = ",pv)

Example: College administrators believe that the midterm exam score for Social Studies students is 70. A Social Studies instructor thinks the mean score is lower than 70. He samples ten students and obtains the scores 65; 65; 70; 67; 66; 63; 63; 68; 72; 71. Performs a hypothesis test using a 5% level of significance. The data are assumed to be from a normal distribution.

H o : μ = 70

H a : μ < 70

Use the t_test_mean function above. But first we have to find the sample mean and standard deviation from the data.

import numpy as np
x = [65, 65, 70, 67, 66, 63, 63, 68, 72, 71]
sample_mean = float(np.mean(x))
sample_std = float(np.std(x,ddof=1))
n = len(x)
print('sample mean is',sample_mean,'sample std is',sample_std,'n is',n)
Show expected output
sample mean is 67.0 sample std is 3.197221015541813 n is 10

Now call t_test_mean with arguments: 70, sample_mean=67.0, sample_std = 3.1972, n=10, 'left'. The variables sample_mean, sample_std and n are computed in the above code.

t_test_mean(70,67.0,3.1972,10,"smaller")   #you need to run the ttestmean function above first
Show expected output
t test statistic =  -2.9672316340876823  p-value =  0.007886584766274029

Interpretation: p-value < α=0.05, reject the null. Or compare the t test statistics with t critical stats.t.ppf(.05/2,n-1,0,1) = -2.26 (see code below)

from scipy import stats
stats.t.ppf(.05/2,10-1,0,1)
Show expected output
-2.262157162740992

Example: A college administrator claims that the average grade point for all students in the college is greater than 3.0. A random sample of 20 students has a sample mean of 3.4 with standard deviation of 0.8. Assume average grade points are normally distributed. Test the college administrator's claim at α of 0.10. Use the t_test_mean function below.

# t test with alternative being 'smaller' or 'greater' or 'two-sided'
def t_test_mean(null_mean,sample_mean,sample_std,n,alternative='two-sided'):  
    from scipy import stats
    t = (sample_mean-null_mean)/(sample_std/n**.5)
    if alternative == 'smaller':
        pv = stats.t.cdf(t,n-1,0,1)
    elif alternative == 'greater':
        pv = 1-stats.t.cdf(t,n-1,0,1)
    elif alternative == 'two-sided':
        if t< 0: 
            pv = 2*stats.t.cdf(t,n-1,0,1)
        else: pv = 2*(1-stats.t.cdf(t,n-1,0,1))
    else:
        print("alternative option syntax error")
    return print("t test statistic = ", t, " p-value = ",pv)

null_mean=3,sample_mean=3.4,sample_std=0.8,n=20,alternative='greater'

t_test_mean(3,3.4,0.8,20,'greater')
Show expected output
t test statistic =  2.2360679774997894  p-value =  0.01877027477426252

Interpretation: Since the p-value 0.01877027477426252 is less than α= 0.1, reject the null hypothesis. There is enough evidence to support the administrator's claim.

Proportions

The sample proportion is defined as the number of individuals in the sample with the specified characteristic, x, divided by the sample size, n.

p ^ = x n

The population proportion is defined as the number of individuals in the population with the specified characteristic, x, divided by the population size, N.

p = x N

Example: 20 students were asked if they were registered to vote. Their responses are listed below. Find the proportion of YES'.

NO, NO, YES, YES, NO, YES, NO, NO, YES, NO, YES, NO, NO, YES, YES, YES, YES, NO, YES, YES

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
x_df.value_counts(normalize=True)
Show expected output
vote
YES     0.55
NO      0.45
dtype: float64
proportion = 11/20  # here yes is counted as "success"
print("proportion is",proportion)
Show expected output
proportion is 0.55

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.