Check A/A Test P-Values for Uniformity With a Kolmogorov-Smirnov Test
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `aa_pvalue_uniformity(n_sims, n_per_group, seed)`. Simulate `n_sims` A/A comparisons and test whether the resulting p-values are Uniform(0, 1) with a one-sample Kolmogorov-Smirnov test.
Requirements:
- Create one `np.random.default_rng(seed)` and reuse it across every iteration.
- In each iteration, draw `control = rng.normal(0, 1, n_per_group)` first, then `treatment = rng.normal(0, 1, n_per_group)`.
- Compute the p-value with `scipy.stats.ttest_ind(control, treatment, equal_var=False).pvalue` (Welch, two-sided).
- Test all collected p-values with `scipy.stats.kstest(pvals, "uniform")`.
- Return `{"ks_statistic": float, "ks_pvalue": float, "is_uniform": bool}` with `is_uniform = (ks_pvalue > 0.05)`.
Example:
```python
aa_pvalue_uniformity(2000, 500, 123)
# -> {'ks_statistic': 0.017567772142505045,
# 'ks_pvalue': 0.5615650356480729,
# 'is_uniform': True}
```
```hint Why uniform
Under the null, what is the probability that a correctly calibrated test returns a p-value at or below 0.3? At or below any threshold t?
```
```hint Reading the KS result
A large KS p-value and a small one do not support the same kind of conclusion. Think about what each one lets you claim about the test being checked.
```
### What a Strong Answer Covers
- Why p-values from a calibrated test are Uniform(0, 1) when the null is true
- The single generator and the fixed draw order that make the result reproducible
- Correct calls to Welch's t-test and the one-sample KS test, with plain Python types in the returned dict
- What a KS p-value above 0.05 does and does not show about calibration
### Follow-up Questions
- Why does the draw order (`control` before `treatment`) matter for reproducibility?
- Is `ks_pvalue > 0.05` a reliable pass criterion?
- In a real A/A test on production traffic, what would make the p-values non-uniform?
- How would you vectorize the simulation without changing its output?
Overview: Simulate many A/A tests with Welch's t-test and check whether the resulting p-values follow a Uniform(0, 1) distribution using a one-sample Kolmogorov-Smirnov test. It tests p-value calibration, reproducible random number generation, and how to read a fail-to-reject result.