Build an SRM Report from Named Arm Counts and Allocation Weights

Quick Overview

Write a Python function that runs a chi-square goodness-of-fit sample ratio mismatch check from dictionaries of named arm counts and allocation weights, then returns the statistic, the p-value and a detection flag. It tests vector alignment, weight normalization and input validation.

Build an SRM Report from Named Arm Counts and Allocation Weights

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `srm_report(counts, expected_ratio, alpha=0.0005)` in `solution.py`. `counts` maps arm names to observed unit counts, and `expected_ratio` maps the same arms to allocation weights, which need not sum to 1. Run a chi-square goodness-of-fit Sample Ratio Mismatch (SRM) check and flag SRM when the p-value is below `alpha`. Requirements: - Order both `f_obs` and `f_exp` by `counts` insertion order, with `f_obs` taken from `counts.values()`. - Use `total = sum(counts.values())` and, per arm, `f_exp = total * (weight / sum_of_weights)`. - Call `scipy.stats.chisquare(f_obs, f_exp)`. - Return `{'statistic': float, 'pvalue': float, 'srm_detected': bool}` with `srm_detected = (pvalue < alpha)`, a strict `<`. Example: ```python srm_report({'control': 10000, 'treatment': 9800}, {'control': 1, 'treatment': 1}) # -> {'statistic': 2.0202020202020203, # 'pvalue': 0.15521848968468002, # 'srm_detected': False} ``` With equal weights each arm expects 9,900 units. A p-value of about 0.155 is far above 0.0005, so no SRM is flagged. ```hint Keep the vectors aligned `chisquare` pairs observed and expected values by position, not by arm name. Pick one arm order and build both vectors from it. ``` ```hint Weights are not counts The weights only describe a ratio. Check what `chisquare` needs the expected values to sum to. ``` ### Constraints & Assumptions - `counts` and `expected_ratio` cover the same set of arms, at least two of them. - Counts are non-negative integers. Weights are positive numbers on any scale, so `1:1`, `50:50` and `0.5:0.5` describe the same split. - `scipy` is available. ### What a Strong Answer Covers - Building the observed and expected vectors in one shared arm order. - Normalizing the weights and scaling them to the observed total. - Returning plain Python types and applying the strict `<` against `alpha`. - Input checks for mismatched arm sets, non-positive weights and an empty experiment. ### Follow-up Questions - Why must `f_exp` sum to the same total as `f_obs`? - How would you handle arms present in `counts` but missing from `expected_ratio`, or the reverse? - Why use a default `alpha` as small as 0.0005 for an SRM guardrail? - After SRM is flagged in a three-arm test, how would you find which arm is off?

Overview: Write a Python function that runs a chi-square goodness-of-fit sample ratio mismatch check from dictionaries of named arm counts and allocation weights, then returns the statistic, the p-value and a detection flag. It tests vector alignment, weight normalization and input validation.

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

Implement srm_report(counts, expected_ratio, alpha=0.0005) in solution.py. counts maps arm names to observed unit counts, and expected_ratio maps the same arms to allocation weights, which need not sum to 1. Run a chi-square goodness-of-fit Sample Ratio Mismatch (SRM) check and flag SRM when the p-value is below alpha.

Requirements:

  • Order both f_obs and f_exp by counts insertion order, with f_obs taken from counts.values() .
  • Use total = sum(counts.values()) and, per arm, f_exp = total * (weight / sum_of_weights) .
  • Call scipy.stats.chisquare(f_obs, f_exp) .
  • Return {'statistic': float, 'pvalue': float, 'srm_detected': bool} with srm_detected = (pvalue < alpha) , a strict < .

Example:

srm_report({'control': 10000, 'treatment': 9800},
           {'control': 1, 'treatment': 1})
# -> {'statistic': 2.0202020202020203,
#     'pvalue': 0.15521848968468002,
#     'srm_detected': False}

With equal weights each arm expects 9,900 units. A p-value of about 0.155 is far above 0.0005, so no SRM is flagged.

Constraints & Assumptions

  • counts and expected_ratio cover the same set of arms, at least two of them.
  • Counts are non-negative integers. Weights are positive numbers on any scale, so 1:1 , 50:50 and 0.5:0.5 describe the same split.
  • scipy is available.

What a Strong Answer Covers Guidance

  • Building the observed and expected vectors in one shared arm order.
  • Normalizing the weights and scaling them to the observed total.
  • Returning plain Python types and applying the strict < against alpha .
  • Input checks for mismatched arm sets, non-positive weights and an empty experiment.

Follow-up Questions Guidance

  • Why must f_exp sum to the same total as f_obs ?
  • How would you handle arms present in counts but missing from expected_ratio , or the reverse?
  • Why use a default alpha as small as 0.0005 for an SRM guardrail?
  • After SRM is flagged in a three-arm test, how would you find which arm is off?