← All posts
Machine Learning · Boosting · Gradient Boosting

2. Regression: The Derivation

Notes on StatQuest with Josh Starmer · Gradient Boost Part 2 · 26:46 video. Explanations and redrawn figures follow the lecture’s worked example.

Lecture 1 showed what boosting does. This lecture derives the same steps from squared-error loss: start at a mean, calculate residuals, fit a tree, average within its leaves, and update predictions.

1. Three rows and one loss

Video 0:56 ↗
The smaller training set used in Lecture 2.
RowHeight (m)ColorGenderWeight $y_i$
11.6BlueMale88
21.6GreenFemale76
31.5BlueFemale56

$x_i$ means the input features of row $i$; $y_i$ is its measured weight. $F(x_i)$ is the current prediction. The loss of one row is

$$L(y_i,F(x_i))=\frac12\bigl(y_i-F(x_i)\bigr)^2.$$

The factor $1/2$ makes derivatives simpler. It halves every squared-error total, so it does not change which prediction has the smallest loss.

2. Why the first prediction is the mean

Video 6:30 ↗

Before building a tree, choose one constant $c$ for every row. The notation $\arg\min_c$ means “find the value of $c$ that makes this total smallest.”

$$F_0=\arg\min_c\sum_{i=1}^{n}\frac12(y_i-c)^2.$$

Differentiate with respect to $c$, and set the result to zero:

$$\sum_i(c-y_i)=0\quad\Longrightarrow\quad nc=\sum_i y_i.$$
$$F_0=\frac{88+76+56}{3}=\frac{220}{3}\approx73.33.$$
Total half squared error is minimized at the common prediction of 73 and one-third kilograms.
The lowest point is the mean. Moving the common prediction either way increases total squared error.

3. Why the negative gradient becomes a residual

Video 10:01 ↗

At round $m$, $F_{m-1}$ is the model containing all trees built so far. Unlike Step 2 (where one shared constant $c$ affected every row in $\sum_{k=1}^n L$), here we want a separate target $r_{im}$ for each individual row $i$. Differentiating the total loss with respect to row $i$’s own prediction $F(x_i)$ makes every other row’s term ($k \ne i$) vanish, leaving no summation:

$$\frac{\partial}{\partial F(x_i)}\sum_{k=1}^{n}\frac12\bigl(y_k-F(x_k)\bigr)^2 = \frac{\partial L\bigl(y_i,F(x_i)\bigr)}{\partial F(x_i)} = F(x_i)-y_i.$$

A positive derivative means increasing the prediction increases loss. Reverse its sign to get the direction we want the next tree to learn:

$$r_{im}=-\left.\frac{\partial L(y_i,F)}{\partial F}\right|_{F=F_{m-1}(x_i)}=y_i-F_{m-1}(x_i).$$

For this loss, the pseudo-residual is exactly the ordinary residual. “Pseudo-residual” is the general name because other losses produce different negative gradients.

Calculations retain the exact mean 220/3; displayed decimals are rounded.
RowActual weightCurrent predictionFirst residual
18873.33…+14.666…
27673.33…+2.666…
35673.33…−17.33…

4. Fit the tree and name its regions

Video 12:47 ↗

Train a regression tree with the original features as inputs and these residuals as targets. The lecture’s stump uses height < 1.55 m. Row 3 goes left; rows 1 and 2 go right.

A height threshold of 1.55 metres creates two leaves: row 3 with correction −17.33, and rows 1 and 2 with correction 8.67.
The tree chooses which rows share a correction. A terminal region is one leaf’s group of inputs, defined by its feature rules. Figure values are rounded to two decimals.

$R_{jm}$ means leaf region $j$ in tree $m$. $\gamma_{jm}$ is the single correction that leaf will output. For the first tree there are two regions: $R_{1,1}$ and $R_{2,1}$.

5. Why each leaf outputs the average residual

Video 14:50 ↗

Keep the previous predictions fixed. For one region $R$, choose a shared addition $\gamma$ that minimizes the loss of its rows:

$$\gamma=\arg\min_a\sum_{i:x_i\in R}\frac12\bigl(y_i-F_{m-1}(x_i)-a\bigr)^2.$$

The expression $y_i-F_{m-1}(x_i)$ is the residual $r_i$. So we are fitting one constant to the residuals inside this leaf:

$$\frac{d}{da}\sum_{i:x_i\in R}\frac12(r_i-a)^2=\sum_{i:x_i\in R}(a-r_i)=0.$$
$$\gamma=\frac{\sum_{i:x_i\in R}r_i}{\text{number of rows in }R}.$$

The left leaf has just one residual, so it outputs $-17.33\ldots$. The right leaf outputs $(14.666\ldots+2.666\ldots)/2=8.666\ldots$. Averaging is therefore the squared-loss solution, not an extra arbitrary rule.

6. Update predictions and repeat

Video 20:38 ↗

With learning rate $\nu=0.1$, add one tenth of the leaf output to each row’s previous prediction:

First-round predictions, calculated before rounding.
RowsPrevious predictionLeaf correctionUpdated prediction
1 and 273.33…+8.666…74.2
373.33…−17.33…71.6

The next residuals are $88-74.2=13.8$, $76-74.2=1.8$, and $56-71.6=-15.6$. Fit the next tree to these new targets.

$$F_m(x)=F_{m-1}(x)+\nu\sum_{j=1}^{J_m}\gamma_{jm}\mathbf1\{x\in R_{jm}\}.$$

$J_m$ is the number of leaves. The indicator is 1 only for the leaf reached by $x$, so exactly one leaf contributes per tree. The sum is a way to write the tree’s prediction; a row does not enter multiple leaves.