How to Conduct One Sample and Two Sample T-Test in Python

Here is how you can do a one sample or two independent samples equality of mean tests in Python. If you are not sure what a Ttest is, please read this short article.  Let’s get started with the codes

  • Import packages such as numpy and Scipy. Create three normally distributed random population using norm.rvs with certain mean and standard deviation. Var x1 has mean of 10,000 and standard deviation of 1000 and sample size of 50,  Var x2 has mean of 1000 and standard deviation of 1000, and finally Var x3 has the same distribution as

Slide1

  • Type the below magic command to print many statements on the same line. Then print x1, x2, and x3. All three are normally distributed arrays with the above defined mean and standard deviation. x

Slide2

  • Check out means of each of the variables.

Slide3

  • First T test- Check if the mean of the x1 is equal to zero. Null Hypothesis (H0) is that mean is equal to zero. Ha is that it is not equal to zero. Since absolute T stat is more than 2 and p value is less than 0.05, we would reject H0 and accept Ha. In other words, mean is not equal to zero.

Slide4

  • Second T Test- Check if x1 and x2 have the same mean. H0 is that they have the same mean. Ha is that they have different means. Since absolute T stat is more than 2 and p value is less than 0.05, we would reject H0 and accept Ha. In other words, means are not equal

Slide5

  • Third T Test. Check if x2 and x3 have the same mean. H0 is that they have the same mean. Ha is that they have different means. Since absolute T stat is less than 2 and p value is more than 0.05, we would accept H0 and reject Ha. In other words, means are statistically equal.

Slide6

Thanks for reading! Please share!