# A/B Testing in Python: A Guide from Sample Size to Sequential Tests

> A guide to A/B testing in Python covering sample size and power, analyzing conversion and revenue, CUPED variance reduction, bootstrap intervals, Bayesian A/B tests and sequential testing, with the order to read each tutorial in.

Source: https://www.pythoncharts.com/ab-testing/ab-testing-python-guide/ | Published: 2026-11-25 | Author: Alex Scott | Category: A/B Testing | Tags: ab testing, experimentation, statistics, sample size, cuped, bayesian, sequential testing, guide

An A/B test in Python has four stages: plan the sample size with `statsmodels` power functions, analyze the result with a z-test or t-test and a confidence interval, reduce noise with CUPED if you have pre-experiment data, and decide how you will monitor the test before it starts. Each stage has a tutorial with seeded sample data and code you can run as is.

![Grid of six A/B testing charts: power curves, lift confidence intervals, CUPED comparison, bootstrap distribution, Bayesian posteriors and p-value paths under peeking](https://www.pythoncharts.com/ab-testing/ab-testing-python-guide/images/ab-hub-montage.png)

## The A/B Testing Workflow, Step by Step

| Stage | Question | Tutorial |
|---|---|---|
| 1. Plan | How many users per arm do I need? | [Sample Size and Power](https://www.pythoncharts.com/ab-testing/ab-test-sample-size-power-python/) |
| 2. Analyze | Did the variant win, and by how much? | [A/B Test Analysis](https://www.pythoncharts.com/ab-testing/ab-test-analysis-python/) |
| 3. Sharpen | Can I get a narrower interval from the same data? | [CUPED](https://www.pythoncharts.com/ab-testing/cuped-variance-reduction-python/) |
| 3. Sharpen | Can a model with many features narrow it further? | [CUPAC](https://www.pythoncharts.com/ab-testing/cupac-ml-variance-reduction-python/) |
| 4. Check | Is the normal approximation safe for skewed revenue? | [Bootstrap Confidence Intervals](https://www.pythoncharts.com/ab-testing/bootstrap-confidence-interval-python/) |
| 4. Check | My metric is a ratio like CTR. Is my interval right? | [Ratio Metrics and the Delta Method](https://www.pythoncharts.com/ab-testing/ratio-metrics-delta-method-python/) |
| 5. Reframe | What is the probability that B beats A? | [Bayesian A/B Testing](https://www.pythoncharts.com/ab-testing/bayesian-ab-test-python/) |
| 6. Monitor | Can I look at the results early? | [Peeking and Sequential Testing](https://www.pythoncharts.com/ab-testing/ab-test-peeking-sequential-testing-python/) |
| 7. Adapt | Can traffic shift to the winner while the test runs? | [Multi-Armed Bandits and Thompson Sampling](https://www.pythoncharts.com/ab-testing/multi-armed-bandit-thompson-sampling-python/) |
| 8. Sustain | Will the lift last after launch? | [Novelty and Primacy Effects](https://www.pythoncharts.com/ab-testing/novelty-primacy-effect-ab-test-python/) |
| 9. Segment | Does the lift differ by country, device or plan? | [Segment Analysis and Heterogeneous Effects](https://www.pythoncharts.com/ab-testing/heterogeneous-treatment-effects-segments-python/) |
| 10. Design | What if users can't be randomized one by one? | [Switchback and Cluster-Randomized Experiments](https://www.pythoncharts.com/ab-testing/switchback-cluster-randomized-experiments-python/) |

## Plan Before You Launch

Pick the minimum detectable effect, the significance level and the power, then solve for the sample size per arm. The sample size post plots power against sample size so you can see how quickly the cost grows as the effect you want to detect shrinks. Decide here whether you will monitor the test early, because that changes the analysis.

## Analyze the Result

For a conversion rate, the standard approach is a two-proportion z-test plus a confidence interval for the difference. For revenue per user, use a t-test or a bootstrap, because revenue is skewed and has many zeros. The analysis post covers both and shows the lift with its interval, which is more useful to a decision-maker than a p-value alone.

## Get More Power from the Same Data

CUPED adjusts each user's outcome using a pre-experiment covariate, which removes variance that has nothing to do with the treatment. If the covariate is strongly correlated with the outcome, the interval narrows without more traffic. The post shows the raw and adjusted intervals on the same data.

## Choose an Inference Style

| Approach | Gives you | Good when |
|---|---|---|
| z-test or t-test | p-value and confidence interval | Large samples, standard metrics |
| Bootstrap | Interval from resampling, no formula | Skewed metrics, ratios, medians |
| Bayesian | P(B > A), credible interval, expected loss | Stakeholders want a probability, or the cost of a wrong call is explicit |
| Sequential test | Valid decisions at interim looks | Results are monitored while the test runs |

With a flat prior and this much data, the Bayesian credible interval and the frequentist confidence interval were both -0.14 to 1.54 percentage points in the Bayesian post. The methods mostly differ in how they describe the answer, not in what the data say. They differ a lot when you peek, which is the next point.

## Do Not Peek Without a Plan

Checking a fixed-horizon test every day and stopping when p < 0.05 inflates false positives. In the peeking post, 20 looks at an A/A test, where there is no real difference, gave a false positive rate of about 25% instead of 5%. Bayesian stopping rules are not immune: stopping when P(B > A) reached 95% still produced a false winner in 43% of A/A runs. Group-sequential boundaries (such as O'Brien-Fleming style) and mSPRT keep the error rate near its target, and the post compares their power and sample size cost.

## Practical Tips

- Write down the metric, the minimum detectable effect and the stopping rule before launch.
- Report an interval, not just a p-value or a winner.
- When you report many metrics, [correct for multiple comparisons](https://www.pythoncharts.com/ab-testing/ab-test-multiple-testing-python/).
- Run a [sample ratio mismatch check](https://www.pythoncharts.com/ab-testing/ab-test-srm-check-python/) before trusting any result: if the split is not what you configured, something upstream is broken.
- Use the same seeded simulation approach as these posts to test your own analysis on A/A data before trusting it.

## Similar Topics

- [Causal Inference in Python: Choosing a Method](https://www.pythoncharts.com/causal-inference/causal-inference-python-guide/)
- [Machine Learning Charts in Python](https://www.pythoncharts.com/machine-learning/machine-learning-python-guide/)
