Tutorial: Uncertainty estimation with CatBoost | by Liudmila Prokhorenkova | Towards Data Science

This tutorial covers the following topics:
What is predictive uncertainty and why should you care about it?
What are the two sources of uncertainty?
How to estimate uncertainty for regression problems using the CatBoost gradient boosting library
You can follow all steps using this Jupyter Notebook.
Machine learning has been widely applied to a range of tasks. However, in certain high-risk applications, such as autonomous driving, medical diagnostics, and financial forecasting, a mistake can lead to either a fatal outcome or large financial loss. In these applications, it is important to detect when the system makes a mistake and take safer actions. Furthermore, it is also desirable to collect these “failure scenarios”, label them, and teach the system to make the correct prediction through active learning.
Predictive uncertainty estimation can be used for detecting errors . Ideally, the model indicates a high level of uncertainty in situations where it is likely to make a mistake. That allows us to detect errors and take safer actions. Crucially, the choice of action can depend on why the model is uncertain. There are two main sources of uncertainty: data uncertainty (also known as aleatoric uncertainty) and knowledge uncertainty (also known as epistemic uncertainty). If our goal is to detect errors, it is not necessary to separate these two uncertainties. However, if our goal is active learning, then we would like to detect novel inputs, and knowledge uncertainty can be used for that.
Data uncertainty arises due to the inherent complexity of the data, such as additive noise or overlapping classes. In these cases, the model knows that the input has attributes of multiple classes or that the target is noisy. Importantly, data uncertainty cannot be reduced by collecting more training data.
Knowledge uncertainty arises when the model is given an input from a region that is either sparsely covered by the training data or far from the training data. In these cases, the model knows very little about this region and is likely to make a mistake. Unlike data uncertainty, knowledge uncertainty can be reduced by collecting more training data from a poorly understood region.
This tutorial post details how to quantify both data and knowledge uncertainty in CatBoost.
To illustrate the concepts, we’ll use a simple synthetic example.
Assume that we have two categorical features x1 and x2 with 9 values each, so there are 81 possible feature combinations. The target depends on the features as
y = mean(x1,x2) + eps(x1,x2)
where mean(x1,x2) is some unknown fixed value and eps(x1,x2) is a normally distributed noise (i.e., data uncertainty) with mean 0 and variance var(x1,x2). In our examples, mean(x1,x2) is randomly generated, and var(x1,x2) has two values (0.01 and 0.04) that are distributed as follows:
Thus, points on the heart have more noise in the targets than points outside the heart. Note that we enumerated the categories for a nicer visualization, but in the dataset, both features are categorical, i.e., the order is not given.
When we generate the dataset with this distribution, we assume that we do not have any training examples inside the heart — these feature combinations are considered outliers for our dataset.
The standard model optimized with the RMSE loss can only predict mean(x1,x2). Ok, but what if we want to estimate the variance of y, i.e., data uncertainty? In other words, what if we want to understand which predictions are noisy? To estimate data uncertainty, one has to use probabilistic regression models that predict both mean and variance. For this purpose, there is a new loss function in CatBoost called RMSEWithUncertainty. With this loss, CatBoost estimates the mean and variance of the normal distribution optimizing the negative log-likelihood and using natural gradients, similarly to the NGBoost algorithm [1]. For each example, CatBoost model returns two values: estimated mean and estimated variance.
Let’s try to apply this loss function to our simple example. We get the following variance:
We can see that CatBoost successfully predicts the variance on the heart and outside it. Inside the heart, we have no training data, so anything can be predicted there.
Ok, we know how to estimate noise in the data. But how to measure knowledge uncertainty coming from the lack of training data in a particular region? What to do if we want to detect outliers? Estimating knowledge uncertainty requires an ensemble of models. If all the models understand an input, they will give similar predictions (low knowledge uncertainty). However, if the models do not understand the input, then they are likely to provide diverse predictions and strongly disagree with each other (high knowledge uncertainty). For regression, knowledge uncertainty can be obtained by measuring the variance of the mean across multiple models. Note that this is different from the predicted variance of a single model, which captures data uncertainty.
Let’s consider ensembles of GBDT models generated as follows:
The models are generated using the option posterior_sampling since this allows the obtained (random) predictions to be nicely distributed (with good theoretical properties, and here we refer to [2] for details).
Then, to estimate the knowledge uncertainty, we just compute the variance of the mean values predicted by models:
knowledge = np.var(ens_preds, axis=0)[:, 0]
We obtain the following result:
The model correctly detected knowledge uncertainty inside the heart (we can see no traces of the original heart border). This illustrates how, by estimating knowledge uncertainty, we can detect anomalous inputs.
In practice, training an ensemble of several CatBoost models can be too expensive. Ideally, we would like to train a single model but still be able to detect outliers. There is a solution for that: we can use a virtual ensemble, obtained from a single trained model:
Having a single trained model, CatBoost returns several predictions for each example. These predictions are obtained via truncating the model:
Again, we use the option posterior_sampling to guarantee a desirable distribution of cropped predictions. Let’s see what we obtain:
Note that the predicted absolute values of knowledge uncertainty are now much smaller since the virtual ensemble elements are correlated. But still, it can successfully detect unoccupied regions (outliers).
Instead of prediction_type=’VirtEnsembles’ that returns the predictions of several models, we could use prediction_type=’TotalUncertainty’ and get the same result easier. For this prediction type, CatBoost calculates all types of uncertainty using a virtual ensemble. Namely, for RMSEWithUncertainty it returns the following statistics: [Mean Prediction, Knowledge Uncertainty, Data Uncertainty]:
Thanks for your attention! I hope this tutorial helped you to better understand the concept of uncertainty and how to estimate it with CatBoost. We’ll tell you more about applications of uncertainty in future posts. Stay tuned 😺
[1] T. Duan et al., NGBoost: Natural Gradient Boosting for Probabilistic Prediction (2020), ICML 2020
[2] A. Ustimenko, L. Prokhorenkova and A. Malinin, Uncertainty in Gradient Boosting via Ensembles” (2020), arXiv preprint arXiv:2006.10562