unit8co / darts

A python library for user-friendly forecasting and anomaly detection on time series.
https://unit8co.github.io/darts/
Apache License 2.0
7.91k stars 858 forks source link

Why can't we use LocalForecastingModel for Anomaly Detection? #2509

Open Ashish-Surve opened 3 weeks ago

Ashish-Surve commented 3 weeks ago

I was trying to use Prophet from darts for anomaly detection but it seems we can only use global forecasting models for anomaly detection.

Is there a way I can use scorer or threshold based method to consider something anomaly based on the actuals and predicted?

dennisbader commented 2 weeks ago

Hi @Ashish-Surve. The reason why we only accept global models is that we want to have a pre-trained model that generalizes well over the non-anomalous data. A global model can be pre-trained on any number of series, and then be used to forecast the future of any (other) number of series using the same model state. If this model is pre-trained on non-anomalous time frames, then we expect that it will not be able to forecast an anomaly, but rather what would have happened if there was no anomaly. This results in a large difference between actual values (with anomaly) and predicted values (no predicted anomaly), and ultimately in larger anomaly scores (good to detect anomalies).

Local models (like Prophet) on the other hand can only predict the future right after the end of the series that they have been trained on. So if we want to generate all (historical) forecasts to compute the anomaly scores over some input series, we have to retrain the forecast model at every step. That means, that the model might include anomalies already in the relative training set, and then it will/might be able to forecast that anomaly in the future. The difference between actual values (with anomaly) and predict values (with predicted anomaly) will be small, and lead to lower anomaly scores (bad for detecting anomalies).

And yes, you can find a step-by-step example on how to perform this here (tasks 1-5).