Implement ensemble diversity diagnostics: ambiguity decomposition, disagreement and Q-statistic matrices
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given `predictions` of shape (M, N) (members × samples) and targets `y` of length N, implement three ensemble diversity diagnostics with NumPy:
```python
def ambiguity_decomposition(predictions, y, weights=None): ...
def disagreement_matrix(predictions, y): ...
def q_statistic_matrix(predictions, y): ...
```
`ambiguity_decomposition` treats the members as regressors and returns a dict of floats: `ensemble_error` (MSE of the weighted mean prediction f), `average_error` (weighted mean of member MSEs) and `average_ambiguity` (weighted mean of each member's mean squared deviation from f).
`disagreement_matrix` and `q_statistic_matrix` treat the members as classifiers and return M×M float arrays computed from per-sample correctness (`prediction == y`). For a pair of members i and k, $N_{11}$ counts the samples both get right, $N_{00}$ the samples both get wrong, $N_{10}$ the samples where i is right and k is wrong, and $N_{01}$ the reverse.
- Disagreement is $(N_{10} + N_{01}) / N$, with 0 on the diagonal.
- Q is $\frac{N_{11} N_{00} - N_{01} N_{10}}{N_{11} N_{00} + N_{01} N_{10}}$, set to 0 when the denominator is 0, with 1 on the diagonal.
Example:
```python
ambiguity_decomposition([[1., 2.], [3., 4.]], [2., 2.])
# {'ensemble_error': 0.5, 'average_error': 1.5, 'average_ambiguity': 1.0}
y = [0, 1, 1, 0, 1]
preds = [[0, 1, 0, 0, 1],
[1, 1, 1, 0, 0],
[0, 1, 1, 0, 1]]
disagreement_matrix(preds, y) # [[0, .6, .2], [.6, 0, .4], [.2, .4, 0]]
q_statistic_matrix(preds, y) # [[1, -1, 0], [-1, 1, 0], [0, 0, 1]]
```
```hint Test your decomposition
Expand each member's squared error around the ensemble mean f. With weights that sum to 1, the three returned numbers satisfy one exact relation, which makes a good unit test.
```
```hint Count all pairs at once
Every pairwise count (both right, both wrong, one right and one wrong) is a dot product of two 0/1 vectors over the samples. One matrix product can give a count for all M×M pairs.
```
### Constraints & Assumptions
- `weights=None` means uniform weights. Otherwise the weights are normalised to sum to 1, so multiplying every weight by the same positive constant does not change the result. A wrong length, any negative entry, or a non-positive sum raises `ValueError`.
- All three functions raise `ValueError` when `predictions` is not 2-D or `y` is not 1-D of length N.
- Class labels may be any integers (multi-class is allowed).
### What a Strong Answer Covers
- Input validation and weight normalisation that match the spec exactly, including the scale invariance it implies.
- The three regression quantities computed from the weighted mean f, the exact relation between them, and the reason it holds.
- Vectorized pairwise counts from a 0/1 correctness matrix, with a stated convention for which member is the "1" in $N_{10}$.
- Handling of the Q diagonal, zero denominators, and the zero diagonal of the disagreement matrix.
- Time and memory cost in terms of M and N.
### Follow-up Questions
- Why can't a classification ensemble use the same ambiguity identity?
- How does the ambiguity decomposition relate to the bias-variance-covariance decomposition?
- How would you use these matrices to prune an ensemble?
Overview: Implement three ensemble diversity diagnostics in NumPy: the ambiguity decomposition of a weighted regression ensemble, plus pairwise disagreement and Q-statistic matrices for classifiers. It tests input validation, weight normalisation, vectorized contingency counts and zero-denominator handling.