# Is there a way to know if a machine learning model is extrapolating at inference time?

I am working on a regression model, trained with a finite dataset. The algorithm that I use is a LightGBM, but I think the solution I am looking for would be algorithm-agnostic in essence.

I suspect that at evaluation time, some of the predictions of my model are off because it is extrapolating too far away from the examples it has seen during training.

Is there a general sound way to detect if a model is in the extrapolation domain during inference? Even better: is there a way to quantify "how far" a test example is from the training set? One could probably use a distance metric such as the Mahalanobis distance, but I am not sure if it would be the correct way to do it (moreover, to use this specific metric would require the features to be distributed according to a Gaussian distribution, which is rarely the case in practice).

I found this interesting related article : To what extent should we trust AI models when they extrapolate?, which introduces the idea of the convex hull defined by the training set to define the extrapolation distance. But more generally, I am a bit surprised that there doesn't seem to be much more discussion, investigations or Python code developed about this issue. Any interesting view about this, or any source / code to share? Thank you!

• @user2974951 In higher dimensions would you use a bounding box? A minimum convex hull? Or something else? It seems there isn't a unique sense of "in the range of the data" in higher dimensions. Sep 5, 2023 at 14:30
• Check if a variable is in the range, repeat for each variable, nothing complicated. Sep 5, 2023 at 14:32
• @user2974951 As Galen says, it does get quite complicated in >1 dimension.
– mkt
Sep 5, 2023 at 14:33
• @user2974951 That approach of checking the axis-wise intervals corresponds to checking if the prediction falls within the axis-aligned minimum bounding box (AAMBB) of the data. There will be tradeoffs in choosing among the different methods. AAMBB is nice for being very easy to compute. On the downside it will ignore many attributes of the shape of the data that may not be box-like. (Also +1 for getting me thinking about the problem) Sep 5, 2023 at 14:41
• How about this: For each point in question, find its (D+1) nearest neighbours (D being the data dimensionality). Check whether the point in question is inside the simplex defined by the neighbours. Or am I missing something? Sep 5, 2023 at 14:58

This is (in my view) a hard problem.

Broadly-speaking you can follow this process, although I don't claim that it is unique or in any sense superior. It is just a rough procedure.

1. Choose a family of geometric shapes (e.g. orthotope, n-sphere, n-ellipsoid, etc).
2. Choose a notion of "smallest"
3. Find the smallest member of that class of shapes which contains the entire dataset.
4. When computing a prediction, check if the points used to make the prediction are inside the shape.

This is a basic template that hardly begins to scratch the surface of how to choose shapes. The broad and vague notion is to find a shape that seems to "fit snugly" around your data. But there can also be computational costs to consider as well.

But there is a further problem: holes. If your data is sampled from a manifold with topological holes then you will need to decide if you want to account for that. If you use topological data analysis, which is quite computationally expensive in my experience, you might be able to detect such holes. In some sense they are inside "range of the data". But from an inference point of view there could be very different statistical properties in those voids, which goes back to the familiar statistical question of "why data is missing"?

This will likely require to be tested for each use case and may not always be straightforward to implement, but another approach may be to use a prediction interval to see how confident the model is in its prediction and discard or flag predictions past a certain threshold. Of course this implies that the prediction interval gets wider and wider when it extrapolates, which must be ascertained by testing.

To determine the prediction interval for model types where there is no analytic expression of the prediction interval, conformal prediction may be used.

Then the threshold may be arbitrary or calibrated on values or data points you consider to be in an acceptable range.