Skip to content
advanced

Gradient Boosting as Additive Function Fitting: Derive the Pseudo-Residuals

Fit the next tree to the error. That instruction works beautifully for squared loss and then quietly falls apart the moment you switch to log loss. The…

Published 2026-10-02Updated 2026-10-0411 min read
Visual abstraction of neural networks in AI technology, featuring data flow and algorithms.
Visual abstraction of neural networks in AI technology, featuring data flow and algorithms. Photo by Google DeepMind on Pexels.

Fit the next tree to the error. That instruction works beautifully for squared loss and then quietly falls apart the moment you switch to log loss. The target you compute is no longer the error. It is something else wearing the same name.

The intuition article in this series gave you the loop: build a weak learner, correct the ensemble, repeat. This piece replaces the special case with the general rule. By the end, you will be able to write down the negative gradient of any differentiable loss and derive the boosting target yourself, instead of trusting the library to hand it to you.

The Additive Model and the Empirical Loss

Fix the object we are optimizing before touching any derivative.

An additive model builds a prediction as a sum of stagewise learners:

Fm(x)=F0(x)+∑j=1mν hj(x)F_m(x) = F_0(x) + \sum_{j=1}^{m} \nu \, h_j(x)

Here F0F_0 is an initial constant, hjh_j is the learner added at stage jj, and ν\nu is a shrinkage multiplier we will return to. The initial model is problem-specific. For least-squares regression, the conventional choice is the mean of the training targets; for binary classification with log loss, it is the log-odds of the base rate.

The empirical loss over nn training examples is

L(F)=∑i=1nℓ(yi,F(xi))L(F) = \sum_{i=1}^{n} \ell(y_i, F(x_i))

Read that carefully. LL is a function of the entire prediction vector (F(x1),…,F(xn))(F(x_1), \dots, F(x_n)), not of a finite parameter vector. There is no θ\theta to differentiate against. The variable is the function itself.

Two constraints define the game:

  • Forward stagewise. At stage mm we add one hmh_m and never revisit earlier stages. No joint re-optimization.
  • Restricted function class. Each hmh_m is drawn from a class H\mathcal{H}, usually fixed-depth regression trees. That restriction is what makes the step tractable. It is also what makes the result greedy rather than globally optimal.

State the assumptions up front, because they bound everything that follows. The per-example loss ℓ(y,F)\ell(y, F) must be differentiable in its second argument. The training set is finite. The class H\mathcal{H} is closed under the operations we apply later — specifically, we need to be able to fit a learner to arbitrary real-valued targets, which regression trees can do.

Knowledge check

Check your understanding

Answer this question before you continue.

Which description matches the article's forward-stagewise additive model?
Comparison Reasoning

Focus: Describe how the additive model changes at each stage and what forward-stagewise fitting leaves fixed.

Why the Ideal Next Learner Is Intractable

The exact stagewise objective is clean to write and impossible to solve directly:

hm=arg⁡min⁡h∈H∑i=1nℓ(yi,  Fm−1(xi)+h(xi))h_m = \arg\min_{h \in \mathcal{H}} \sum_{i=1}^{n} \ell\big(y_i, \; F_{m-1}(x_i) + h(x_i)\big)

To solve this exactly, you would enumerate every function in H\mathcal{H} and evaluate the total loss after adding each one. For a tree class, that means searching over every possible partition of the feature space at every depth. The search space is combinatorial and the loss surface is not convex in the tree structure.

So we cheat, but we cheat in a principled direction. Instead of solving the stagewise problem, we take a step in function space that mirrors gradient descent in parameter space. In parameter space, we cannot jump to the minimum, so we compute the gradient and move a small distance along it. In function space, we cannot enumerate H\mathcal{H}, so we compute the steepest-descent direction and fit the best available learner to it.

Note: This is a greedy heuristic. It does not solve the stagewise problem exactly. It solves a first-order approximation of it, and the approximation is only as good as the learner class and the step size allow.

