Unit 7, lesson 51

Boosting and Gradient-Boosted Trees

How boosting methods build an additive model stage by stage, why gradient boosting fits weak learners to negative gradients with residual fitting as the squared-error case, and how shrinkage, stage count and AdaBoost work.

  • Video 51
  • Ensemble Methods

A tree with a single split predicts only two values, while a deep tree can follow its training data so closely that it predicts new cases poorly. Boosting methods take a middle path. They start from a simple prediction, fit a small tree to what the current model still gets wrong, accept part of that correction and repeat. What exactly should each new tree fit when the loss is not squared error, and how many corrections should be accepted? This lesson derives the training signal from the loss, shows that ordinary residuals are the special case of squared error, contrasts the reweighting used by AdaBoost, and explains why the learning rate, the number of stages and the tree depth are chosen on validation data.

Key terms

Stagewise additive model

Definition A stagewise additive model predicts with a sum of simple functions that are added one stage at a time. After MM stages it has the form

FM(x)=F0(x)+∑m=1Mβmhm(x),\begin{aligned} F_M(\mathbf{x}) &= F_0(\mathbf{x}) \\[0.4em] &\quad + \sum_{m=1}^{M} \beta_m h_m(\mathbf{x}) , \end{aligned}

where F0F_0 is a starting model, usually a constant, each hmh_m is a function returned by a base learner and each βm\beta_m is a real coefficient. Forward stagewise fitting builds the sum greedily for a chosen loss LL and training examples (x1,y1),…,(xn,yn)(\mathbf{x}_1, y_1), \dots, (\mathbf{x}_n, y_n). At stage mm the earlier terms are frozen, and only the new term βh\beta h is chosen, to make the training loss

∑i=1nL(yi, Fm−1(xi)+β h(xi))\begin{aligned} &\sum_{i=1}^{n} L\bigl(y_i,\, F_{m-1}(\mathbf{x}_i) \\[0.4em] &\qquad + \beta\, h(\mathbf{x}_i)\bigr) \end{aligned}

small, after which Fm=Fm−1+βmhmF_m = F_{m-1} + \beta_m h_m. Earlier terms are never refitted, which separates this from fitting all the terms jointly. Boosting methods are the learning algorithms that build such a sum in sequence, with each new term responding to the errors of the current model. Bagging, by contrast, fits its trees independently on bootstrap samples and averages them. In the gradient boosting of this page every coefficient equals a fixed learning rate, βm=ν\beta_m = \nu, and the size of each correction sits in the leaf values of the tree hmh_m.

In plain words The model is a starting guess plus a list of corrections. Each stage looks at the current model, adds one more correction and never goes back to edit the earlier ones. The prediction at an input is the starting value plus every correction that applies there. Simple corrections add up to a flexible function, which is why a sum of many small trees can describe patterns that no single small tree can.

Example Start from the constant F0=5F_0 = 5 on one feature xx, accept half of each correction, and add two stumps. The first outputs −3-3 for x≤2.5x \le 2.5 and 33 otherwise, and the second outputs −1-1 for x≤1.5x \le 1.5 and 11 otherwise. The model F2(x)=5+0.5 h1(x)+0.5 h2(x)F_2(x) = 5 + 0.5\,h_1(x) + 0.5\,h_2(x) then takes three values,

