Greedy ensemble selection with replacement (Caruana) to minimize validation MSE
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
You have validation predictions from a library of trained models, stored as an `(n_members, n_samples)` matrix `preds`, and the validation targets `y`. Implement `greedy_ensemble_selection(preds, y, max_size)`, which builds an ensemble by greedy forward selection with replacement (Caruana's ensemble selection). A member can be picked more than once, and the ensemble prediction is the plain mean of all picks. The goal is to minimize the mean squared error against `y`. Return `(selected, counts, weights, history)`.
- At each step with k picks already made, score every member `m` as `mean(((running_sum + preds[m]) / (k + 1) - y) ** 2)`, where `running_sum` is the sum of the picked predictions, and pick the lowest score. Ties go to the lowest index.
- Stop when the best score is not strictly below the current score (initially `inf`), or when `max_size` picks have been made.
- `selected` is the list of picked indices in order. `counts` is an int array of length `n_members`. `weights` is `counts / len(selected)`. `history` holds the ensemble MSE after each pick.
- Raise `ValueError` if `preds` is not 2-D, if `y` is not 1-D with length `n_samples`, or if `max_size < 1`.
Example:
```text
Input: preds = [[3.0, 3.0], [0.0, 0.0]], y = [1.0, 1.0], max_size = 10
Output: selected = [1, 0, 1]
counts = [1, 2]
weights = [1/3, 2/3]
history = [1.0, 0.25, 0.0]
```
After three picks the ensemble predicts exactly 1.0. The best fourth pick would score 0.0625, which does not beat 0.0, so selection stops.
```hint Cheap candidate scoring
You never need to rebuild the ensemble from scratch. Think about what you can carry from one step to the next so that scoring a candidate costs one vector operation.
```
```hint Stopping
Keep the current ensemble score separate from the candidate scores, and compare them with a strict inequality. Walk the example by hand to confirm where your loop stops.
```
### Constraints & Assumptions
- `n_members` and `n_samples` are between 1 and 1,000. All values are finite.
- `max_size` counts picks including repeats.
- The first pick always improves on `inf`, so `selected` is never empty for valid input.
### What a Strong Answer Covers
- A greedy loop that scores each candidate's averaged prediction by MSE using a running sum, with ties broken toward the lowest index.
- The strict-improvement stop and the `max_size` cap, reproducing the example exactly.
- Correct outputs: integer counts, weights that sum to 1 and reproduce the ensemble prediction, and a strictly decreasing history.
- Input validation, time and space complexity, and the fact that greedy selection is not guaranteed to find the best weights.
### Follow-up Questions
- Why does selection with replacement matter compared to selection without replacement?
- How would you reduce overfitting to the validation set used for selection?
- How would you generalize this to an arbitrary metric such as AUC or log loss?
Overview: Implement Caruana-style greedy ensemble selection with replacement, adding validation models one pick at a time to minimize the mean squared error of their averaged prediction. Tests running-sum scoring, deterministic tie and stopping rules, weights learned from pick counts, and ways to limit overfitting to the validation set.