
Hypothesis testing is a statistical method used to make decisions about a population based on sample data. In this article, we’ll explore how to perform hypothesis testing using Python’s NumPy library, providing you with the tools to determine if there is enough evidence to support a given claim about your data.
Understanding Hypothesis Testing
Hypothesis testing involves two hypotheses: the null hypothesis (H0) and the alternative hypothesis (H1). The null hypothesis represents a default assumption, such as “there is no difference” or “there is no effect,” while the alternative hypothesis represents the claim we are testing against. Our goal is to evaluate if our sample data provides enough evidence to reject the null hypothesis in favor of the alternative.
To perform hypothesis testing, we often calculate a test statistic, compare it to a critical value, or compute a p-value to determine the probability of observing the data if the null hypothesis is true. Below, we’ll demonstrate some common types of hypothesis tests using NumPy.
Note on NumPy and p-values
While NumPy is a powerful library for numerical computations, it does not include built-in functions to directly compute p-values for hypothesis testing. For this reason, we recommend using other packages like SciPy or statsmodels if you need to compute p-values. These packages have built-in functions for performing hypothesis tests, which can simplify the process.
If you are using NumPy alone, you can still manually compare your calculated test statistic against critical values from statistical tables. This approach allows you to decide whether to reject or fail to reject the null hypothesis based on the significance level you choose.
To help you complete your hypothesis tests, you can use resources like statistical tables for t-distribution, z-distribution, and chi-square distribution. These tables will help you determine critical values based on your desired significance level. You can also take advantage of the calculators we provide on this website.
1. One-Sample t-Test with NumPy
A one-sample t-test checks whether the mean of a sample is significantly different from a known or hypothesized population mean.
Suppose we have a dataset representing the average scores of students in a test, and we want to determine if the mean score is significantly different from 70.
import numpy as np
# Sample data
scores = np.array([68, 72, 75, 71, 69, 73, 68, 74, 70, 71])
# Population mean to test against
population_mean = 70
# Calculate the sample mean and standard deviation
sample_mean = np.mean(scores)
sample_std = np.std(scores, ddof=1)
# Calculate the t-statistic
t_statistic = (sample_mean - population_mean) / (sample_std / np.sqrt(len(scores)))
# Print the t-statistic
print(f"t-statistic: {t_statistic:.2f}")
Output:
t-statistic: 1.43
In this example, we calculate the t-statistic to see how far the sample mean is from the population mean in terms of standard errors. You would compare this t-statistic to a critical value from the t-distribution.
If we choose a significance level of 0.05 and have 9 degrees of freedom (n-1 = 10-1), the critical value from the t-distribution table is approximately 2.262. Since our t-statistic (1.43) is less than the critical value (2.262), we fail to reject the null hypothesis. There is not enough evidence to conclude that the mean score is significantly different from 70.
2. Two-Sample t-Test with NumPy
A two-sample t-test is used to determine if the means of two independent samples are significantly different from each other.
Consider two groups of students, Group A and Group B, who took different training courses. We want to see if there is a significant difference in their scores.
# Sample data for two groups
group_a = np.array([85, 88, 90, 87, 86, 89, 84])
group_b = np.array([82, 81, 85, 83, 80, 79, 84])
# Calculate the means and standard deviations
mean_a = np.mean(group_a)
mean_b = np.mean(group_b)
std_a = np.std(group_a, ddof=1)
std_b = np.std(group_b, ddof=1)
# Calculate the pooled standard deviation
n_a, n_b = len(group_a), len(group_b)
pooled_std = np.sqrt(((n_a - 1) * std_a**2 + (n_b - 1) * std_b**2) / (n_a + n_b - 2))
# Calculate the t-statistic
t_statistic = (mean_a - mean_b) / (pooled_std * np.sqrt(1/n_a + 1/n_b))
# Print the t-statistic
print(f"t-statistic: {t_statistic:.2f}")
Output:
t-statistic: 4.33
Here, we calculate the t-statistic for the difference between the two group means. If we choose a significance level of 0.05 and have 12 degrees of freedom (n_a + n_b – 2 = 7 + 7 – 2), the critical value from the t-distribution table is approximately 2.179. Since our t-statistic (4.33) is greater than the critical value (2.179), we reject the null hypothesis. There is enough evidence to conclude that there is a significant difference between the two group means.
3. Z-Test for Population Proportions
A z-test is often used when we want to compare population proportions. For example, suppose we want to see if the proportion of people who prefer a new product is significantly different from 50%.
# Sample data
successes = 52 # Number of people who prefer the new product
total = 100 # Total number of people surveyed
# Hypothesized proportion
p_null = 0.5
# Sample proportion
p_hat = successes / total
# Calculate the standard error
se = np.sqrt(p_null * (1 - p_null) / total)
# Calculate the z-statistic
z_statistic = (p_hat - p_null) / se
# Print the z-statistic
print(f"z-statistic: {z_statistic:.2f}")
Output:
z-statistic: 0.40
In this example, we calculate the z-statistic to test if the proportion of people who prefer the new product is significantly different from 50%. If we choose a significance level of 0.05, the critical z-value is approximately 1.96. Since our z-statistic (0.40) is less than the critical value (1.96), we fail to reject the null hypothesis. There is not enough evidence to conclude that the proportion of people who prefer the new product is significantly different from 50%.
4. Chi-Square Test for Independence
The chi-square test is used to determine if there is an association between two categorical variables. Suppose we have survey data of 50 people, showing their preferences for two products (A and B) by gender (Male and Female).
# Contingency table
observed = np.array([[20, 15], # Product A preferences
[10, 5]]) # Product B preferences
# Calculate the expected frequencies
row_totals = np.sum(observed, axis=1).reshape(-1, 1)
col_totals = np.sum(observed, axis=0)
grand_total = np.sum(observed)
expected = row_totals @ col_totals.reshape(1, -1) / grand_total
# Calculate the chi-square statistic
chi_square_stat = np.sum((observed - expected)**2 / expected)
# Print the chi-square statistic
print(f"Chi-square statistic: {chi_square_stat:.2f}")
Output:
Chi-square statistic: 0.40
In this example, we calculate the chi-square statistic to test if there is a relationship between gender and product preference. If we choose a significance level of 0.05 and have 1 degree of freedom ((rows – 1) * (columns – 1) = (2 – 1) * (2 – 1)), the critical value from the chi-square distribution table is approximately 3.841. Since our chi-square statistic (0.40) is less than the critical value (3.841), we fail to reject the null hypothesis. There is not enough evidence to conclude that there is a significant association between gender and product preference.
Conclusion
Hypothesis testing is an essential tool in statistics that allows us to make informed decisions about data. With NumPy, you can easily calculate test statistics for different types of hypothesis tests, including t-tests, z-tests, and chi-square tests. However, NumPy does not include built-in functions for computing p-values, which can make completing a hypothesis test more challenging.
To obtain p-values and streamline your hypothesis testing, consider using SciPy or statsmodels, which offer built-in functions that perform these tests and provide p-values directly. You can also opt to use our calculators for this final step or assess your result against distribution tables. Understanding these concepts and how to implement them with Python will help you draw meaningful conclusions from your data.
