Beginner: data foundations · Probability and inference · topic 15 of 62 · 35 min
Hypothesis tests, p-values and A/B testing
Explanation
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.
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))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 H0 | Type I error (false positive), happens α of the time | Correct: you found the effect |
| You keep H0 | Correct | Type II error (false negative); a bigger sample makes it rarer |
Running a good A/B test
- Decide the metric (for example, order rate) and α before starting.
- Work out the sample size you need with a power calculation, so a real effect has a good chance (often 80%) of being detected.
- Randomly assign users, and keep each user in one group.
- Run for the planned time, including full weeks, since weekends behave differently.
- Do not stop early the moment p dips below 0.05. Checking again and again ("peeking") raises false positives a lot.
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
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)
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).
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Random variables and distributionsProbability and inference · same module
- A method for debuggingC Programming (from zero to systems) · Debugging and tools
- Using frameworks in detection and threat huntingCyber Security: from zero to SOC analyst · Threat intelligence and frameworks
- Testing a full-stack appFull-Stack Development · Performance, security and testing
- A structured troubleshooting methodComputer Networking (CCNA level) · Troubleshooting and the CCNA exam
- Design project: an online storeSQL and Databases · Schema design and normalisation
Questions people ask about this
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
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.
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.
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.
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
- This is the last topic of “Probability and inference”. The module exam comes next.
- Module 3 exam: Probability and inference · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- 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