The trials table has one row per user in an experiment with three or more variants (for example 'A', 'B', 'C'). Each user's outcome is either 'converted' or 'not_converted'. Use a chi-square test of homogeneity to check whether conversion is distributed the same way in every variant.
Build the variant-by-outcome table. For the cell in variant row i and outcome column j, the expected count is the row total times the column total divided by the grand total:
Eij=NRi⋅Cj
Return one row with chi_square, the sum over all cells of (Oij−Eij)2/Eij rounded to 4 decimals, and df =(g−1)(k−1), where g is the number of variants and k is the number of outcomes.
Input Table(s)
trials
| Column | Type | Meaning |
|---|
| variant | VARCHAR(20) | Experiment group the user was assigned to |
| outcome | VARCHAR(20) | 'converted' or 'not_converted' |
The table has no key column. Each row is one user, so many rows share the same (variant, outcome) values.
Output Contract
| Column | Type | Meaning |
|---|
| chi_square | NUMERIC | Chi-square homogeneity statistic, rounded to 4 decimal places |
| df | INTEGER | Degrees of freedom, (g−1)(k−1) |
The result is exactly one row, so no ordering is needed.
Example
Input trials:
| variant | outcome |
|---|
| A | not_converted |
| B | not_converted |
| C | converted |
| A | converted |
| C | not_converted |
| C | not_converted |
| B | converted |
| C | converted |
| B | not_converted |
| C | not_converted |
| A | not_converted |
| C | not_converted |
| B | not_converted |
| C | converted |
| A | not_converted |
| B | converted |
| A | converted |
| A | converted |
| B | not_converted |
| A | not_converted |
| A | converted |
| B | not_converted |
Output:
Variant A has 8 users (4 converted), B has 7 (2 converted) and C has 7 (3 converted), for 22 users and 9 conversions in total. With 3 variants and 2 outcomes, df is 2.
Constraints and Clarifications
-
Neither column is NULL.
-
There are at least three variants, and their labels are not known in advance, so do not hard-code them.
-
Both outcome values appear somewhere in the table, and every variant has at least one user, so every expected count is positive.
-
A variant can have zero users with one of the outcomes. That cell is still part of the table, with an observed count of 0.
-
Each row is a different user. Count rows as they are; there is nothing to deduplicate.
-
Round only the final statistic, not the individual cell terms.