Simulate validation overfitting from picking the best of many random classifiers
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
A model selection loop can overfit the validation set even when no candidate has learned anything. Simulate this on data whose 0/1 labels are pure noise. Score many random linear classifiers on one validation set, pick the one with the best validation accuracy, and record both its validation accuracy and its accuracy on a fresh test set. Repeat the experiment, then summarize how far the selected validation score sits above the chance-level test score.
Implement four functions:
- `evaluate_candidates(weights, X_val, y_val, X_test, y_test)`. Each column `w` of `weights`, shape `(n_features, n_candidates)`, is a candidate that predicts `1` when `X @ w > 0` and `0` otherwise. Return `(val_acc, test_acc)`, two float arrays holding one accuracy per candidate.
- `select_best_candidate(val_accuracies)`. Return the index of the highest validation accuracy as an `int`, with ties going to the lowest index.
- `simulate_selection_bias(n_val, n_test, n_features, n_candidates, n_repeats, seed)`. Create `rng = np.random.default_rng(seed)` once and, in each repeat, draw the data in exactly the order shown below. Then evaluate the candidates, select the best one, and record it. Raise `ValueError` if any size argument is below 1. Return a `DataFrame` with one row per repeat and columns `repeat` (0-based), `best_candidate` (`int64`), `best_val_accuracy` and `best_test_accuracy` (both `float64`).
- `summarize_selection_bias(table)`. Return a dict with `mean_best_val_accuracy` (the mean of `best_val_accuracy`), `mean_test_accuracy` (the mean of `best_test_accuracy`) and `optimism` (the first minus the second).
Draw order inside each repeat:
```python
X_val = rng.standard_normal((n_val, n_features))
y_val = rng.integers(0, 2, size=n_val)
X_test = rng.standard_normal((n_test, n_features))
y_test = rng.integers(0, 2, size=n_test)
W = rng.standard_normal((n_features, n_candidates))
```
Example:
```python
W = [[1, 0, 1], [0, 1, 1]]
X_val = [[1, 0], [0, 1], [-1, 0], [0, -1]]; y_val = [1, 1, 0, 0]
X_test = [[2, -1], [-1, 2], [1, 1]]; y_test = [0, 0, 1]
evaluate_candidates(W, X_val, y_val, X_test, y_test)
# -> ([0.75, 0.75, 1.0], [2/3, 2/3, 1/3])
select_best_candidate([0.5, 0.7, 0.7, 0.6]) # -> 1
```
The third candidate, `w = (1, 1)`, classifies every validation row correctly and gets only one of the three test rows right.
```hint Score every candidate at once
One matrix product gives the scores of all candidates together. Broadcasting the labels across the candidate columns turns accuracy into a single mean along one axis.
```
```hint Keep the random stream fixed
All five draws in a repeat come from the one generator created before the loop. Drawing `W` before the labels changes the table, and reseeding inside the loop with the same seed makes every repeat a copy of the first.
```
### Constraints & Assumptions
- All five size arguments are integers of at least 1. `seed` is any value `np.random.default_rng` accepts.
- Labels are 0/1 integers. A score of exactly 0 predicts class 0.
- `weights` may arrive as a nested list. Treat it as a float array of shape `(n_features, n_candidates)`.
### What a Strong Answer Covers
- Vectorized per-candidate accuracy on both sets that reproduces the worked example and applies the strict `> 0` rule, so a score of exactly 0 predicts class 0.
- Selection on validation accuracy only, with ties going to the lowest index and the result returned as a Python `int`.
- A simulation that creates one generator, follows the exact draw order, validates every size, and returns the table with the required columns and dtypes.
- A summary whose `optimism` is the mean selected validation accuracy minus the mean test accuracy of the same selected candidates.
- Why the selected validation score is inflated while the test score of the same candidate stays near 0.5.
### Follow-up Questions
- How does the expected optimism change as the number of candidates grows, and as the validation set grows?
- Why is the test accuracy of the selected candidate unbiased here, and what would break that?
- How does this experiment relate to tuning hyperparameters with k-fold cross-validation, and what does nested cross-validation fix?
- If the labels carried real signal, would selecting the best candidate still inflate its validation score?
Overview: Simulate model selection on random labels: score many random linear classifiers on one validation set, keep the best, and compare its validation accuracy with its accuracy on fresh test data. It tests vectorized NumPy scoring, seeded simulation, and why picking the maximum of noisy scores overstates performance.