Implement the Nadeau-Bengio Corrected Resampled t-Test to Compare Two Models
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement three functions that compare two models on the same data:
```python
def repeated_holdout_splits(n_samples, n_test, n_repeats, random_state): ...
def corrected_resampled_ttest(scores_a, scores_b, n_train, n_test): ...
def compare_estimators(X, y, estimator_a, estimator_b, n_test, n_repeats, random_state): ...
```
`repeated_holdout_splits` generates random hold-out splits. It creates one `np.random.default_rng(random_state)`, and each repeat draws `rng.permutation(n_samples)`. The first `n_test` indices are the test set and the rest are the train set, both sorted. It returns a list of `(train_idx, test_idx)` tuples.
`corrected_resampled_ttest` compares paired scores with the variance-corrected t-test below. The p-value is two-sided, from Student's t with `J - 1` degrees of freedom, where J is the number of pairs. It returns `(t_stat, p_value)` as Python `float`s. When the differences have zero variance, it returns `(0.0, 1.0)` if their mean is 0, and otherwise `(±inf, 0.0)` with the sign of the mean.
```python
d = scores_a - scores_b # J paired differences
t = d.mean() / sqrt((1/J + n_test/n_train) * d.var(ddof=1))
p = 2 * stats.t.sf(abs(t), J - 1)
```
`compare_estimators` runs both models over the splits. For each split it fits a `clone` of each estimator on the train rows and scores it with `.score` on the test rows, then runs the test with `n_train = n_samples - n_test`. It returns a dict with keys `scores_a`, `scores_b` (arrays), `t_stat` and `p_value`.
```hint Detecting zero variance
Decide how you will detect zero variance before you divide. Floating-point arithmetic can leave a small positive variance when every difference is mathematically equal.
```
```hint Keep the comparison paired
Both models must be scored on the same split in the same iteration, and each split needs fresh, unfitted copies of the estimators.
```
### Constraints & Assumptions
- `repeated_holdout_splits` raises `ValueError` unless `n_samples >= 2`, `1 <= n_test < n_samples` and `n_repeats >= 1`.
- `corrected_resampled_ttest` raises `ValueError` for scores that are not 1-D or have unequal lengths, for fewer than 2 pairs, and for `n_train <= 0` or `n_test <= 0`.
- `random_state` goes straight to `np.random.default_rng`, so it may be `None`, an int or a `Generator`.
### What a Strong Answer Covers
- Splits drawn from one generator created once, with disjoint sorted index arrays and the stated validation.
- The correction factor `1/J + n_test/n_train` applied to the sample variance with `ddof=1`, and a two-sided p-value with `J - 1` degrees of freedom.
- Zero-variance handling that returns the specified values and never divides by zero.
- Paired evaluation in which fresh clones of both estimators are fit and scored on the same split.
- Return types that match the contract: Python floats for the test result and arrays in the dict.
### Follow-up Questions
- Why does the naive paired t-test overstate significance here?
- How would you adapt this to repeated k-fold cross-validation?
- How would you test whether model A is better by a margin that matters in practice?
- For a fixed true difference between the models, why do more repeats not push the corrected p-value toward zero?
Overview: Implement repeated hold-out splits and the Nadeau-Bengio corrected resampled t-test to compare two scikit-learn models on the same data. It tests paired evaluation, the variance correction for overlapping training sets, degrees of freedom, and zero-variance edge cases.