What is a p-value and how is it interpreted?

Authors
What is a p-value and how is it interpreted?

A p-value is the probability of seeing our data, or something even more extreme, assuming the null hypothesis is true. It is not the chance that the null is true.

In this blog, we will learn about the p-value and how it is interpreted. We will also see what a hypothesis is, a simple coin example, how the significance level alpha works, what a p-value is not, a machine learning example, and the common mistakes we must avoid.

We will cover the following:

  • What is a hypothesis?
  • What is a p-value?
  • A simple coin example
  • How is a p-value interpreted?
  • The significance level (alpha)
  • What a p-value is NOT
  • A machine learning example
  • Common mistakes engineers make

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What is a hypothesis?

Before we learn about the p-value, we must understand a hypothesis.

A hypothesis is a claim or a guess that we want to test.

It is a statement that may be true or may be false, and we want to check it using data.

Let's say a friend gives us a coin and says, "This is a fair coin." This claim is a hypothesis.

Now, we want to test this claim. So, in testing, we always start with two opposite statements.

+-------------------------------------------------+
|  Null Hypothesis (H0):                          |
|      "The coin is fair. Nothing special here."  |
|                                                 |
|  Alternative Hypothesis (H1):                   |
|      "The coin is NOT fair. Something is up."   |
+-------------------------------------------------+

Here, we can see that there are two statements. The null hypothesis is the default assumption that nothing unusual is happening. The alternative hypothesis is the claim that something unusual is happening.

We always assume the null hypothesis is true, and then we look at the data to see if we have enough proof to reject it.

What is a p-value?

Now, it's time to learn about the p-value.

A p-value is the probability of seeing our data (or something even more extreme), assuming the null hypothesis is true.

The p-value answers this question: if the default is actually true, how surprising is the result we just saw?

A p-value is a probability, so it is always a number between 0 and 1.

A small p-value means our data is very surprising if the null hypothesis is true. This makes us doubt the null hypothesis.

A large p-value means our data is not surprising at all. So, we have no strong reason to doubt the null hypothesis.

A simple coin example

The best way to learn this is by taking an example.

Let's say our friend claims the coin is fair. So, our null hypothesis is "the coin is fair."

Now, we flip the coin 10 times, and we get 10 heads in a row.

Flip:    1   2   3   4   5   6   7   8   9   10
Result:  H   H   H   H   H   H   H   H   H   H

Here, we can see that all 10 flips landed on heads. Let's calculate how surprising this is if the coin is fair.

Chance of 10 heads = (1/2) x (1/2) x ... 10 times
                   = 1 / 1024
                   = about 0.001

Our alternative hypothesis is "the coin is not fair", which can be unfair in either direction. So, a result that is just as extreme the other way, 10 tails, also counts. Its chance is also 1 / 1024.

p-value = 1/1024 + 1/1024
        = 2 / 1024
        = about 0.002

Here, we can see that the p-value is about 0.002, which is very close to zero. This is a very small p-value. It tells us that if the coin were fair, our result would be extremely surprising.

Because the result is so surprising, we start to doubt the claim that the coin is fair. We reject the null hypothesis and say, "This coin is probably not fair."

This is how a p-value helps us make a decision.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

How is a p-value interpreted?

Now, let's understand the interpretation, because this is where most people do mistakes.

The p-value is compared against a threshold. If the p-value is small, we reject the null hypothesis. If it is large, we do not reject it.

p-valueWhat it means
Very small (near 0)Data is surprising. Reject the null.
Large (near 1)Not surprising. Do not reject null.

Here, we can see the rule. A small p-value pushes us to reject the default. A large p-value gives us no reason to reject it.

A large p-value does not prove the null hypothesis is true. It only means we did not find enough evidence against it.

The significance level (alpha)

Now, the question is: how small is small enough?

We need a fixed cutoff before we run the test. This line is called the significance level, and we write it as alpha.

The significance level (alpha) is the threshold we choose to decide when a p-value is small enough to reject the null hypothesis.

The most common value is 0.05.

+--------------------------------------------------------+
|  If  p-value <= alpha  ->  Reject the null hypothesis  |
|  If  p-value >  alpha  ->  Do not reject the null      |
|                                                        |
|  Common alpha = 0.05                                   |
+--------------------------------------------------------+

Here, we can see that if our p-value is less than or equal to alpha, we reject the null hypothesis. Otherwise, we do not.

In our coin example, the p-value was about 0.002, which is much smaller than 0.05. So, we reject the null hypothesis. The coin is probably not fair.

Note: We must choose alpha before we run the test, not after looking at the data. Choosing it after looking at the data is cheating, and it leads to false results.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

What a p-value is NOT

Now, let's understand what a p-value does NOT mean, because this is where we do mistakes most often.

