10  Z-score

\[ z = \frac{x-\mu}{\sigma}, \]

where

  • \(x=\) data point,
  • \(\mu=\) time series mean
  • \(\sigma=\) time series standard deviation.

Let’s write a function that identifies outliers according to the Z-score.

def zscore(df, degree=3):
    data = df.copy()
    data['zscore'] = (data - data.mean())/data.std()
    outliers = data[(data['zscore'] <= -degree) | (data['zscore'] >= degree)]
    return outliers['value'], data

Now we can simply use this function:

threshold = 2.5
outliers, transformed = zscore(tx, threshold)

Source: Atwan (2022)