Absolute Deviation and Absolute Mean Deviation using NumPy


In Statistical analysis study of data variability in a sample indicates how dispersed are the values in a given data sample. The two important ways we calculate the variability are Absolute Deviation and  Mean Absolute Deviation.

Absolute Deviation

In this method we first find the mean value of the given sample and then calculate the difference between each value and the mean value of the sample called as the absolute deviation value of each data sample. So for the values higher than mean the value the deviation value will be positive and for those lower than the mean value the deviation value will be negative. Next we take the absolute values using the absolute function to make each deviation as positive. Summing up all these absolute deviation gives a positive result. Without absolute value the sum of these deviations would be zero.

In the below example we take a data sample and calculate the absolute deviation for each data element.

Example

from numpy import mean, absolute
   data = [12, 42, 53, 13, 112]
# Find mean value of the sample
M = mean(data)
print "Sample Mean Value = ",mean(data)
print "\n"
# Calculate absolute deviation
print "Data-Mean","","deviation"
for i in range(len(data)):
   dev = absolute(data[i] - M)
   print data[i],"-",M,round((dev),2)

Output

Running the above code gives us the following result −

Sample Mean Value = 46.4
Data-Mean deviation
12 - 46.4 34.4
42 - 46.4 4.4
53 - 46.4 6.6
13 - 46.4 33.4
112 - 46.4 65.6

Mean Absolute Deviation (MAD)

Mean Absolute Deviation (MAD) is the mean value of all the absolute deviations we calculate for each data point. Taking the same sample as in previous example, we add code to sum up the value of the absolute deviations and divide it by the sample size.

Example

from numpy import mean, absolute
data = [12, 42, 53, 13, 112]
# Find mean value of the sample
M = mean(data)
print "Sample Mean Value = ",mean(data)
sum = 0
# Calculate mean absolute deviation
for i in range(len(data)):
   dev = absolute(data[i] - M)
   sum = sum + round(dev,2)
print "Mean Absolute Deviation: ", sum/len(data)

Output

Running the above code gives us the following result −

Sample Mean Value = 46.4
Mean Absolute Deviation: 28.88

Updated on: 03-Jul-2020

846 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements