Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Data Analysis with Python (pandas, polars, DuckDB)  Module 11 – Statistics an analyst uses every week

Comparing groups and A/B tests

Plan an A/B test before you look, test a lift by shuffling labels with NumPy, and report the effect with an interval, checked against SciPy.

  • Intermediate
  • 35 minutes
  • Examples run with Python 3.14.8, pandas 3.0.6, numpy 2.5.3 and scipy 1.18.1
  • By MySmartCoPilot
Free core, Pro depth: 11 of 35 minutes free
Free: A planned metric and stopping rule define the comparison · Shuffled labels put the observed lift against a null · Choose the comparison for its question and assumptions. With a pass: 2 in-depth parts and 3 interview answers, about 24 minutes.

What you will learn

  • Define the metric, the groups and the decision rule of a comparison before looking at the data
  • Run a permutation test for a difference in means or rates with NumPy
  • Report effect sizes with intervals, not just p-values
  • Recognise peeking, multiple comparisons and Simpson's paradox

Before you start

What you’ll learn · Before you start

What you will learn

  • Define the metric, the groups and the decision rule of a comparison before looking at the data
  • Run a permutation test for a difference in means or rates with NumPy
  • Report effect sizes with intervals, not just p-values
  • Recognise peeking, multiple comparisons and Simpson's paradox

Before you start

On this page

Comparing groups means asking whether a difference between them is larger than chance alone would produce. A/B tests make that question answerable: visitors are assigned at random between A and B, balancing other characteristics in expectation while ordinary chance imbalances remain. The method has four steps. Fix the metric, the groups and the rule for a decision before the data arrives. Measure the difference, the lift: A difference between comparison groups, stated either in original units such as percentage points or relative to a declared baseline.. Ask how often a lift that large would appear if the version made no difference, which a permutation test: A procedure that reassigns labels or pairings under a stated null and compares the observed statistic with the resulting null distribution. answers by shuffling the group labels. Then report the lift with an interval, so that a reader sees how large the effect might be and not only whether it exists. This lesson runs the whole method on an invented landing-page test with NumPy, and checks the answer against the classical tests.

A planned metric and stopping rule define the comparison

Comparisons and A/B tests are fair only if their rules were set before anyone saw the results. Write down four things first:

  • The metric: the sign-up rate of new visitors, counted once per visitor.
  • The groups: version A, the current page, and version B, the new one, assigned at random, half each.
  • The sample size: how many visitors each version needs before the result is read. Here the plan is 5,000 per version, about a week of traffic.
  • The decision rule: ship B if its sign-up rate is higher at p < 0.05.

The sample size decides which lifts the test can see. With a 5 % base rate, 5,000 visitors per version detect a lift of about 26 % with 80 % power, and a lift of 20 % needs 8,158 visitors per version.

A/B Test Sample Size Calculator Enter a 5 % base rate and a 20 % lift to see the 8,158 visitors per version, and how many days your traffic needs.

Step through the same case and its result:

Interactive · stage by stage One planned landing-page test Read the metric, assignment and decision rule before looking at outcomes.
One planned landing-page test, step 3 of 3. Showing Metric, Assignment, Decision. In focus: Decision.Metricone signup per visitorAssignmentplan: 5,000 per versionDecisionone look; p<0.05

The drawing shows the whole scene; its 3 steps are listed below.

Every step as text
  1. Count new-visitor sign-ups once per visitor; fixing the unit avoids counting activity as if it were people.
  2. Plan random assignment to A and B with 5,000 visitors per version. Randomization balances other characteristics in expectation.
  3. Read the chosen metric once at the planned horizon. A p<0.05 rule is a decision criterion, while chance false alarms remain possible.

Checkpoint

Check: Planning the comparison

  1. Question 1 of 2 Which choice belongs before seeing an A/B result?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: The primary metric and stopping rule

    The design includes the metric, groups, sample size and rule before looking. Choosing after looking adds selection to the decision.

  2. Question 2 of 2 A planned test needs 5,000 visitors in each of two versions. How many visitors does it need altogether?

    Type a number.

    Show the answer: Question 2 of 2

    Answer: 10000 visitors

    There are two independent group counts: 5,000+5,000=10,000 visitors.

