Sub topic 2
Picking the randomization unit and who is eligible
This chapter assumes you know what a hypothesis, a primary metric and a feature flag are, and that you can read rates and percentages. It covers the randomization unit, eligibility, sample size, the experiment brief, event specifications, variant assignment, and sample ratio mismatch checks.
Choosing the randomization unit
The randomization unit is the entity that receives a variant, such as a user, session, account or device. Match it to how your primary metric is measured. If the metric is checkout completion per user, randomize by user_id. Randomizing by session can place the same person in both variants and distort the results.
Eligibility is the rule that decides who enters the experiment, for example "logged-in web users with at least one saved address." Define it before launch, assign only eligible units, and log the eligibility check so the analysis population can be reproduced.
Estimating sample size and duration
Sample size depends on the baseline rate, the minimum detectable effect (MDE), alpha and power. This function uses the normal approximation for two proportions:
import math
from statistics import NormalDist
def sample_size_per_variant(baseline, mde_abs, alpha=0.05, power=0.8):
p1 = baseline
p2 = baseline + mde_abs
z = NormalDist().inv_cdf
variance = p1 * (1 - p1) + p2 * (1 - p2)
return math.ceil((z(1 - alpha / 2) + z(power)) ** 2 * variance / (p2 - p1) ** 2)
print(sample_size_per_variant(0.10, 0.01))A 10% baseline with a one-point lift needs about 14,750 users per variant. Divide the total needed by daily eligible traffic to get the minimum run time, then round up to full weeks so weekday patterns balance out.
Writing the experiment brief
The brief is a short written plan that all three functions sign before launch. It states the hypothesis, randomization unit, eligibility, split, metrics, MDE, sample size, alpha, power, duration and decision rule.
| Owner | Responsibility |
|---|---|
| Product | Hypothesis, metrics, eligibility, decision rule |
| Engineering | Assignment logic, exposure and event logging, flag rollout |
| Data | Metric definitions, sample size check, SRM check, analysis |
Specifying events and metric definitions for engineering
Give engineering exact definitions. For each metric, state the event name, the trigger, the required fields and the deduplication rule. For example, checkout_completed fires once per order on the server after payment succeeds, with the fields user_id, order_id, variant and timestamp.
Log an experiment_exposed event when a user actually sees the variant, not when the page loads for everyone.
How variants are assigned and exposed
A deterministic hash gives each user the same variant on every visit. Including the experiment key in the hash keeps separate experiments independent.
import hashlib
def assign_variant(user_id, experiment_key, treatment_share=0.5):
digest = hashlib.sha256(f"{experiment_key}:{user_id}".encode()).hexdigest()
bucket = int(digest[:8], 16) % 10000
return "treatment" if bucket < int(treatment_share * 10000) else "control"
print(assign_variant("user-123", "checkout_address_autofill"))Checking data quality and sample ratio mismatch
A sample ratio mismatch (SRM) occurs when the observed split differs from the planned split. A chi-square goodness-of-fit test measures this:
from scipy.stats import chisquare
def srm_p_value(control_count, treatment_count, expected_treatment_share=0.5):
total = control_count + treatment_count
expected = [total * (1 - expected_treatment_share), total * expected_treatment_share]
return chisquare([control_count, treatment_count], f_exp=expected).pvalue
print(srm_p_value(50210, 49790))Exercise
A team needs about 29,500 users in total (14,750 per variant) and has 3,000 eligible users per day, split 50/50. How many days should the test run?
Show answer
29,500 divided by 3,000 is about 9.8, so the test needs at least 10 days. Run 14 days, two full weeks, so weekday patterns balance out.