Verify Monotonic Constraint on Boosted Ensemble
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
A gradient-boosted regression ensemble is stored as a list of trees. Implement `ensemble_predict`, which returns `base_score + learning_rate * Σ leaf_value` for each row, summing the leaf value each tree assigns to that row. Then implement `check_monotonic_constraint`, which holds each row of `X_ref` fixed and sweeps column `feature` across a set of probe values. The probes are every unique split threshold on that feature across all trees, plus `max_threshold + 1`, in sorted order. The function returns `(is_monotone, max_violation, worst_row_index)`.
```python
def ensemble_predict(trees, base_score, learning_rate, X): ...
def check_monotonic_constraint(trees, base_score, learning_rate, X_ref, feature,
direction, tol=0.0): ...
```
The functions must follow these rules:
- Each tree is a dict of arrays `feature`, `threshold`, `left`, `right` and `value`. Node 0 is the root, a node is a leaf when `left[node] == -1`, and a row goes left when `x[feature[node]] <= threshold[node]`.
- For each pair of consecutive probe predictions with difference `diff`, the violation is `max(0, -diff)` when `direction == 1` and `max(0, diff)` when `direction == -1`. `max_violation` is the largest violation over all rows, returned as a `float`.
- If `max_violation <= tol`, return `(True, max_violation, -1)`. Otherwise return `(False, max_violation, i)`, where `i` is the first row that attains the maximum.
- If no tree splits on `feature`, or `X_ref` is empty, return `(True, 0.0, -1)`.
- A `direction` other than 1 or -1 raises `ValueError`. A 1-D input is treated as a single row.
For example:
```python
tree_a = {"feature": [0, -2, -2], "threshold": [2.0, -2.0, -2.0],
"left": [1, -1, -1], "right": [2, -1, -1], "value": [0.0, 0.0, 4.0]}
# x0 <= 2 -> 0, else 4
tree_b = {"feature": [1, 0, -2, -2, -2], "threshold": [0.5, 3.0, -2.0, -2.0, -2.0],
"left": [1, 2, -1, -1, -1], "right": [4, 3, -1, -1, -1],
"value": [0.0, 0.0, 0.0, -6.0, 0.0]}
# x1 <= 0.5 -> (x0 <= 3 -> 0, else -6), else 0
X_ref = [[0, 1], [0, 0], [5, 0]]
check_monotonic_constraint([tree_a, tree_b], 1.0, 0.5, X_ref, feature=0, direction=1)
# probes = [2, 3, 4]
# row 1 predictions: [1.0, 3.0, 0.0], a drop of 3.0
# Output: (False, 3.0, 1)
```
Row 2 also drops by 3.0 once its first feature is overwritten by the probes, but row 1 is the first row that reaches the maximum.
```hint Where the output can change
Fix every feature except the swept one. Think about what the ensemble's output looks like as a function of that one feature, and at which values it is allowed to change.
```
```hint Which side a probe lands on
A probe placed exactly on a threshold is routed by the `<=` rule. Work out which interval each probe represents, and whether any interval is left without a probe.
```
```hint Empty input
Check what `np.asarray([])` becomes after the 1-D input is reshaped into a single row.
```
### Constraints & Assumptions
- Tree arrays follow scikit-learn's `tree_` layout, and each node's `value` is a scalar.
- Predictions must match `GradientBoostingRegressor.predict` within a relative tolerance of `1e-7`. scikit-learn casts inputs to float32 before routing, so for that comparison assume the inputs are float32 values.
- `tol >= 0`, and all inputs are finite.
### Clarifying Questions to Ask
- Is `max_violation` the largest single step between adjacent probes, or the largest total decrease along the sweep?
- Does the check need to hold for every possible input, or only along sweeps from the given reference rows?
- Can `learning_rate` be zero or negative? A negative value flips the direction of every tree.
### What a Strong Answer Covers
- Vectorised tree routing with the `<=` rule and an additive prediction that matches scikit-learn
- An argument for why the chosen probes are sufficient
- Batched evaluation of the probe grid and the per-row violation computed from consecutive differences
- The return contract: tolerance, first worst row, a never-split feature, empty and 1-D inputs, and an invalid direction
- The cost and memory of the probe grid, and what the check does not prove about inputs outside `X_ref`
### Follow-up Questions
- Why are the thresholds plus one extra point sufficient probes?
- How would you certify monotonicity over the whole input space, beyond the rows in `X_ref`?
- How does this scale to thousands of trees and many reference rows?
- How would you enforce the constraint while training the ensemble?
Overview: Implement prediction for a gradient-boosted tree ensemble, then check whether it is monotone in one feature by sweeping reference rows across that feature's split thresholds. It tests reasoning about piecewise-constant tree models, vectorised probing and exact reporting of the worst violation.