Huber Loss Gradient Boosting Step with Friedman's Leaf Line Search
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement one gradient boosting round under Huber loss. Write the loss, its pseudo-residuals, Friedman's per-leaf line search, and the step that combines them. Throughout, `r = y - raw`.
- `huber_loss` returns the mean of `0.5*r**2` if `|r| <= delta`, else `delta*(|r| - delta/2)`, as a `float`.
- `huber_negative_gradient` returns `r` if `|r| <= delta`, else `delta*sign(r)`, as a per-sample array.
- `huber_leaf_value` returns `med + mean(sign(d)*min(|d|, delta))`, where `med = median(residuals)` and `d = residuals - med`.
- `huber_boosting_step` fits `DecisionTreeRegressor(max_depth, random_state)` to the pseudo-residuals, groups samples with `tree.apply(X)`, and computes each leaf value from the raw residuals `y - raw` of that leaf.
- The step returns `(new_raw, leaf_values)`, with `new_raw = raw + learning_rate*leaf_value` per sample and `leaf_values` a dict keyed by `int` leaf id. Do not mutate `raw`.
```python
def huber_loss(y, raw, delta): ...
def huber_negative_gradient(y, raw, delta): ...
def huber_leaf_value(residuals, delta): ...
def huber_boosting_step(X, y, raw, delta, learning_rate=0.1, max_depth=3, random_state=0): ...
```
Example:
```python
y, raw = np.zeros(4), np.array([0.5, -3.0, 1.0, 2.0])
huber_loss(y, raw, 1.0) # 1.15625
huber_negative_gradient(y, raw, 1.0) # [-0.5, 1.0, -1.0, -1.0]
huber_leaf_value(np.array([1., 2., 3., 10.]), 2.0) # 2.625
```
```hint Two kinds of residual
Keep the clipped pseudo-residuals and the raw residuals `y - raw` as separate arrays. Each one feeds exactly one part of the step, and mixing them up still runs without an error.
```
```hint Check the limits
Work out what `huber_leaf_value` returns when `delta` is very large and when `delta = 0`. Both answers should be familiar statistics.
```
### Constraints & Assumptions
- `delta >= 0` and all inputs are finite.
- `y`, `raw` and the rows of `X` have equal length.
- `sklearn.tree.DecisionTreeRegressor` is available.
### What a Strong Answer Covers
- A vectorized Huber loss and negative gradient that return a float and a per-sample array.
- Friedman's per-leaf line search computed from each leaf's raw residuals, and how it behaves at small and large `delta`.
- A step that fits the tree to the pseudo-residuals, groups rows with `tree.apply(X)`, returns int-keyed leaf values and leaves `raw` unchanged.
- Why the tree's own leaf means are replaced before the update.
- The cost of one step and edge cases such as `delta = 0`.
### Follow-up Questions
- Why fit the tree on pseudo-residuals but set leaf values from raw residuals?
- How should `delta` be chosen in practice?
- Why is a one-step line search used instead of exactly minimizing Huber loss per leaf?
Overview: Implement one gradient boosting round under Huber loss: the loss, its clipped pseudo-residuals, Friedman's per-leaf line search and the update that ties them to a regression tree. It tests outlier-resistant regression, the split versus leaf-value distinction and careful NumPy work.