Skip to content

Beginner: data foundations · Probability and inference · topic 15 of 62 · 35 min

Hypothesis tests, p-values and A/B testing

Explanation

A hypothesis test asks: could this difference be just luck? You assume there is no real difference (the null hypothesis) and compute a p-value, the chance of seeing a result at least this extreme if that were true. A small p-value is evidence of a real effect. An A/B test uses this to compare two versions of an app or page.

Why we need tests

A food-delivery app shows a green "Order now" button to half its users and an orange one to the other half. Green gets 4.0% of users to order, orange gets 5.2%. Is orange really better, or did it just get lucky with who saw it? Random samples always differ a little. A hypothesis test tells us whether a difference is bigger than luck usually produces.

The words

  • Null hypothesis (H0): the boring claim, "there is no difference". Orange and green convert equally.
  • Alternative hypothesis (H1): what you suspect, "there is a difference".
  • Test statistic: one number that measures how far the data is from H0, such as a z-score.
  • p-value: the probability of seeing a difference at least this big IF H0 were true.
  • Significance level (α, alpha): the cut-off you choose before looking, often 0.05. If p < α, you reject H0.

A two-proportion z-test, step by step

Green: 200 orders from 5,000 users (4.0%). Orange: 260 from 5,000 (5.2%). First pool the data as if H0 were true: p = 460 ÷ 10,000 = 0.046. The standard error of the difference is √(p(1 − p)(1/n_A + 1/n_B)) ≈ 0.00419. The z statistic is the difference divided by that: 0.012 ÷ 0.00419 ≈ 2.86. Then the two-sided p-value is 2 × (1 − Φ(|z|)), where Φ is the normal curve's cumulative probability.

python
import math
from statistics import NormalDist

ca, na, cb, nb = 200, 5000, 260, 5000
pa, pb = ca / na, cb / nb
pool = (ca + cb) / (na + nb)
se = math.sqrt(pool * (1 - pool) * (1 / na + 1 / nb))
z = (pb - pa) / se
p_value = 2 * (1 - NormalDist().cdf(abs(z)))
print(round(z, 2), round(p_value, 4))
Shows 2.86 0.0042. If the buttons were really equal, a gap this big would happen only about 0.4% of the time.

Because 0.0042 is below 0.05, we reject H0 and conclude the orange button very likely performs better. The lift (relative improvement) is (5.2 − 4.0) ÷ 4.0 = 30%.

Reading a p-value correctly

  • A p-value is NOT the probability that H0 is true.
  • A p-value is NOT the probability that the result was "due to chance".
  • A large p-value does NOT prove there is no difference; maybe the sample was too small to see it.
  • p < 0.05 does NOT mean the effect is big or important. With crores of users, a 0.01% change can be "significant" and still not worth the work. This is statistical vs practical significance.

Two kinds of errors

H0 really true (no effect)H0 really false (real effect)
You reject H0Type I error (false positive), happens α of the timeCorrect: you found the effect
You keep H0CorrectType II error (false negative); a bigger sample makes it rarer

Running a good A/B test

  1. Decide the metric (for example, order rate) and α before starting.
  2. Work out the sample size you need with a power calculation, so a real effect has a good chance (often 80%) of being detected.
  3. Randomly assign users, and keep each user in one group.
  4. Run for the planned time, including full weeks, since weekends behave differently.
  5. Do not stop early the moment p dips below 0.05. Checking again and again ("peeking") raises false positives a lot.
Careful: Testing 20 metrics at α = 0.05 will give about one "significant" result by luck alone. Decide the main metric in advance, or correct for many tests.

Common mistakes

  • Reading p = 0.03 as "97% sure the new version is better". Fix: say "if there were no difference, data this extreme would be rare (3%)".
  • Stopping the test as soon as it looks good. Fix: fix the sample size in advance.
  • Ignoring effect size. Fix: always report the lift and its confidence interval next to the p-value.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Hypothesis tests, p-values and A/B testing · 1/7

Hypothesis testing

This lesson answers one question: is a difference real, or just luck?