Shuffled labels put the observed lift against a null

The test ran: 250 of 5,000 visitors signed up with version A (5.0 %) and 300 of 5,000 with version B (6.0 %), a lift of 1 percentage point. If the version made no difference, the labels A and B would be arbitrary, and shuffling them should produce lifts like the real one quite often. The permutation test does exactly that: it pools the 10,000 visitors, deals the labels out again at random, recomputes the lift, and repeats.

A permutation test pools the visitors, shuffles the A and B labels 10,000 times and counts how often a lift of 1 point appears.The test: A 250 of 5,000 (5.0 %)B 300 of 5,000 (6.0 %)lift +1.00 pointPool the 10,000 visitors550 sign-ups, labels set asideRepeat 10,000 times10,000 shuffled lifts,centred on 0p-value: the share at least as farfrom 0 as +1.00 point304 of 10,000, so about 0.03Shuffle the labels:5,000 new A, 5,000 new BLift of the shuffled groups

The permutation test, step by step, on the landing-page test

Text description of the diagram

The test showed version A to 5,000 visitors, of whom 250 signed up (5.0 %), and version B to 5,000 visitors, of whom 300 signed up (6.0 %). The lift is 1.00 percentage point.

  1. Pool the 10,000 visitors, with their 550 sign-ups, and set the labels A and B aside.
  2. Shuffle the labels: give 5,000 visitors chosen at random the label A and the other 5,000 the label B.
  3. Compute the lift of these shuffled groups.
  4. Repeat steps 2 and 3 10,000 times. Because the labels were assigned by chance, the 10,000 shuffled lifts are centred on 0.
  5. The p-value is the share of shuffled lifts at least as far from 0 as the real lift of 1.00 point, in either direction: 304 of 10,000, so about 0.03.
A permutation test of the landing-page result, and the z-test Python · permutation_test.py
import math

import numpy as np

# Invented results of a landing-page test: 1 = the visitor signed up, 0 = they did not.
a = np.repeat([1, 0], [250, 4_750])  # version A: 250 sign-ups from 5,000 visitors
b = np.repeat([1, 0], [300, 4_700])  # version B: 300 sign-ups from 5,000 visitors

observed = b.mean() - a.mean()
print(f"A {a.mean():.2%}   B {b.mean():.2%}   lift {observed * 100:+.2f} points ({observed / a.mean():+.0%})")

# If the version made no difference, the labels A and B are arbitrary: shuffle them and look again.
rng = np.random.default_rng(42)
pooled = np.concatenate([a, b])
diffs = np.empty(10_000)
for i in range(len(diffs)):
    shuffled = rng.permutation(pooled)
    diffs[i] = shuffled[len(a):].mean() - shuffled[: len(a)].mean()
extreme = np.sum(np.abs(diffs) >= abs(observed) - 1e-12)  # as far from 0 as the real lift, in either direction
print(f"shuffles with a lift at least as large: {extreme} of {len(diffs):,}")
print(f"permutation p-value: {(extreme + 1) / (len(diffs) + 1):.4f}")

# The two-proportion z-test, the classical shortcut for the same question.
p = (a.sum() + b.sum()) / (len(a) + len(b))
z = observed / math.sqrt(p * (1 - p) * (1 / len(a) + 1 / len(b)))
print(f"z = {z:.2f}, two-sided p-value: {math.erfc(abs(z) / math.sqrt(2)):.4f}")

Output

A 5.00%   B 6.00%   lift +1.00 points (+20%)
shuffles with a lift at least as large: 304 of 10,000
permutation p-value: 0.0305
z = 2.19, two-sided p-value: 0.0283

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 permutation_test.py

