Reference library

A/B testing & experimentation

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

2 matches
A/B testing & experimentation medium

Check Covariate Balance in Python

Compute standardized mean differences and KS tests to check covariate balance between treatment and control groups in Python.

covariate balance ab-testing
Python
import numpy as np
from scipy import stats

def balance_check(treatment, covariate):
    """Check covariate balance between treatment and control groups."""
    treat_vals = covariate[treatment == 1]
    control_vals = covariate[treatment == 0]
    
    # Standardized mean difference
    pooled_std = np.sqrt((np.var(t…
13 0 Open
A/B testing & experimentation easy

How to Do Random Assignment in Python for A/B Tests

Assign each item to a binary group (0 or 1) with uniform probability using a small reusable function, optionally weighted, for A/B testing mocks.

random ab-testing assignment
Python
import random

def random_assignment_uniform_mock(items, weights=None):
    """Assign each item to a group (0 or 1) with uniform probability."""
    if weights is None:
        # Default: each item independently gets 0 or 1 with 50% probability
        return [random.randint(0, 1) for _ in items]
    # Optional weight…
13 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.