Deriving the Negative Gradient Target

A left-to-right flow shows current predictions producing per-example loss gradients g_i, which are negated to form pseudo-residuals r_i = -g_i. A regression tree fits those targets, and its scaled output is added to the current model to produce the next prediction function.
Boosting turns each loss slope into a target, fits the best available learner to that direction, then adds a scaled correction.

Now the derivation. It is short, and every symbol has a job.

Differentiate the empirical loss with respect to the current prediction for a single training example. Define

gi=∂ ℓ(yi,F(xi))∂F(xi)∣F=Fm−1g_i = \frac{\partial \, \ell(y_i, F(x_i))}{\partial F(x_i)} \bigg|_{F = F_{m-1}}

This is the per-example gradient: how much the loss changes when you nudge the model's prediction for example ii by a small amount, holding every other prediction fixed.

The steepest-descent direction in function space is the negative gradient, evaluated at the current model's predictions. So the ideal next learner would satisfy

hm(xi)≈−gifor each ih_m(x_i) \approx -g_i \quad \text{for each } i

We cannot fit an arbitrary function, so we fit the best function in H\mathcal{H} to the values −gi-g_i. Define the pseudo-residual

ri=−gi=−∂ ℓ(yi,F(xi))∂F(xi)∣F=Fm−1r_i = -g_i = -\frac{\partial \, \ell(y_i, F(x_i))}{\partial F(x_i)} \bigg|_{F = F_{m-1}}

and train hmh_m as a regression model with targets rir_i.

That is the whole trick. The "residual" label is a historical accident: for squared loss, rir_i happens to equal the ordinary residual. For every other loss, it is a gradient-derived quantity with different units, a different range, and a different meaning.

Convention matters here. You will see the update written as Fm=Fm−1+νhmF_m = F_{m-1} + \nu h_m and also as Fm=Fm−1−νhmF_m = F_{m-1} - \nu h_m. Both are correct; they differ in whether the learner is fit to ri=−gir_i = -g_i (add) or to gig_i (subtract). Pick one and stay consistent, because mixing conventions flips the sign of your entire ensemble. The squared-loss case aligns pseudo-residuals with ordinary residuals only when the sign convention is held fixed.

One more boundary. If ℓ\ell is not differentiable in the prediction — 0-1 loss is the canonical example — the derivation does not apply. You cannot take the gradient of a step function. This is why classification uses smooth surrogates like log loss or the logistic loss: they approximate the decision boundary you care about while remaining differentiable. Hinge loss is a partial exception: it is differentiable almost everywhere, but its flat regions and kink mean the gradient carries no information at some points, so the derivation applies only where the gradient is defined and nonzero.

Knowledge check

Check your understanding

Answer this question before you continue.

At the current model, the per-example loss gradient is gᵢ. Under the article's add-update convention, what targets should the next learner fit?
Single Choice

Focus: Determine the regression targets and update sign when fitting the negative-gradient direction.

Worked Example: Squared Loss and Log Loss

Two cases make the derivation concrete. One where pseudo-residuals are residuals, one where they visibly are not.

Squared loss

ℓ(y,F)=12(y−F)2\ell(y, F) = \tfrac{1}{2}(y - F)^2

Differentiate with respect to FF:

∂ℓ∂F=−(y−F)=F−y\frac{\partial \ell}{\partial F} = -(y - F) = F - y

The negative gradient is

ri=−(F(xi)−yi)=yi−F(xi)r_i = -(F(x_i) - y_i) = y_i - F(x_i)

That is the ordinary residual. The derivation recovers the intuition exactly, under the convention that the learner is added to the ensemble.

Knowledge check

Check your understanding

Answer this question before you continue.

For ℓ(y,F) = ½(y − F)², an example has y = 3 and current prediction F = 1. What pseudo-residual does the article's add-update convention produce?
Output Prediction

Focus: Compute the negative-gradient target for one example under the article's half-squared-loss convention.

