Implement the .632 bootstrap error estimator with out-of-bag replicates
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement two functions. `bootstrap_indices` generates bootstrap replicates together with their out-of-bag (OOB) complements. `bootstrap_632` uses them to compute Efron's .632 estimate of mean squared error for a regressor built by `model_factory()`, where each call returns a new object with `fit` and `predict` methods.
Requirements:
- `bootstrap_indices` returns a list of `(train_idx, oob_idx)` pairs. Use a single `np.random.default_rng(random_state)` for all replicates. Each `train_idx` is `rng.integers(0, n_samples, size=n_samples)`, kept in draw order with duplicates, and `oob_idx` is the sorted array of indices that were never drawn.
- `apparent_error` is the MSE, on all the data, of a model fitted on all the data.
- `oob_error` is the mean of the per-replicate OOB MSE, with a fresh model fitted for each replicate. Skip replicates whose OOB set is empty. `n_used` is the number of replicates counted.
- `error_632 = 0.368 * apparent_error + 0.632 * oob_error`.
- Return a dict with the keys `apparent_error`, `oob_error` and `error_632` (floats) and `n_used` (an int).
- If every replicate has an empty OOB set, raise `ValueError`.
```python
def bootstrap_indices(n_samples, n_bootstraps, random_state=0):
...
def bootstrap_632(X, y, model_factory, n_bootstraps=50, random_state=0):
...
```
Example:
```python
from sklearn.linear_model import Ridge
res = bootstrap_632(X, y, lambda: Ridge(alpha=1.0), n_bootstraps=50, random_state=0)
# {'apparent_error': ..., 'oob_error': ..., 'error_632': ..., 'n_used': 50}
# typically apparent_error < error_632 < oob_error
```
```hint Finding the never-drawn indices
The draw contains duplicates, so think about a structure of length n_samples that you can mark at the drawn positions in one vectorized step, then read the unmarked positions back in order.
```
```hint Where state can leak
Check two things: where the random generator is created relative to the replicate loop, and whether any model object is fitted more than once.
```
### Constraints & Assumptions
- `X` has shape `(n_samples, n_features)` and `y` has shape `(n_samples,)`.
- `model_factory()` returns an unfitted regressor with `fit(X, y)` and `predict(X)`, in the style of scikit-learn.
- The output must be reproducible: the same `random_state` gives the same replicates and the same result.
### What a Strong Answer Covers
- Replicate generation from one seeded generator, with training indices kept in draw order and duplicates preserved.
- A correct, sorted OOB complement, the skipping of empty OOB replicates, and the error when none are left.
- A fresh model for the full-data fit and for every replicate, with the apparent error measured on the same data the model was trained on.
- Where the 0.368 and 0.632 weights come from and why blending the two errors reduces bias.
- Time and memory cost in terms of the number of replicates and the model's fit cost.
### Follow-up Questions
- Why can the .632 estimator be biased low for models that overfit, and how does the .632+ variant correct it?
- Why is the expected OOB fraction about 36.8%?
- How would you adapt this for classification error?
Overview: Implement bootstrap resampling with out-of-bag sets and use it to compute Efron's .632 estimate of mean squared error for any regressor. It tests reproducible seeding, handling of replicates with empty out-of-bag sets, and why the apparent and out-of-bag errors are blended with these weights.