CatBoost › Formula
Machine Learning from ScratchAll chapters CatBoost › 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
CatBoost builds upon the fundamental gradient boosting framework we've already explored, but introduces several key modifications to the standard gradient boosting formula. Let's examine how CatBoost's approach differs from traditional gradient boosting implementations.
The core gradient boosting formula remains the same foundation:
where:
- : The ensemble model at iteration (cumulative prediction after adding trees)
- : The ensemble model from the previous iteration (cumulative prediction after trees)
- : The learning rate at iteration (controls the contribution of each new tree)
- : The new tree added at iteration (trained on residuals or gradients)
- : Input feature vector for a given observation
However, CatBoost modifies how we compute the gradients and how we build the trees.
Categorical Feature Processing:
CatBoost's most distinctive feature is its handling of categorical variables. Instead of preprocessing categorical features, CatBoost computes target statistics during training. For a categorical feature with categories , CatBoost computes an encoded value for each observation as follows:
where:
- : The encoded numerical value for the -th observation's category in feature
- : The categorical value of feature for observation
- : Indicator function that equals 1 if observation has the same category as observation , and 0 otherwise
- : The target value for observation
- : Total number of observations in the dataset
- : Smoothing parameter that controls regularization (typically set to 1)
- : Prior probability of the target (usually the mean of the target variable across the entire dataset)
Numerator:
adds up the target values for all observations that share the same category as the -th observation. The term is added to introduce smoothing, pulling the encoding towards the global mean when there are few samples for a category.
Denominator:
counts how many times the category appears in the dataset. Adding to the denominator ensures the encoding is well-defined even for rare categories and further smooths the result.
This formula computes a smoothed mean target value for each category. For each observation, the encoded value is the average target for its category, but "shrunk" towards the overall mean according to the smoothing parameter . This helps prevent overfitting, especially for categories that appear infrequently.
CatBoost uses this dynamic, smoothed target encoding for categorical features during training. By combining the mean target value for each category with the global mean, CatBoost creates numerical representations of categorical variables that capture their relationship with the target variable while reducing the risk of overfitting.

Symmetric Trees (Oblivious Trees):
CatBoost uses symmetric trees, also known as oblivious trees, where all nodes at the same depth (level) split on the same feature and threshold. This is in contrast to traditional decision trees, where each node can split on any feature independently. In a symmetric tree of depth , there are binary splits, and thus leaf nodes. The structure of the tree is fully determined by the sequence of features (and their thresholds) chosen for each level.
Let's formalize the prediction function for a symmetric tree.
Suppose the -th tree in the ensemble has depth , and at each level (), the tree splits on feature at threshold . For an input , we can define a binary decision at each level:
where:
- : Binary decision at level for input (0 for left branch, 1 for right branch)
- : Value of feature in input vector
- : Feature index used for splitting at level
- : Threshold value for the split at level
The path from the root to a leaf is determined by the vector of decisions . This vector can be interpreted as a binary number, which uniquely identifies one of the leaves. The index of the leaf node for input can be computed as:
where:
- : Index of the leaf node that input reaches (ranges from 1 to , or 0 to if zero-indexed)
- : Depth of the symmetric tree (number of levels)
- : Binary weight for the decision at level
The prediction of the -th symmetric tree for input is then:
where:
- : Prediction of the -th tree for input
- : The value (prediction) assigned to leaf in tree
- : The leaf index reached by input (computed using the formula above)
Alternatively, this can be written in indicator function notation as:
where:
-
: The region (set of all inputs ) corresponding to leaf in tree
-
: Indicator function that equals 1 if falls into region , and 0 otherwise
-
: Total number of leaves in the symmetric tree of depth
-
Each input is routed through binary splits (same feature per level for all nodes at that level).
-
The sequence of split decisions forms a binary code that selects one of leaves.
-
The output is the value assigned to that leaf.
This structure ensures that the tree is perfectly balanced and that the same features are considered at each level, which helps regularize the model and reduce overfitting.


Gradient Computation with Ordered Boosting:
CatBoost implements a technique called "ordered boosting" to reduce overfitting. Instead of using all available data to compute gradients, CatBoost uses only the data that came before the current observation in the training set. This approach simulates the real-world scenario where we don't have access to future data when making predictions.
Let's expand the math behind this process.
Suppose we have a training dataset of observations, ordered as . At boosting iteration , the model so far is . For each observation , the gradient is:
where:
- : Gradient of the loss function for observation at iteration
- : Loss function (e.g., squared error for regression, log-loss for classification)
- : Actual target value for observation
- : Current model prediction for observation at iteration
- : Feature vector for observation
Traditional boosting computes the split criteria for the next tree using all gradients , i.e., all data points contribute to the statistics used for finding the best splits.
Ordered boosting in CatBoost, however, restricts the information available for each observation. When computing the split statistics for observation , only the gradients from observations with indices are used. That is, for each , the statistics for building the tree at iteration are computed using:
This can be formalized as follows. For each observation , the split statistics used to determine the best split are computed as:
where "Aggregate" denotes the sum or other relevant statistic (such as sum of gradients and Hessians) required for the split-finding algorithm.
This process ensures that, for each observation, the model and the splits are built using only information that would have been available up to that point in the data sequence, thus preventing "target leakage" from future data and reducing overfitting.
In summary:
- The gradient for observation at iteration is:
- When building the tree at iteration , CatBoost uses only the gradients from observations to compute the split criteria for observation :
This ordered approach reduces leakage during training by simulating the real-world scenario where future data is not available.


Mathematical Properties
CatBoost's symmetric tree structure provides several important mathematical properties. The constraint that all nodes at the same level use the same feature creates a more balanced tree structure, which helps prevent overfitting and improves generalization. This structure also makes the model more interpretable, as the decision path is more predictable and easier to understand.
The ordered boosting technique provides a form of regularization by preventing the model from using information that wouldn't be available in real-world scenarios. This approach helps models perform better on unseen data, particularly in time-series or sequential data scenarios.
CatBoost's categorical feature processing creates smooth, continuous representations of categorical variables that are learned during training. This approach often results in better performance than traditional encoding methods because the encoding is optimized specifically for the task at hand, rather than using generic preprocessing techniques.