/* @flow */ import median from "./median"; /** * The [Median Absolute Deviation](http://en.wikipedia.org/wiki/Median_absolute_deviation) is * a robust measure of statistical * dispersion. It is more resilient to outliers than the standard deviation. * * @param {Array} x input array * @returns {number} median absolute deviation * @example * medianAbsoluteDeviation([1, 1, 2, 2, 4, 6, 9]); // => 1 */ function medianAbsoluteDeviation(x /*: Array */) /*: number */ { // The mad of nothing is null const medianValue = median(x); const medianAbsoluteDeviations = []; // Make a list of absolute deviations from the median for (let i = 0; i < x.length; i++) { medianAbsoluteDeviations.push(Math.abs(x[i] - medianValue)); } // Find the median value of that list return median(medianAbsoluteDeviations); } export default medianAbsoluteDeviation;