Ad
Algorithms
Logic
Arrays
Data Types
Mathematics
Numbers
Statistics
Data

The Mean Average Distribution (MAD) is a math concept which is taught before Standard Deviation and then mostly forgotten. Like a standard deviation, the MAD is a value which shows the variability or spread for a dataset around the mean.

The steps to calculate the MAD are as follows:

  1. Determine the average

  2. For each datapoint determine the distance from the average.

  3. Sum all of the distances

  4. Take an average of the sum

For instance for the followings datasets:

(2,2,4,4) has an Average of 3 and a MAD of 1

(1,1,4,6) has an Average of 3 and a MAD of 2

Therefore the data in the second dataset is more spread out.

Write a function to calculate the Mean Average Distribution of a dataset of arbitrary length.

function mad(array) {
if(array.length === 0){return null}
if(array.length === 1){return 0}

let average = (array.reduce((acc, cV) => acc + cV))/array.length
let array2 = array.map((item,index,arr)=> Math.abs(item-average))
return (array2.reduce((acc, cV) => acc + cV))/array2.length;
}