SRM Chi-Square Goodness-of-Fit Statistic for a 50/50 A/B Test

Quick Overview

Implement a Python function that returns the Pearson chi-square goodness-of-fit statistic used to check a 50/50 A/B test for sample ratio mismatch. It tests whether you can set up expected counts correctly and explain why a contingency-table test with Yates correction gives a different number.

SRM Chi-Square Goodness-of-Fit Statistic for a 50/50 A/B Test

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `def srm_gof_statistic(n_control: int, n_treatment: int) -> float` in `solution.py`. For an A/B test designed as a 50/50 split, return the Pearson chi-square goodness-of-fit statistic (not the p-value) used to detect Sample Ratio Mismatch (SRM). Requirements: - The observed counts are `f_obs = [n_control, n_treatment]`, and the expected count in each cell is `e = total / 2`, where `total = n_control + n_treatment`. - Return `sum((o - e)**2 / e)` over both cells. This equals `scipy.stats.chisquare(f_obs, [total/2, total/2]).statistic`. - Do not use `scipy.stats.chi2_contingency`. It runs an independence test with Yates' continuity correction and gives a different value. - Return a `float`. Example: ```python srm_gof_statistic(10000, 9800) # -> 2.0202020202020203 ``` ```hint What to compare against Under a 50/50 design each arm should hold half of all users. Work out that expected count first, then measure how far each observed count is from it. ``` ### Constraints & Assumptions - `n_control` and `n_treatment` are non-negative integers. - Assume `total` is at least 1. With both arms empty the expected count is zero and the statistic is undefined. - The design split is exactly 50/50, so both cells share the same expected count. ### What a Strong Answer Covers - How the expected count per arm follows from the total and the 50/50 design. - The per-cell term, the sum over both arms, and the float return type. - Why a contingency-table helper on a 2x2 table returns a different number for the same counts. - Edge cases: an empty experiment, an odd total, and perfectly balanced arms. ### Follow-up Questions - How would you turn this statistic into a p-value, and how many degrees of freedom does it have? - How would you generalize the function to an unequal design split such as 90/10, or to three or more arms? - What p-value threshold would you use to flag SRM, and why might it be stricter than 0.05? - For a fixed relative imbalance, how does the statistic change as the experiment grows, and what does that mean for SRM checks on very large tests?

Overview: Implement a Python function that returns the Pearson chi-square goodness-of-fit statistic used to check a 50/50 A/B test for sample ratio mismatch. It tests whether you can set up expected counts correctly and explain why a contingency-table test with Yates correction gives a different number.

General logo
General
Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement def srm_gof_statistic(n_control: int, n_treatment: int) -> float in solution.py. For an A/B test designed as a 50/50 split, return the Pearson chi-square goodness-of-fit statistic (not the p-value) used to detect Sample Ratio Mismatch (SRM).

Requirements:

  • The observed counts are f_obs = [n_control, n_treatment] , and the expected count in each cell is e = total / 2 , where total = n_control + n_treatment .
  • Return sum((o - e)**2 / e) over both cells. This equals scipy.stats.chisquare(f_obs, [total/2, total/2]).statistic .
  • Do not use scipy.stats.chi2_contingency . It runs an independence test with Yates' continuity correction and gives a different value.
  • Return a float .

Example:

srm_gof_statistic(10000, 9800)  # -> 2.0202020202020203

Constraints & Assumptions

  • n_control and n_treatment are non-negative integers.
  • Assume total is at least 1. With both arms empty the expected count is zero and the statistic is undefined.
  • The design split is exactly 50/50, so both cells share the same expected count.

What a Strong Answer Covers Guidance

  • How the expected count per arm follows from the total and the 50/50 design.
  • The per-cell term, the sum over both arms, and the float return type.
  • Why a contingency-table helper on a 2x2 table returns a different number for the same counts.
  • Edge cases: an empty experiment, an odd total, and perfectly balanced arms.

Follow-up Questions Guidance

  • How would you turn this statistic into a p-value, and how many degrees of freedom does it have?
  • How would you generalize the function to an unequal design split such as 90/10, or to three or more arms?
  • What p-value threshold would you use to flag SRM, and why might it be stricter than 0.05?
  • For a fixed relative imbalance, how does the statistic change as the experiment grows, and what does that mean for SRM checks on very large tests?