The assignments table has one row per user in an A/B test, holding the variant the user was assigned to ('control' or 'treatment'). The test was designed as a 50/50 split. Check it for a sample ratio mismatch (SRM) by computing the chi-square goodness-of-fit statistic with one degree of freedom:
χ2=v∈{control, treatment}∑Ev(Ov−Ev)2
Here Ov is the number of users in variant v, and each expected count is Ev=N/2, where N is the total number of users. Return a single row with chi_square (the statistic rounded to 4 decimals), critical_value (read from the critical_values row with df = 1 and alpha = 0.05), and srm_flag, which is 1 when the statistic is greater than the critical value and 0 otherwise.
Input Table(s)
Table assignments:
| Column | Type | Meaning |
|---|
variant | VARCHAR(20) | Arm the user was assigned to: 'control' or 'treatment' |
One row per user. The table has no other columns and no key.
Table critical_values:
| Column | Type | Meaning |
|---|
df | INT | Degrees of freedom |
alpha | DECIMAL(4,3) | Significance level |
critical_value | DECIMAL(8,4) | Upper critical value of the chi-square distribution for that df and alpha |
Each (df, alpha) pair appears at most once.
Output Contract
-
Exactly one row with the columns
chi_square
,
critical_value
,
srm_flag
, in that order.
-
chi_square
is the statistic rounded to 4 decimal places with
ROUND(..., 4)
.
-
critical_value
is the value stored in the lookup row, unchanged.
-
srm_flag
is the integer 1 or 0.
-
The result is a single row, so no ordering applies.
Example
Input assignments (19 rows):
| variant |
|---|
| treatment |
| control |
| control |
| control |
| treatment |
| control |
| treatment |
| control |
| treatment |
| treatment |
| treatment |
| treatment |
| control |
| control |
| treatment |
| control |
| control |
| treatment |
| control |
Input critical_values:
| df | alpha | critical_value |
|---|
| 1 | 0.050 | 3.8410 |
| 2 | 0.050 | 5.9910 |
| 3 | 0.050 | 7.8150 |
Expected output:
| chi_square | critical_value | srm_flag |
|---|
| 0.0526 | 3.8410 | 0 |
There are 10 control users and 9 treatment users, so N=19 and each expected count is 9.5. Both arms are 0.5 away from 9.5, so the statistic is 2⋅0.25/9.5, about 0.052632. That is far below 3.841, so the split shows no mismatch.
Constraints and Clarifications
-
assignments
has at least one row.
variant
is never NULL and is always exactly
'control'
or
'treatment'
.
-
An arm can have zero users. It still contributes
(0−N/2)2/(N/2)
to the statistic.
-
critical_values
has exactly one row with
df = 1
and
alpha = 0.05
. It can also hold rows for other degrees of freedom and other significance levels.
-
Compare the unrounded statistic with the critical value. A statistic exactly equal to the critical value gives
srm_flag = 0
.
-
Use the plain statistic above, with no continuity correction.