Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 52

T-Tests and P-Value Interpretation

Recommended resources

Hello! Let's dive into the final lesson of our module on probability and statistics.

In our last session, we drew a clear line between a population and a sample. We established that in data science and machine learning, we almost always work with a sample, and we calculate statistics (like the sample mean, ) to make educated guesses, or inferences, about the population's true parameters (like the population mean, ).

This leaves us with a critical question: if our sample mean differs from a value we expect, is that difference real and meaningful, or is it just random noise from the specific sample we happened to collect?

Today, we will learn how to answer that question using a formal procedure called a hypothesis test. Specifically, our goal is to perform a t-test and interpret the p-value. A t-test is one of the most fundamental tools in statistics, used to compare the means of one or two groups. Understanding it is essential for interpreting experimental results and, later, for evaluating the performance of machine learning models.

1. The Framework of Hypothesis Testing

Before we write any code, we need to understand the logical framework behind a t-test. At its core, a hypothesis test is like a court case for data. We start by assuming the "defendant" (our initial assumption) is innocent, and we need to see if there's enough evidence to convict.

Null and Alternative Hypotheses

In statistics, we formalize this with two competing hypotheses:

  • Null Hypothesis (): This is the "innocence" plea. It's the default assumption that there is no effect, no difference, or no relationship. For example, "The average age of our users is 30," or "There is no difference in average purchase amount between Group A and Group B."
  • Alternative Hypothesis ( or ): This is the claim we want to test. It's the "guilty" verdict, suggesting that there is an effect, a difference, or a relationship. For example, "The average age of our users is not 30," or "There is a difference in average purchase amount between Group A and Group B."

The goal of the test is to determine if we have enough evidence in our sample data to reject the null hypothesis in favor of the alternative hypothesis.

The video below introduces the core idea of hypothesis testing and frames it within the context of the t-test.

Python for Data Analysis: Hypothesis Testing and T-Tests

This video from DataDaft introduces the fundamental concepts of statistical hypothesis testing. It clearly explains the roles of the null and alternative hypotheses, which is the starting point for any test.

Watch the segment from 00:19 to 02:01. Focus on understanding the difference between the null and alternative hypotheses.

The Significance Level () and the p-value

So, how much evidence is "enough"?

