Search code examples
pythonalgorithmalgorithmic-trading

Finding unusual value in an array, list


I have sales statistic data in array form to calc standard deviation or average from this data.

stats = [100, 98, 102, 100, 108, 23, 120] 

let said +-20% differential is normal situation, 23 is obviously a special case.

what's the best algorithm (in any language, pseudo or any principle) to find this unusual value?


Solution

  • You could convert them to Z-scores and look for outliers.

    >>> import numpy as np
    >>> stats = [100, 98, 102, 100, 108, 23, 120]
    >>> mean = np.mean(stats)
    >>> std = np.std(stats)
    >>> stats_z = [(s - mean)/std for s in stats]
    >>> np.abs(stats_z) > 2
    array([False, False, False, False, False,  True, False], dtype=bool)