Multi-Arm Sample Ratio Mismatch (SRM) Goodness-of-Fit Check in Python

Quick Overview

Implement a multi-arm sample ratio mismatch check in Python that turns unnormalized allocation weights into expected counts and runs one chi-square goodness-of-fit test across all arms. It tests setting up expected frequencies, reporting the statistic, p-value and degrees of freedom, and knowing when the test is reliable.

Multi-Arm Sample Ratio Mismatch (SRM) Goodness-of-Fit Check in Python

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

A sample ratio mismatch (SRM) happens when the arms of an experiment receive a different share of traffic than the design called for. With more than two arms, the standard check is one goodness-of-fit test across all of them. Implement `def srm_multi_arm(counts: list[int], expected_ratio: list[float]) -> dict` in `solution.py`. Given observed per-arm counts and designed allocation weights (same length, all positive, not necessarily summing to 1), run a single chi-square goodness-of-fit SRM test and return the statistic, p-value, and degrees of freedom. The implementation must follow these rules: - `f_obs = counts`, `total = sum(counts)`. - `f_exp = [total * w / sum(expected_ratio) for w in expected_ratio]`. - One `scipy.stats.chisquare(f_obs, f_exp)` over all arms (not pairwise) with default `ddof=0`. - Return `{'statistic': float, 'pvalue': float, 'df': int}` with `df = len(counts) - 1`. Example (values rounded): ```python srm_multi_arm([3300, 3400, 3300], [1, 1, 1]) # -> {'statistic': 2.0, 'pvalue': 0.3679, 'df': 2} ``` ```hint Expected counts The weights are not normalized. Work out what the expected counts have to add up to before you compare them with the observed counts. ``` ### Constraints & Assumptions - `counts` and `expected_ratio` have the same length k, one entry per arm. - Counts are non-negative integers. Weights are positive and need not sum to 1, so `[2, 1, 1]` describes a 50/25/25 split. - `scipy` is available. ### What a Strong Answer Covers - Expected counts built from unnormalized weights that sum to the observed total. - One test across all k arms, with a reason why many pairwise tests at the same alpha raise the false alarm rate. - The degrees of freedom and plain Python return types. - When the chi-square approximation stops being reliable, plus edge cases such as zero traffic or a single arm. ### Follow-up Questions - When would a pairwise SRM check be preferable to one omnibus test? - How would you handle very small expected counts in some arms? - The check flags an SRM with a p-value below 0.001. What would you investigate before trusting the experiment's metric results?

Overview: Implement a multi-arm sample ratio mismatch check in Python that turns unnormalized allocation weights into expected counts and runs one chi-square goodness-of-fit test across all arms. It tests setting up expected frequencies, reporting the statistic, p-value and degrees of freedom, and knowing when the test is reliable.

Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

A sample ratio mismatch (SRM) happens when the arms of an experiment receive a different share of traffic than the design called for. With more than two arms, the standard check is one goodness-of-fit test across all of them.

Implement def srm_multi_arm(counts: list[int], expected_ratio: list[float]) -> dict in solution.py. Given observed per-arm counts and designed allocation weights (same length, all positive, not necessarily summing to 1), run a single chi-square goodness-of-fit SRM test and return the statistic, p-value, and degrees of freedom.

The implementation must follow these rules:

  • f_obs = counts , total = sum(counts) .
  • f_exp = [total * w / sum(expected_ratio) for w in expected_ratio] .
  • One scipy.stats.chisquare(f_obs, f_exp) over all arms (not pairwise) with default ddof=0 .
  • Return {'statistic': float, 'pvalue': float, 'df': int} with df = len(counts) - 1 .

Example (values rounded):

srm_multi_arm([3300, 3400, 3300], [1, 1, 1])
# -> {'statistic': 2.0, 'pvalue': 0.3679, 'df': 2}

Constraints & Assumptions

  • counts and expected_ratio have the same length k, one entry per arm.
  • Counts are non-negative integers. Weights are positive and need not sum to 1, so [2, 1, 1] describes a 50/25/25 split.
  • scipy is available.

What a Strong Answer Covers Guidance

  • Expected counts built from unnormalized weights that sum to the observed total.
  • One test across all k arms, with a reason why many pairwise tests at the same alpha raise the false alarm rate.
  • The degrees of freedom and plain Python return types.
  • When the chi-square approximation stops being reliable, plus edge cases such as zero traffic or a single arm.

Follow-up Questions Guidance

  • When would a pairwise SRM check be preferable to one omnibus test?
  • How would you handle very small expected counts in some arms?
  • The check flags an SRM with a p-value below 0.001. What would you investigate before trusting the experiment's metric results?