Compare the SRM Goodness-of-Fit Statistic with a Yates-Corrected Contingency Test

Quick Overview

Implement a Python function that returns both the chi-square goodness-of-fit statistic for a sample ratio mismatch check and the Yates-corrected 2x2 contingency statistic for the same counts. It tests whether you know which null hypothesis fits SRM and why the two numbers differ.

Compare the SRM Goodness-of-Fit Statistic with a Yates-Corrected Contingency Test

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `srm_vs_independence_chi2(n_control: int, n_treatment: int, expected_control_frac: float) -> dict` in `solution.py`. You are given the observed arm counts `[n_control, n_treatment]` and the designed split `expected_control_frac / (1 - expected_control_frac)`. Compute both the correct Sample Ratio Mismatch (SRM) goodness-of-fit statistic and the Yates-corrected 2x2 contingency statistic that is often used for SRM by mistake. Requirements: - Let `total = n_control + n_treatment`. - `srm_statistic` is `scipy.stats.chisquare([n_control, n_treatment], [total*expected_control_frac, total*(1-expected_control_frac)]).statistic`. - `contingency_statistic_yates` is `scipy.stats.chi2_contingency([[n_control, total*expected_control_frac], [n_treatment, total*(1-expected_control_frac)]]).statistic` with the default `correction=True`. - Return a dict with exactly the keys `'srm_statistic'` and `'contingency_statistic_yates'`, both values as floats. - `scipy` is available. Example call: ```python srm_vs_independence_chi2(10000, 9800, 0.5) # -> {'srm_statistic': 2.0202..., 'contingency_statistic_yates': 0.9900...} ``` To see the Yates correction on its own, take the illustrative 2x2 table `[[500, 500], [450, 550]]`: ```python # chi2_contingency default (Yates) -> 4.814035087719298 # chi2_contingency correction=False -> 5.012531328320803 ``` ```hint Which null hypothesis Write down the null hypothesis each SciPy function tests. Only one of them treats the designed split as fixed. ``` ```hint Where the contingency expected counts come from For the 2x2 table, work out the row and column totals. The independence test builds its expected counts from those margins, so check what that does to each cell's deviation. ``` ### Constraints & Assumptions - `n_control` and `n_treatment` are non-negative integers, and `total` is at least 1. - `0 < expected_control_frac < 1`, so both expected counts are positive. - Expected counts can be non-integers (a 1/3 split of 1,000 users gives 333.33); pass them as floats. ### What a Strong Answer Covers - Scaling the design fractions to expected counts that sum to the observed total. - Calling each SciPy function as pinned, keeping the default Yates correction, and returning exactly the two float-valued keys. - The null hypothesis each test checks, and which one matches the SRM question. - How the Yates correction changes the statistic and when that change matters. ### Follow-up Questions - Why is the goodness-of-fit test the right choice for SRM, and what goes wrong with `chi2_contingency`? - When does the Yates correction meaningfully change the conclusion? - With `correction=False`, the contingency statistic is still far from the SRM statistic. Roughly how far, and why? - How would you run the SRM check for an experiment with three or more arms?

Overview: Implement a Python function that returns both the chi-square goodness-of-fit statistic for a sample ratio mismatch check and the Yates-corrected 2x2 contingency statistic for the same counts. It tests whether you know which null hypothesis fits SRM and why the two numbers differ.

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

Implement srm_vs_independence_chi2(n_control: int, n_treatment: int, expected_control_frac: float) -> dict in solution.py. You are given the observed arm counts [n_control, n_treatment] and the designed split expected_control_frac / (1 - expected_control_frac). Compute both the correct Sample Ratio Mismatch (SRM) goodness-of-fit statistic and the Yates-corrected 2x2 contingency statistic that is often used for SRM by mistake.

Requirements:

  • Let total = n_control + n_treatment .
  • srm_statistic is scipy.stats.chisquare([n_control, n_treatment], [total*expected_control_frac, total*(1-expected_control_frac)]).statistic .
  • contingency_statistic_yates is scipy.stats.chi2_contingency([[n_control, total*expected_control_frac], [n_treatment, total*(1-expected_control_frac)]]).statistic with the default correction=True .
  • Return a dict with exactly the keys 'srm_statistic' and 'contingency_statistic_yates' , both values as floats.
  • scipy is available.

Example call:

srm_vs_independence_chi2(10000, 9800, 0.5)
# -> {'srm_statistic': 2.0202..., 'contingency_statistic_yates': 0.9900...}

To see the Yates correction on its own, take the illustrative 2x2 table [[500, 500], [450, 550]]:

# chi2_contingency default (Yates)   -> 4.814035087719298
# chi2_contingency correction=False  -> 5.012531328320803

Constraints & Assumptions

  • n_control and n_treatment are non-negative integers, and total is at least 1.
  • 0 < expected_control_frac < 1 , so both expected counts are positive.
  • Expected counts can be non-integers (a 1/3 split of 1,000 users gives 333.33); pass them as floats.

What a Strong Answer Covers Guidance

  • Scaling the design fractions to expected counts that sum to the observed total.
  • Calling each SciPy function as pinned, keeping the default Yates correction, and returning exactly the two float-valued keys.
  • The null hypothesis each test checks, and which one matches the SRM question.
  • How the Yates correction changes the statistic and when that change matters.

Follow-up Questions Guidance

  • Why is the goodness-of-fit test the right choice for SRM, and what goes wrong with chi2_contingency ?
  • When does the Yates correction meaningfully change the conclusion?
  • With correction=False , the contingency statistic is still far from the SRM statistic. Roughly how far, and why?
  • How would you run the SRM check for an experiment with three or more arms?