Log loss for binary classification

Let FF be the log-odds score and p=σ(F)=1/(1+e−F)p = \sigma(F) = 1/(1 + e^{-F}) the predicted probability. The per-example loss is

ℓ(y,F)=−[ylog⁡p+(1−y)log⁡(1−p)]\ell(y, F) = -\big[y \log p + (1 - y)\log(1 - p)\big]

Differentiate with respect to FF. Using ∂p/∂F=p(1−p)\partial p / \partial F = p(1-p):

∂ℓ∂F=p−y\frac{\partial \ell}{\partial F} = p - y

The negative gradient is

ri=yi−pir_i = y_i - p_i

This is a residual in probability space — the gap between the label and the predicted probability — but it is not a residual in score space. The tree is fit to values in [0,1][0, 1] while the ensemble accumulates scores in (−∞,∞)(-\infty, \infty). The units do not match, and that mismatch has a practical consequence: the leaf values produced by fitting to rir_i are not on the same scale as the raw label, so implementations refine leaf values after the tree structure is fixed.

LossGradient ∂ℓ/∂F\partial \ell / \partial FPseudo-residual rir_iRange of rir_i
SquaredF−yF - yy−Fy - FSame as label
Log lossp−yp - yy−py - p[−1,1][-1, 1]

Same derivation, two different objects. The table is the whole point of this section.

Fitting hmh_m to the pseudo-residuals gives you a direction. It does not give you a distance.

After the tree structure is fixed, solve a one-dimensional line search for the multiplier that minimizes the loss along that direction:

γm=arg⁡min⁡γ∑i=1nℓ(yi,  Fm−1(xi)+γ hm(xi))\gamma_m = \arg\min_{\gamma} \sum_{i=1}^{n} \ell\big(y_i, \; F_{m-1}(x_i) + \gamma \, h_m(x_i)\big)

For squared loss, this has a closed form: γm\gamma_m is the least-squares coefficient of the residual on the fitted values, which reduces to a simple ratio of sums. For log loss and most other losses, there is no closed form and you solve it numerically or approximate it.

In practice, libraries do not run a full line search. They use a fixed shrinkage parameter ν\nu — the learning rate — and often compute per-leaf optimal values instead of a single global γm\gamma_m. Shrinkage and line search are related but not identical: line search finds the best step along the direction, shrinkage deliberately takes a smaller step than optimal to leave room for later learners to correct.

The tradeoff is direct. Small steps need more learners to reach the same loss, which costs training time and memory. Large steps risk overshooting the minimum and destabilizing the ensemble, especially when the loss surface is steep near the current predictions. The learning rate and the number of estimators are coupled; tuning one without the other is guesswork.

Knowledge check

Check your understanding

Answer this question before you continue.

After fixing a learner's tree structure, which statement correctly contrasts line search with fixed shrinkage?
Comparison Reasoning

Focus: Distinguish a loss-minimizing line-search step from a fixed shrinkage step.

From Derivation to the Boosting Procedure

Here is the algorithm, with each step mapped to the derivation that justifies it. The two step-size branches are kept separate, because they produce different updates.

  1. Initialize F0(x)F_0(x) to a constant that minimizes the loss — the mean for squared loss, the log-odds for log loss. This is the starting point of the additive model.
  2. Compute pseudo-residuals ri=−∂ℓ/∂F(xi)r_i = -\partial \ell / \partial F(x_i) at the current predictions. This is the negative gradient step.
  3. Fit a learner hmh_m to the targets rir_i using the restricted class H\mathcal{H}. This is the projection of the steepest-descent direction onto the available function class.
  4. Choose the step size. Either solve the line search for γm\gamma_m, or fix a shrinkage parameter ν\nu. These are alternatives, not a single combined step.
  5. Update the ensemble. With line search: Fm=Fm−1+γm hmF_m = F_{m-1} + \gamma_m \, h_m. With fixed shrinkage: Fm=Fm−1+ν hmF_m = F_{m-1} + \nu \, h_m. Use one branch consistently; do not mix γm\gamma_m and ν\nu in the same update.
  6. Repeat until a stopping condition is met.

