Compute the Yates continuity-corrected chi-square for an A/B test in SQL

Quick Overview

Compute the Yates continuity-corrected chi-square statistic for a two-variant conversion test in SQL, starting from one row per visitor. Tests conditional aggregation into a 2x2 table, the n/2 correction and its clamp at zero, and arithmetic that avoids integer division and overflow.

Compute the Yates continuity-corrected chi-square for an A/B test in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

`ab_events` has one row per visitor in a two-variant conversion test, with the variant the visitor saw and whether they converted. Compute the Yates continuity-corrected chi-square statistic for the 2x2 variant-by-outcome table and return one row with `chi_square_yates` rounded to 4 decimals. Use this formula, where $a, b, c, d$ are the cell counts, $r_1, r_2, k_1, k_2$ the row and column totals and $n$ the grand total: $$\chi^2_{\text{Yates}} = \frac{n\,\big(\max(|ad - bc| - n/2,\ 0)\big)^2}{r_1\, r_2\, k_1\, k_2}$$ | | converted | not_converted | row total | |---|---|---|---| | control | a | b | r1 = a + b | | treatment | c | d | r2 = c + d | | column total | k1 = a + c | k2 = b + d | n | ```hint Check the half The visitor total can be odd. Make sure your engine computes $n/2$ the way the formula means it. ``` ### Input Table(s) `ab_events` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(20) | The arm the visitor saw: `'control'` or `'treatment'` | | outcome | VARCHAR(20) | `'converted'` or `'not_converted'` | There is no primary key. Each row is one visitor, so two identical rows are two different visitors. ### Output Contract Exactly one row with one column: | Column | Type | Meaning | |---|---|---| | chi_square_yates | NUMERIC | The continuity-corrected statistic, rounded to 4 decimal places | The result always has one row, so no ordering is needed. ### Example Input `ab_events` (17 rows): | variant | outcome | |---|---| | control | not_converted | | control | converted | | treatment | not_converted | | treatment | converted | | treatment | converted | | treatment | not_converted | | control | not_converted | | treatment | not_converted | | control | not_converted | | control | not_converted | | control | not_converted | | treatment | converted | | treatment | converted | | control | not_converted | | control | converted | | control | converted | | treatment | converted | Expected output: | chi_square_yates | |---| | 0.5124 | Control has 3 converted and 6 not converted visitors, and treatment has 5 and 3, so $n = 17$, the row totals are 9 and 8 and the column totals are 8 and 9. Then $|ad - bc| = |9 - 30| = 21$, the correction removes 8.5, and $$\chi^2_{\text{Yates}} = \frac{17 \cdot 12.5^2}{9 \cdot 8 \cdot 8 \cdot 9} = \frac{2656.25}{5184} \approx 0.5124.$$ ### Constraints and Clarifications - `variant` is always `'control'` or `'treatment'`, and `outcome` is always `'converted'` or `'not_converted'`. Neither column contains NULLs. - Both variants and both outcomes appear at least once, so every row and column total is positive and the statistic is defined. - $n/2$ is exact division: for $n = 17$ it is 8.5. - Keep the clamp at 0 exactly as written. When $|ad - bc|$ is less than $n/2$, the statistic is 0. - The query should stay correct at production sizes (hundreds of thousands of visitors per variant), not only on the small example.

Overview: Compute the Yates continuity-corrected chi-square statistic for a two-variant conversion test in SQL, starting from one row per visitor. Tests conditional aggregation into a 2x2 table, the n/2 correction and its clamp at zero, and arithmetic that avoids integer division and overflow.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

ab_events has one row per visitor in a two-variant conversion test, with the variant the visitor saw and whether they converted. Compute the Yates continuity-corrected chi-square statistic for the 2x2 variant-by-outcome table and return one row with chi_square_yates rounded to 4 decimals.

Use this formula, where a,b,c,da, b, c, d are the cell counts, r1,r2,k1,k2r_1, r_2, k_1, k_2 the row and column totals and nn the grand total:

χYates2=n (max⁡(∣ad−bc∣−n/2, 0))2r1 r2 k1 k2\chi^2_{\text{Yates}} = \frac{n\,\big(\max(|ad - bc| - n/2,\ 0)\big)^2}{r_1\, r_2\, k_1\, k_2}

convertednot_convertedrow total
controlabr1 = a + b
treatmentcdr2 = c + d
column totalk1 = a + ck2 = b + dn

Input Table(s)

ab_events

ColumnTypeMeaning
variantVARCHAR(20)The arm the visitor saw: 'control' or 'treatment'
outcomeVARCHAR(20)'converted' or 'not_converted'

There is no primary key. Each row is one visitor, so two identical rows are two different visitors.

Output Contract

Exactly one row with one column:

ColumnTypeMeaning
chi_square_yatesNUMERICThe continuity-corrected statistic, rounded to 4 decimal places

The result always has one row, so no ordering is needed.

Example

Input ab_events (17 rows):

variantoutcome
controlnot_converted
controlconverted
treatmentnot_converted
treatmentconverted
treatmentconverted
treatmentnot_converted
controlnot_converted
treatmentnot_converted
controlnot_converted
controlnot_converted
controlnot_converted
treatmentconverted
treatmentconverted
controlnot_converted
controlconverted
controlconverted
treatmentconverted

Expected output:

chi_square_yates
0.5124

Control has 3 converted and 6 not converted visitors, and treatment has 5 and 3, so n=17n = 17, the row totals are 9 and 8 and the column totals are 8 and 9. Then ∣ad−bc∣=∣9−30∣=21|ad - bc| = |9 - 30| = 21, the correction removes 8.5, and

χYates2=17⋅12.529⋅8⋅8⋅9=2656.255184≈0.5124.\chi^2_{\text{Yates}} = \frac{17 \cdot 12.5^2}{9 \cdot 8 \cdot 8 \cdot 9} = \frac{2656.25}{5184} \approx 0.5124.

Constraints and Clarifications

  • variant is always 'control' or 'treatment' , and outcome is always 'converted' or 'not_converted' . Neither column contains NULLs.
  • Both variants and both outcomes appear at least once, so every row and column total is positive and the statistic is defined.
  • n/2n/2 is exact division: for n=17n = 17 it is 8.5.
  • Keep the clamp at 0 exactly as written. When ∣ad−bc∣|ad - bc| is less than n/2n/2 , the statistic is 0.
  • The query should stay correct at production sizes (hundreds of thousands of visitors per variant), not only on the small example.