Quantile Gradient Boosting with Pinball Loss
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement the pinball-loss helpers and `QuantileGradientBoosting`, a from-scratch booster that predicts the conditional `alpha`-quantile. Each round fits a `DecisionTreeRegressor` to pseudo-residuals, then replaces its leaf outputs with quantile-based leaf values.
The pieces must behave as follows:
- `pinball_loss(y, pred, alpha)` returns the mean of `alpha * d` where `d >= 0` and `(alpha - 1) * d` where `d < 0`, with `d = y - pred`, as a `float`. A scalar `pred` broadcasts.
- `quantile_initial_prediction(y, alpha)` returns the inverted-CDF quantile: the smallest `y` whose empirical CDF is at least `alpha`, with no interpolation.
- `quantile_pseudo_residuals(y, pred, alpha)` returns `alpha` where `y > pred` and `alpha - 1` where `y <= pred` (ties included).
- `quantile_leaf_values(y, pred, leaf_ids, alpha)` returns a dict from `int` leaf id to `float`: the inverted-CDF `alpha`-quantile of `y - pred` over that leaf's samples.
- `QuantileGradientBoosting(alpha, n_estimators, learning_rate, max_depth, random_state)` has a `fit(X, y)` that returns `self` and sets `init_` and `train_loss_` (length `n_estimators`, the training loss after each stage). The update is `pred += learning_rate * leaf_value`. `staged_predict(X)` yields the prediction after each tree, and `predict(X)` equals the last stage.
Build each tree with `DecisionTreeRegressor(max_depth=max_depth, random_state=random_state)` and use `tree.apply(X)` for leaf ids.
Example:
```python
pinball_loss([1., 2., 3.], [2., 2., 2.], 0.25) # 1/3
quantile_initial_prediction([3,1,4,1,5,9,2,6], 0.5) # 3.0
quantile_leaf_values([1,2,3,4,10,20], [0,0,0,0,5,5],
[3,3,3,3,7,7], 0.5) # {3: 2.0, 7: 5.0}
X = [[0.]]*5 + [[1.]]*5
y = [1,2,3,4,5, 10,11,12,13,14]
m = QuantileGradientBoosting(alpha=0.5, n_estimators=1,
learning_rate=1.0, max_depth=1).fit(X, y)
m.init_ # 5.0
m.predict(X) # [3.]*5 + [12.]*5 (conditional medians)
```
```hint Matching the quantile definition
NumPy's default quantile interpolates between neighboring values. Check which `method` returns an actual sample value and reproduces the `3.0` in the example.
```
```hint Leaf ids
`tree.apply` returns node indices from the whole tree, not 0 to k-1. Store the leaf values in a form that `staged_predict` can index on new data, where there is no `y`.
```
### What a Strong Answer Covers
- Correct pinball loss and pseudo-residuals, including the tie rule at `y == pred` and broadcasting of a scalar prediction.
- An inverted-CDF quantile with no interpolation, used for both the initial prediction and the leaf values, that matches the example outputs.
- A mapping from `tree.apply` node ids to stored leaf values, so that `fit`, `staged_predict` and `predict` produce the same stage-by-stage predictions.
- Why the tree is fit to the sub-gradient only for its partition, and why the per-leaf quantile is the line-search step for pinball loss.
- Time complexity and edge cases such as a tree that cannot split.
### Follow-up Questions
- Why replace the tree's own leaf outputs with quantile leaf values? What would happen to the example if you kept the tree's mean predictions?
- Show that the training loss cannot increase when `learning_rate <= 1`. What can happen with a larger learning rate?
- Models trained separately for `alpha = 0.1`, `0.5` and `0.9` can produce crossing quantiles. How would you detect and prevent that?
- How would you add sample weights to the loss, the initial prediction and the leaf values?
Overview: Implement quantile gradient boosting from scratch in Python with NumPy and scikit-learn regression trees: pinball loss, inverted-CDF quantiles, sub-gradient pseudo-residuals and per-leaf quantile updates. It tests whether you can match an exact spec and explain why each leaf needs its own line search for pinball loss.