Weighted Geometric-Mean Probability Ensemble
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `geometric_mean_ensemble(probas, weights=None, eps=1e-15)`. Given member probabilities `probas` of shape `(n_members, n_samples, n_classes)`, return an `(n_samples, n_classes)` array where each row is proportional to $\prod_m p_m^{w_m}$ and renormalized to sum to 1. Compute in log space after clipping probabilities to `[eps, 1]`.
For example:
```python
probas = [[[0.8, 0.2]], [[0.5, 0.5]]] # 2 members, 1 sample
geometric_mean_ensemble(probas)
# -> [[0.6667, 0.3333]] sqrt(0.4) : sqrt(0.1) = 2 : 1
geometric_mean_ensemble([[[1.0, 0.0]], [[0.5, 0.5]]], eps=1e-4)
# -> [[0.9901, 0.0099]] 1/1.01 and 0.01/1.01, because 0.0 is clipped to 1e-4
```
```hint Products become sums
A product of powers is a weighted sum of logs. Look for one NumPy call that contracts the member axis of the log array against the weight vector.
```
```hint Zeros in two places
Work out what `0 * log(0)` evaluates to in floating point, and check that a zero-weight member really drops out of your result.
```
### Constraints & Assumptions
- `weights=None` means equal weights. Otherwise weights are normalized to sum to 1 (scale-invariant), and a zero weight ignores that member.
- Raise `ValueError` if `probas` is not 3-D, has zero members, `len(weights) != n_members`, any weight is negative or non-finite, or the weights sum to 0 or less.
- Output must be finite, non-negative, and rows sum to 1 (within `1e-7`).
### What a Strong Answer Covers
- Validation that raises `ValueError` for every listed case, and weight normalization that makes the output independent of the weights' scale.
- Where the clip happens relative to the log, and how that keeps zero probabilities and zero-weight members from producing `-inf` or `NaN`.
- A vectorized combination over the member axis with no Python loop over samples or classes.
- A renormalization that stays numerically stable, with an argument for why it does not change the result.
- Time and space complexity in terms of members, samples and classes.
### Follow-up Questions
- Why subtract the row max before exponentiating?
- When would you prefer the geometric mean over arithmetic averaging?
- How would you learn the weights instead of fixing them?
Overview: Implement a weighted geometric-mean ensemble in NumPy that combines class probabilities from several models in log space and renormalizes each row to sum to 1. It tests input validation, weight normalization, handling of zero probabilities, vectorized array code and numerically stable normalization.