A p-value is NOT the probability that the null hypothesis is true.

A p-value is NOT the probability that our result happened by chance.

A p-value is NOT proof that our result is important or useful in the real world.

Let's see the difference clearly.

+--------------------------------------------------------+
|  WRONG:  "p = 0.03 means there is a 3% chance the      |
|           null hypothesis is true."                    |
|                                                        |
|  RIGHT:  "p = 0.03 means IF the null were true,        |
|           there is a 3% chance of data this extreme."  |
+--------------------------------------------------------+

Here, we can see the key difference. The p-value assumes the null hypothesis is true and then measures how surprising the data is. It does not measure the chance that the hypothesis itself is true.

The p-value is a statement about the data under an assumption, not a statement about the truth of the hypothesis.

We have a detailed blog on Bayes' Theorem that explains why the probability of the evidence given a claim is not the same as the probability of the claim given the evidence.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

A machine learning example

Now, let's take a real use-case in machine learning.

Assume that we build two models, Model A and Model B. We want to know if Model B is truly better than Model A, or if the difference is just random luck.

Our null hypothesis is: "There is no real difference between Model A and Model B."

We run a statistical test comparing their accuracy scores across many test sets, and we get a p-value.

+--------------------------------+
|  Model A    accuracy = 90.0%   |
|  Model B    accuracy = 90.4%   |
|                                |
|  p-value from the test = 0.60  |
+--------------------------------+

Here, we can see that Model B looks slightly better, but the p-value is 0.60, which is large. This is much bigger than our alpha of 0.05.

So, we do not reject the null hypothesis. We do not have enough evidence that the small difference in accuracy is a real improvement.

If we had shipped Model B as a clear improvement, we would have been acting on weak evidence. This is how the p-value protects us from treating random noise as a real improvement.

We have a detailed blog on Cross-Validation that explains how we get a fair accuracy score for a model from many different test splits.

Now, let's see the code for a simple test in Python as below:

from scipy import stats

# accuracy scores of two models across many test runs
model_a = [0.90, 0.89, 0.91, 0.90, 0.90]
model_b = [0.90, 0.91, 0.90, 0.91, 0.90]

# run a t-test to compare the two models
t_stat, p_value = stats.ttest_ind(model_a, model_b)

print("p-value:", p_value)

if p_value <= 0.05:
    print("Reject the null. The difference is statistically significant.")
else:
    print("Do not reject the null. Not enough evidence of a real difference.")

Here, we have used the ttest_ind function from scipy to compare the two lists of accuracy scores. We get a p_value, and then we compare it against 0.05.

If the p_value is small, we say the difference is likely real. If it is large, we say we do not have enough evidence that the difference is real.

To master Model Development and Training, and Evaluation and Testing for machine learning models from the ground up, check out our AI and Machine Learning Program at Outcome School.

Common mistakes engineers make

Now, let's discuss the mistakes we must avoid.

Mistake 1: Treating a small p-value as proof of a big, important effect. A small p-value only means our data would be surprising if there were no real effect. It does not mean the effect is large or useful. A tiny, useless improvement can still have a small p-value if we have a huge amount of data.

Mistake 2: Running many tests and picking the one with a small p-value. If we test 20 things where nothing real is happening, on average one will show a p-value below 0.05 just by luck. This is called p-hacking, and we must avoid it.

Mistake 3: Thinking a large p-value proves there is no effect. A large p-value only means we did not find enough evidence. The effect may still exist. We just could not detect it.

We must always separate "is the effect real" from "is the effect big and useful."

Now, we have understood the p-value.

Frequently Asked Questions

What is p-hacking?

P-hacking is running many tests and picking the one that shows a small p-value. If we test 20 things where nothing real is happening, on average one will show a p-value below 0.05 just by luck. That result looks significant, but it is only noise, so we must avoid this practice.

Does a large p-value prove that the null hypothesis is true?

No. A large p-value does not prove the null hypothesis is true. It only means we did not find enough evidence against it. The effect may still exist, and we just could not detect it with the data we have.

Does a small p-value mean the effect is large or important?

No. A small p-value only means our data would be surprising if there were no real effect. It does not tell us how big or useful the effect is. A tiny, useless improvement can still have a small p-value when we have a huge amount of data.

Why do we count results in both directions when calculating a p-value?

We count both directions when the alternative hypothesis allows an effect either way. In the coin example, the alternative is "the coin is not fair", and a coin can be unfair toward heads or toward tails. So, 10 tails is just as extreme as 10 heads, and the p-value adds both chances to get about 0.002.

Why must we choose alpha before running the test?

We must choose the significance level alpha before running the test because choosing it after looking at the data is cheating. If we pick the cutoff after seeing the p-value, we can always move it to get the answer we want, and this leads to false results.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.