pingouin.mad#

pingouin.mad(a, normalize=True, axis=0)[source]#

Median Absolute Deviation (MAD) along given axis of an array.

Parameters:
aarray-like

Input array.

normalizeboolean.

If True, scale by a normalization constant c0.67 to ensure consistency with the standard deviation for normally distributed data.

axisint or None, optional

Axis along which the MAD is computed. Default is 0. Can also be None to compute the MAD over the entire array.

Returns:
madfloat

mad = median(abs(a - median(a))) / c

Notes

The median absolute deviation (MAD) computes the median over the absolute deviations from the median. It is a measure of dispersion similar to the standard deviation, but is more robust to outliers.

SciPy 1.3 and higher includes a similar function: scipy.stats.median_abs_deviation().

Please note that missing values are automatically removed.

Examples

>>> from pingouin import mad
>>> a = [1.2, 5.4, 3.2, 7.8, 2.5]
>>> mad(a)
2.965204437011204
>>> mad(a, normalize=False)
2.0

2D arrays with missing values (axis handling example)

>>> import numpy as np
>>> np.random.seed(123)
>>> w = np.random.normal(size=(5, 10))
>>> w[3, 2] = np.nan
>>> mad(w)  # Axis = 0 (default) = iterate over the columns
array([0.60304023, 2.35057834, 0.90350696, 1.28599837, 1.16024152,
       0.38653752, 1.92564066, 1.2480913 , 0.42580373, 1.69814622])
>>> mad(w, axis=1)  # Axis = 1 = iterate over the rows
array([1.32639022, 1.19295036, 1.41198672, 0.78020689, 1.01531254])
>>> mad(w, axis=None)  # Axis = None = over the entire array
1.1607762457644006

Compare with Scipy >= 1.3

>>> from scipy.stats import median_abs_deviation
>>> median_abs_deviation(w, scale='normal', axis=None, nan_policy='omit')
1.1607762457644006