Boosted Trees › Formula
Machine Learning from ScratchAll chapters Boosted Trees › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The mathematical foundation of boosted trees involves understanding how the algorithm sequentially builds an ensemble by minimizing a loss function through gradient descent. Let's break this down step by step, starting with the basic structure and progressing to the complete algorithm formulation.
The Ensemble Model
The final boosted tree model is an ensemble of decision trees, where each tree is trained sequentially to correct the errors of the previous trees. The prediction for a given input is:
where:
- is the final prediction
- is the total number of trees in the ensemble
- is the learning rate (step size) for tree
- is the prediction from the -th tree for input
The Loss Function
Boosted trees minimize a loss function that measures how well the model's predictions match the true values. Common loss functions include:
For Regression (Mean Squared Error):
We've already covered these loss functions in the regression chapters, so we are keeping the explanation brief.
The most common loss function for regression tasks in gradient boosting is the Mean Squared Error (MSE). It measures the average squared difference between the actual target value and the predicted value :
The factor of is included for mathematical convenience because when you take the derivative of with respect to , the 2 from the exponent cancels with the , leaving just ; this makes the gradient expression simpler and avoids carrying an extra constant through the calculations.
During training, the algorithm tries to minimize this loss by adjusting the model so that predictions get as close as possible to the true values . The gradient of this loss with respect to is simply , which means each new tree in the ensemble is trained to predict the residuals (errors) of the current model.
For Classification (Logistic Loss):
For binary classification problems, gradient boosting typically uses the logistic loss (also called log-loss or binary cross-entropy). This loss is specifically designed for situations where the target is 0 or 1, and the model's output is a real number (not a probability, but a "logit" or "log-odds" score).
The logistic loss for a single data point is:
In gradient boosting, the logistic loss is central to the entire training process. At each boosting iteration (i.e., for each new tree that is added to the ensemble), the algorithm evaluates the logistic loss for every training example using the current prediction (which is the sum of the outputs from all previous trees).
The gradients of this loss with respect to are then computed for each example, and the next tree is trained to predict these gradients (also called "pseudo-residuals"). This means that the logistic loss is used repeatedly, at every boosting step and for every data point, to guide the model's updates and help it focus on the examples that are currently hardest to classify.
Let's look at how this loss function connects to log-odds and probabilities, step by step.
-
Model Output (): In gradient boosting for classification, the model does not directly output a probability. Instead, it outputs a real number for each example. This number is called the logit or log-odds.
-
From Log-Odds to Probability: To get a probability from , we use the sigmoid (logistic) function:
This maps any real value to a number between 0 and 1.
-
From Probability to Log-Odds: If you start with a probability , you can get back to the log-odds (the value the model outputs) by inverting the sigmoid:
So, the model is really building up an estimate of the log-odds for each example.


