Optimize Simplex Ensemble Weights for Log Loss and Brier Score
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Several classifiers have produced class probabilities for the same samples. They are stacked in `probs` with shape `(members, samples, classes)`, and `y` holds one integer label per sample. The ensemble prediction is the weighted average of the member probabilities. Implement these two functions:
```python
def ensemble_loss(probs, y, weights, metric="log_loss", eps=1e-15) -> float: ...
def optimize_weights(probs, y, metric="log_loss", eps=1e-15) -> dict: ...
```
`ensemble_loss` returns the loss of the weighted average of member probabilities. `optimize_weights` finds non-negative, sum-to-one weights that minimize that loss using `scipy.optimize`, and compares the result against equal weights.
Requirements:
- `metric` is `"log_loss"` (mean of `-log(p_true)`, with `p_true` clipped to `[eps, 1-eps]`) or `"brier"` (mean over samples of the squared error summed over classes against the one-hot label).
- `ensemble_loss` normalizes `weights` to sum to 1 and returns a float.
- `optimize_weights` returns a dict with `weights` (shape `(members,)`), `loss`, `equal_loss`, and `improvement = equal_loss - loss`.
- `loss` must never exceed `equal_loss` (fall back to equal weights). A single member gets weight `[1.0]` and improvement `0.0`.
- Raise `ValueError` for an unknown metric, non-3D `probs`, a label count that does not match the number of samples, a wrong weight length, negative weights, or a non-positive weight sum.
Example:
```python
probs = [[[0.8, 0.2], [0.4, 0.6]],
[[0.6, 0.4], [0.2, 0.8]]]
y = [0, 1]
ensemble_loss(probs, y, [1, 1]) # -log(0.7) ≈ 0.3567
ensemble_loss(probs, y, [1, 1], metric="brier") # 0.18
ensemble_loss(probs, y, [3, 1]) # -(log 0.75 + log 0.65) / 2 ≈ 0.3592
```
With weights `[3, 1]` the blend is 0.75 of the first member plus 0.25 of the second, so the true-class probabilities are 0.75 on the first sample and 0.65 on the second.
```hint Blending
The blended probabilities are linear in the weights. One contraction over the member axis gives the `(samples, classes)` array, and the same linearity gives a short analytic gradient.
```
```hint Solver settings
Look for a `scipy.optimize.minimize` method that accepts both bounds and an equality constraint, and compare its default stopping tolerance with the `1e-5` grading tolerance.
```
### Constraints & Assumptions
- `probs` has shape `(members, samples, classes)`, and each member's row for a sample is a probability vector over the classes.
- `y` holds integer labels in `[0, classes)`, one per sample.
- `eps` defaults to `1e-15`.
- Optimized weights are graded against the true optimum (for example, the closed-form two-member Brier solution) with tolerance `1e-5`, so the optimizer has to converge tightly.
### What a Strong Answer Covers
- Computes the loss of the blended probabilities, with the log-loss clipping and the per-sample Brier sum over classes as specified.
- Keeps the weights non-negative and summing to one during the optimization, and converges closely enough to match the exact optimum within `1e-5`.
- Guarantees `loss <= equal_loss` with an explicit fallback and handles the single-member case.
- Raises `ValueError` for each listed bad input and normalizes the weights passed to `ensemble_loss`.
- States the cost per solver iteration and whether a local optimum found by the solver is global.
### Follow-up Questions
- Why can optimized weights overfit, and how would you guard against it?
- How would you avoid an explicit equality constraint?
- Is the log-loss weighting problem convex?
Overview: Implement the loss of a weighted average of classifier probabilities, then find non-negative weights that sum to one and minimize log loss or Brier score with scipy.optimize. It tests constrained optimization over the probability simplex, analytic gradients, input validation and a fallback to equal weights.