Bias-Variance Remediation Triage Engine
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `triage(train_loss, val_loss, target_loss, n_samples, n_features, tol=0.01, ratio_threshold=10.0)`. Compute `bias_gap = max(train_loss - target_loss, 0)` and `variance_gap = max(val_loss - train_loss, 0)`. A gap is flagged only when strictly greater than `tol`. Return a diagnosis plus ranked remedial actions.
The data is data-rich when `n_samples / n_features >= ratio_threshold`. Each action's score is its gap times its weight, and only actions for flagged gaps are emitted. The weights are:
| Gap | Action | Weight when data-rich | Weight otherwise |
|---|---|---|---|
| `bias_gap` | `increase_capacity` | 1.0 | 1.0 |
| `bias_gap` | `add_features` | 1.0 | 0.25 |
| `bias_gap` | `reduce_regularization` | 0.75 | 0.75 |
| `bias_gap` | `train_longer` | 0.5 | 0.5 |
| `variance_gap` | `collect_more_data` | 0.5 | 1.0 |
| `variance_gap` | `reduce_features` | 0.25 | 0.75 |
| `variance_gap` | `increase_regularization` | 0.75 | 0.75 |
| `variance_gap` | `reduce_capacity` | 0.5 | 0.5 |
Sort the actions by descending score, with ties broken by the order of the rows above.
Return a dict with `diagnosis` (one of `'good_fit'`, `'high_bias'`, `'high_variance'`, `'high_bias_and_variance'`), `bias_gap` and `variance_gap` (floats), `actions` (list of str) and `scores` (float `np.ndarray` aligned with `actions`, shape `(0,)` when empty).
```hint Separate the steps
Validation, the two flags, the data-rich switch and scoring are independent steps. Get each one right on its own before you sort.
```
```hint Watch the tie rule
Two actions can share a score, including a bias action and a variance action in a combined diagnosis. Build a sort key that makes the order unique.
```
### Example
```text
triage(0.5, 0.5, 0.25, 50, 10)
diagnosis: 'high_bias', bias_gap: 0.25, variance_gap: 0.0
actions: ['increase_capacity', 'reduce_regularization', 'train_longer', 'add_features']
scores: [0.25, 0.1875, 0.125, 0.0625]
```
The ratio 50 / 10 = 5 is below 10, so the data is not rich and `add_features` gets weight 0.25. The variance gap is 0, so no variance actions appear.
### Constraints & Assumptions
- Raise `ValueError` if any loss is negative or non-finite, or if `tol` is negative or non-finite.
- Raise `ValueError` unless `n_samples` and `n_features` are integers of at least 1 (`bool` is rejected).
### What a Strong Answer Covers
- Input validation for the three losses, `tol` and the two counts, including the rejection of `bool`.
- The clipped gaps, the strict comparison against `tol`, and the mapping from the two flags to the four diagnosis labels.
- The data-rich switch and its effect on the three context-dependent weights, with actions emitted only for flagged gaps.
- A deterministic order (descending score, canonical tie-break) and the required return types, including the empty `scores` array.
- Why scaling by the gap and using the samples-per-feature ratio are reasonable triage rules, and where they mislead.
### Follow-up Questions
- How would you account for noise in the loss estimates before flagging a gap?
- How would you use learning curves instead of a single snapshot?
- How would you make the action weights configurable and learnable?
Overview: Implement a rule-based function that diagnoses high bias, high variance or both from training, validation and target losses, then ranks remedial actions by gap-weighted scores. It tests careful handling of a written spec, input validation, deterministic tie-breaking and practical judgment about underfitting and overfitting.