-
Why does the loss use and not ?
You might notice that the loss function is expressed in terms of (the log-odds), while the sigmoid function uses (the probability). This is intentional: the model predicts and updates values in the log-odds space, not directly in probability space.This is because the logistic loss is derived from the negative log-likelihood of the Bernoulli distribution. Let's walk through the full derivation:
-
Start with the Bernoulli likelihood: First, we write down the likelihood function for a single data point, which gives the probability of observing given the predicted probability .
-
Take the negative log-likelihood: Next, we take the negative logarithm of the likelihood to obtain the loss function that we want to minimize.
-
Express in terms of the model output : Then, we rewrite the probability in terms of the model's output (the log-odds), since this is what the model predicts.
and
-
Substitute and into the loss: Now, we substitute the expressions for and in terms of back into the loss function.
-
Simplify the expression: Finally, we simplify the resulting expression to obtain the standard form of the logistic loss in terms of .
Alternatively, you may see the loss written as:
which is algebraically equivalent.
This full derivation shows how the logistic loss arises naturally from the probabilistic model, and why it is expressed in terms of . This formulation is preferred because it simplifies the optimization process and makes the gradients easy to compute, which is essential for efficient training.
To recap, the logistic loss is derived from the negative log-likelihood of the Bernoulli distribution, and is expressed in terms of (the log-odds) because the model predicts and updates that value at each boosting step.
Gradients and Pseudo-Residuals
The boosting algorithm computes the gradient of the loss with respect to (where is the log-odds score for the model's prediction, or the raw score output by the model for a given input ) for each training example. For logistic loss, this gradient is:
This means that the "pseudo-residuals" (the targets for the next tree) are simply the difference between the true label and the current predicted probability.
- If the model predicts close to (e.g., and ), the gradient is small, so the next tree makes only a small correction.
- If the model predicts far from (e.g., and ), the gradient is large, so the next tree tries to make a bigger correction.
This process is repeated, with each new tree focusing on the mistakes of the current model, gradually improving the fit.
Log-loss has several properties that make it well-suited for classification tasks in gradient boosting. First, it is a strictly proper scoring rule, which means it encourages the model to produce well-calibrated probability estimates. It is also differentiable, so gradient-based optimization methods can be applied directly. Another useful property is that log-loss imposes a strong penalty on predictions that are both confident and incorrect, discouraging the model from making overconfident mistakes.
In gradient boosting for classification, the logistic loss provides a principled way to measure prediction error and guide the model's updates. At each step, the model fits a new tree to the difference between the true label and the current predicted probability, steadily reducing the overall loss and improving classification accuracy.
The Gradient Boosting Algorithm
The key insight of gradient boosting is to use the gradient of the loss function to guide the training of each new tree. Here's how it works:
Step 1: Initialize the model
Start with a simple model, typically the mean of the target variable for regression or the log-odds for classification:
- Here, is the initial prediction for all inputs .
- is the loss function, measuring the error between the true value and a constant prediction .
- is the number of training examples.
- means we choose the value of that minimizes the total loss over all training examples.
For regression with mean squared error (MSE) loss, this simplifies to:
That is, the initial prediction is just the average of the target values.
Step 2: For each iteration :
- is the index of the current boosting iteration (tree), and is the total number of boosting rounds (trees).
2a. Calculate the negative gradient (pseudo-residuals)
For each training example , calculate the negative gradient of the loss function with respect to the current model's prediction:
- is the pseudo-residual for example at iteration .
- is the prediction for example from the previous iteration.
- The negative gradient tells us how to adjust our prediction to reduce the loss.
This gives us the "pseudo-residuals" that tell us in which direction and by how much we need to adjust our predictions.
2b. Train a new tree on the pseudo-residuals
Train a decision tree to predict the pseudo-residuals:
- is the new tree fitted at iteration .
- The tree is trained to predict from the input features .
- The sum of squared differences means we fit the tree to the pseudo-residuals using least squares.
2c. Find the optimal step size
For each leaf in the new tree, find the optimal step size that minimizes the loss:
- is the optimal value to add to the prediction for all examples in leaf of tree .
- is the set of training examples that fall into leaf of tree .
- This step ensures that each leaf's output is chosen to minimize the loss for the examples it contains.
2d. Update the model
Add the new tree to the ensemble:
- is the updated prediction after adding the -th tree.
- is the step size (sometimes called the learning rate or shrinkage parameter) for this iteration. In some implementations, each leaf has its own , and the update is performed leaf-wise.
- is the prediction of the new tree.
Mathematical Properties
Boosted trees possess several key mathematical properties that help explain their effectiveness. One of the most important is the connection to gradient descent optimization. The algorithm can be viewed as performing gradient descent in function space, where each tree represents a step in the direction of steepest descent of the loss function.
The bias-variance decomposition also applies to boosted trees, but in a different way than Random Forest. Early in the training process, the model has high bias (underfitting) but low variance. As more trees are added, the bias decreases but variance increases. The optimal number of trees balances this trade-off to minimize the total error.
Another important property is the concept of shrinkage, controlled by the learning rate . A smaller learning rate means smaller steps in the optimization process, which can lead to better generalization but requires more trees to achieve the same level of performance. This is often expressed as:
where is the shrinkage parameter (typically between 0.01 and 0.1) that controls the learning rate.

