LAD Gradient Boosting Regressor From Scratch
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a gradient boosting regressor that minimizes absolute error (least absolute deviation, or LAD) from scratch. You need two helper functions, `lad_initial_prediction(y)` and `lad_pseudo_residuals(y, f)`, and a class `LADGradientBoostingRegressor`. Each boosting round fits a scikit-learn `DecisionTreeRegressor` to the pseudo-residuals, then replaces the tree's leaf values with medians of the raw residuals.
Requirements:
- `lad_initial_prediction` returns the median of `y` as a float. `lad_pseudo_residuals` returns `sign(y - f)`, with `0` at ties.
- The constructor takes `n_estimators`, `learning_rate`, `max_depth` and `random_state`.
- `fit(X, y)` stores `init_`, fits each tree with `DecisionTreeRegressor(max_depth=max_depth, random_state=random_state)` on the pseudo-residuals, and sets each leaf value to the median of `y - f` over the training samples in that leaf.
- Each round updates `f += learning_rate * leaf_value`. Store the trees in `estimators_` and the training MAE after each round in `train_score_` (length `n_estimators`).
- `staged_predict(X)` yields a prediction array after each tree, and `predict(X)` equals the last stage.
- `fit` returns `self`.
Example:
```python
X = [[0], [1], [2], [3], [4], [5]]
y = [1, 2, 3, 10, 20, 30]
m = LADGradientBoostingRegressor(n_estimators=2, learning_rate=0.5, max_depth=1).fit(X, y)
m.init_ # 6.5
list(m.staged_predict(X))
# [[4.25, 4.25, 4.25, 13.25, 13.25, 13.25],
# [2.875, 2.875, 2.875, 11.875, 19.125, 19.125]]
m.train_score_ # [5.5833..., 2.75]
```
```hint Mapping samples to leaves
`tree.apply(X)` returns the node id of each sample's leaf. An array indexed by node id lets you look up the step for every sample at once, both during `fit` and at prediction time.
```
```hint Which outputs to replay
The tree was trained on signs, so `tree.predict` returns sign averages. Decide what you need to store next to each tree so that `staged_predict` reproduces the exact updates made during training.
```
### Constraints & Assumptions
- `X` is a 2-D numeric array of shape `(n_samples, n_features)` and `y` has length `n_samples`. Plain Python lists should also be accepted.
- scikit-learn's `DecisionTreeRegressor` is available. Use `tree.apply` for leaf indices.
- Subsampling and early stopping are out of scope.
### What a Strong Answer Covers
- Correct helper functions: the median as the constant start and the sign of the residual as the pseudo-residual, including the zero at ties.
- Fitting each tree to the signs, then replacing every leaf output with the median of the raw residuals computed before that round's update.
- Storing the per-leaf values so that `staged_predict` and `predict` apply the same shrunken steps as `fit`, starting from `init_`.
- Correct `train_score_` bookkeeping, plus time complexity and edge cases such as `n_estimators = 0` or a constant target.
### Follow-up Questions
- Why re-estimate leaf values instead of using the tree's own outputs?
- How would you extend this to Huber or quantile loss?
- Why is training MAE guaranteed not to increase here?
Overview: Implement a gradient boosting regressor for absolute error from scratch on top of scikit-learn decision trees, with sign pseudo-residuals and per-leaf value updates. Tests how boosting picks step sizes, how staged prediction replays training, and why the training loss never rises.