Deep Ensemble Uncertainty Decomposition and OOD Flags
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given `means` and `variances` of shape `(M, N)` (M Gaussian ensemble members, N inputs) and optional per-member `weights`, implement `decompose_predictive_uncertainty` returning a tuple `(mean, total_variance, aleatoric, epistemic)` of length-N arrays. Then implement `flag_out_of_distribution` returning a boolean mask where `epistemic > threshold` (strict).
```python
def decompose_predictive_uncertainty(means, variances, weights=None):
...
def flag_out_of_distribution(means, variances, threshold, weights=None):
...
```
Weights are normalized to sum to 1 (default: uniform `1/M`). With normalized weights $w_i$, member means $\mu_i$ and member variances $\sigma_i^2$, computed separately for each of the N inputs:
$$\text{mean} = \sum_i w_i \mu_i, \qquad \text{aleatoric} = \sum_i w_i \sigma_i^2, \qquad \text{epistemic} = \sum_i w_i (\mu_i - \text{mean})^2$$
and `total_variance = aleatoric + epistemic`.
Example:
```text
means = [[0.0], [4.0]], variances = [[2.0], [1.0]], weights = [1.0, 3.0]
Output: mean=[3.0], total_variance=[4.25], aleatoric=[1.25], epistemic=[3.0]
flag_out_of_distribution(means, variances, 2.9, weights=[1.0, 3.0]) -> [True]
```
```hint Vectorize over inputs
Once the weights are a normalized vector of length M, a single matrix-vector product per quantity handles all N inputs at once. Think about which form of the epistemic term stays non-negative in floating point.
```
```hint Order of validation
Check the raw weights for negative and non-finite values before you divide by their sum. A NaN weight fails every comparison, so a test like `w < 0` alone will not catch it.
```
### Constraints & Assumptions
- Raise `ValueError` if inputs are not 2-D, shapes differ, `M < 1`, or any value is non-finite.
- Raise `ValueError` for any negative variance.
- Raise `ValueError` if the length of `weights` is not M, any weight is negative or non-finite, or the weights sum to 0.
- The OOD mask has `dtype == bool` and ignores aleatoric variance.
### What a Strong Answer Covers
- Every listed error case, with the weight checks run on the raw values before normalization.
- A vectorized computation of the mixture mean, aleatoric and epistemic terms over all N inputs using the normalized weights.
- How the epistemic term is computed (weighted, with no sample-size correction) and why it cannot come out negative.
- Why `total_variance = aleatoric + epistemic` holds for a Gaussian mixture.
- An OOD mask built from epistemic variance alone, with a strict comparison and a boolean dtype.
### Follow-up Questions
- How would you decompose uncertainty for a classification ensemble?
- How would you choose the OOD `threshold` in practice?
- Why can a small deep ensemble underestimate epistemic uncertainty?
Overview: Implement uncertainty decomposition for a weighted Gaussian deep ensemble in NumPy, returning the mixture mean, total, aleatoric and epistemic variance for each input. Then flag out-of-distribution inputs with a threshold. Tests the law of total variance, vectorized weighted sums and careful input validation.