Implementations diverge from this textbook version in three places worth knowing. XGBoost and similar libraries use a second-order Taylor approximation of the loss, replacing the gradient-only step with a Newton step that uses both gradient and Hessian. They compute per-leaf optimal values rather than a single global step size. And they add explicit regularization terms on leaf weights and tree complexity. None of these change the core derivation; they refine the step.

For stopping, validation-based early stopping is the honest choice. Training loss decreases monotonically as you add learners, so it cannot tell you when to stop. A held-out set can.

When Pseudo-Residuals Are Not Residuals

This is the section that prevents the most expensive debugging mistakes.

Pseudo-residuals equal ordinary residuals only for squared loss under a consistent sign convention. For every other loss, they are gradient-derived targets with different units and ranges. Three consequences follow.

Debugging. A shrinking pseudo-residual magnitude does not mean the model is fitting the label better in the original units. For log loss, ri=yi−pir_i = y_i - p_i is bounded in [−1,1][-1, 1] regardless of how wrong the score is. A model can have small pseudo-residuals and still produce badly calibrated probabilities.

Loss choice. Switching loss changes the target distribution. Tree depth and learning rate tuned for squared loss may not transfer to log loss, because the targets have different variance, different scale, and different sensitivity to outliers. Re-tune when you switch.

Assumption boundary. The derivation requires differentiability and a function class rich enough to approximate the gradient direction. When H\mathcal{H} is too weak — trees that are too shallow, or a linear class on a nonlinear problem — boosting stalls regardless of iteration count. Adding more learners to a class that cannot represent the gradient direction just accumulates the same correction over and over.

Common mistake: Treating pseudo-residuals as "the error" and expecting them to shrink toward zero in the label's units. They shrink toward zero in the gradient's units, which is a different thing.

What to Do Next

The derivation is a decision rule, not a ritual. When you can write down −∂ℓ/∂F-\partial \ell / \partial F for your loss, you can derive the boosting target yourself and stop treating the library as a black box. When you cannot — because the loss is non-differentiable, or the function class cannot represent the gradient direction — the derivation stops being the right tool, and you need a different approach.

The practical next step is to run a boosting experiment and watch how learning rate and tree depth interact with the derived update. Fit a model with a small learning rate and many estimators, then a large learning rate and few. Compare the validation curves. The shape of those curves is the line search and the shrinkage parameter doing their work in the open.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

Which combination of conditions matches the article's stated boundary for using the pseudo-residual derivation?
Question 1 of 2Misconception Check

Focus: Identify the differentiability and function-class assumptions that support the pseudo-residual derivation.

For a positive label y = 1 and predicted probability p = 0.8 under binary log loss, which interpretation of the add-update pseudo-residual is correct?
Question 2 of 2Scenario Interpretation

Focus: Interpret a log-loss pseudo-residual as a probability-space target rather than an ordinary score residual.

References

  1. 1.9. Ensemble methods — scikit-learn 0.15-git documentationscikit-learn.org
  2. Gradient Boosted Decision Trees  |  Machine Learning  |  Google for Developersdevelopers.google.com
  3. Gradient Boosting from Theory to Practice (Part 1) | Towards Data Sciencetowardsdatascience.com
Practical resource

Build stronger machine learning foundations

Use structured resources to connect theory, scikit-learn workflows, and evaluation practice.

Browse resources
Related sites

Continue across the AI learning path

Use LearnPyFast for Python foundations and LearnLLMFast when you are ready to move from classical ML into LLM applications.

Python tutorialstutorial

LearnPyFast

Beginner-friendly Python tutorials, examples, and learning paths for practical programming foundations.

PythonProgrammingBeginners
Visit LearnPyFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related machine learning tutorials

Continue with nearby concepts, model families, evaluation methods, and practical workflows.