← All posts
Machine Learning · Boosting · XGBoost

1. Regression: Build the First Tree

Notes on StatQuest with Josh Starmer · Regression: Build the First Tree · 25:46 video. Original explanations and redrawn figures accompany the video.

XGBoost starts with a prediction and adds trees that correct it. A leaf’s output is an amount to add. The feature rules decide which inputs receive that correction.

Start with what the prediction got wrong

Video 2:36 ↗

The lecture predicts drug effectiveness from dosage. Fix the starting prediction at $0.5$ for every row. A residual is the observed value minus that prediction:

DosageObserved effectiveness $y$PredictionResidual $r=y-\hat y$
10−100.5−10.5
2070.56.5
2580.57.5
35−70.5−7.5

At dosage 10, the prediction is 10.5 too high, so we want a negative correction. At dosages 20 and 25, it is too low, so we want positive corrections. The tree still splits on dosage. Residuals tell it whether the rows grouped by a proposed split can share a useful correction.

Observed effectiveness and the initial prediction, followed by the correction each dosage needs.
The vertical gaps become the new tree’s training signal. Nearby dosages 20 and 25 need similar upward corrections.

What XGBoost adds to the boosting idea

The outer loop is still “predict, measure the remaining error, add a correction tree.” The new part in this lecture is how the tree is built: regularized scores compare possible groups, and a related formula supplies the correction each group will receive.

The observed effectiveness stays fixed throughout training. The residual column changes after every tree because the current prediction changes. A new input still needs only its dosage to follow the fitted rules.

Score the rows in one leaf

Video 4:07 ↗

For squared-error regression, the lecture’s similarity score is

$$S=\frac{(\sum r_i)^2}{n_{\text{leaf}}+\lambda}.$$

Add the residuals in that leaf, square the sum, and divide by the row count plus $\lambda$. Here $\lambda$ is a nonnegative regularization setting. Start with $\lambda=0$.

Why add the residuals first? A leaf gives everyone the same correction. Positive and negative requests cancel: $[-7,+7]$ have sum zero and no useful shared correction. Requests $[6.5,7.5]$ reinforce one another. The score measures how much a shared correction can help; it is not a general-purpose measure of pairwise similarity.

Before splitting, all four residuals occupy one leaf. Their sum is $-4$, so

$$S_{\text{parent}}=\frac{(-10.5+6.5+7.5-7.5)^2}{4}=\frac{16}{4}=4.$$

Keep the leaf’s score and its output separate

The root’s score is 4, but its best shared correction at $\lambda=0$ is $-4/4=-1$. These numbers answer different questions. The correction says what to add to predictions. The score says how useful that optimized correction is when comparing this region with alternative regions.

If we used just the root correction, every prediction would move from 0.5 to −0.5, the mean of the four observed effectiveness values. That helps a little, but the positive and negative residuals still need very different changes. Splitting allows separate corrections instead of this one compromise.

A similarity score cannot tell you which direction to move because its numerator is squared. Both a strong upward request and a strong downward request can earn a high score. The unsquared numerator in the leaf-output formula preserves the sign.

Try a feature threshold and measure its gain

Video 8:23 ↗

Try dosage $<15$. The left child contains $[-10.5]$; the right contains $[6.5,7.5,-7.5]$.

$$S_L=\frac{(-10.5)^2}{1}=110.25,\qquad S_R=\frac{(6.5+7.5-7.5)^2}{3}\approx14.08.$$

We need to know whether two separate corrections help more than the one correction available before the split. Subtract the parent’s score:

$$\text{gain}=S_L+S_R-S_{\text{parent}}\approx110.25+14.08-4=120.33.$$

There are three possible ways to separate these four ordered dosages. The lecture places a threshold halfway between each adjacent pair.

Candidate ruleLeft residualsRight residualsGain
dosage $<15$−10.56.5, 7.5, −7.5120.33
dosage $<22.5$−10.5, 6.57.5, −7.54.00
dosage $<30$−10.5, 6.5, 7.5−7.556.33

The rule dosage $<15$ wins. Notice why the middle threshold is weak: both children still contain requests to move in opposite directions.

Why subtract the parent?

The parent already had the option of giving its rows one optimized correction. The gain measures the additional benefit of giving them two. Adding the children’s scores alone would count improvement that was already possible without introducing this split.

The candidate at 15 gives left output −10.5 and right output $6.5/3\approx2.17$. These outputs are temporary while we compare candidates. Once we split the right child again, its descendants will get their own corrections instead.

The gain 120.33 is neither a prediction nor a correction to add. It selects a rule. Only the final leaf reached by a row supplies that tree’s output.

Repeat inside the child that still mixes corrections

Video 11:01 ↗

The right child still mixes two positive residuals with a negative one. Splitting it at 30 gives $[6.5,7.5]$ and $[-7.5]$:

$$\text{gain}=\frac{14^2}{2}+\frac{(-7.5)^2}{1}-\frac{6.5^2}{3}\approx140.17.$$

Its other candidate, 22.5, gives only 28.17. Choose 30 and stop at depth 2 for this example.

A dosage tree with rules below 15 and below 30, ending in corrections minus 10.5, plus 7, and minus 7.5.
Each path defines a region of input space. The middle region contains dosages from 15 up to 30; its rows share an upward correction.

At the next level, the “parent” in the gain calculation is the right child we are considering splitting. Its score is 14.08, not the original root score of 4. The comparison is always local: these two proposed children versus their own unsplit parent.

