Seeded Simulation of FWER Inflation With and Without Bonferroni Correction
Quick Overview
Write a seeded NumPy simulation of many A/A experiments that each test several null metrics, and report the family-wise false-positive rate with and without Bonferroni correction. It tests multiple-testing intuition, reproducible random draws, and how the empirical rate compares with the closed-form value.
Seeded Simulation of FWER Inflation With and Without Bonferroni Correction
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `def simulate_fwer(m: int, n_sims: int, alpha: float, seed: int) -> dict` that simulates `n_sims` A/A experiments, each testing `m` null metrics, and returns the family-wise false-positive rate with and without Bonferroni correction.
- Seed with `rng = np.random.default_rng(seed)` and draw in exactly one call: `z = rng.normal(size=(n_sims, m))`.
- Two-sided p-values: `p = 2 * (1 - scipy.stats.norm.cdf(np.abs(z)))`.
- `'fwer_uncorrected'` (float): the fraction of simulations with any `p <= alpha`.
- `'fwer_bonferroni'` (float): the fraction of simulations with any `p <= alpha / m`.
- `'theoretical_fwer'` (float): `1 - (1 - alpha) ** m`.
- Return exactly these three keys. `numpy` and `scipy.stats` are available.
Example:
```python
simulate_fwer(m=10, n_sims=100_000, alpha=0.05, seed=0)
# {
# 'fwer_uncorrected': about 0.40, # tracks the theoretical value
# 'fwer_bonferroni': about 0.049, # at or below alpha up to simulation noise
# 'theoretical_fwer': 0.40126306... # 1 - 0.95 ** 10
# }
```
```hint What counts as a family-wise error
A simulation contributes a false positive to the family-wise rate if at least one of its `m` metrics rejects. Choose the axis you reduce over with that in mind.
```
```hint Know the target for the corrected rate
Work out `1 - (1 - alpha/m) ** m` for the example so you know what the Bonferroni rate should land near.
```
### Constraints & Assumptions
- `m >= 1`, `n_sims >= 1` and `0 < alpha < 1`.
- Every metric is a true null with an independent standard normal z-score.
- The example holds `100_000 * 10` z-scores, about 8 MB of float64, so building the full matrix in memory is fine.
### What a Strong Answer Covers
- One seeded draw of the whole matrix with the pinned shape and two-sided p-values from the given formula
- A per-simulation "any rejection" reduction followed by the mean over simulations, for both thresholds
- The closed form `1 - (1 - alpha) ** m` and why the empirical uncorrected rate tracks it
- Why Bonferroni keeps the family-wise rate at or below `alpha`, and what it costs in power
### Follow-up Questions
- Why does drawing the matrix in one call matter for reproducibility?
- How would the results change if the metrics were correlated?
- When would you prefer controlling the false discovery rate (FDR) over FWER here?
Overview: Write a seeded NumPy simulation of many A/A experiments that each test several null metrics, and report the family-wise false-positive rate with and without Bonferroni correction. It tests multiple-testing intuition, reproducible random draws, and how the empirical rate compares with the closed-form value.
Implement def simulate_fwer(m: int, n_sims: int, alpha: float, seed: int) -> dict that simulates n_sims A/A experiments, each testing m null metrics, and returns the family-wise false-positive rate with and without Bonferroni correction.
Seed with
rng = np.random.default_rng(seed)
and draw in exactly one call:
z = rng.normal(size=(n_sims, m))
.
Two-sided p-values:
p = 2 * (1 - scipy.stats.norm.cdf(np.abs(z)))
.
'fwer_uncorrected'
(float): the fraction of simulations with any
p <= alpha
.
'fwer_bonferroni'
(float): the fraction of simulations with any
p <= alpha / m
.
'theoretical_fwer'
(float):
1 - (1 - alpha) ** m
.
Return exactly these three keys.
numpy
and
scipy.stats
are available.
Example:
simulate_fwer(m=10, n_sims=100_000, alpha=0.05, seed=0)
# {
# 'fwer_uncorrected': about 0.40, # tracks the theoretical value
# 'fwer_bonferroni': about 0.049, # at or below alpha up to simulation noise
# 'theoretical_fwer': 0.40126306... # 1 - 0.95 ** 10
# }
Constraints & Assumptions
m >= 1
,
n_sims >= 1
and
0 < alpha < 1
.
Every metric is a true null with an independent standard normal z-score.
The example holds
100_000 * 10
z-scores, about 8 MB of float64, so building the full matrix in memory is fine.
What a Strong Answer Covers Guidance
One seeded draw of the whole matrix with the pinned shape and two-sided p-values from the given formula
A per-simulation "any rejection" reduction followed by the mean over simulations, for both thresholds
The closed form
1 - (1 - alpha) ** m
and why the empirical uncorrected rate tracks it
Why Bonferroni keeps the family-wise rate at or below
alpha
, and what it costs in power
Follow-up Questions Guidance
Why does drawing the matrix in one call matter for reproducibility?
How would the results change if the metrics were correlated?
When would you prefer controlling the false discovery rate (FDR) over FWER here?