Foundations to Frontier
Tests
D. Classical Machine Learning › D7 Ensemble Methods

Gradient Boosting for Regression (Full Derivation)

Gradient boosting (Friedman, 2001) builds a model as a sum of small trees added one at a time. Each new tree is fitted to the negative gradient of the loss with respect to the current predictions (the pseudo-residuals), its leaf values are optimised for the loss, and the result is added with a small learning rate. For squared loss this is literally fitting the residuals; other losses (absolute, Huber) make the same algorithm robust to outliers. This page derives the algorithm as gradient descent in function space, works through the squared and absolute losses, explains shrinkage, tree depth and the number of trees, and connects it to the bias-variance trade-off. In the exercises you implement gradient boosting with squared and absolute loss; then you map how shrinkage, the number of trees and tree depth interact on a standard benchmark.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Fix what’s left, a little at a time

Start with a crude prediction, such as the average. Look at what’s still wrong, the residuals, and fit a small tree to them. Add a fraction of that tree to your model. Look at the new residuals, fit another small tree, add a fraction, and repeat hundreds of times. Each tree only nudges the model toward what the previous trees missed. That is gradient boosting, the method behind XGBoost, LightGBM and CatBoost and the usual winner on tabular data.

Gradient boosting: fit the residuals, take a small step, repeat
stages →
training MSE 0.143 · test MSE 0.218 (best 0.161 at M = 45, ringed; noise variance 0.09)
With ν = 1, each tree jumps all the way to the residuals and the model overfits within a few stages. Smaller ν takes cautious steps: more stages are needed, but the best test error is usually lower and the minimum is flatter. Deeper trees fit faster and overfit sooner. Training error always keeps falling; test error does not.
Why engineers care

Gradient-boosted trees are the most accurate off-the-shelf models for tabular engineering data: telemetry regression, load and remaining-life prediction, anomaly scoring. Understanding the derivation tells you which knobs matter (learning rate, number of trees, depth), why early stopping is essential, and how to swap in a loss that matches your problem.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Each new tree in L2 gradient boosting is fitted to…
  2. 2. The learning rate ν…
  3. 3. In the demo, with ν = 1 the test error…

Level 1: Beginner

★★☆☆☆ Basic⏱ 30 minSecondary-school mathematics: arithmetic and elementary algebra.

Two boosting steps by hand

Data. x = 1, 2, 3, 4 with y = 1, 2, 6, 7. Squared loss, ν = 0.5, stumps.

Stage 0. F₀ = mean(y) = 4. Residuals r = y − F₀ = (−3, −2, 2, 3).

Stage 1.

  • The best stump on r splits between x = 2 and 3, with leaf means −2.5 and +2.5.
  • Update: F₁ = 4 + 0.5·(−2.5, −2.5, 2.5, 2.5) = (2.75, 2.75, 5.25, 5.25).
  • New residuals: (−1.75, −0.75, 0.75, 1.75). The training SSE fell from 9 + 4 + 4 + 9 = 26 to 3.0625 + 0.5625 + 0.5625 + 3.0625 = 7.25.

Stage 2.

  • The best stump on the new residuals splits in the same place again, with leaf means −1.25 and +1.25.
  • F₂ = F₁ + 0.5·(−1.25, −1.25, 1.25, 1.25) = (2.125, 2.125, 5.875, 5.875). The residuals are (−1.125, −0.125, 0.125, 1.125).
  • To fix the within-group differences (1 against 2, and 6 against 7), a later stump must split at x = 1.5 or x = 3.5.

Absolute loss instead. The pseudo-residuals become sign(y − F), and each leaf takes the median of the actual residuals. A huge outlier then counts as just +1 in the gradient, which is why absolute loss resists outliers.

The steps in plain English:

  1. Initialise with the best constant: the mean for squared loss, the median for absolute loss.
  2. Repeat M times: compute pseudo-residuals rᵢ = −∂L/∂F at the current predictions, fit a small tree to them, set each leaf’s value to the loss-optimal step, and add ν times the tree.
  3. Monitor validation loss and stop where it is lowest (early stopping).
  4. Tune ν (0.01–0.3), tree depth (1–8) and the number of trees together.
📝 Quick check (Level 1)
0/3 answered
  1. 1. F₀ for squared loss with y = 1, 2, 6, 7?
  2. 2. Training SSE after stage 1 in the example?
  3. 3. Pseudo-residual for absolute loss when y − F = −5?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Derivation: gradient descent in function space

Goal. Minimise the empirical risk R(F) = Σᵢ L(yᵢ, F(xᵢ)) over functions F.

Functional gradient descent. At the training points, the steepest-descent direction is the vector gᵢ = ∂L(yᵢ, F)/∂F evaluated at F(xᵢ). We can’t move F arbitrarily at unseen x, so we approximate −g with a function from a base class, a regression tree h_m fitted by least squares to the pseudo-residuals rᵢ = −gᵢ. That is a projection of the gradient onto trees.

Line search per leaf. A tree with leaves R_jm gives F ← F + Σⱼ γ_jm·1[x ∈ R_jm], with γ_jm = argmin over γ of Σ over i ∈ R_jm of L(yᵢ, F(xᵢ) + γ). Optimising each leaf separately is better than one global step.

Shrinkage. Use F_m = F_(m−1) + ν·Σⱼ γ_jm·1[x ∈ R_jm], with ν between 0 and 1.

Losses.

Loss L(y, F) pseudo-residual leaf value
squared ½(y − F)² y − F mean residual
absolute abs(y − F) sign(y − F) median residual
Huber (δ) quadratic below δ, linear above clip(y − F, −δ, δ) approx. median plus a mean correction
quantile (τ) τ·(y − F)₊ + (1 − τ)·(F − y)₊ τ − 1[y below F] τ-quantile of residuals

