Implement a Selective Classifier with Chow's Rule and Bayes-Risk Rejection
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `chow_reject`, `bayes_reject` and `SelectiveClassifier`. Given posteriors `P` of shape `(n, C)`, each function returns per-row class indices as an `int64` array (`-1` for rejected rows) plus a boolean reject mask.
The code must meet these requirements:
- `chow_reject(posteriors, rejection_cost)` returns `(decisions, mask)`. With `d = rejection_cost`, a row is rejected if and only if `max(P) < 1 - d` (strict). Argmax ties go to the lowest index.
- `bayes_reject(posteriors, loss_matrix, rejection_cost)` returns `(decisions, mask, risks)`, where `L[i, j]` is the loss of deciding class `j` when the true class is `i`. `risks` has shape `(n, C+1)`: the columns of `P @ L` followed by `P @ cost`.
- In the Bayes rule, `cost` is a scalar or a length-`C` vector. A row is rejected if and only if its minimum class risk is strictly greater than its reject risk. Argmin ties go to the lowest index.
- Both functions raise `ValueError` for a negative cost. `bayes_reject` also raises it for a `loss_matrix` that is not `(C, C)` or a cost vector of the wrong length.
- `SelectiveClassifier(base_estimator, rejection_cost, loss_matrix=None, reject_label=-1)`: `fit` clones the base estimator, fits the clone, and sets sorted `classes_`. `predict_proba` returns columns in the order of `classes_`. `predict_with_mask` uses Chow's rule when `loss_matrix is None` and the Bayes rule otherwise, and returns `(labels, mask)` with `reject_label` on rejected rows. `predict` returns the labels only.
Example (arrays shown as lists):
```python
chow_reject([[0.7, 0.2, 0.1], [0.75, 0.25, 0.0]], 0.25)
# -> ([-1, 0], [True, False]) # 0.75 == 1 - d is kept
bayes_reject([[0.8, 0.2]], [[0, 1], [5, 0]], 0.6)
# risks = [[1.0, 0.8, 0.6]] -> decisions [-1], mask [True]
```
```hint Abstention is an action
Give rejecting its own expected loss and compare it with the expected loss of the best class decision. Then check what that comparison becomes under 0-1 loss.
```
```hint Watch the label mapping
The base estimator orders its probability columns by its own `classes_`. Also check what indexing your `classes_` array with `-1` returns before you map decisions back to labels.
```
### Constraints & Assumptions
- Posterior rows are non-negative and sum to 1.
- Training labels may be arbitrary integers (for example `10, 11, 12`).
- The base estimator exposes `predict_proba` and `classes_`.
### What a Strong Answer Covers
- Chow's rule with the strict boundary and lowest-index ties, and how it follows from 0-1 loss.
- Expected risk of each class decision and of rejecting, with scalar and per-class rejection costs, and the strict comparison.
- Input validation with the required errors.
- Aligning estimator probabilities with sorted `classes_` and mapping decisions back to labels, including rejected rows.
- Time and space complexity of each path.
### Follow-up Questions
- For which values of the rejection cost does Chow's rule never reject, and why?
- How would you choose the rejection cost to hit a target coverage on held-out data?
- What happens to Chow's rule when the posteriors are poorly calibrated, and how would you fix it?
- How would you evaluate a selective classifier across all rejection thresholds instead of at one cost?
Overview: Implement Chow's reject rule and a Bayes-risk decision rule with a loss matrix, then wrap a probabilistic classifier so it abstains on uncertain rows. It tests expected-loss reasoning, strict boundary handling, cost validation, and aligning predicted probabilities with class labels.