21  sliding algorithms

None of the methods learned before seem appropriate to deal with non-stationary data. A simple solution is to apply those methods for sliding windows of “appropriate” widths.

21.1 Sliding Z-score

Now the Z-score seems to give really nice results (but not perfect). Maybe playing with the window width and Z-score threshold would give better results?

In any case, we clearly see why the Z-score is not a robust algorithm. See how the standard deviation is sensitive to outliers?

Two things to keep in mind. The window includes the point we are testing, so an outlier makes its own threshold wider. And the window width has to be chosen with care: it should be much longer than the outlier events we want to catch (otherwise they dominate the window), but shorter than the time scale in which the signal changes. This is the same question we asked about smoothing.

21.2 Sliding IQR

Let’s see how well the sliding IQR method fares.

It identified all the outliers, but also found that a few other points should be considered outliers? What do you think of that?

See that the threshold does not jump abruptly when the sliding window includes an outlier. In fact, the threshold doesn’t even care! This is what it means to be robust.

However, we do see large fluctuations in the threshold. When does this happen? Why?

21.3 Sliding MAD

Now it’s MAD’s time to shine. A sliding median with a MAD threshold is known as the Hampel filter: search for it if you ever need a ready-made implementation.

Compare this result to the previous two. Which yields best results?

MAD is robust to outliers, and again we see that the threshold envelope widens when there is a rising or falling trend in the data.

In real data, periodic cycles (like the daily cycle of temperature) widen the envelope in the same way. Knowing how to take care of periodic cycles could be very useful here: we will learn about it in the chapter on seasonality.

21.4 not every outlier is a single point

So far we have been looking for isolated points, but outliers come in other shapes too: a group of points, like the plateau of a stuck sensor, or a level shift: the whole series jumps to a new level, and stays there.

Level shifts are common in environmental data. Dendrometers are a good example: when the spring of the instrument is recalibrated, the record shifts, even though the tree did nothing. Here is such a shift in one of our records. The drop of almost 0.4 mm in half an hour is much larger than the daily cycle (about 0.1 mm), and the cycle goes on, at the new level.

The sliding methods of this chapter cannot deal with this. The window may flag the points around the jump, but a few points later it is full of the new level, and everything looks normal again. A level shift is not something to delete: we need to find out when it happened, measure the size of the jump, and correct the series after it.

21.5 there is much more out there

We only scratched the surface. Outlier detection is a huge field, and many techniques have a name, which makes them easy to find online. When you need something that we did not cover, search for them: the Hampel filter (the sliding MAD that we saw), Grubbs’ test, the generalized ESD test, STL decomposition (to remove the seasonal cycle before looking for outliers), Isolation Forest, Local Outlier Factor, or the ARIMA-based outlier detection of the R package tsoutliers. A good place to start is section 13.9 of Hyndman’s book.

A warning: not everything you find online is good. Before you trust a method, test it on data where you know the answer, like we do in the homework.

21.6 Challenges

Now it’s your turn to work, I’m tired! Write algorithms for the following outlier identification methods:

  1. visual inspection
  2. Z-score
  3. IQR
  4. MAD

Excluding the visual inspection method, write first an algorithm that operates on a full time series, and then write a new version that can work with sliding windows.