Forecasting: How to Detect Outliers?
By: Nicolas Vandeput
Tags: Demand Planning, Supply Chain, Outliers, Time Series Analysis, Forecasting

“I shall not today attempt further to define this kind of material (…), and perhaps I could never succeed in intelligibly doing so. But I know it when I see it.”

Potter Stewart

In 1964, Potter Stewart was a United States Supreme Court Justice. He wasn’t discussing outliers but whether the movie The Lovers was or wasn’t obscene.

As you work on forecast, you will notice that your dataset will have outliers. And even though I know it when I see it might be the only practical definition, these outliers pose a real threat to supply chains. These high (or low) points will result in over-reactions in your forecast or your safety stocks, ultimately resulting in (at best) manual corrections or (at worst) dead stocks, losses, and a nasty bullwhip effect. Actually, when you look at blogs, books, articles, or software on forecast, the question of outlier detection is often eluded. This is a pity. Outlier detection is a serious business.

These outliers pop out all the time in modern supply chains. They are mostly due to two main reasons:

Mistakes & Errors These are obvious outliers. If you spot such kind of errors or encoding mistakes, it calls for process improvement to prevent these from happening again.

Exceptional demand Even though some demand observations are real, it does not mean they are not exceptional and shouldn’t be cleaned or smoothed. This kind of extraordinary sales is actually not so uncommon in supply chains. Think about promotions, marketing, strange customer behaviors, or destocking. Typically, you might not want to take into account for your forecast the exceptional -80% sales you did last year to get rid of an old nearly-obsolete inventory.

If you can spot outliers and smooth them out, you will make a better forecast. I have seen numerous examples where the forecast error was reduced by a couple of percent just thanks to outlier cleaning. Actually, the bigger the dataset, the more critical it is to automate this detection and cleaning. Let’s see how we can do this.

In this article, we will discuss three and a half ideas to spot these outliers and put them back to a reasonable level.

Idea #1 — Winsorization

As we said, an outlier is an exceptionally high or low value. Based on this simple definition, a first idea to detect outliers would be to simply cut down the top x highest and lowest points of the dataset. Let’s see how this would work on the two (dummy) datasets on the tables below.

This first technique will simply decrease the top/down x% values of our historical demand down to the limit of the Xth percentile.

The Xth percentile is a value below which x% of the observations in a group will fall. For example, 99% of the demand observations for a product will be lower than its 99th percentile.

This technique of simply shrinking the demand down to a particular percentile is called winsorization. The name comes from Charles P. Winsor, a statistician from the first half of the XXth century.

If we look at the 1st and 99th percentile on our two dummy datasets above, this is what we obtain:

On this table, we see that in both datasets, all the low values would be increased up to 4.4. You can see in the figure below that this cuts a part of our dataset. The high values would be decreased down to 16.6 on the dataset without outliers (see figure 10.1) and down to 70.9 for the dataset with an outlier (see figure 10.2).

You might have noticed that the winsorization didn’t give us round results such as 4 or 5, but instead, we got this 4.4. Actually, as we don’t have an exact value that cuts the dataset by 99%, we make a linear approximation based on the two closest values. This is how we got these numbers instead of round numbers.

So, are we happy with this technique?

No, we’re not.

- We have spotted fake outliers on a dataset without outliers.

- On the dataset with outliers, we haven’t sufficiently reduced the outlier (it went from 100 to 70.9).

Of course, one could simply propose to decrease the higher limit of the winsorization from 99% to 95% to reduce the outlier on dataset #2 further. Still, unfortunately, this would also affect dataset #1. This is not a good solution. One could also propose to remove this lower limit so that we don’t increase our demand to 4.4. But, what if we have periods with missing demand? Shouldn’t we clean these as well, if any?

Do It Yourself

Excel You can quickly get the different percentiles of a range of cells in Excel by using the formula =PERCENTILE.INC(range,limit). Of course, you’ll have to use this formula once for the upper limit (with a value around 0.95–0.99) and once for the lower limit (with a value around 0.01–0.05).

Python We can easily winsorize our dataset in Python thanks to NumPy. We can compute the different percentiles of an array thanks to the np.percentile(array,percentile) function.

import numpy as np

higher_limit = np.percentile(array, 99)

lower_limit = np.percentile(array, 1)

Note that the percentile function takes a percentile expressed as a value between 0 and 100 and not a ratio (i.e., a value between 0 and 1) like in Excel.

We can then simply cut the array to these lower and higher limits thanks to the function np.clip(array,min,max):