F2(x)={3,x≤1.5,4,1.5<x≤2.5,7,x>2.5.F_2(x) = \begin{cases} 3, & x \le 1.5, \\ 4, & 1.5 < x \le 2.5, \\ 7, & x > 2.5. \end{cases}

For instance, at x=2x = 2 it adds −1.5-1.5 from the first stump and 0.50.5 from the second. Each stump predicts two values, and their sum is a staircase with three steps.

Weak learner

Definition A weak learner, or base learner, is a fitting procedure that returns a function from a restricted class H\mathcal{H}. Typical classes are decision stumps, which are trees with one split and two leaves, and regression trees limited to a small depth dd or a small number of leaves. In the classical analysis of boosting algorithms for binary labels, a learner is weak in a precise sense if there is a fixed γ>0\gamma > 0 such that, for every weighting of the training examples, it returns a classifier with weighted error at most 12−γ\tfrac12 - \gamma, so it is reliably a little better than guessing. In gradient boosting the word is used more loosely for a low-capacity learner, usually a shallow regression tree fitted by least squares. Each leaf of a tree of depth dd is described by at most dd split conditions, so the tree is a sum of terms that each depend on at most dd features, and so is any sum of such trees. A sum of stumps is therefore additive in the features, of the form f1(x1)+⋯+fp(xp)f_1(x_1) + \dots + f_p(x_p), with no interactions.

In plain words A weak learner is deliberately too simple to fit the data on its own. Its value lies in being quick to fit and stable, so that many of them can be added in small steps. The depth of the trees decides what kind of pattern the whole sum can express. Stumps can only add up separate effects of single features, while trees of depth 22 can also describe how two features act together.

Example Take the four points (0,0)(0, 0), (1,1)(1, 1), (0,1)(0, 1) and (1,0)(1, 0) with labels −1-1, −1-1, +1+1 and +1+1, the exclusive-or pattern. Every stump splits on one feature, and each of its two sides then contains one point of each label, so under equal weights every stump has weighted error exactly 12\tfrac12. Stumps are not weak learners for this problem in the classical sense, and no sum of stumps classifies all four points correctly by its sign, however many stages it has. Such a sum has the form g(x1)+k(x2)g(x_1) + k(x_2), and correct signs would need

g(0)+k(0)<0,g(1)+k(1)<0,g(0)+k(1)>0,g(1)+k(0)>0.\begin{aligned} g(0) + k(0) &< 0 , \\ g(1) + k(1) &< 0 , \\ g(0) + k(1) &> 0 , \\ g(1) + k(0) &> 0 . \end{aligned}

The first two inequalities add up to a negative number and the last two to a positive one, yet both totals equal g(0)+g(1)+k(0)+k(1)g(0) + g(1) + k(0) + k(1). A single tree of depth 22 that splits on x1x_1 and then on x2x_2 classifies all four points correctly.

Negative gradient

Definition Let the loss L(y,F)L(y, F) be differentiable in the prediction or score FF for each target value yy. At stage mm the negative gradient, or pseudo-residual, of training example ii is the negative derivative of its loss with respect to the prediction, evaluated at the current prediction,

rim=−∂L(yi,F)∂F,F=Fm−1(xi).\begin{aligned} r_{im} &= -\frac{\partial L(y_i, F)}{\partial F} , \\[0.4em] F &= F_{m-1}(\mathbf{x}_i) . \end{aligned}

The vector (r1m,…,rnm)(r_{1m}, \dots, r_{nm}) is the negative gradient of the training loss ∑iL(yi,Fi)\sum_i L(y_i, F_i) with respect to the vector (F1,…,Fn)(F_1, \dots, F_n) of training predictions, at the current predictions. Moving those predictions a short enough step along it lowers the training loss whenever it is not the zero vector. For the squared loss L(y,F)=12(y−F)2L(y, F) = \tfrac12 (y - F)^2 the derivative is F−yF - y, so the pseudo-residual is the ordinary residual yi−Fm−1(xi)y_i - F_{m-1}(\mathbf{x}_i). For binary labels y∈{0,1}y \in \{0, 1\} and a log-odds score FF with probability p=σ(F)p = \sigma(F), the log loss

L(y,F)=ln⁡(1+eF)−yFL(y, F) = \ln\bigl(1 + e^{F}\bigr) - yF

equals −ln⁡p-\ln p when y=1y = 1 and −ln⁡(1−p)-\ln(1 - p) when y=0y = 0. Its derivative is σ(F)−y\sigma(F) - y, so the pseudo-residual is yi−σ(Fm−1(xi))y_i - \sigma\bigl(F_{m-1}(\mathbf{x}_i)\bigr), the label minus the current probability. For the absolute loss ∣y−F∣\lvert y - F \rvert it is sign⁡(y−F)\operatorname{sign}(y - F) wherever y≠Fy \ne F, and the loss has no derivative where y=Fy = F.

In plain words The pseudo-residual says, for each training example, which way its prediction should move to lower that example’s loss and how fast the loss changes in that direction. It exists only at the training inputs, since a new input has no target. That is why gradient boosting fits a function to these numbers, so that the correction can be applied anywhere. The loss decides what counts as an error. Squared error turns the signal into the ordinary residual, and the log loss turns it into the label minus a probability, a number between −1-1 and 11.

Example For squared error, four training targets 11, 33, 77 and 99 and the constant prediction 55 give the pseudo-residuals −4-4, −2-2, 22 and 44. For the log loss, a positive example with current score F=2F = 2 has probability σ(2)≈0.8808\sigma(2) \approx 0.8808 and pseudo-residual 1−0.8808=0.11921 - 0.8808 = 0.1192, a small push upward because it is already classified with confidence. A negative example with the same score has pseudo-residual −0.8808-0.8808, a strong push downward. Subtracting the score from the label instead would give 1−2=−11 - 2 = -1 for the positive example, a push in the wrong direction, because labels and probabilities lie in [0,1][0, 1] while a score can be any real number.

Gradient boosting

Definition Gradient boosting with a differentiable loss LL, a class H\mathcal{H} of regression trees, a learning rate 0<ν≤10 < \nu \le 1 and a number of stages MM fits training examples (xi,yi)(\mathbf{x}_i, y_i), i=1,…,ni = 1, \dots, n, in three steps.

  1. Set F0F_0 to the constant cc that minimizes ∑iL(yi,c)\sum_i L(y_i, c). For squared error this is the mean of the targets, and for the log loss it is ln⁡(yˉ/(1−yˉ))\ln\bigl(\bar{y}/(1 - \bar{y})\bigr), where the share yˉ\bar{y} of positive labels must lie strictly between 00 and 11.
  2. For m=1,…,Mm = 1, \dots, M, compute the pseudo-residuals rimr_{im} at the current predictions, fit a tree hmh_m from H\mathcal{H} to the pairs (xi,rim)(\mathbf{x}_i, r_{im}) by greedy least-squares splitting, and set Fm=Fm−1+νhmF_m = F_{m-1} + \nu h_m.
  3. Return FMF_M, and for the log loss also the probability estimate σ(FM(x))\sigma\bigl(F_M(\mathbf{x})\bigr).

The tree is an approximation of the negative gradient that can be evaluated at any input. Many implementations then replace each leaf value by the constant that lowers the original loss most on the examples in that leaf, or use second derivatives of the loss, and stochastic gradient boosting fits each tree to a random subsample of the rows. Residual fitting is the squared-error case. With L=12(y−F)2L = \tfrac12 (y - F)^2 the pseudo-residuals are the ordinary residuals, a least-squares tree predicts the mean residual in each leaf, and the leaf adjustment just described changes nothing. Fitting ordinary residuals under a loss whose negative gradient is not a multiple of the residual, such as the log loss or the absolute loss, is a different algorithm, not gradient boosting for that loss.

In plain words Gradient descent would move every training prediction along the negative gradient, but a model must also predict at inputs it has never seen. Gradient boosting therefore fits a small tree to the desired moves and takes the step along the tree, so that nearby inputs receive similar corrections. For squared error this is the familiar idea of fitting the next tree to what the current model still gets wrong.

Example Take four constructed training examples with inputs 11, 22, 33 and 44 and targets 11, 33, 77 and 99, squared error, stumps that split midway between neighboring inputs, and ν=0.5\nu = 0.5. The constant model predicts the mean 55, with residuals −4-4, −2-2, 22 and 44 and sum of squared errors 4040. The best stump for these residuals splits at 2.52.5, with leaf means −3-3 and 33 and a residual sum of squares of 44 around them, against 56/3≈18.6756/3 \approx 18.67 for the splits at 1.51.5 and 3.53.5. Adding half of it gives the predictions

F1=(3.5, 3.5, 6.5, 6.5)F_1 = (3.5,\ 3.5,\ 6.5,\ 6.5)

at the four inputs, with residuals −2.5-2.5, −0.5-0.5, 0.50.5 and 2.52.5 and sum of squared errors 1313. At the second stage the split at 2.52.5 wins again, with 44 against 14/3≈4.6714/3 \approx 4.67, and its leaf means are −1.5-1.5 and 1.51.5. Half of that correction gives

F2=(2.75, 2.75, 7.25, 7.25),F_2 = (2.75,\ 2.75,\ 7.25,\ 7.25) ,

and the sum of squared errors falls to 6.256.25. The second improvement, 6.756.75, is smaller than the first, 2727, because the residuals left to correct are smaller. Applying the learning rate twice, once to the residuals before fitting and again to the update, would add only ±0.75\pm 0.75 at the first stage, which is a different algorithm with an effective rate of 0.250.25.

Shrinkage and number of stages

Definition Shrinkage multiplies each fitted correction by a learning rate ν\nu with 0<ν≤10 < \nu \le 1 before adding it, so that Fm=Fm−1+νhmF_m = F_{m-1} + \nu h_m. The number of stages MM is the number of fitted trees in the sum. Together with the tree size, set by the depth, the number of leaves or the minimum leaf size, and any row subsampling, they are hyperparameters, chosen with validation data or with cross-validation inside the training data and never with the test set. For squared error with least-squares leaf values, the values of a tree at the training inputs are the orthogonal projection of the residual vector onto the span of the tree’s leaf indicator vectors, so the training sum of squared errors changes by

SSEm=SSEm−1−ν(2−ν)∑i=1nhm(xi)2.\begin{aligned} &\text{SSE}_m = \text{SSE}_{m-1} \\[0.4em] &\quad - \nu(2 - \nu) \sum_{i=1}^{n} h_m(\mathbf{x}_i)^2 . \end{aligned}

It never increases for 0<ν≤10 < \nu \le 1, and it falls strictly whenever the new tree is not zero at every training input. The loss on validation data usually falls at first and can rise later, once the trees start to fit noise. Early stopping adds stages while monitoring the validation loss, often stops after a set number of stages without improvement, and keeps the stage count with the lowest validation loss.

In plain words The learning rate decides how much of each proposed correction to accept, and the number of stages decides how many corrections accumulate. A smaller rate usually needs more stages to reach the same training fit, and in practice it often predicts new data better, at the cost of more computation, but neither choice is guaranteed to help. Deeper trees make each stage more flexible and let it model interactions among more features. Since the training loss falls almost regardless of these settings, all three are chosen together by validation loss, as in hyperparameter search. The data used for early stopping have taken part in choosing the model, so a final claim needs data used for nothing else. With the log loss, the probabilities σ(FM)\sigma(F_M) can be poorly calibrated, so they need checking, and possibly recalibration, on held-out data before they drive decisions.

Example In the two stages above, ν(2−ν)=0.75\nu(2 - \nu) = 0.75, and the corrections have ∑ih1(xi)2=36\sum_i h_1(\mathbf{x}_i)^2 = 36 and ∑ih2(xi)2=9\sum_i h_2(\mathbf{x}_i)^2 = 9. The identity gives 40−27=1340 - 27 = 13 and 13−6.75=6.2513 - 6.75 = 6.25, as computed directly. With ν=1\nu = 1 the first stage alone would reach 40−36=440 - 36 = 4, the spread left inside the two leaves. Now score two separate constructed validation examples, inputs 1.51.5 and 3.53.5 with targets 22 and 88. With ν=0.5\nu = 0.5 the predictions are 55 and 55 after no stage, 3.53.5 and 6.56.5 after one and 2.752.75 and 7.257.25 after two, so the mean absolute error falls from 33 to 1.51.5 to 0.750.75. The full step ν=1\nu = 1 would predict 22 and 88 after one stage, with error 00, so on these data the smaller rate is not better. Two constructed points cannot settle such a choice. If they are used to pick the rate or the stage count, they are validation data, and a final claim needs further cases.

AdaBoost

Definition AdaBoost, short for adaptive boosting, is a boosting algorithm for binary labels yi∈{−1,+1}y_i \in \{-1, +1\}. It starts with weights wi=1/nw_i = 1/n and repeats three steps for m=1,…,Mm = 1, \dots, M.

  1. Fit a classifier hmh_m with values in {−1,+1}\{-1, +1\} to the weighted training data, and compute its weighted error eme_m, the total weight of the examples with hm(xi)≠yih_m(\mathbf{x}_i) \ne y_i.
  2. Provided 0<em<120 < e_m < \tfrac12, give it the vote weight αm=12ln⁡((1−em)/em)>0\alpha_m = \tfrac12 \ln\bigl((1 - e_m)/e_m\bigr) > 0.
  3. Multiply each weight by e−αmyihm(xi)e^{-\alpha_m y_i h_m(\mathbf{x}_i)}, which is e−αme^{-\alpha_m} for a correct example and eαme^{\alpha_m} for a mistake, and divide by the total so that the weights again sum to 11.

The final classifier is the sign of FM(x)=∑mαmhm(x)F_M(\mathbf{x}) = \sum_m \alpha_m h_m(\mathbf{x}), with a stated rule for a score of exactly 00. An error em=0e_m = 0 means a classifier that is perfect on the training data, and em≥12e_m \ge \tfrac12 means no improvement over guessing, so the standard algorithm stops in both cases instead of putting them into the logarithm. After each update, the examples misclassified by hmh_m hold total weight exactly 12\tfrac12. The training error of the final classifier, counting a score of 00 as an error, is at most

∏m=1M2em(1−em),\prod_{m=1}^{M} 2\sqrt{e_m (1 - e_m)} ,

and this bound goes to 00 as stages are added when every em≤12−γe_m \le \tfrac12 - \gamma for a fixed γ>0\gamma > 0. When each classifier minimizes the weighted error over its class, AdaBoost is forward stagewise fitting of the exponential loss e−yFe^{-yF}, and its weights are proportional to e−yiFm−1(xi)e^{-y_i F_{m-1}(\mathbf{x}_i)}, the size of that loss’s negative gradient at each example.

In plain words AdaBoost keeps the labels fixed and changes how much each example counts. Examples that the current classifier gets wrong gain weight, so the next classifier is pushed toward them, and the vote weight gives more say to classifiers with lower weighted error. Gradient boosting keeps the examples equally weighted and changes the targets that the next tree fits instead. The vote weight αm\alpha_m is computed from the data at each stage, while the learning rate ν\nu of gradient boosting is a setting fixed in advance. Weight grows exponentially on examples that keep being misclassified, so mislabeled examples can draw more and more attention. With noisy labels the number of stages needs validation like any other setting, and a loss that grows more slowly for badly misclassified examples, such as the log loss, is a common alternative.

Example Four training examples start with weight 0.250.25 each, and the first stump misclassifies one of them, so e1=0.25e_1 = 0.25 and

α1=12ln⁡3≈0.5493.\alpha_1 = \tfrac12 \ln 3 \approx 0.5493 .

The mistaken example is multiplied by eα1=3e^{\alpha_1} = \sqrt{3} and the others by e−α1=1/3e^{-\alpha_1} = 1/\sqrt{3}, a ratio of 33. The total before dividing is 0.253+0.75/3=3/2≈0.86600.25\sqrt{3} + 0.75/\sqrt{3} = \sqrt{3}/2 \approx 0.8660, which equals 2e1(1−e1)2\sqrt{e_1(1 - e_1)}, the first factor of the bound. The new weights are 0.50.5 for the mistaken example and 1/61/6 for each of the three others. Under these weights the first stump has error exactly 12\tfrac12, so fitting it again would earn the vote weight 00, and the next stump has to classify differently.

Check yourself

  1. A current model predicts 66 for a training example with target 1010, and the loss is 12(y−F)2\tfrac12 (y - F)^2. The next tree outputs 33 at this input and the learning rate is 0.20.2. Find the pseudo-residual, the new prediction and the loss before and after the update, and explain why the tree’s output differs from this example’s residual.
Show the answer

The pseudo-residual is the ordinary residual 10−6=410 - 6 = 4. The accepted update is 0.2×3=0.60.2 \times 3 = 0.6, so the new prediction is

Fnew=6+0.2×3=6.6.F_{\text{new}} = 6 + 0.2 \times 3 = 6.6 .

The loss falls from 12⋅42=8\tfrac12 \cdot 4^2 = 8 to 12⋅3.42=5.78\tfrac12 \cdot 3.4^2 = 5.78. The tree was fitted to the residuals of all the training examples at once, and each of its leaves predicts the mean residual of the examples in that leaf, so its output 33 is an average over this example’s leaf rather than this example’s own 44. Shrinkage then accepts only a fifth of it. The lower loss on one training example checks the arithmetic, but it says nothing about the loss on new data.

  1. A gradient boosting classifier uses the log loss with labels in {0,1}\{0, 1\} and log-odds scores. A positive training example has current score 22 and a negative one has current score −1-1. A colleague fits the next tree to the label minus the score. Compute the colleague’s targets and the correct pseudo-residuals, and say which way each one pushes the score.
Show the answer

The current probabilities are σ(2)≈0.8808\sigma(2) \approx 0.8808 and σ(−1)≈0.2689\sigma(-1) \approx 0.2689. The derivative of ln⁡(1+eF)−yF\ln(1 + e^{F}) - yF with respect to FF is σ(F)−y\sigma(F) - y, so the pseudo-residuals are

1−0.8808=0.1192,0−0.2689=−0.2689.\begin{aligned} 1 - 0.8808 &= 0.1192 , \\[0.4em] 0 - 0.2689 &= -0.2689 . \end{aligned}

The first raises the score of the positive example a little and the second lowers the score of the negative example, and both moves lower the log loss, which is about 0.12690.1269 and 0.31330.3133 for the two examples now. The colleague’s targets are 1−2=−11 - 2 = -1 and 0−(−1)=10 - (-1) = 1. They push the positive example’s score down and the negative example’s score up, so both point the wrong way. The label minus the score treats a label in {0,1}\{0, 1\} as a target for a log-odds score that can be any real number, which is the negative gradient of a squared error between the two, not of the log loss.

  1. An analyst fits gradient-boosted regression trees of depth 33 with learning rates 0.30.3 and 0.030.03, adds up to 10001000 stages, and for each rate keeps the stage count with the lowest loss on the test set. The training loss falls at every stage for both rates. The analyst concludes that more stages never hurt, picks the better rate, and reports its best test loss as the expected loss on new data. What is wrong, and what would a sound procedure look like?
Show the answer

A falling training loss is expected and proves little. For squared error each stage changes the training sum of squared errors by −ν(2−ν)∑ihm(xi)2-\nu(2 - \nu)\sum_i h_m(\mathbf{x}_i)^2, which is never positive, so the training curve cannot show when later trees start to fit noise. On data not used for fitting, the loss usually stops improving at some stage and can rise afterward, so the claim that more stages never hurt is not supported.

Choosing the stage count and the rate by test loss uses the test set for selection. The reported number is the smallest of 20002000 test losses, one for each rate and stage count, and the smallest of many noisy estimates tends to lie below the loss of the chosen model on fresh data, so the report is optimistic.

A sound procedure splits the training data into a fitting part and a validation part, or uses cross-validation inside the training data, with folds that keep groups together or respect time order when the data require it. For each rate and depth, stages are added while the validation loss is monitored, and the rate, depth and stage count with the lowest validation loss are chosen. The chosen pipeline is then scored once on the test set, or the whole tuning procedure is wrapped in nested cross-validation. The settings interact. With rate 0.030.03 each stage accepts 33 percent of its tree’s correction instead of 3030 percent, so its best stage count is usually much larger, and each rate has to be compared at its own best stage count rather than at a shared one. Deeper trees capture interactions among more features and usually reach their best validation loss in fewer stages, with more variance in each tree.

How this lesson connects