Step into the intriguing world of hypothesis testing, where your natural curiosity meets the power of data to reveal truths!
This article is your key to unlocking how those everyday hunches—like guessing a group’s average income or figuring out who owns their home—can be thoroughly checked and proven with data.
I am going to take you by the hand and show you, in simple steps, how to use Python to explore a hypothesis about the average yearly income.
By the time we’re done, you’ll not only get the hang of creating and testing hypotheses but also how to use statistical tests on actual data.
Perfect for up-and-coming data scientists, anyone with a knack for analysis, or just if you’re keen on data, get ready to gain the skills to make informed decisions and turn insights into real-world actions.
Join me as we dive deep into the data, one hypothesis at a time!
🔌 Plug-in
Before we get started, elevate your data skills with my expert eBooks—the culmination of my experiences and insights.
Support my work and enhance your journey. Check them out:
eBook 2: Personal INTERVIEW Ready “Statistics” Cornell Notes
Best Selling eBook: Top 50+ ChatGPT Personas for Custom Instructions
Data Science Bundle (Cheapest): The Ultimate Data Science Bundle: Complete
ChatGPT Bundle (Cheapest): The Ultimate ChatGPT Bundle: Complete
💡 Checkout for more such resources: https://codewarepam.gumroad.com/
What is a hypothesis, and how do you test it?
A hypothesis is like a guess or prediction about something specific, such as the average income or the percentage of homeowners in a group of people.
It’s based on theories, past observations, or questions that spark our curiosity.
For instance, you might predict that the average yearly income of potential customers is over $50,000 or that 60% of them own their homes.
To see if your guess is right, you gather data from a smaller group within the larger population and check if the numbers (like the average income, percentage of homeowners, etc.) from this smaller group match your initial prediction.
You also set a rule for how sure you need to be to trust your findings, often using a 5% chance of error as a standard measure. This means you’re 95% confident in your results. — Level of Significance (0.05)
There are two main types of hypotheses: the null hypothesis, which is your baseline saying there’s no change or difference, and the alternative hypothesis, which suggests there is a change or difference.
For example,
If you start with the idea that the average yearly income of potential customers is $50,000,
The alternative could be that it’s not $50,000—it could be less or more, depending on what you’re trying to find out.
To test your hypothesis, you calculate a test statistic—a number that shows how much your sample data deviates from what you predicted.
How you calculate this depends on what you’re studying and the kind of data you have. For example, to check an average, you might use a formula that considers your sample’s average, the predicted average, the variation in your sample data, and how big your sample is.
This test statistic follows a known distribution (like the t-distribution or z-distribution), which helps you figure out the p-value.
The p-value tells you the odds of seeing a test statistic as extreme as yours if your initial guess was correct.
A small p-value means your data strongly disagrees with your initial guess.
Finally, you decide on your hypothesis by comparing the p-value to your error threshold.
If the p-value is smaller or equal, you reject the null hypothesis, meaning your data shows a significant difference that’s unlikely due to chance.
If the p-value is larger, you stick with the null hypothesis, suggesting your data doesn’t show a meaningful difference and any change might just be by chance.
We’ll go through an example that tests if the average annual income of prospective customers exceeds $50,000.
This process involves stating hypotheses, specifying a significance level, collecting and analyzing data, and drawing conclusions based on statistical tests.
Example: Testing a Hypothesis About Average Annual Income
Step 1: State the Hypotheses
Null Hypothesis (H0): The average annual income of prospective customers is $50,000.
Alternative Hypothesis (H1): The average annual income of prospective customers is more than $50,000.
Step 2: Specify the Significance Level
Significance Level: 0.05, meaning we’re 95% confident in our findings and allow a 5% chance of error.
Step 3: Collect Sample Data
We’ll use the
ProspectiveBuyer
table, assuming it's a random sample from the population.This table has 2,059 entries, representing prospective customers' annual incomes.
Step 4: Calculate the Sample Statistic
In Python, we can use libraries like Pandas and Numpy to calculate the sample mean and standard deviation.
import pandas as pd
import numpy as np
df = pd.read_csv('ProspectiveBuyer.csv')
sample_mean = df['YearlyIncome'].mean()
sample_sd = df['YearlyIncome'].std()
sample_size = len(df)
print(f"Sample Mean: {sample_mean}")
print(f"Sample Standard Deviation: {sample_sd}")
print(f"Sample Size: {sample_size}")
Result:
SampleMean: 56,992.43
SampleSD: 32,079.16
SampleSize: 2,059
Step 5: Calculate the Test Statistic
We use the t-test formula to calculate how significantly our sample mean deviates from the hypothesized mean.
Python’s Scipy library can handle this calculation:
from scipy import stats
# Hypothesized mean
mu = 50000
t_statistic, p_value = stats.ttest_1samp(df['YearlyIncome'], mu)
print(f"T-Statistic: {t_statistic}")
Result:
T-Statistic: 4.62
Step 6: Calculate the P-Value
The p-value is already calculated in the previous step using Scipy's
ttest_1samp
function, which returns both the test statistic and the p-value.
print(f"P-Value: {p_value/2}") #specific to one-tailed tests
Result:
P-Value = 0.0000021
Step 7: State the Statistical Conclusion
We compare the p-value with our significance level to decide on our hypothesis:
Since the p-value is less than 0.05, we reject the null hypothesis in favor of the alternative.
Conclusion:
There’s strong evidence to suggest that the average annual income of prospective customers is indeed more than $50,000.
Summary
This example illustrates how Python can be a powerful tool for hypothesis testing, enabling us to derive insights from data through statistical analysis.
How to Choose the Right Test Statistics
Choosing the right test statistic is crucial and depends on what you’re trying to find out, the kind of data you have, and how that data is spread out.
Here are some common types of test statistics and when to use them:
T-test statistic:
This one’s great for checking out the average of a group when your data follows a normal distribution or when you’re comparing the averages of two such groups.
The t-test follows a special curve called the t-distribution. This curve looks a lot like the normal bell curve but with thicker ends, which means more chances for extreme values.
The t-distribution’s shape changes based on something called degrees of freedom, which is a fancy way of talking about your sample size and how many groups you’re comparing.
Z-test statistic:
Use this when you’re looking at the average of a normally distributed group or the difference between two group averages, and you already know the standard deviation for all in the population.
The z-test follows the standard normal distribution, which is your classic bell curve centered at zero and spreading out evenly on both sides.
Chi-square test statistic:
This is your go-to for checking if there’s a difference in variability within a normally distributed group or if two categories are related.
The chi-square statistic follows its own distribution, which leans to the right and gets its shape from the degrees of freedom—basically, how many categories or groups you’re comparing.
F-test statistic:
This one helps you compare the variability between two groups or see if the averages of more than two groups are all the same, assuming all groups are normally distributed.
The F-test follows the F-distribution, which is also right-skewed and has two types of degrees of freedom that depend on how many groups you have and the size of each group.
In simple terms, the test you pick hinges on what you’re curious about, whether your data fits the normal curve, and if you know certain specifics, like the population’s standard deviation.
Each test has its own special curve and rules based on your sample’s details and what you’re comparing.
Join my community of learners! Subscribe to my newsletter for more tips, tricks, and exclusive content on mastering Data Science & AI. —