Test a three-arm A/B/C split for sample ratio mismatch with a chi-square goodness-of-fit statistic in SQL
Quick Overview
Write a PostgreSQL query that runs a chi-square goodness-of-fit test for sample ratio mismatch on a three-arm experiment designed as an equal split, returning the statistic and its degrees of freedom. It tests counting per arm when an arm can be empty, decimal division and how degrees of freedom follow from the design.
Test a three-arm A/B/C split for sample ratio mismatch with a chi-square goodness-of-fit statistic in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `assignments` table has one row per user, holding the `variant` the user was bucketed into: `'A'`, `'B'` or `'C'`. The experiment was designed as an equal three-way split. Run a sample ratio mismatch (SRM) chi-square goodness-of-fit test against that design. Each variant's expected count is $E_v = N / 3$, where $N$ is the total number of users, and the statistic is
$$
\chi^2 = \sum_{v \in \{A,\ B,\ C\}} \frac{(O_v - E_v)^2}{E_v}
$$
where $O_v$ is the observed number of users in variant $v$. Return one row with `chi_square`, the statistic rounded to 4 decimals, and `df`, the number of variants minus 1.
```hint Fractional expected counts
With 22 users the expected count per arm is about 7.33. Make sure the division happens in decimal arithmetic.
```
### Input Table(s)
Table `assignments`:
| Column | Type | Meaning |
|---|---|---|
| `variant` | VARCHAR(5) | Arm the user was bucketed into: `'A'`, `'B'` or `'C'` |
One row per user. The table has no other columns and no key.
### Output Contract
- Exactly one row with the columns `chi_square`, `df`, in that order.
- `chi_square` is the statistic rounded to 4 decimal places with `ROUND(..., 4)`.
- `df` is an integer: the number of designed variants minus 1.
- The result is a single row, so no ordering applies.
### Example
Example 1. Input `assignments` (21 rows):
| variant |
|---|
| A |
| C |
| A |
| C |
| B |
| A |
| A |
| B |
| B |
| C |
| C |
| B |
| B |
| B |
| C |
| A |
| B |
| A |
| A |
| C |
| C |
Expected output:
| chi_square | df |
|---|---|
| 0.0000 | 2 |
Each variant has 7 users, and the expected count is 21 / 3 = 7, so every term is 0 and the statistic is 0. There are three variants, so `df` is 2.
Example 2. Input `assignments` (6 rows):
| variant |
|---|
| A |
| A |
| B |
| A |
| B |
| A |
Expected output:
| chi_square | df |
|---|---|
| 4.0000 | 2 |
The counts are 4 for A, 2 for B and 0 for C, and the expected count is 6 / 3 = 2. The terms are $(4 - 2)^2 / 2 = 2$ for A, 0 for B and $(0 - 2)^2 / 2 = 2$ for C, so the statistic is 4. Variant C has no users but is still one of the three designed arms.
### Constraints and Clarifications
- `assignments` has at least one row. `variant` is never NULL and is always exactly `'A'`, `'B'` or `'C'`.
- A variant can have zero users. It still contributes its term $(0 - N/3)^2 / (N/3)$ to the statistic and still counts toward `df`, so `df` is always 2.
- Use the plain statistic above, with no continuity correction. No p-value or flag is needed.
Overview: Write a PostgreSQL query that runs a chi-square goodness-of-fit test for sample ratio mismatch on a three-arm experiment designed as an equal split, returning the statistic and its degrees of freedom. It tests counting per arm when an arm can be empty, decimal division and how degrees of freedom follow from the design.
The assignments table has one row per user, holding the variant the user was bucketed into: 'A', 'B' or 'C'. The experiment was designed as an equal three-way split. Run a sample ratio mismatch (SRM) chi-square goodness-of-fit test against that design. Each variant's expected count is Ev=N/3, where N is the total number of users, and the statistic is
χ2=v∈{A,B,C}∑Ev(Ov−Ev)2
where Ov is the observed number of users in variant v. Return one row with chi_square, the statistic rounded to 4 decimals, and df, the number of variants minus 1.
Input Table(s)
Table assignments:
Column
Type
Meaning
variant
VARCHAR(5)
Arm the user was bucketed into: 'A', 'B' or 'C'
One row per user. The table has no other columns and no key.
Output Contract
Exactly one row with the columns
chi_square
,
df
, in that order.
chi_square
is the statistic rounded to 4 decimal places with
ROUND(..., 4)
.
df
is an integer: the number of designed variants minus 1.
The result is a single row, so no ordering applies.
Example
Example 1. Input assignments (21 rows):
variant
A
C
A
C
B
A
A
B
B
C
C
B
B
B
C
A
B
A
A
C
C
Expected output:
chi_square
df
0.0000
2
Each variant has 7 users, and the expected count is 21 / 3 = 7, so every term is 0 and the statistic is 0. There are three variants, so df is 2.
Example 2. Input assignments (6 rows):
variant
A
A
B
A
B
A
Expected output:
chi_square
df
4.0000
2
The counts are 4 for A, 2 for B and 0 for C, and the expected count is 6 / 3 = 2. The terms are (4−2)2/2=2 for A, 0 for B and (0−2)2/2=2 for C, so the statistic is 4. Variant C has no users but is still one of the three designed arms.
Constraints and Clarifications
assignments
has at least one row.
variant
is never NULL and is always exactly
'A'
,
'B'
or
'C'
.
A variant can have zero users. It still contributes its term
(0−N/3)2/(N/3)
to the statistic and still counts toward
df
, so
df
is always 2.
Use the plain statistic above, with no continuity correction. No p-value or flag is needed.