Only 304 of the 10,000 shuffles produced a lift at least as far from zero as the real one, so the p-value is about 0.03: a result this extreme would turn up about 3 times in 100 if the version made no difference. Adding 1 to both counts, as the code does, keeps a permutation p-value from ever being exactly zero. The two-proportion z-test, the textbook shortcut for rates, gives 0.028. The answers differ a little, because the z-test is an approximation and the shuffles are random, but they say the same thing. By the rule written down before the test, B ships.

A/B Test Significance Calculator Enter 5,000 visitors and 250 sign-ups for A, 5,000 and 300 for B: the calculator's z-test gives p = 0.028.

Step through the same case and its result:

Interactive · stage by stage Observed lift against shuffled labels Follow the original 10,000 shuffles and keep each visitor’s outcome fixed.
Observed lift against shuffled labels, step 3 of 3. Showing Observed, Shuffle labels, Count extremes. In focus: Count extremes.ObservedA 5%; B 6%; lift 1 pointShuffle labels550 signups; 10,000 rowsCount extremes304; corrected p 0.0305

The drawing shows the whole scene; its 3 steps are listed below.

Every step as text
  1. A has 250 sign-ups in 5,000 visitors and B has 300 in 5,000, so B−A is 1.00 percentage point.
  2. Pool the 550 sign-ups and 9,450 non-sign-ups; each shuffle changes labels while preserving every outcome and the two group sizes.
  3. 304 of 10,000 shuffles have an absolute lift at least as large. Including the observed case gives (304+1)/(10,000+1), approximately 0.0305.

Key idea

A p-value describes extremeness under the stated null, not the probability that B is better.

Checkpoint

Check: The permutation null

  1. Question 1 of 2 What stays fixed in this permutation test?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: Outcomes and group sizes

    The null procedure redistributes version labels over the fixed outcomes at the planned sizes. The labels are exactly what is reassigned.

  2. Question 2 of 2 With 304 extreme shuffles among 10,000, what is the corrected p-value?

    Type a number.

    Show the answer: Question 2 of 2

    Answer: 0.030496950305 p-value (anything from 0.030486950305 to 0.030506950305 counts)

    Include the observed result: (304+1)/(10,000+1)=305/10,001≈0.0305.

Choose the comparison for its question and assumptions

The permutation test is one of several ways of comparing groups, and each answers a slightly different question.

Four ways to compare two groups
Criterion Permutation testz-test or chi-square testWelch's t-testBootstrap interval
Question it answers Is the lift larger than shuffled labels produce?Is the gap between two rates larger than chance?Is the gap between two means larger than chance?How large is the lift, and how sure are we?
Works for Any statistic: rates, means, mediansCounts of yes and noNumbers per visitor, such as revenueEstimates with suitable bootstrap conditions
Relies on Random assignmentLarge counts in every cellLarge samples or roughly normal meansA valid resampling design and suitable statistic; assumptions checked
Result p-valuep-valuep-valueAn interval for the lift
  • The z-test and the uncorrected chi-square test compare two rates through a normal approximation; on a two-by-two table they give the same two-sided p-value.
  • Welch’s t-test compares two means without assuming the groups have equal spread.
  • A bootstrap interval resamples each group to show the range of lifts the data supports.

Three traps undo even a correct test. optional stopping: Choosing when to end an analysis from the observed outcomes; its inferential properties depend on a stopping rule evaluated for that use. means checking the result every day and stopping once p drops below 0.05, which turns chance into false wins. Multiple comparisons, testing many metrics or versions at once, make at least one false alarm likely. Simpson’s paradox is a lift in every segment that reverses in the total, when the segments are mixed differently in the two groups.

Step through the comparison and identify its result.

Interactive · stage by stage Choose a procedure for the question Compare the output you need before choosing from the four methods in the table.
Choose a procedure for the question, step 3 of 3. Showing Rates, Means or statistic, Effect range. In focus: Effect range.Ratesz or uncorrected chi²Means or statisticWelch or valid permutationEffect rangebootstrap interval

The drawing shows the whole scene; its 3 steps are listed below.

