Gradient Boosting — Step-by-Step Mathematical Derivation
Imagine you are trying to guess a number. You make a first rough guess, someone tells you "you are a little too low," so you nudge your guess up. They say "still a bit low," so you nudge again. After many small, guided nudges, you land very close to the right answer. Gradient Boosting works exactly like this: it starts with a rough guess and then adds many small correction steps, where each correction is guided by the mistakes made so far.
Before proceeding, it is helpful to establish a few terms that recur throughout this derivation:
- Model — The current predictive function, denoted
: given an input , it returns a prediction. - Loss — A scalar measure of prediction error. A smaller loss indicates a more accurate prediction, and our objective is to minimize it.
- Weak learner — A simple model, typically a shallow decision tree, whose individual accuracy is only marginally better than random guessing. Although limited on its own, a collection of such learners can achieve strong performance.
- Ensemble — A combination of many weak learners, where the final prediction is the aggregate of their individual contributions.
- Iteration / Stage — A single round of the procedure, in which exactly one new weak learner is added. Rounds are indexed by
, with rounds performed in total.
Big picture in one line: at every round, add a new weak learner that pushes the current model in the direction that reduces the loss the fastest.
Problem Setup
Let's first write down what we are given and what we want.
- We are given training data: pairs of inputs and their correct answers, written as
. is the -th input (its features), is the correct target value, and is how many examples we have.
- Our goal is to learn a function
that makes predictions with the smallest total loss over all training examples. - The loss function
compares the true answer with the prediction and returns a "wrongness" score. It only needs to be , which is exactly why Gradient Boosting works for both regression and classification.
Putting all together, the ideal model is the one that minimizes the total loss:
Here
- Finding this perfect
in one shot is too hard. So instead of learning one giant model at once, Gradient Boosting builds the model gradually, as a sum of many small pieces (an additive ensemble):
is the initial model — our first rough guess. is the weak learner added at round — one small correction. is the learning rate. It is a small number that scales down each correction so we take gentle steps instead of overshooting.
Stagewise Additive Construction
"Stagewise" simply means we improve the model one stage (round) at a time, and we never go back and change earlier pieces — we only add new ones.
- At stage
, we take the model we already have, , and add one new weak learner to it:
-
Read:
-
In a perfect world, at each stage we would directly solve for the best possible correction and the best step size together:
where
is the step size chosen freshly at each stage, while is the global learning rate that shrinks every step by the same fixed amount for safety.
Why this step? This sets up the "one round at a time" recipe and honestly admits that the ideal one-shot correction is too hard to compute. That admission is what motivates the gradient-based shortcut used in every step that follows.
Step by Step — Mathematical Derivation
Step 1: Initialize the Model
- Start with a simple constant model
. - This gives the boosting process a reasonable baseline before any tree is added.
- The initial model is chosen to minimize the loss over all constant values.
where
- For regression,
is usually the mean of the target values. - For classification,
is often the log-odds of the positive class.
Step 2: Iteration Loop
The algorithm then iterates
Step 2.1: Compute the Pseudo-Residuals
- At iteration
, look at the current model . - For each training example, we are calculating its residuals
by taking a derivative of the loss function with respect to the previous prediction and negative gradient. - These values (
) are called pseudo-residuals.
- The little
symbol means "derivative" — it measures how fast the loss changes if we tweak the prediction. The minus sign flips it so the value points in the direction that reduces loss (this is the "negative gradient"). - Each pseudo-residual is a correction signal: its sign (
or ) says which way to move, and its size says how strongly. - Worked example of squared-error loss,
We can compute the pseudo-residual
- So for regression with squared loss, Gradient Boosting repeatedly fits trees to the residual errors.
Step 2.2: Fit a Weak Learner to the Pseudo-Residuals
- Now train a weak learner, usually a shallow regression tree, to predict the pseudo-residuals.
- The training pairs for this step are
. - The tree approximates the direction of steepest descent in function space.
- Note: The tree does not try to predict the original target
directly. - It only learns the pattern of the current errors made by the ensemble.
are the leaf (terminal) regions created by the tree built at boosting step . It divides the feature space into non-overlapping terminal regions. - Each input point
falls into exactly one region, and within that region the tree predicts one constant correction value. - These regions are rule-based partitions formed by tree splits (for example, feature thresholds), and together they cover the full space seen by the tree.
- This is why the model update is piecewise: only the correction value of the leaf containing
is applied.
- Each input point
Step 2.3: Find the Optimal Step Size
- After fitting the tree structure, we determine how much of this new tree should be added to the model.
- This is done using a line search on the actual loss.
- In the most general form, we find a single multiplier
such that:
- Because each leaf is independent, it is common to compute a separate value for each leaf instead of one shared multiplier. If
is the -th leaf of tree , its optimal value is found by looking only at the points inside that leaf:
- Notice the sum runs over
only — that is the direct link back to Step 2.2: we reuse the exact leaves it built and solve one tiny optimization per leaf. - So Step 2.2 answered "which points belong together?" and Step 2.3 answers "what value goes in each group?"
- For squared-error loss, this optimal value
simply works out to be the mean (average) residual of the points in that leaf.
Step 2.4: Update the Ensemble
- Once the tree and its leaf values are computed, add the new learner to the current model.
- The learning rate
keeps the update small and stable. - This helps reduce overfitting and usually improves generalization.
- Recall the tree
returns a single constant per leaf — the value of whichever leaf falls into:
- A compact way to write this same piecewise function is with the indicator
, which equals when its condition holds and otherwise. Since lands in exactly one leaf, only that leaf's term survives:
- Plugging this scaled correction into the update
gives:
- Written out as an explicit piecewise rule, the same update reads:
- In words, the new prediction equals the old prediction plus a small correction from the new tree.
Step 2.5: Repeat the Process
- Repeat the residual computation, tree fitting, step-size estimation, and model update for
. - Each new tree focuses on the remaining mistakes of the current ensemble.
- Over many iterations, the model becomes increasingly accurate.
- This final model is the sum of many small corrective learners.
- Gradient Boosting works well because each step makes a targeted improvement instead of attempting a large correction all at once.
Why the Method Works
- Gradient descent updates a model by moving in the negative gradient direction.
- Gradient Boosting applies the same idea, but the update is done in function space rather than directly on parameter vectors.
- The weak learner approximates the negative gradient, and the line search decides how far to move in that direction.
- This is why the method is called Gradient Boosting.
- It is boosting because we add weak learners sequentially.
- It is gradient-based because each learner follows the negative gradient of the loss.
where is trained to approximate the negative gradient of the loss at stage .
Gradient Boosting = fit a weak learner to the negative gradient of the loss, then take a small step in that direction—repeated many times. Choosing squared-error loss recovers the "fit trees to residuals" intuition; choosing other losses (log-loss, absolute error, etc.) gives us classification, robust regression, and more, all from the same framework.
Deep Dive on Step 2.3 and 2.3
Example
Suppose at round
graph TD
A["size > 1500 ?"] -->|No| B["size > 800 ?"]
A -->|Yes| R3["Leaf R_3m
points inside: 5
value +12"]
B -->|No| R1["Leaf R_1m
points inside: 4
value -7"]
B -->|Yes| R2["Leaf R_2m
points inside: 6
value +3"]Here
- Worked example. Say we want the correction for a new house with
size = 1200.- Start at the top: is
1200 > 1500? No → go left. - Next: is
1200 > 800? Yes → land in leaf. - The tree returns that leaf's single constant,
. Every other leaf is ignored for this input.
- Start at the top: is
- Why "non-overlapping and covering the full space." Any size you pick (10, 1200, 9000, …) follows the rules to exactly one leaf — never zero, never two. The three leaves together account for every possible house size.
- Why "piecewise / staircase." The tree's output as size increases is not a smooth ramp; it jumps between three flat values:
for small houses, for medium, for large.
Input size |
Leaf reached | Correction returned |
|---|---|---|
| 400 | ||
| 1200 | ||
| 2000 |
Key idea for the next step: the numbers
shown above came from
- Step 2.2 fitting the residuals, but they are only rough placeholders.
- Step 2.3 keeps these three leaf boxes fixed and recomputes the best number to store in each one so the real loss drops as much as possible.
Where do these numbers actually come from?
Two different things get "derived" when a tree is built, and it helps to keep them separate:
- The split thresholds (e.g.
800,1500) — these define the shape of the boxes and are chosen in Step 2.2. - The leaf values (e.g.
-7,+3,+12) — these are the number stored in each box. Step 2.2 sets a rough version; Step 2.3 computes the optimal one.
(a) How each leaf value is derived — it is just an average of residuals.
For squared-error loss, the best constant to place in a leaf is the mean of the residuals of the points that fell into it. To see why, minimize the leaf's loss:
Differentiate with respect to
So in our example,
(b) How the split thresholds are derived — a greedy search.
The tree is not told 800 or 1500 in advance. It scans candidate thresholds for the feature and, for each one, checks how much it reduces the squared error of the residuals (how much "purer" the two resulting groups become). It keeps the single split that reduces the error the most, then repeats the same search independently on each child group until a stopping rule (max depth, min points per leaf) is hit.
where size > 800.
(c) How the feature space gets partitioned.
Every chosen split is an axis-aligned cut (size > 800). Stacking these cuts carves the input space into the non-overlapping leaf boxes
In one line: split thresholds come from greedily minimizing residual error (Step 2.2); leaf values come from averaging the residuals inside each box (refined in Step 2.3).