How to Perform Hypothesis Testing with SciPy

How to Perform Hypothesis Testing with SciPy

SciPy, a Python library for scientific and technical computing, is notoriously handy for performing statistical tests. It particularly excels at hypothesis testing, helping assess whether a data population differs significantly from a null hypothesis. SciPy offers tools like the t-test, chi-square test, and ANOVA for analyzing means, variances, proportions, and correlations in datasets.

This article illustrates how SciPy can be used to perform hypothesis testing.

Step-by-Step Process

We start importing the stats module in SciPy, necessary to perform statistical tests, as well as numpy which we’ll use to generate some synthetic data.

from scipy import stats
import numpy as np

To start simple, let’s consider applying a one-sample t-test to determine whether the mean of a sample of current flight prices differs from a historical average value of $350. Since we do not have real airline pricing data at hand, we’ll randomly generate a data sample of 50 flight prices normally distributed around $380.

historical_mean = 350
current_prices = np.random.normal(380, 30, 50)

Performing the intended test with SciPy is as simple as calling the stats.ttest_1samp() method, which returns the specific statistic to this test along with the p-value:

t_stat, p_value = stats.ttest_1samp(current_prices, historical_mean)

We print both outputs and interpret the p-value to conclude:

print("One-sample t-test results:")
print(f"t-statistic: {t_stat:.3f}")
print(f"p-value: {p_value:.3f}")
print(f"Conclusion: {'Reject' if p_value < 0.05 else 'Fail to reject'} null hypothesis")
print("Interpretation: Current prices are" + 
      " significantly different from the historical average\n" if p_value < 0.05 
      else " not significantly different from historical average\n")

Possible output:

One-sample t-test results:
t-statistic: 7.066
p-value: 0.000
Conclusion: Reject null hypothesis
Interpretation: Current prices are significantly different from historical average

Recall that there is a statistically significant difference when the p-value falls below 0.05 (or any other significance threshold we may have used instead).

What if we have two samples of domestic flight prices, one from last summer and one from last winter, and want to check if there are significant differences between them? Then we may use the independent two-sample t-test instead. SciPy implements this test through the stats.ttest_ind method.

# Two data samples are generated synthetically
summer_prices = np.random.normal(200, 25, 40)
winter_prices = np.random.normal(180, 20, 40)

# Performing independent t-test
t_stat, p_value = stats.ttest_ind(summer_prices, winter_prices)

# Results and interpretation
print("Two-sample t-test results:")
print(f"t-statistic: {t_stat:.3f}")
print(f"p-value: {p_value:.3f}")
print(f"Conclusion: {'Reject' if p_value < 0.05 else 'Fail to reject'} null hypothesis")
print("Interpretation: There is" + 
      " a significant difference between seasons\n" if p_value < 0.05 
      else " no significant difference between seasons\n")

Possible output:

Two-sample t-test results:
t-statistic: 2.743
p-value: 0.008
Conclusion: Reject null hypothesis
Interpretation: There is a significant difference between seasons

Wrapping Up: Other SciPy Hypothesis Testing Methods

Through two different parametric tests, we illustrated the use of SciPy library for performing hypothesis testing processes. We conclude by mentioning other methods included in the stats module of the library to perform other parametric tests.

  • stats.ttest_rel: for paired t-test among two related samples.
  • stats.f_oneway: for one-way ANOVA to compare the means of two or more independent samples.
  • stats.levene: for testing homogeneity of variance among groups.
  • stats.bartlett: for testing homogeneity of variance under normality assumptions
  • stats.pearsonr: for computing Pearson correlation coefficient and testing its significance.

Non-parametric tests will be covered in a separate article.

Leave a Reply

Your email address will not be published. Required fields are marked *