Every step as text
  1. For two large-count rates, the two-proportion z-test and uncorrected 2×2 chi-square calculation give the same two-sided p-value.
  2. Welch compares means without equal-variance assumptions. A permutation procedure needs a valid exchangeable-label null for the chosen statistic.
  3. A bootstrap interval estimates the range of effect sizes rather than assigning a probability that B is better. Peeking, metric selection and segment mixes need separate design checks.

Checkpoint

Check: Selecting a comparison

  1. Question 1 of 2 Which result directly describes a range of possible lift sizes?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: An effect interval

    The interval concerns the effect estimate and its precision. A p-value assesses extremeness under a null; it is not an effect range.

  2. Question 2 of 2 Two versions show differently mixed device segments. What should be checked before deciding from their totals?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: Assignment and segment mix

    A different mix can make totals compare devices as well as versions. A small pooled p-value cannot repair an invalid comparison.

Effect sizes with intervals, and the classical tests side by side Pro: opens with a Pro, Premium or Ultimate pass

A p-value says whether a lift is surprising, not how large it is or whether it is worth having. This part puts an interval on the lift, runs SciPy’s classical tests on the same data and turns a significant result into a decision with numbers.

In this part:

  • A confidence interval reports lift in points and per cent
  • SciPy’s tests side by side, and why they agree here
  • A significant lift that is not worth shipping, in numbers
  • Your turn: analyse an A/B test end to end

4 diagrams · 6 runnable examples (Python) · about 10 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Peeking, many comparisons and Simpson's paradox, reproduced Pro: opens with a Pro, Premium or Ultimate pass

Three habits turn honest data into wrong decisions: stopping a test when it first looks good, judging it on many metrics at once, and pooling segments that the two versions saw in different mixes. Each one is reproduced here in code that runs, with a fix that holds up.

In this part:

  • Peeking reproduced: daily checks on 10,000 tests with no real difference
  • Ten metrics, one false alarm: Holm’s correction worked through
  • Simpson’s paradox reproduced: B wins on every device and loses overall

3 diagrams · 3 runnable examples (Python) · about 7 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Practise: decide from the stated evidence

Practice that adapts to you

Practise this lesson

10 questions about 10 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 10 Which choice belongs before seeing an A/B result?

    Choose one answer.

    Show the answer: Question 1 of 10

    Answer: The primary metric and stopping rule

    The design includes the metric, groups, sample size and rule before looking. Choosing after looking adds selection to the decision.

  2. Question 2 of 10 A fixed-horizon experiment plans 8,158 visitors for each of two versions. What total is planned?

    Type a number.

    Show the answer: Question 2 of 10

    Answer: 16316 visitors

    8,158+8,158=16,316 visitors. Neither group uses the total as its rate denominator.

  3. Question 3 of 10 What stays fixed in this permutation test?

    Choose one answer.

    Show the answer: Question 3 of 10

    Answer: Outcomes and group sizes

    The null procedure redistributes version labels over the fixed outcomes at the planned sizes. The labels are exactly what is reassigned.

  4. Question 4 of 10 No shuffled result is as extreme among 999 random permutations. What corrected p-value is returned?

    Type a number.

    Show the answer: Question 4 of 10

    Answer: 0.001 p-value (anything from 0.000999 to 0.001001 counts)

    Include the observed case: (0+1)/(999+1)=0.001 rather than zero.

  5. Question 5 of 10 Which result directly describes a range of possible lift sizes?

    Choose one answer.

    Show the answer: Question 5 of 10

    Answer: An effect interval

    The interval concerns the effect estimate and its precision. A p-value assesses extremeness under a null; it is not an effect range.

  6. Question 6 of 10 Two tests both report p=0.03 but have different effect intervals. What must be compared before deciding which change matters?

    Choose one answer.

    Show the answer: Question 6 of 10

    Answer: Effect size, interval and the decision costs

    A p-value does not describe effect magnitude or practical value. Equal p-values do not imply equal gains or precision.

  7. Question 7 of 10 Version A's 5,000 visitors gave 250 sign-ups and version B's 5,000 visitors gave 300. What is the lift of B over A in percentage points?

    Type a number.

    Show the answer: Question 7 of 10

    Answer: 1 points (anything from 0.99 to 1.01 counts)

    A converts 250 / 5,000 = 5.0 % and B 300 / 5,000 = 6.0 %, so the lift is 6.0 − 5.0 = 1 percentage point. In relative terms the same lift is 20 %, because 1 point is a fifth of A's 5 %; say which of the two you report.

  8. Question 8 of 10 In a permutation test of an A/B result, what does each shuffle change?

    Choose one answer.

    Show the answer: Question 8 of 10

    Answer: Which visitors carry the labels A and B; every visitor keeps their own outcome

    Under the idea that the version made no difference, each visitor would have signed up or not whatever label they got. A shuffle keeps every outcome and deals the labels out again, so the lifts of many shuffles show what chance alone produces with these visitors.

  9. Question 9 of 10 A test reports a lift of 1 point with p = 0.03. Which statement is right?

    Choose one answer.

    Show the answer: Question 9 of 10

    Answer: If the version made no difference, a lift at least this large would appear in about 3 % of such tests

    A p-value is computed under the assumption of no difference: it is the share of tests that would show a lift at least this far from zero by chance alone. It is not the probability that B is better, and it says nothing about the size of the lift, which is why the lift needs its own interval.

  10. Question 10 of 10 Which departures from a planned single-metric, fixed-horizon comparison can inflate false wins or confound the total? Choose every one.

    Choose every answer that is right.

    Show the answer: Question 10 of 10

    Answer:

    • Testing ten metrics and reporting the one that reached p < 0.05
    • Checking every day and stopping at the first p below 0.05
    • Comparing totals when far more phone visitors saw one version

    Every extra look and every extra metric is another chance for noise to cross p < 0.05, and a different mix of phone and desktop visitors makes identical versions look different in the totals. One planned look at one primary metric keeps the false-alarm rate at the 5 % the threshold promises.

