How to Calculate Median Absolute Deviation in Python

Median Absolute Deviation (MAD) is a measure of variation that can be used to calculate the spread of a dataset in Python. It is calculated by first finding the median of the dataset, then subtracting each data point from the median and taking the absolute value of the difference. The absolute values are then summed up and divided by the total number of data points to find the MAD. The MAD is a more robust measure of variation than the standard deviation since it is less affected by outliers.


The median absolute deviation measures the spread of in a dataset.

It’s a particularly useful metric because it’s less affected by outliers than other like standard deviation and variance.

The formula to calculate median absolute deviation, often abbreviated MAD, is as follows:

MAD = median(|xi – xm|)

where:

  • xi: The ith value in the dataset
  • xm: The median value in the dataset

The following examples shows how to calculate the median absolute deviation in Python by using the mad function from .

Example 1: Calculate MAD for an Array

The following code shows how to calculate the median absolute deviation for a single NumPy array in Python:

import numpy as np
from statsmodels import robust

#define data
data = np.array([1, 4, 4, 7, 12, 13, 16, 19, 22, 24])

#calculate MAD
robust.mad(data)

11.1195

The median absolute deviation for the dataset turns out to be 11.1195.

It’s important to note that the formula used to calculate MAD computes a robust estimate of the standard deviation assuming a by scaling the result by a factor of roughly 0.67.

To avoid using this scaling factor, simply set c = 1 as follows:

#calculate MAD without scaling factor
robust.mad(data, c=1)

7.5

Example 2: Calculate MAD for a DataFrame

The following code shows how to calculate MAD for a single column in a pandas DataFrame:

#make this example reproducible
np.random.seed(1)

#create pandas DataFrame
data = pd.DataFrame(np.random.randint(0, 10, size=(5, 3)), columns=['A', 'B', 'C'])

#view DataFrame
data

        A	B	C
0	5	8	9
1	5	0	0
2	1	7	6
3	9	2	4
4	5	2	4

#calculate MAD for column B
data[['B']].apply(robust.mad)

B    2.965204
dtype: float64

We can use similar syntax to calculate MAD for multiple columns in the pandas DataFrame:

#calculate MAD for all columns
data[['A', 'B', 'C']].apply(robust.mad)

A    0.000000
B    2.965204
C    2.965204
dtype: float64

The median absolute deviation is 0 for column A, 2.965204 for column B, and 2.965204 for column C.

x