Part 2

Watch two small trees repair one prediction.

We will predict delivery time from distance using four training rows. The numbers are intentionally small so every prediction, residual, split, and error can be checked by hand.

Initial-prediction question

If you must predict one delivery time for every distance, which constant minimizes squared error?

Step 1: begin with the mean

DeliveryDistance \(x\)Actual time \(y\)
A1 km2 min
B2 km8 min
C3 km10 min
D4 km12 min

With squared error, the best constant is the mean:

\[F_0(x)=\bar y=\frac{2+8+10+12}{4}=8\]

The model predicts 8 minutes everywhere. Its residual is actual minus prediction:

\[r_i^{(1)}=y_i-F_0(x_i)\]
\(x\)ActualPrediction \(F_0\)ResidualMeaning
128-6Prediction is 6 too high
2880Exactly right
3108+2Prediction is 2 too low
4128+4Prediction is 4 too low
1 km2 km3 km4 kminitial prediction = 8time

The vertical arrows are the corrections the current model needs. At 1 km, the prediction must move down; at 3 and 4 km, it must move up.

\[\operatorname{MSE}(F_0)=\frac{(-6)^2+0^2+2^2+4^2}{4}=14\]

Correction-tree question

Can a one-split tree group rows that need similar corrections?

Step 2: fit tree 1 to the residuals

The tree receives the original input \(x\), but its target is no longer the original delivery time \(y\). Its supervised training pairs are now:

\[(1,-6),\quad(2,0),\quad(3,2),\quad(4,4)\]

The first number in each pair is the distance. The second is the residual that the current model wants corrected. Therefore, this tree learns a function from distance to required correction.

Before calculating

A depth-1 regression tree can make only one split. Which split appears to group similar residuals together?

How CART trains this one-split tree

  1. Generate candidate splits. Sort the distinct distances and test the midpoint between every adjacent pair:
    \[1.5,\qquad2.5,\qquad3.5\]
  2. Divide the rows. Each candidate creates a left leaf and a right leaf.
  3. Calculate each leaf's prediction. A regression-tree leaf predicts the mean of the target values that reach it. Here those targets are residuals.
  4. Calculate split error. Measure the squared distance between every residual and its leaf mean:
    \[\operatorname{SSE}_{\text{split}}=\sum_{i\in L}(r_i-\bar r_L)^2+\sum_{i\in R}(r_i-\bar r_R)^2\]
  5. Choose the smallest SSE. That split groups correction targets most effectively.

Evaluate the candidate \(x\leq1.5\)

The left leaf contains only residual \(-6\), so its mean prediction is \(-6\). The right leaf contains \(0,2,4\), so its prediction is:

\[\bar r_R=\frac{0+2+4}{3}=2\]

Its total training error is:

\[\operatorname{SSE}=(-6-(-6))^2+(0-2)^2+(2-2)^2+(4-2)^2=8\]

Compare all possible splits

Candidate splitLeft residualsLeft predictionRight residualsRight predictionTotal SSE
\(x\leq1.5\)\([-6]\)\(-6\)\([0,2,4]\)\(2\)8
\(x\leq2.5\)\([-6,0]\)\(-3\)\([2,4]\)\(3\)20
\(x\leq3.5\)\([-6,0,2]\)\(-4/3\)\([4]\)\(4\)\(104/3\approx34.67\)

The smallest SSE is \(8\), so the trained stump is:

\[h_1(x)=\begin{cases}-6,&x\le1.5\\2,&x>1.5\end{cases}\]

What has the tree learned? For a distance near 1 km, the current prediction should move down by 6. For a distance above 1.5 km, it should move up by 2. The tree predicts a correction, not delivery time itself.

Update question

Should the ensemble accept the tree's entire correction immediately?

Use learning rate \(\eta=0.5\), so the model accepts half of the correction:

\[F_1(x)=F_0(x)+0.5h_1(x)\]
\(x\)ActualOld \(F_0\)Tree correction \(h_1\)Applied correctionNew \(F_1\)New residual
128-6-35-3
288+2+19-1
3108+2+19+1
4128+2+19+3
\[\operatorname{MSE}(F_1)=\frac{(-3)^2+(-1)^2+1^2+3^2}{4}=5\]

What happened? One weak tree reduced MSE from 14 to 5. It made B temporarily worse, but improved the total objective. Boosting optimizes the overall loss, not every row independently at every round.

Second-round question

Should tree 2 fit the original targets again, or the remaining residuals \((-3,-1,1,3)\)?

Step 3: recalculate, then fit tree 2

The first tree changed the model, so we calculate new residuals. A one-split regression tree now chooses \(x\le2.5\):

\[h_2(x)=\begin{cases}-2,&x\le2.5\\2,&x>2.5\end{cases}\]

The left residual mean is \((-3-1)/2=-2\). The right residual mean is \((1+3)/2=2\). Apply half again:

\[F_2(x)=F_1(x)+0.5h_2(x)\]
\(x\)ActualOld \(F_1\)Tree correction \(h_2\)New \(F_2\)Residual after tree 2
125-24-2
289-280
3109+2100
4129+210+2
\[\operatorname{MSE}(F_2)=\frac{(-2)^2+0^2+0^2+2^2}{4}=2\]
1 km2 km3 km4 km
Actual \(F_0\): MSE 14 \(F_1\): MSE 5 \(F_2\): MSE 2

Adding shallow step functions creates a more detailed step function. The plotted values match the tables.

What students should notice before seeing any calculus

  1. The initial model is already a valid prediction.
  2. Each tree predicts a correction, not the final target by itself.
  3. The correction target changes after every tree.
  4. The learning rate shrinks every tree's contribution.
  5. The final model is the sum of the initial value and all tree contributions.
\[F_2(x)=8+0.5h_1(x)+0.5h_2(x)\]
Previous: The ideaNext: Why gradient?