Why squared-loss boosting is “fitting residuals”. For L = ½(y − F)², −∂L/∂F = y − F is the residual, and the least-squares leaf value is the mean residual.

from sklearn.ensemble import HistGradientBoostingRegressor
gbr = HistGradientBoostingRegressor(loss="absolute_error", learning_rate=0.1, max_depth=3,
                                    max_iter=1000, early_stopping=True, validation_fraction=0.15).fit(X, y)
🐍 Exercise 1: gradient boosting with squared and absolute loss
Implement a regression tree that returns leaf assignments, then gradient boosting with per-leaf line search for squared loss (mean residual) and absolute loss (median residual); check that training loss never rises, that one squared-loss stage equals a tree fitted to residuals, and that absolute loss resists gross outliers.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Pseudo-residuals are…
  2. 2. For absolute loss, leaf values are…
  3. 3. A quantile loss gives…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory

Shrinkage and regularisation. Small ν with many trees approximates a continuous path of L1-regularised models (forward stagewise ≈ lasso path; Efron et al., 2004; Rosset, Zhu and Hastie, 2004). Empirically, smaller ν gives better test error provided M grows accordingly (Friedman, 2001). Exercise 2: ν = 1 bottomed at 6.86 after 10 trees, while ν = 0.3 reached 2.73.

Bias and variance.

  • Each stage lowers bias.
  • Variance accumulates with M, so test error is U-shaped in M for any ν, and early stopping is the main regulariser.
  • Depth controls the interaction order: depth-d trees can represent interactions of up to d features.

Consistency. Boosting with early stopping is consistent under mild conditions (Zhang and Yu, 2005; Bühlmann and Yu, 2003 for L2 boosting).

Depth in exercise 2. On Friedman #1 with 300 samples, stumps (an additive model) did slightly better (2.77) than depth 3 (2.81), and depth 6 overfit (4.27). The x₀·x₁ interaction was not worth its variance at this sample size.

Interview questions.

  • Why use a small learning rate? Each tree corrects only a fraction of the error, so later trees can refine, and the model follows a smoother, better-regularised path. The cost is more trees, so use early stopping to pick M.
  • How do you make gradient boosting robust to outliers? Use absolute or Huber loss, whose bounded gradients cap each point’s influence on the next tree.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Small ν with many trees approximates…
  2. 2. Depth-d trees model interactions up to order…
  3. 3. The main regulariser in gradient boosting is…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Shrinkage, number of trees and depth

History.

  • Breiman (1997, 1998) recast AdaBoost as optimisation (“arcing”).
  • Mason et al. (1999) introduced functional gradient descent views (“AnyBoost”).
  • Friedman (2001) published “Greedy function approximation: a gradient boosting machine”, with general losses, shrinkage and leaf line search.
  • Friedman (2002) added stochastic gradient boosting.
  • XGBoost (2016), LightGBM (2017) and CatBoost (2018) turned it into the dominant tabular method.

Exercise 1.

  • Training loss fell at every stage, and one squared-loss stage equals a tree fitted to residuals.
  • Test MSE went from 2.334 (constant) to 0.061 with 150 depth-3 trees at ν = 0.1.
  • With 5% gross outliers, squared loss reached 2.840 and absolute loss 0.089.

Exercise 2. Setup: Friedman #1, 10 features (5 informative), n = 300, noise variance 1. The table shows test MSE against the noise-free target, with depth-3 trees.

ν 10 trees 50 100 200 300 best
1 6.86 7.68 7.67 7.66 7.66 6.86 (M = 10)
0.3 4.68 2.84 2.77 2.73 2.73 2.73 (M = 223)
0.1 9.49 3.68 3.05 2.85 2.81 2.81 (M = 300)
0.03 16.53 7.61 4.68 3.31 2.91 2.91 (M = 300, still falling)

Best test MSE by depth at ν = 0.1: depth 1: 2.77, depth 3: 2.81, depth 6: 4.27.

  • Without shrinkage (ν = 1) the model overfit after about 10 trees and never recovered.
  • Every ν ≤ 0.3 reached a much lower error, but smaller ν needed more trees. ν = 0.03 was still improving at 300 trees. The usual practice is a small ν with early stopping on a validation set.
  • Shallow trees won at n = 300. Deep trees overfit badly.
🐍 Exercise 2 (research): shrinkage, trees and depth
On Friedman's #1 benchmark, trace L2 gradient boosting's test MSE for learning rates 1, 0.3, 0.1 and 0.03 (depth-3 trees, up to 300 trees), and compare tree depths 1, 3 and 6 at ν = 0.1.

Gradient boosting in AI, robotics and aerospace

  • Telemetry regression: temperatures, currents, loads, with tabular features.
  • Remaining-useful-life and degradation prediction with quantile losses for intervals.
  • Learning-to-rank and anomaly scoring in operations dashboards.
  • Robust variants (Huber, absolute loss) for sensor data with glitches.

References

  1. J. H. Friedman (2001). “Greedy function approximation: a gradient boosting machine.” Annals of Statistics 29(5).
  2. L. Mason, J. Baxter, P. Bartlett, M. Frean (1999). “Boosting algorithms as gradient descent.” NeurIPS.
  3. P. Bühlmann, B. Yu (2003). “Boosting with the L2 loss: regression and classification.” JASA 98.
  4. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., chapter 10. Springer.
  5. A. Natekin, A. Knoll (2013). “Gradient boosting machines, a tutorial.” Frontiers in Neurorobotics 7.