Compute the Chi-Square Goodness-of-Fit P-Value for a 50/50 Sample Ratio Mismatch (SRM) Check
Quick Overview
Write a Python function that returns the chi-square goodness-of-fit p-value for a sample ratio mismatch check on a 50/50 A/B test. It tests scaling expected counts to the observed total, choosing a goodness-of-fit test over a contingency test, and reading the p-value for an SRM decision.
Compute the Chi-Square Goodness-of-Fit P-Value for a 50/50 Sample Ratio Mismatch (SRM) Check
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `def srm_pvalue(n_control: int, n_treatment: int) -> float` in `solution.py`. Return the p-value of a chi-square goodness-of-fit test (Sample Ratio Mismatch check) of the observed counts against an expected 50/50 split.
The implementation must follow these rules:
- `f_obs = [n_control, n_treatment]`, `f_exp = [total * 0.5, total * 0.5]` where `total = n_control + n_treatment`.
- Scale `f_exp` to the total of `f_obs`. Passing the raw proportions `[0.5, 0.5]` raises `ValueError` in recent SciPy versions.
- Call `scipy.stats.chisquare(f_obs, f_exp)` and return `.pvalue` cast to `float`.
- Do not use `scipy.stats.chi2_contingency` (it tests independence and applies the Yates correction on 2x2 tables).
Example (value rounded):
```python
srm_pvalue(10000, 9800)
# -> 0.1552
```
```hint Which test
Ask what the null hypothesis is here: a fixed, known distribution of traffic, or a relationship between two variables. That decides which SciPy function fits.
```
### Constraints & Assumptions
- Both counts are non-negative integers counted at assignment, and the design is a 50/50 split.
- `scipy` is available.
### What a Strong Answer Covers
- Expected counts scaled to the observed total.
- Why a goodness-of-fit test fits this null hypothesis and a contingency test does not.
- A plain Python `float` return value.
- How to read the p-value for an SRM decision, and edge cases such as zero traffic.
### Follow-up Questions
- What would `chi2_contingency` compute if you used it here, and why is its p-value different?
- How does this p-value relate to a two-sided z-test on the control share?
- What p-value threshold would you use to raise an SRM alarm on a platform that runs many experiments?
Overview: Write a Python function that returns the chi-square goodness-of-fit p-value for a sample ratio mismatch check on a 50/50 A/B test. It tests scaling expected counts to the observed total, choosing a goodness-of-fit test over a contingency test, and reading the p-value for an SRM decision.
Implement def srm_pvalue(n_control: int, n_treatment: int) -> float in solution.py. Return the p-value of a chi-square goodness-of-fit test (Sample Ratio Mismatch check) of the observed counts against an expected 50/50 split.
The implementation must follow these rules:
f_obs = [n_control, n_treatment]
,
f_exp = [total * 0.5, total * 0.5]
where
total = n_control + n_treatment
.
Scale
f_exp
to the total of
f_obs
. Passing the raw proportions
[0.5, 0.5]
raises
ValueError
in recent SciPy versions.
Call
scipy.stats.chisquare(f_obs, f_exp)
and return
.pvalue
cast to
float
.
Do not use
scipy.stats.chi2_contingency
(it tests independence and applies the Yates correction on 2x2 tables).
Example (value rounded):
srm_pvalue(10000, 9800)
# -> 0.1552
Constraints & Assumptions
Both counts are non-negative integers counted at assignment, and the design is a 50/50 split.
scipy
is available.
What a Strong Answer Covers Guidance
Expected counts scaled to the observed total.
Why a goodness-of-fit test fits this null hypothesis and a contingency test does not.
A plain Python
float
return value.
How to read the p-value for an SRM decision, and edge cases such as zero traffic.
Follow-up Questions Guidance
What would
chi2_contingency
compute if you used it here, and why is its p-value different?
How does this p-value relate to a two-sided z-test on the control share?
What p-value threshold would you use to raise an SRM alarm on a platform that runs many experiments?