Skip to content

Commit

Permalink
analysis.getModZScore() calculates the modified z-score of some data …
Browse files Browse the repository at this point in the history
…arrays
  • Loading branch information
ibressler committed Jun 10, 2021
1 parent 712d590 commit 2250a59
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# analysis.py

import numpy as np

# from https://stackoverflow.com/a/22357811
# and https://github.com/joferkington/oost_paper_code/blob/master/utilities.py#L167
# (code with MIT License)
def getModZScore(points):
"""
Returns a boolean array with True if points are outliers and False
otherwise.
Note: Similar to https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.zscore.html
but using the median instead of the mean.
Parameters:
-----------
points : An numobservations by numdimensions array of observations
thresh : The modified z-score to use as a threshold. Observations with
a modified z-score (based on the median absolute deviation) greater
than this value will be classified as outliers.
Returns:
--------
mask : A numobservations-length boolean array.
References:
----------
Boris Iglewicz and David Hoaglin (1993), "Volume 16: How to Detect and
Handle Outliers", The ASQC Basic References in Quality Control:
Statistical Techniques, Edward F. Mykytka, Ph.D., Editor.
"""
if len(points.shape) == 1:
points = points[:,None]
median = np.median(points, axis=0)
diff = np.sqrt(np.sum((points - median)**2, axis=-1))
med_abs_deviation = np.median(diff)

# scale being the inverse of the standard normal quantile function at 0.75,
# which is approximately 0.67449, see also:
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.median_abs_deviation.html
#modified_z_score = 0.6745 * diff / med_abs_deviation
# let this indicator be =1 for the same data, makes it more intuitive to understand
modified_z_score = diff / med_abs_deviation

return modified_z_score

0 comments on commit 2250a59

Please sign in to comment.