How to Perform One-sample T-Test in Python with Examples

In this article, we will discuss how to do a one-sample t-test in Python with some practical examples.

What is One-sample t-test for mean?

The one-sample t-test for the mean is used to test whether the population mean is equal to the pre-defined (standard/hypothetical) mean (μ) value when the population standard deviation is unknown and sample size is small.

What are the conditions required for conducting a one-sample t-test for mean?

Assumptions for One Sample Mean t-test

  • The parent population from which the sample is drawn should be normal.
  • The sample observations should be independent of each other i.e sample should be random.
  • The population standard deviation is unknown.

Hypothesis for the one sample t-test for mean

Let μ0 denote the hypothesized value for the mean and x̄ denotes the sample mean.

Null Hypothesis:

H0 : x̄= μ0  The population means is equal to hypothesized(standard) mean.

Alternative Hypothesis:
Three possible forms of alternative hypothesis are as follows:

  • Ha : x̄< μ0  Population mean is less than the hypothesized mean.It is called lower tail test (left-tailed test).
  • Ha : x̄>μ0 Population mean is greater than the hypothesized mean.It is called Upper tail test(right-tailed test).
  • Ha : x̄ ≠μ0 Population mean is not equal to hypothesized mean.It is called two tail test.

Formula for the test statistic of one sample t- test for mean is:

t-statistic


where :

x̄: observed sample mean

μ0: hypothesized population mean

n: sample size

s: sample standard deviation with n-1 degree of freedom

Summary for the one sample t-test for mean


Left-tailed Test
Left-tailed TestRight-tailed TestTwo-tailed Test
Null HypothesisH0 : x̄≥ μ0H0 : x̄≤ μ0H0 : x̄= μ0
Alternate HypothesisHa : x̄< μ0Ha : x̄> μ0Ha : x̄ ≠ μ0
Test Statistict= (x̄ – μ0 )/(s/ √ n)t= (x̄ – μ0 )/(s/ √ n)t= (x̄ – μ0 )/(s/ √ n)
Decision Rule: p-value approach (where α is level of significance)If p-value ≤α
then Reject H0
If p-value ≤α
then Reject H0
If p-value ≤α
then Reject H0
Decision Rule: Critical-value approachIf t ≤ -tα
then Reject H0
If t ≥ tα
then Reject H0
If t ≤ -tα or t ≥ tα then Reject H0


Function in Python for t-test

ttest_1samp()  function in Python from the stats package from SciPy library is used to perform a one-sample t-test for mean.

ttest_1samp(a, popmean, alternative='two-sided')


a: an array of given sample observations.

popmean: Expected value in null hypothesis.

alternative: {‘two-sided’, ‘less’, ‘greater’}, optional

Defines the alternative hypothesis. The following options are available (default is ‘two-sided’):

  • ‘two-sided’: the mean of the underlying distribution of the sample is different than the given population mean (popmean)
  • ‘less’: the mean of the underlying distribution of the sample is less than the given population mean (popmean)
  • ‘greater’: the mean of the underlying distribution of the sample is greater than the given population mean (popmean)

pip install scipy:

If you find yourself without the SciPy library installed, you can easily remedy that by running the following command in your Python environment:

pip install scipy

How to conduct one sample t-test for mean in Python?

We will calculate the test statistic with one-sample t-test for the mean.

Procedure to perform One Sample t-test for mean.

Step 1: Define the both Null Hypothesis and Alternate Hypothesis.

Step 2: Decide the level of significance α (i.e. alpha).

Step 3: Calculate the test statistic using the ttest_1samp() function from Python.

Step 4: Interpret the t-test results.

Step 5: Determine the rejection criteria for the given confidence level and interpret the results whether the test statistic lies in the rejection region or non-rejection region.

Let’s see practical examples that show how to do the t.test in Python.

Example of One Sample t-test in Python

A random sample of 10 students had the following I.Q.’ s :
80, 110, 130, 101, 108, 83, 90, 92, 100, 105. Does this data support the
assumption of a population mean I.Q. of 105 or it is less than that.

Assume that the I.Q. values of students follows a normal distribution. Test at 5% level of significance?

Solution: Given data :

sample size (n) = 10

hypothesized mean value (μ0)= 105

level of significance (α) = 0.05

confidence level = 0.95

Let’s solve this example by the step-by-step procedure.

Step 1: Define the Null Hypothesis and Alternate Hypothesis.

let μ be the mean IQ of students

Null Hypothesis: the mean IQ of students in the class is equal to 105.

H0 : μ = 105

Alternate Hypothesis: the mean IQ of students in the class is less than 105

Ha : μ < 105

Step 2: level of significance (α) = 0.05

Step 3: Calculate the test statistic using the ttest_1samp() function from Python.

# Import the stats library
import scipy.stats as stats

# Define given dataset
dataset = [80, 110, 130, 101, 108, 83, 90, 92, 100, 105]

#perform one sample t-test
stats.ttest_1samp(a=dataset, popmean=105,alternative='less')

Specify the alternative hypothesis as “less” because we are performing left tailed test. The results for the one-sample t-test are as follows.

TtestResult(statistic=np.float64(-1.0976558761566697), pvalue=np.float64(0.15042609252516528), df=np.int64(9))

Step 4: Interpret the t-test results.

How to interpret t-test results in Python?

Let’s see the interpretation of t-test results in Python.

statistic: It is the test statistic of the t-test. In our case test statistic = -1.0976558761566697

df: It is the degree of freedom for the t-test statistic. In our case df=9

p-value: This is the p-value corresponding to t-test statistic i.e. -1.0976558761566697 and degree of freedom i.e. 9. In our case, the p-value is 0.15042609252516528.

Conclusion:

Since the p-value[ 0.150426] is not less than the level of significance (α) = 0.05, we fail to reject the null hypothesis.

This means we do not have sufficient evidence to say that the mean IQ of the students in the class is different from 105.


Leave a Comment