Bootstrap Bias-Variance Decomposition of Log Loss
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `log_loss_decomposition(probs, y)`, which splits the expected log loss of `R` refit predictions into bias, variance and noise. Then implement `bootstrap_log_loss_decomposition(estimator, X_train, y_train, X_test, y_test, n_boot, seed)`, which builds `probs` from bootstrap refits and decomposes the test loss.
The terms are defined as follows, where `probs` has shape `(R, n, k)` and `t` is the target distribution for each point (one-hot when `y` is 1-D):
```text
q[i] = normalize(exp(mean_r log probs[r, i])) # main_prediction
expected_loss = mean_{r,i} -sum_c t[i,c] * log probs[r,i,c]
noise = mean_i -sum_c t log t (0 log 0 = 0)
bias = mean_i KL(t[i] || q[i])
variance = mean_i mean_r KL(q[i] || probs[r,i])
```
`log_loss_decomposition` returns a dict with `main_prediction` (shape `(n, k)`) and the floats `expected_loss`, `bias`, `variance` and `noise`.
For example, with two refits, one point and two classes:
```python
probs = [[[0.5, 0.5]], [[0.8, 0.2]]]
y = [0]
out = log_loss_decomposition(probs, y)
out["main_prediction"] # [[2/3, 1/3]]
out["bias"] # log(1.5) ≈ 0.4055
out["variance"] # -log(sqrt(0.4) + sqrt(0.1)) ≈ 0.0527
out["expected_loss"] # ≈ 0.4581
out["noise"] # 0.0
```
```hint Work in log space
Average the log probabilities over refits and keep the normalizing constant of the main prediction as its own quantity. Several of the terms simplify once it is written out.
```
```hint Zeros in the targets
One-hot and soft targets contain zeros. Compute `t * log t` so those entries contribute exactly 0 and never turn into `nan`.
```
```hint Column order
Each refit's `predict_proba` columns follow that model's `classes_`, which can be a strict subset of the training labels.
```
### Constraints & Assumptions
- `y` is either 1-D integer labels in `[0, k)` or an `(n, k)` soft-target array. A wrong target shape or a `probs` array that is not 3-D raises `ValueError`.
- Entries of `probs` are positive. The bootstrap path guarantees this by clipping.
- `estimator` is a scikit-learn classifier with `predict_proba`.
- Bootstrap: create `rng = np.random.default_rng(seed)` once, and have each refit draw `idx = rng.integers(0, n, size=n)` in order and fit `clone(estimator)`.
- Classes are `np.unique(y_train)`, and labels may be strings. Map `predict_proba` columns through `model.classes_` (absent classes get 0), clip at `1e-12`, and renormalize rows.
- The bootstrap result also includes `"probs"` of shape `(n_boot, m, k)`, where `m` is the number of test points.
### What a Strong Answer Covers
- The normalized geometric mean as the main prediction, computed in log space, and why it makes noise, bias and variance add up to the expected loss with no residual.
- Target handling: one-hot vectors from labels, soft targets used as given, `0 log 0 = 0`, and `ValueError` on bad shapes.
- Reproducible bootstrap refits with one seeded generator and a fresh `clone` each round.
- Aligning `predict_proba` columns to the full class list when a resample misses a class, then clipping and renormalizing.
- The cost of the refits and of the `(n_boot, m, k)` probability tensor.
### Follow-up Questions
- Why use the normalized geometric mean instead of the arithmetic mean as the main prediction?
- How does a single class missing from a bootstrap sample affect the decomposition?
- How would the regularization strength `C` in `LogisticRegression` shift bias and variance?
Overview: Implement a noise, bias and variance decomposition of log loss that uses the normalized geometric mean of refit probabilities as the main prediction, then build those probabilities from bootstrap refits of a scikit-learn classifier. It tests KL divergence algebra, numerically careful NumPy, and class alignment when a resample misses a label.