Sub topic 10
Checking data quality and sample ratio mismatch
This chapter assumes you know what a randomization unit is, how hash-based assignment works, and what a confidence interval says. You do not need to write the Python yourself, but you should be able to read it. The goal is to decide whether the data can be trusted before you read any result.
Why check the data first
An experiment result is only as good as the data behind it. If assignment or logging is broken, a statistically clean confidence interval can still be wrong, because the two groups no longer differ only by the treatment. Checking data quality comes before analysis for that reason. A failed check means you stop and investigate. It does not mean you keep reading the result with a caveat.
The checks fall into three groups:
- Assignment checks ask whether users were split as planned.
- Instrumentation checks ask whether events were logged completely and once.
- Metric sanity checks ask whether the numbers look plausible against history.
What sample ratio mismatch is
Sample ratio mismatch (SRM) means the observed split between variants differs from the planned split by more than chance explains. You planned 50/50 and got 50,800 versus 49,200 users. That looks small, but with samples this large it is very unlikely under a true 50/50 assignment.
SRM matters because the cause is almost never random. Something removed or added users in a way that depends on the variant. The users who remain in each group are then no longer comparable, so the difference in your primary metric may reflect who is left and not what the feature did.
Running the SRM test
The test is a chi-square goodness-of-fit test. It compares the observed counts with the counts the planned split predicts. Run it on the number of units that were assigned and analyzed, such as users, not on events or sessions.
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
# Healthy: a gap this size is plausible by chance.
print(f"{srm_p_value(50210, 49790):.3f}") # about 0.184
# Suspicious: the same total with a larger gap.
print(f"{srm_p_value(50800, 49200):.2e}") # about 4e-07Read the p-value as follows. A p-value of 0.184 means a gap this large would appear about 18% of the time if assignment were truly 50/50, so there is no evidence of a problem. A p-value of 4e-07 means that would almost never happen. Many teams alert at p < 0.001. The threshold is stricter than the usual 0.05 because you run this check on every experiment, often on many segments, and a false alarm halts a launch.
If the planned split is not 50/50, pass the real share. For a 90/10 ramp, call srm_p_value(c, t, expected_treatment_share=0.10). A common mistake is to test against 50/50 during a ramp.
Where the check sits in the workflow
Common causes of SRM
Engineering and data partners should know this list, because most SRMs trace back to one item on it.
| Cause | How it creates a mismatch | Typical clue |
|---|---|---|
| Treatment crashes or loads slowly | Users leave before the exposure event fires, so treatment loses users from the count | Treatment is smaller, and page load or error guardrails look worse |
| Redirects in one variant | Some users never arrive at the page that logs exposure | Mismatch appears on slow networks or one browser |
| Bot or internal traffic | Bots react differently to the variant, or are filtered in only one arm | Mismatch concentrated in a few IP ranges or user agents |
| Assignment changes mid-test | Users re-bucketed after a ramp or flag change | Users appear in both variants |
| Exposure logged on a condition | The event fires only if a feature renders, which differs by variant | Mismatch among users who never saw the changed element |
| Data pipeline filters | A join or deduplication step drops more rows in one arm | Counts differ between raw logs and the analysis table |
| Hash collisions with other experiments | The experiment key is not part of the hash input, so assignments correlate | Mismatch within overlapping experiments |
The last row is why the assignment code includes the experiment key in the hash. Without it, a user in treatment for one experiment tends to be in treatment for all of them.
Looking for the cause in segments
An overall SRM tells you something is wrong. Splitting the check by segment tells you where. Run the same test per platform, browser, country, and day, and look for the segment where the mismatch is concentrated.
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
# (control users, treatment users) per platform
segments = {
"web_chrome": (20100, 20050),
"web_safari": (15200, 15300),
"web_firefox": (5100, 3900),
"android_web": (9800, 9900),
}
for name, (c, t) in segments.items():
p = srm_p_value(c, t)
flag = "SRM" if p < 0.001 else "ok"
print(f"{name:12s} control={c:6d} treatment={t:6d} p={p:.2e} {flag}")Here only web_firefox fails. That points engineering toward a Firefox-specific problem in the treatment, such as a script error that stops the exposure event. Without the segment view, the team would only know that something is off somewhere.
Testing many segments creates its own multiple-comparison problem. With 50 segments at p < 0.001, a false alarm is still unlikely, but treat a single borderline segment as a lead to check and not as proof.
Checking instrumentation
SRM catches assignment problems that change group sizes. Some logging problems leave the sizes alone, so run these as well:
- Duplicate events. Count events per user per event ID. Retries and double-fired handlers inflate conversion counts and can differ between variants.
- Missing events. Compare the count of assigned users with the count of users who have at least one exposure event. A large gap means exposure logging is incomplete.
- Cross-contamination. Check that no user ID appears in both variants. Even a small share means assignment was not stable, for example because the unit changed between logged-out and logged-in states.
- Timestamp order. Conversions should come after exposure. Events logged before exposure are either mis-timed or belong to users who were not yet in the experiment, and they should be excluded in the same way for both arms.
- Metric definition match. The metric computed in the analysis must match the definition in the brief, including the window and the eligible population.
Metric sanity checks
Compare each metric with its history and with its definition:
- Does the control baseline match the baseline in the brief? If the brief says 10% completion and control shows 4%, the eligibility rule or the event definition has changed.
- Are the values in range? Rates between 0 and 1, durations not negative, revenue not absurdly high from a test purchase.
- Do the guardrail metrics have data in both arms? A guardrail with no data cannot protect you.
- Are there outliers in metrics such as revenue? A single extreme order can move a mean. Decide the handling rule, such as capping, before looking at results.
An A/A test is a useful companion. It runs the same assignment machinery with two identical experiences. Run it when a platform or pipeline is new. A healthy system shows no SRM and a significant difference in about 5% of repeated A/A runs at alpha 0.05. Many more than that point to a flaw in the pipeline or the analysis method.
Who does what
The three functions have distinct jobs here, and the experiment brief should name them:
- Data owns the automated SRM test, the segment breakdown, and the alert threshold.
- Engineering owns assignment stability, exposure logging, and diagnosing causes in code.
- Product owns the decision to pause, restart, or extend, and writes down what happened.
Run the SRM check daily during the experiment, not only at the end. A problem caught on day two costs two days. The same problem found on day fourteen costs the whole test.
What to do when a check fails
- Pause the readout. Do not look at the primary metric until the cause is understood.
- Reproduce the mismatch from raw logs to rule out a pipeline bug in the analysis table.
- Use the segment breakdown to narrow the cause.
- Fix the cause and restart the experiment. Data collected before the fix is not usable for the decision, because the biased users are mixed into it.
- Record the incident in the learning record, since the same cause often recurs.
Exercise
An experiment is planned as 90% control and 10% treatment during an early ramp. After a day you see 91,800 control users and 8,200 treatment users. Your colleague runs an SRM test against a 50/50 split and reports an extreme p-value. Is the SRM conclusion correct, and what should you do?
Show answer
No. The test used the wrong expected share. Against the planned 90/10 split the expected counts are 90,000 and 10,000, so the observed 91,800 and 8,200 differ by 1,800 users. The chi-square statistic is 1,800²/90,000 + 1,800²/10,000 = 36 + 324 = 360, which gives an extremely small p-value, so there is a real SRM. Rerun with expectedtreatmentshare=0.10 and you get the same conclusion for the right reason. Pause the readout, check segments to find where treatment is losing users, and have engineering look for crashes or exposure logging failures before restarting.
Exercise
The SRM test passes, but 3% of users appear in both the control and treatment tables. Why does this matter, and what is the likely cause?
Show answer
The groups are no longer cleanly separated, so each variant contains users who experienced both versions, which dilutes and can bias the measured difference. A likely cause is an unstable randomization unit, for example assignment by device or cookie before login and by user ID after login, so the same person gets two assignments. Fix the assignment key, then restart the experiment.