Scenario1 min read

Sample Ratio Mismatch

Automated conversion of SRM.ipynb

Sample Ratio Mismatch

Math of Sample Ratio Mismatch (SRM)

Sample Ratio Mismatch (SRM) is a diagnostic test used in Randomized Controlled Trials (RCTs) to detect if the actual distribution of participants between variants (e.g., Control and Treatment) significantly deviates from the intended design. It uses a Pearson’s Chi-square Goodness-of-Fit test.

1. Setup

  • Variants: kk experimental groups (e.g., k=2k=2 for Control and Treatment).
  • Observed Counts (OiO_i): The actual number of users assigned to variant ii.
  • Total Sample Size (NN): The sum of all observed counts, N=i=1kOiN = \sum_{i=1}^k O_i.
  • Target Allocation (pip_i): The intended probability of assignment for variant ii (e.g., 0.50.5 for a 50/50 split).

2. Expected Counts (EiE_i)

The number of users we expected to see in each variant if the randomization worked perfectly: Ei=N×piE_i = N \times p_i

3. Chi-square Statistic (χ2\chi^2)

We calculate the cumulative squared deviation between observed and expected counts, normalized by the expected counts: χ2=i=1k(OiEi)2Ei\chi^2 = \sum_{i=1}^k \frac{(O_i - E_i)^2}{E_i}

4. Hypothesis Testing

  • Null Hypothesis (H0H_0): The observed counts follow the target distribution (no mismatch).
  • Degrees of Freedom (dfdf): df=k1df = k - 1.
  • P-value: The probability of observing a χ2\chi^2 statistic as extreme as the one calculated, assuming H0H_0 is true. It is derived from the Chi-square distribution: p-value=P(χdf2>χcalculated2)p\text{-value} = P(\chi^2_{df} > \chi^2_{calculated})
  • Decision: If p-value<αp\text{-value} < \alpha (where α\alpha is a conservative threshold like 0.0010.001), we reject H0H_0 and flag an SRM.

Example (50/50 split)

If you intended to split 1,000 users evenly but observed 450 in Control and 550 in Treatment:

  1. Econtrol=1000×0.5=500E_{control} = 1000 \times 0.5 = 500
  2. Etreatment=1000×0.5=500E_{treatment} = 1000 \times 0.5 = 500
  3. χ2=(450500)2500+(550500)2500=2500500+2500500=5+5=10\chi^2 = \frac{(450-500)^2}{500} + \frac{(550-500)^2}{500} = \frac{2500}{500} + \frac{2500}{500} = 5 + 5 = 10
  4. With df=1df=1, a χ2\chi^2 of 10 gives p-value0.0015p\text{-value} \approx 0.0015, indicating a likely mismatch.

Real Example

Result

CausalData(df=(10000, 6), treatment='d', outcome='conversion', confounders=['platform_ios', 'country_usa', 'source_paid'], user_id='user_id')

Result
treatmentcountmeanstdminp10p25medianp75p90max
00.049550.1989910.3992810.00.00.00.00.01.01.0
11.050450.2329040.4227230.00.00.00.00.01.01.0
Result

SRMResult(status=no SRM, p_value=0.36812, chi2=0.8100)

Using without CausalData

Result

SRMResult(status=no SRM, p_value=0.47778, chi2=0.5039)