Interview questions

  1. Fresher to mid · Warm-up

    What does a p-value of 0.03 in an A/B test tell you, and what does it not tell you?

    It says that if both versions had the same true conversion rate, a lift at least as large as the one observed would appear in about 3 % of tests of this size under that null model. A small p-value indicates incompatibility with the specified null and assumptions; it does not establish a real effect. It is not the probability that B is better, nor the probability that the result is a fluke, and it says nothing about the size of the lift: 0.1 point on millions of visitors and 3 points on a few thousand can both give p = 0.03. It also assumes the test ran as planned, with random assignment and one look at the end; daily peeking or many metrics make small p-values far more common. Report the lift with its interval beside the p-value.

  2. Fresher to mid

    Your A/B test shows version B converting 6.0 % against 5.0 % for A, with 5,000 visitors per version and p = 0.03. The product manager wants to ship B today. What do you check first, and what exactly do you report?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  3. Mid to senior

    A team checks its A/B dashboard every morning and stops each test as soon as p drops below 0.05. Over a year, most of its "winning" changes show no effect after launch. Explain what is happening and how you would redesign the process.

    Model answer, Pro. First step and follow-ups

  4. Fresher to senior

    Version B beats version A among phone visitors and among desktop visitors, but loses in the total. The designer says B wins everywhere; finance says B lost. Who is right, what do you check, and what do you report?

    Model answer, Pro. First step and follow-ups

Key takeaways

  • Write the metric, the groups, the sample size and the decision rule down before the test starts.
  • A permutation test shuffles the group labels to see how often chance produces the observed lift; NumPy is enough.
  • A p-value measures surprise, not size or value: report the lift with an interval.
  • Peeking and metric selection can inflate false wins; differently mixed segments can confound a pooled comparison.

Terms in this lesson

lift
A difference between comparison groups, stated either in original units such as percentage points or relative to a declared baseline.
optional stopping
Choosing when to end an analysis from the observed outcomes; its inferential properties depend on a stopping rule evaluated for that use.
permutation test
A procedure that reassigns labels or pairings under a stated null and compares the observed statistic with the resulting null distribution.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.