# T-Test -Performing Hypothesis Testing With Python

## Introduction

Hi, Enthusiastic readers!

I have a Masters’s degree in Statistics and a year ago, I stepped into the field of data science. Writing a blog was on my bucket list for many days, and here I am making an attempt to share my knowledge.

The main focus of this article is to introduce hypothesis testing and illustrate with a few examples in Python. Whatever be the concept, its execution can be done easily with programming languages like Python. But, the most important part is drawing inference out of the output and it is highly recommended to know the math behind the executed code.

Hypothesis testing is important in statistics because it gives statistical evidence to show the validity of the study. The null hypothesis states that there is no statistical significance exists between sets of data which implies that the population parameter will be equal to a hypothesized value. Usually, We state the alternative hypothesis which we want to prove. For a null hypothesis H_{0} and its complementary alternative hypothesis H_{1}, there are 3 cases when the parametric value under H_{0} ≠ H_{1} or H_{0} < H_{1} or H_{0} > H_{1}.

Let’s consider a scenario where I have stated the hypothesis, a relevant test statistic, and the Python code for your understanding. I have coded the conclusion part too. Here, I have shared with you few cases instead of covering all. In this blog, I would like to give examples for one sample t-test, two-sample t-test, and paired t-test using Python.

**One sample t-test**

**Data:**

Systolic blood pressures of 14 patients are given below:

183, 152, 178, 157, 194, 163, 144, 114, 178, 152, 118, 158, 172, 138

Test, whether the population mean, is less than 165

**Hypothesis**

H_{0}: There is no significant mean difference in systolic blood pressure. i.e., μ = 165

H_{1}: The population mean is less than 165. i.e., μ < 165

**Test statistic **

Where,

x̄ is sample mean

μ is the population mean

s is sample standard deviation

n is the number of observations;

**Code**

sys_bp=[183, 152, 178, 157, 194, 163, 144, 114, 178, 152, 118, 158, 172, 138] mu=165 from scipy import stats t_value,p_value=stats.ttest_1samp(sys_bp,mu) one_tailed_p_value=float("{:.6f}".format(p_value/2)) # Since alternative hypothesis is one tailed, We need to divide the p value by 2. print('Test statistic is %f'%float("{:.6f}".format(t_value))) print('p-value for one tailed test is %f'%one_tailed_p_value) alpha = 0.05 if one_tailed_p_value<=alpha: print('Conclusion','n','Since p-value(=%f)'%p_value,'<','alpha(=%.2f)'%alpha,'''We reject the null hypothesis H0. So we conclude that there is no significant mean difference in systolic blood pressure. i.e., μ = 165 at %.2f level of significance'''%alpha) else: print('Conclusion','n','Since p-value(=%f)'%one_tailed_p_value,'>','alpha(=%.2f)'%alpha,' ''We do not reject the null hypothesis H0.

**Two sample t-test**

**Data**:

Compare the effectiveness of ammonium chloride and urea, on the grain yield of paddy, an experiment was conducted. The results are given below:

Ammonium
chloride (X |
13.4 | 10.9 | 11.2 | 11.8 | 14 | 15.3 | 14.2 | 12.6 | 17 | 16.2 | 16.5 | 15.7 |

Urea (X_{2}) |
12 | 11.7 | 10.7 | 11.2 | 14.8 | 14.4 | 13.9 | 13.7 | 16.9 | 16 | 15.6 | 16 |

**Hypothesis**

H_{0}: The effect of ammonium chloride and urea on grain yield of paddy are equal i.e., μ_{1} = μ_{2}

H_{1}: The effect of ammonium chloride and urea on grain yield of paddy is not equal i.e., μ_{1} ≠ μ_{2}

**Test** **statistic**

Where,

x̄_{1 }and x̄_{2} are sample means for x_{1} and x_{2 }respectively.

n_{1} and n_{2} are the numbers of observations in x_{1} and x_{2} respectively.

s_{1} and s_{2 } are the sample standard deviation for x_{1} and x_{2 }respectively.

**Code**

Ammonium_chloride=[13.4,10.9,11.2,11.8,14,15.3,14.2,12.6,17,16.2,16.5,15.7] Urea=[12,11.7,10.7,11.2,14.8,14.4,13.9,13.7,16.9,16,15.6,16]

from scipy import stats t_value,p_value=stats.ttest_ind(Ammonium_chloride,Urea) print('Test statistic is %f'%float("{:.6f}".format(t_value))) print('p-value for two tailed test is %f'%p_value) alpha = 0.05 if p_value<=alpha: print('Conclusion','n','Since p-value(=%f)'%p_value,'<','alpha(=%.2f)'%alpha,'''We reject the null hypothesis H0. So we conclude that the effect of ammonium chloride and urea on grain yield of paddy are not equal i.e., μ1 = μ2 at %.2f level of significance.'''%alpha) else: print('Conclusion','n','Since p-value(=%f)'%p_value,'>','alpha(=%.2f)'%alpha,'''We do not reject the null hypothesis H0.

**paired t-test**

**Data**:

Eleven schoolboys were given a test in Statistics. They were given a Month’s tuition and a second test were held at the end of it. Do the marks give evidence that the students have benefited from the exam coaching?

Marks in 1st test: 23 20 19 21 18 20 18 17 23 16 19

Marks in 2nd test: 24 19 22 18 20 22 20 20 23 20 18

**Hypothesis**

H_{0}: The students have not benefited from the tuition class. i.e., d = 0

H_{1}: The students have benefited from the tuition class. i.e., d < 0

Where, d = x-y; d is the difference between marks in the first test (say x) and marks in the second test (say y).

Test statistic

Where, n is the number of samples ‘s’ is sample standard deviation

**Code**

alpha = 0.05 first_test =[23, 20, 19, 21, 18, 20, 18, 17, 23, 16, 19] second_test=[24, 19, 22, 18, 20, 22, 20, 20, 23, 20, 18]

from scipy import stats t_value,p_value=stats.ttest_rel(first_test,second_test) one_tailed_p_value=float("{:.6f}".format(p_value/2)) print('Test statistic is %f'%float("{:.6f}".format(t_value))) print('p-value for one_tailed_test is %f'%one_tailed_p_value) alpha = 0.05 if one_tailed_p_value<=alpha: print('Conclusion','n','Since p-value(=%f)'%one_tailed_p_value,'<','alpha(=%.2f)'%alpha,'''We reject the null hypothesis H0. So we conclude that the students have benefited by the tuition class. i.e., d = 0 at %.2f level of significance.'''%alpha) else: print('Conclusion','n','Since p-value(=%f)'%one_tailed_p_value,'>','alpha(=%.2f)'%alpha,'''We do not reject the null hypothesis H0. So we conclude that the students have not benefited by the tuition class. i.e., d = 0 at %.2f level of significance.'''%alpha)