Prefer a video by a teacher? Find free videos on “p value hypothesis testing A/B testing explained two proportion z test” on YouTube ↗

In real life: A shopping app tests a new checkout page

An e-commerce company shows a one-page checkout to a random half of users for two full weeks. The new page lifts completed orders from 61.0% to 62.2% with p = 0.01. The team also checks practical value: with lakhs of daily checkouts, 1.2 extra points means many more orders, so they roll it out, while watching refunds and complaints to make sure nothing else got worse.

Practice (0/3 done)

Task 1 · Code · Python

Write ab_summary(conv_a, n_a, conv_b, n_b) that returns a tuple (rate_a, rate_b, lift): the conversion rates conv_a / n_a and conv_b / n_b, and the relative lift (rate_b − rate_a) / rate_a. Round all three to 4 decimal places. If rate_a is 0, the lift is 0.0. Example: ab_summary(200, 5000, 260, 5000) returns (0.04, 0.052, 0.3).

Task 2 · Code · Python

Write two_prop_z(conv_a, n_a, conv_b, n_b) that returns the pooled two-proportion z statistic, rounded to 2 decimal places: pool = (conv_a + conv_b) / (n_a + n_b); se = sqrt(pool × (1 − pool) × (1/n_a + 1/n_b)); z = (rate_b − rate_a) / se. If se is 0, return 0.0. Example: two_prop_z(200, 5000, 260, 5000) returns 2.86.

Task 3 · Code · Python

Write p_value_two_sided(z) that returns 2 × (1 − NormalDist().cdf(abs(z))), rounded to 4 decimal places (from statistics import NormalDist). Example: p_value_two_sided(1.96) returns 0.05.

Quick quiz

Question 1

1.What is the null hypothesis in an A/B test of two page designs?

Question 2

2.A test gives p = 0.03. Which statement is correct?

Question 3

3.What is a Type I error?

Question 4

4.An analyst checks the A/B test every hour and stops as soon as p < 0.05. What is the problem?

Question 5

5.With 2 crore users, a change raises clicks from 10.00% to 10.01% with p = 0.001. What should you conclude?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is the null hypothesis in an A/B test of two page designs?

Answer: There is no difference between the designs

H0 is the "nothing is happening" claim that we try to find evidence against.

A test gives p = 0.03. Which statement is correct?

Answer: If there were no real difference, a result this extreme would happen about 3% of the time

The p-value is computed assuming H0 is true. It is not the probability that H0 is true.

What is a Type I error?

Answer: Declaring an effect that is not really there (a false positive)

Rejecting a true null hypothesis is a Type I error; its rate is α.

An analyst checks the A/B test every hour and stops as soon as p < 0.05. What is the problem?

Answer: Repeated peeking makes false positives much more likely

Each look is another chance to cross 0.05 by luck. Fix the sample size and duration in advance.

With 2 crore users, a change raises clicks from 10.00% to 10.01% with p = 0.001. What should you conclude?

Answer: The difference is statistically significant but may be too small to matter in practice

Huge samples make tiny effects significant. Always judge the size of the effect too.

What is the first step in debugging?Related

Answer: Reproduce the bug reliably

You cannot confirm a fix otherwise.

From A method for debugging

A technique is marked "covered" but the rule was never tested. What is the risk?Related

Answer: The rule may never fire, giving false confidence

Coverage must be tested, not assumed.

From Using frameworks in detection and threat hunting

In the testing pyramid, which kind of test should you have the most of?Related

Answer: Unit tests

Unit tests are fast and cheap, so you can have hundreds. End-to-end tests are slow, so keep only a few key flows.

From Testing a full-stack app

What is the last step of the troubleshooting method?Related

Answer: Document the problem, cause and fix

Documenting helps the next person, keeps diagrams up to date and builds a history of faults.

From A structured troubleshooting method

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. This is the last topic of “Probability and inference”. The module exam comes next.
  2. Module 3 exam: Probability and inference · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Cleaning, exploring and visualising data. 62 topics left in the course before the final exam.

← Previous: Samples, the central limit theorem and confidence intervals