We decide this with two key values:

  • Significance Level (): This is a threshold we set before we conduct the test. It represents the probability of rejecting the null hypothesis when it is actually true (we'll come back to this). A common choice for is 0.05 (or 5%). Think of this as setting the standard for "beyond a reasonable doubt."

  • p-value: This is the result calculated from our data. It represents the probability of observing a result as extreme as, or more extreme than, our sample result, assuming the null hypothesis is true.

The decision rule is simple:

If p-value < , the result is statistically significant. We reject the null hypothesis.
If p-value ≥ , the result is not statistically significant. We fail to reject the null hypothesis.

A small p-value (e.g., 0.01) means our data is very surprising if the null hypothesis is true. This leads us to doubt the null hypothesis and reject it. A larger p-value (e.g., 0.30) means our data is not very surprising under the null hypothesis, so we don't have enough evidence to reject it.

This video from numiqo provides an excellent conceptual explanation of the p-value.

t-Test - Full Course - Everything you need to know

To really understand what the p-value means, let's watch a short, focused explanation.

Watch the clip from 09:23 to 11:01. It does a great job of explaining what the p-value represents and how it connects to the significance level.

T-test and Hypothesis Testing Visualization
This visualizes the concept of a p-value. If our sample mean falls far into one of the 'Least likely' tails, its corresponding p-value will be small, giving us evidence to reject the null hypothesis that the true mean is at the center.
Test your understanding!

A data scientist performs a t-test to see if a new website design increases user engagement. They set a significance level () of 0.05. The test yields a p-value of 0.02. What should they conclude?

Show answer

Since the p-value (0.02) is less than the significance level ( = 0.05), the data scientist should reject the null hypothesis. They have found a statistically significant difference and can conclude that the new website design likely has an effect on user engagement.


2. Performing T-Tests in Python

Now that we have the framework, let's put it into practice. A t-test is specifically used to compare means. There are three main types, and thankfully, Python's scipy library makes it easy to perform all of them.

Before we dive in, it's important to know that t-tests rely on a few assumptions about the data, primarily that it is normally distributed. For this lesson, we will assume our data meets these assumptions. The article below provides a good, quick overview of the assumptions.

How to Do a T-Test in Python

For a quick reference on the assumptions that a t-test relies on to be valid, take a look at this article from Built In.

Read the section titled "Assumptions of a T-Test". This will give you important context for when a t-test is appropriate.

The video below will guide us through the practical Python implementation for all three types of t-tests.

Python for Data Analysis: Hypothesis Testing and T-Tests

This DataDaft video is an excellent, practical guide to performing t-tests in Python. We will walk through it section by section.

We will be watching several segments of this video. For now, get your coding environment ready and prepare to follow along.

One-Sample T-Test

This test compares the mean of a single sample to a known or hypothesized population mean.

  • Example Question: A school claims its students score an average of 85 on a national exam. We take a sample of students and want to know if their average score is significantly different from 85.
  • Hypotheses:
    • : The sample mean is equal to 85.
    • : The sample mean is not equal to 85.
  • Python Function: scipy.stats.ttest_1samp()

Now, watch the section of the video that demonstrates this.

Python for Data Analysis: Hypothesis Testing and T-Tests

Let's see how to perform a one-sample t-test in Python.

Watch from 02:35 to 09:11. Pay attention to how the ttest_1samp function is called and how the resulting p-value is interpreted using a significance level of 5% (0.05).

The function returns two key values:

  • statistic: This is the t-statistic. It tells you how many standard errors the sample mean is away from the hypothesized mean. A larger absolute value indicates a larger difference.
  • pvalue: This is the p-value we discussed. We compare it to our to make a decision.

Independent Two-Sample T-Test

This test compares the means of two independent groups.

  • Example Question: We want to know if there is a significant difference in average miles-per-gallon (MPG) between two car models, Model A and Model B.
  • Hypotheses:
    • : The mean MPG of Model A is equal to the mean MPG of Model B.
    • : The mean MPG of Model A is not equal to the mean MPG of Model B.
  • Python Function: scipy.stats.ttest_ind()

Let's see this in action.

Python for Data Analysis: Hypothesis Testing and T-Tests

Now for the independent two-sample t-test, which is very common in practice.

Watch the segment from 11:06 to 13:10. Note how two separate data samples are passed into the ttest_ind function.

Paired T-Test

This test compares the means of two related groups. This is typically used when you have two measurements for the same subject, like a "before" and "after" scenario.

  • Example Question: A group of patients is weighed before and after a new diet program. We want to know if there was a significant change in weight.
  • Hypotheses:
    • : The mean weight change is zero.
    • : The mean weight change is not zero.
  • Python Function: scipy.stats.ttest_rel()

Let's watch the final practical example.

Python for Data Analysis: Hypothesis Testing and T-Tests

Finally, let's look at the paired t-test for dependent samples.

Watch the clip from 13:10 to 15:01. Understand why this scenario requires a different test (ttest_rel) than the independent two-sample case.


3. Interpreting Results: Errors and Nuances

Rejecting or failing to reject the null hypothesis isn't the end of the story. Since we're dealing with probabilities and samples, we can never be 100% certain. Our conclusions can be wrong in two ways.

  • Type I Error (False Positive): We reject the null hypothesis when it was actually true. (We conclude there is an effect when there isn't one). The probability of making a Type I error is equal to our significance level, . If , there's a 5% chance of making this error.
  • Type II Error (False Negative): We fail to reject the null hypothesis when it was actually false. (We miss a real effect that was there).

There is always a trade-off. If we make our extremely small to avoid Type I errors, we increase the chance of making a Type II error. The standard is generally considered a good balance.

The last segment of the DataDaft video explains this crucial concept beautifully.

Python for Data Analysis: Hypothesis Testing and T-Tests

Understanding the potential for errors is key to responsibly interpreting statistical tests. Let's explore Type I and Type II errors.

Watch the final segment from 15:01 to 19:26. This is a very important part of the lesson that explains the trade-offs in hypothesis testing.

Finally, always remember the difference between statistical significance and practical significance. With a very large dataset, you might find a statistically significant p-value for a very tiny, meaningless effect (e.g., a diet that causes an average weight loss of 50 grams). Always consider the context and the magnitude of the effect, not just the p-value.


Conclusion

You've now learned one of the most widely used statistical tools. Hypothesis testing is a cornerstone of scientific research and data-driven decision-making.

Key Takeaways:

  • A t-test is a statistical hypothesis test used to compare the means of one or two samples.
  • We start by defining a null hypothesis () (no effect) and an alternative hypothesis () (an effect exists).
  • The p-value tells us the probability of observing our data if the null hypothesis were true.
  • We compare the p-value to a pre-defined significance level (), usually 0.05. If p < , we reject .
  • Python's scipy.stats library provides easy-to-use functions for t-tests: ttest_1samp, ttest_ind, and ttest_rel.
  • When interpreting results, be aware of Type I and Type II errors and the difference between statistical and practical significance.

Preview of the Next Lesson:
This lesson marks the end of our foundational statistics module. You are now equipped with the essential concepts of probability, descriptive statistics, and inferential statistics that you'll need for your machine learning journey. In our next module, we will officially begin our exploration of machine learning by learning to distinguish between supervised, unsupervised, and reinforcement learning. You'll see how the statistical concepts we've covered are applied in building and evaluating intelligent systems.