In anomaly detection of time series, should global outliers and contextual outliers be separated? I am trying to create a pipeline in Python which automatically identifies global and contextual anomalies of a time series.
Which one of these approaches do you believe is more correct?
Method 1)

*

*Detect global outliers using z-score threshold.

*Remove the outliers from the time series and impute values

*Detect if seasonality/periodicity is present

*If yes on 3) perform seasonal decomposition

*Detect contextual outliers using z-score threshold on residuals.

*Combine both groups of outliers.

Method 2)

*

*Detect global outliers using z-score threshold.

*Remove the outliers from the time series and impute values

*Detect if seasonality/periodicity is present

*If yes on 3) perform seasonal decomposition

*Remove seasonality and trend from original signal potentially containing global outliers as well

*Detect contextual outliers and global outliers using z-score threshold on residuals.

Method 3)
Something else which you know of?
Appreciate any insights or tips!


It depends on how the nature of your anomalies. If global and contextual outliers co-exist often, and outliers are common outliers in general, and are extreme enough, have clusters or otherwise could impact the seasonal decomposition, then something like method 1 may work better. In all other cases it is just additional complexity, including more hyperparameters to tune to each good performance.
Method 2 is simpler, so implement it first. Evaluate how well it performs. If it is insufficient, then consider a more complex method based on the short-comings that have been identified. Which may not be