The fitted tree now defines three input regions: dosage below 15, dosage from 15 up to 30, and dosage at least 30. Within each region, the output is constant. The middle correction is 7 because $(6.5+7.5)/2=7$; it does not come from the numerical midpoint of the dosage interval.

Gamma asks whether a split earns its complexity

Video 13:02 ↗

The lecture grows this tree, then checks splits from the bottom upward. A split whose gain falls below $\gamma$ can be pruned when its children are leaves.

With $\gamma=130$, the lower split survives because $140.17>130$. Its parent is retained to reach that useful subtree, even though the parent’s own immediate gain is 120.33. With $\gamma=150$, the lower split is removed first; the root split can then be removed too.

Removing every split leaves a constant root leaf. It can still have a nonzero correction; “no splits” does not by itself mean “no update.” In this example the root output would be $-4/4=-1$.

Checking from the bottom explains why the two gains are not treated as independent yes/no gates in the lecture’s pruning illustration. A parent supports the descendants beneath it. Once those descendants disappear, we can compare replacing the resulting two-leaf branch with a single leaf.

Growing and pruning details depend on the tree method. The numbers here reproduce the lecture’s bottom-up example; they are not a claim that every current growth method first constructs the same overgrown tree for every value of $\gamma$.

Lambda shrinks leaf corrections

Video 15:15 ↗

Now set $\lambda=1$. For the same first split, the scores become

$$S_L=\frac{110.25}{2}=55.125,\quad S_R=\frac{42.25}{4}=10.5625,\quad S_P=\frac{16}{5}=3.2.$$

Its gain is $55.125+10.5625-3.2=62.4875$, smaller than before. This is the corrected value noted in the video description; the slide at 16:50 says 66.

The leaf’s output uses the same denominator, but does not square the numerator:

$$w=\frac{\sum r_i}{n_{\text{leaf}}+\lambda}.$$

For the single residual $-10.5$, increasing $\lambda$ from 0 to 1 changes the correction from $-10.5$ to $-5.25$. For $[6.5,7.5]$, it changes $14/2=7$ to $14/3\approx4.67$. The extra denominator pulls corrections toward zero, with a stronger relative effect on smaller leaves.

Change λ and inspect the same candidate split

The rows and threshold stay fixed at dosage < 15. This recalculates its score and both leaf outputs; it does not refit the whole tree.

Lambda and the learning rate act at different stages

Increasing $\lambda$ changes the leaf outputs and the scores used to select splits. It can therefore change the tree’s structure. The learning rate scales the chosen tree’s contribution after fitting. On the next round, that scaled update changes the residuals used to build another tree.

Even when two settings happen to give one leaf the same contribution, they need not produce the same future ensemble. They can have selected different splits or left different residual patterns elsewhere.

Add a small part of the leaf output

Video 19:29 ↗

Return to the three-leaf tree with $\lambda=0$. Use learning rate $\eta=0.3$. A dosage of 10 follows the left branch, whose correction is $-10.5$:

$$\hat y_{\text{new}}=0.5+0.3(-10.5)=-2.65.$$

We moved toward −10 without taking the full correction. The other leaves work the same way.

DosageLeaf outputNew predictionNew residual
10−10.5−2.65−7.35
2072.604.40
2572.605.40
35−7.5−1.75−5.25

For a new dosage of 22, follow the same feature rules and return 2.60 after this first tree. Its true effectiveness and residual are not needed to choose a branch.

The next tree learns what is still wrong

Video 23:30 ↗

Keep the first tree. Recompute the residuals from the updated predictions, fit another tree, and add its scaled corrections. After $M$ rounds, the prediction is

$$\hat y(x)=0.5+\eta w_1(x)+\eta w_2(x)+\cdots+\eta w_M(x).$$

Here $w_m(x)$ means the output of the leaf reached by $x$ in tree $m$. The trees accumulate corrections; their outputs are not averaged.

Work through tree 2

Continue the unregularized example with depth 2 and learning rate 0.3. After the first tree, the residuals are $[-7.35,4.40,5.40,-5.25]$. Scanning the root candidates again gives gains about 58.96, 2.40, and 27.60 for thresholds 15, 22.5, and 30, so 15 wins again.

The right child again splits at 30. Its middle pair now has mean residual $(4.4+5.4)/2=4.9$, while the outer leaves output −7.35 and −5.25. The second tree has the same regions in this example, but new leaf values:

Two boosting rounds, with λ = 0 and η = 0.3.
DosageAfter tree 1Tree 2 outputAfter tree 2Remaining residual
10−2.650−7.35−4.855−5.145
202.600+4.904.070+2.930
252.600+4.904.070+3.930
35−1.750−5.25−3.325−3.675

For a new dosage of 22, the first tree contributes $0.3(7)=2.1$ and the second contributes $0.3(4.9)=1.47$. Add both to 0.5 to get 4.07. The second tree is not a replacement for the first.

The controls in this example have different jobs.
SettingWhat it controls
$\lambda$Penalizes leaf-output size and affects split scores.
$\gamma$Sets the required split benefit on the chosen gain scale.
$\eta$Scales the fitted tree’s addition to the ensemble.
Maximum depthLimits how many feature decisions a path can contain.
Two XGBoost correction trees and their accumulated predictions, compared with the four observed targets.
Tree 2 fits the errors left after tree 1. The lower plot adds both scaled contributions to the baseline; it does not replace or average the trees.

The gains above use the video’s score convention, also used by XGBoost’s basic tree scorer. Part 3 explains the factor of ½ in the paper’s objective. The examples deliberately set λ = 0; current XGBoost’s default λ is 1. See the parameter reference.