Simulate the false positive rate of naive vs Bonferroni-corrected peeking in an A/A test
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Write `compare_peeking_correction` in `solution.py`. It simulates `n_sims` A/A experiments in which both arms draw from `N(0,1)`, peeks at the running z-statistic `n_looks` times in each experiment, and returns the fraction of experiments that produce a false positive under a naive cutoff and under a Bonferroni-corrected cutoff.
```python
def compare_peeking_correction(n_looks: int, per_look: int, alpha: float = 0.05,
n_sims: int = 4000, seed: int = 0) -> dict:
...
```
Requirements:
- Create the generator with `rng = np.random.default_rng(seed)`. At each look, draw `per_look` fresh `N(0,1)` values for arm A, then `per_look` values for arm B, and add them to the running totals.
- At each look compute `z = (mean_A - mean_B) / sqrt(2/n)`, where `n` is the running per-arm sample size.
- Apply the same cutoffs at every look: `z_naive = norm.ppf(1 - alpha/2)` and `z_bonf = norm.ppf(1 - (alpha/n_looks)/2)`.
- A rule has a false positive in an experiment when `|z| >= cutoff` at any look. Evaluate both rules on the same simulated stream, with no resampling.
- Return `{'naive_fpr': <float>, 'bonferroni_fpr': <float>}`.
```hint Count the chances
Under the null, ask how many separate opportunities one experiment gets to cross the naive cutoff, and whether a single crossing is enough to declare a result.
```
```hint One stream, two flags
Carry one flag per rule through the looks of an experiment, and think about what an early `break` would do to the random numbers the next experiment receives.
```
### Constraints & Assumptions
- Both arms have mean 0 and variance 1, so every rejection is a false positive.
- The variance is known to be 1, so `sqrt(2/n)` is the exact standard error of the difference in means and the cutoffs come from the normal distribution.
- Every experiment draws all `n_looks` looks, including looks after a crossing, so the output is reproducible for a given `seed`.
- Defaults are `alpha = 0.05`, `n_sims = 4000` and `seed = 0`. `numpy` and `scipy.stats.norm` are available.
### What a Strong Answer Covers
- A simulation that follows the specified draw order and cumulative update, so the result is reproducible for a seed.
- The running z-statistic with the correct standard error, and both cutoffs derived from `alpha` and `n_looks`.
- Any-look crossing logic for both rules on one shared stream.
- Why naive peeking inflates the false positive rate and why the Bonferroni rate lands below `alpha` here.
- Complexity and a sanity check such as `n_looks = 1`.
### Follow-up Questions
- Why does naive peeking inflate the false positive rate while a single fixed-sample test does not?
- Why is Bonferroni conservative here, and what would you use instead?
- How would you reduce Monte Carlo noise in the estimated false positive rates?
Overview: Implement a Monte Carlo simulation of A/A tests with repeated peeking and compare the false positive rate of a naive cutoff with a Bonferroni-corrected cutoff. Tests understanding of sequential testing, running z-statistics and reproducible seeded simulation in Python.