Sub topic 11
CatBoost: ordered boosting and prediction shift
This chapter assumes you know how gradient boosting builds trees on residuals (the "How gradient boosted trees work" chapter), what a train/validation split does, and basic Python. It explains one design choice in CatBoost: how the library computes the gradients that each new tree fits. The problem it solves is called prediction shift.
The problem: gradients computed on data the model has seen
In standard gradient boosting, iteration t does the following:
- Take the current model
F(t-1). - Compute the gradient of the loss for every training row.
- Fit a new tree to those gradients.
- Add the tree to the model.
The gradients at step 2 are computed for the same rows that trained F(t-1). The model has already seen those rows' labels, so its predictions on them are better than its predictions on new data. The residuals are smaller and less noisy than they would be for unseen rows.
That is a mismatch between two distributions:
- The gradients the tree is fit to come from rows the model has already memorized.
- The gradients the model meets at prediction time come from rows it has never seen.
The CatBoost authors call this mismatch prediction shift. The shift is a small bias at each iteration. It compounds over hundreds of trees and shows up as overfitting, mostly on small or noisy datasets.
A quick demonstration
The script below fits a tree to pure noise, so the true signal is zero. It then compares residual variance on the training rows with the variance on fresh rows.
import numpy as np
from sklearn.tree import DecisionTreeRegressor
rng = np.random.default_rng(0)
n, p = 500, 10
X_train = rng.normal(size=(n, p))
y_train = rng.normal(size=n)
X_fresh = rng.normal(size=(n, p))
y_fresh = rng.normal(size=n)
tree = DecisionTreeRegressor(max_leaf_nodes=32, random_state=0)
tree.fit(X_train, y_train)
train_resid = y_train - tree.predict(X_train)
fresh_resid = y_fresh - tree.predict(X_fresh)
print("train residual variance:", round(train_resid.var(), 3))
print("fresh residual variance:", round(fresh_resid.var(), 3))The training residuals have a much smaller variance than the fresh ones, because the tree has fitted noise. The next boosting round would receive these shrunken residuals as its target. They are not representative of the residuals on new data.
The idea: never score a row with a model that trained on it
Ordered boosting removes the dependence. The idea is to compute the gradient for row i using a model that was trained without row i.
CatBoost does this with a random permutation of the training rows, treated as an artificial time order:
- For each row
i, build a supporting modelM(i)trained only on the rows that come beforeiin the permutation. - Use
M(i)to compute the gradient for rowi. - Use those gradients to choose the tree structure and the leaf values.
The label of row i never influences the prediction used to compute its own gradient. The gradient for a row is then an unbiased estimate of what the model would see on unseen data.
Making it affordable
Training a separate model for each of n rows at every iteration would be far too slow. CatBoost reduces the cost with three approximations:
- It keeps supporting models only for prefixes of sizes 1, 2, 4, 8, and so on, about
log2(n)models. A row at positioniuses the model trained on the largest prefix that fits before it. - It updates those models incrementally as trees are added, instead of retraining them from scratch.
- It uses several permutations (the paper uses
s + 1of them). A freshly drawn permutation helps pick each tree's structure, and the final leaf values use the full-data model. This reduces the variance that one random order would add.
Rows early in a permutation are scored by models trained on very few rows, so their gradients are noisy. This is why ordered mode helps most on small datasets, where the bias of plain boosting is largest, and costs more than it gains on large ones.
Choosing the mode in CatBoost
The parameter is boosting_type. It has two values:
| Value | Behavior | Typical use |
|---|---|---|
| `"Plain"` | Classic gradient boosting. Gradients come from the model trained on all rows. | Large datasets, speed |
| `"Ordered"` | Ordered boosting with permutation-based supporting models. | Small datasets, where overfitting is the main risk |
CatBoost picks a default based on dataset size and hardware. The rule has changed between releases, so set boosting_type explicitly in any experiment you want to reproduce.
The example below trains both modes on a small noisy dataset and compares validation AUC.
import numpy as np
from catboost import CatBoostClassifier
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
n = 1500
X = rng.normal(size=(n, 8))
logit = 1.2 * X[:, 0] - 0.8 * X[:, 1] + 0.5 * X[:, 2] * X[:, 3]
y = (logit + rng.normal(scale=1.5, size=n) > 0).astype(int)
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.3, random_state=0
)
for mode in ["Plain", "Ordered"]:
model = CatBoostClassifier(
iterations=500,
learning_rate=0.05,
depth=6,
boosting_type=mode,
random_seed=0,
verbose=0,
)
model.fit(X_train, y_train, eval_set=(X_valid, y_valid),
early_stopping_rounds=50)
auc = roc_auc_score(y_valid, model.predict_proba(X_valid)[:, 1])
print(mode, "best iteration:", model.get_best_iteration(),
"valid AUC:", round(auc, 4))The two modes may differ by a little or not at all. A gap on one split with one seed is not evidence. Repeat the comparison with several seeds or with cross-validation before you decide.
Common mistakes and limits
- Treating ordered boosting as free. It costs more time and memory than
Plain. On a dataset with millions of rows the bias it removes is small, and the extra training time is rarely worth it. - Confusing it with ordered target statistics. Both use a random permutation and the idea of "only rows before this one", but they solve different problems. Ordered boosting fixes the bias of gradients. Ordered target statistics fix target leakage when encoding categorical features. They are separate mechanisms that happen to share a principle. The next chapter covers the second one.
- Expecting identical results across runs. The permutations are random. Fix
random_seedfor reproducibility and average over seeds when comparing settings. - Assuming every option works with
Ordered. Some loss functions, device types, and parameters are not available with it. If CatBoost raises an error after you switch the mode, check the documentation for your installed version. - Judging the mode on training metrics. Prediction shift is a train/test mismatch, so training loss will not show it. Compare modes on validation data.
Exercises
Exercise
In your own words, why are gradients computed by a model on its own training rows biased compared with gradients on unseen rows? Name the effect CatBoost's authors give to the resulting mismatch.
Show answer
The model has already fitted the labels of its training rows, so its predictions there are closer to the labels than on unseen rows. The residuals and gradients are therefore smaller and less noisy for training rows, and the next tree learns from a distribution that differs from the one seen at prediction time. This mismatch is called prediction shift.
Exercise
Ordered boosting trains a supporting model M(i) for each row i using only the rows before i in a random permutation. Why does CatBoost not train n separate models in practice, and what does it do instead?
Show answer
Training n models at every iteration would cost far too much time and memory. CatBoost keeps supporting models only for prefixes of exponentially growing size (about log2(n) models), updates them incrementally as trees are added, and uses several random permutations to reduce the variance of any single ordering.
Exercise
You run the Plain vs Ordered comparison on a 400,000-row dataset and find that Ordered trains about twice as slowly with almost the same validation AUC. What do you conclude, and which setting do you keep?
Show answer
Prediction shift is a bias that matters most on small datasets. With 400,000 rows the bias is small, so Ordered adds cost without a meaningful gain. Keep Plain, after confirming with several seeds or cross-validation that the AUC difference is within noise.