Reference library

A/B testing & experimentation

User bucketing, experiment metrics, statistical comparison, and rollout guardrails.

12 matches
A/B testing & experimentation medium

Bayesian A/B Test Credible Interval in Python

Simulates A/B test data and computes posterior credible intervals and the probability that variant B outperforms A using Bayesian Beta-Binomial inference.

bayesian ab-testing credible-interval
Python
import numpy as np
from scipy import stats

# Simulated A/B test data
n_A = 1000
n_B = 1000
conversions_A = 120
conversions_B = 140

# Prior: Beta(1, 1) uniform
alpha_prior, beta_prior = 1, 1

# Posterior parameters
alpha_A = alpha_prior + conversions_A
beta_A = beta_prior + n_A - conversions_A
alpha_B = alpha_prior +…
15 0 Open
A/B testing & experimentation medium

Benjamini Hochberg FDR Correction in Python

Implement the Benjamini-HHochberg false discovery rate (FDR) procedure in Python to control the expected proportion of false positives among rejected hypotheses.

fdr multiple testing hypothesis testing
Python
import numpy as np

def benjamini_hochberg(p_values, alpha=0.05):
    p_values = np.array(p_values)
    n = len(p_values)
    sorted_idx = np.argsort(p_values)
    sorted_p = p_values[sorted_idx]
    
    thresholds = (np.arange(1, n + 1) / n) * alpha
    significant = sorted_p <= thresholds
    
    if not significan…
15 0 Open
A/B testing & experimentation medium

Bootstrap Confidence Interval in Python

Estimates a confidence interval for a statistic (like the mean) using bootstrap resampling in pure Python.

bootstrap confidence-interval statistics
Python
import random


def bootstrap_ci(data, statistic, n_bootstraps=1000, ci_level=0.95, seed=42):
    random.seed(seed)
    n = len(data)
    boot_stats = []

    for _ in range(n_bootstraps):
        sample = [random.choice(data) for _ in range(n)]
        boot_stats.append(statistic(sample))

    boot_stats.sort()
    l…
17 0 Open
A/B testing & experimentation medium

Check Sample Ratio Mismatch in Python

Estimates the probability that a simple random sample's proportion differs from the population proportion by more than 10% using simulation.

simulation statistics ab-testing
Python
import random


def sample_ratio_mismatch(population_size: int, sample_size: int, p: float) -> float:
    """
    Estimate the probability that a simple random sample's proportion
    differs from the population proportion by more than 10%.
    """
    total_counts = [0, 0]
    for _ in range(10000):
        sample = …
15 0 Open
A/B testing & experimentation medium

Chi-Square Test in Python for Conversion Mock Data

Compute the chi-square statistic and approximate p-value for a mock A/B conversion test using the standard library.

chi-square statistics ab-testing
Python
import math
from collections import Counter

def chi_square_statistic(observed):
    """
    Compute chi-square statistic for a mock conversion test.
    observed: dict mapping outcomes to observed frequencies.
    """
    observed = Counter(observed)
    n = sum(observed.values())
    expected = n / len(observed) if …
12 0 Open
A/B testing & experimentation medium

Delta Method for Ratio Metrics in A/B Testing with Python

Computes the confidence interval for the difference between two ratio metrics using the delta method, with mock A/B test data.

delta-method ab-testing ratio-metrics
Python
import numpy as np
from scipy.stats import norm


def delta_method_ratio_delta(control: np.ndarray, treatment: np.ndarray, confidence: float = 0.95):
    """Estimate confidence interval for ratio metric using delta method.

    Args:
        control: numerator/denominator pairs from control group (n x 2 array)
       …
15 0 Open
A/B testing & experimentation medium

How to Compute Mann-Whitney U Test in Python

Compute the Mann-Whitney U statistic and p-value manually in Python with tie correction and a normal approximation for independent samples.

statistics hypothesis-testing ab-testing
Python
import numpy as np
from scipy import stats

def mann_whitney_u_mock(sample_a, sample_b):
    """Compute Mann-Whitney U and p-value manually."""
    # Combine and rank
    combined = sample_a + sample_b
    n_a, n_b = len(sample_a), len(sample_b)
    n_total = n_a + n_b
    
    # Rank with ties handling (average ranks…
12 0 Open
A/B testing & experimentation medium

How to Conduct a Two-Sample T-Test in Python

Performs Welch's t-test for two independent samples, computing the t-statistic, degrees of freedom, and p-value using NumPy and SciPy.

statistics hypothesis-testing t-test
Python
import numpy as np

def two_sample_t_test(sample1, sample2):
    """Perform Welch's t-test for two independent samples."""
    n1, n2 = len(sample1), len(sample2)
    mean1, mean2 = np.mean(sample1), np.mean(sample2)
    var1, var2 = np.var(sample1, ddof=1), np.var(sample2, ddof=1)

    # Standard error of difference
…
15 0 Open
A/B testing & experimentation medium

How to Perform Intent-to-Treat Analysis in Python

Runs an intent-to-treat analysis on mock A/B test data, comparing outcomes by initial group assignment with a t-test for significance.

ab-testing intent-to-treat statistics
Python
import pandas as pd
import numpy as np


def intent_to_treat_analysis(data):
    """Perform intent-to-treat (ITT) analysis.

    ITT compares outcomes based on initial treatment assignment,
    regardless of whether participants actually received the treatment.
    """
    # Create a copy to avoid mutating the origina…
13 0 Open
A/B testing & experimentation medium

How to Perform Welch's t-Test in Python

Calculate the Welch t-statistic and degrees of freedom for two samples with unequal variances using Python's statistics module.

statistics t-test hypothesis-testing
Python
import math
from statistics import mean, variance


def welch_t_test(sample1, sample2):
    n1, n2 = len(sample1), len(sample2)
    mean1, mean2 = mean(sample1), mean(sample2)
    var1, var2 = variance(sample1), variance(sample2)

    # Welch's t statistic
    t_stat = (mean1 - mean2) / math.sqrt(var1 / n1 + var2 / n2…
14 0 Open
A/B testing & experimentation medium

How to Run a Fisher Exact Test in Python

Compute the two-sided Fisher exact test p-value for a 2x2 contingency table using pure Python and the math module.

statistics fisher-exact ab-testing
Python
from math import comb, factorial
from itertools import combinations


def hypergeometric_probability(a, b, c, d):
    """Probability of observing table [[a, b], [c, d]] under the null."""
    row1 = a + b
    row2 = c + d
    col1 = a + c
    col2 = b + d
    total = row1 + row2
    return (comb(row1, a) * comb(row2, …
18 0 Open
A/B testing & experimentation medium

How to Run a Permutation Test in Python

Run a Monte Carlo permutation test to compute a p-value for comparing two group means without parametric assumptions.

permutation-test statistics ab-testing
Python
import random
import statistics

def permutation_test(group_a, group_b, n_permutations=10000, seed=42):
    random.seed(seed)
    combined = group_a + group_b
    observed_diff = abs(statistics.mean(group_a) - statistics.mean(group_b))
    
    count = 0
    n = len(group_a)
    for _ in range(n_permutations):
       …
15 0 Open

Browse by section

Each section groups closely related Python snippets.

A/B testing & experimentation — Python code examples

What you will find here

This page collects a/b testing & experimentation snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.