-
-
Save bitfool/f63a55349683409ea54b8f74dc02f14b to your computer and use it in GitHub Desktop.
JavaScript statistical functions for arrays: max, min, range, midrange, sum, mean / average, median, modes, variance, standard deviation, mean absolute deviation, z scores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = { | |
max: function(array) { | |
return Math.max.apply(null, array); | |
}, | |
min: function(array) { | |
return Math.min.apply(null, array); | |
}, | |
range: function(array) { | |
return arr.max(array) - arr.min(array); | |
}, | |
midrange: function(array) { | |
return arr.range(array) / 2; | |
}, | |
sum: function(array) { | |
var num = 0; | |
for (var i = 0, l = array.length; i < l; i++) num += array[i]; | |
return num; | |
}, | |
mean: function(array) { | |
return arr.sum(array) / array.length; | |
}, | |
median: function(array) { | |
array.sort(function(a, b) { | |
return a - b; | |
}); | |
var mid = array.length / 2; | |
return mid % 1 ? array[mid - 0.5] : (array[mid - 1] + array[mid]) / 2; | |
}, | |
modes: function(array) { | |
if (!array.length) return []; | |
var modeMap = {}, | |
maxCount = 0, | |
modes = []; | |
array.forEach(function(val) { | |
if (!modeMap[val]) modeMap[val] = 1; | |
else modeMap[val]++; | |
if (modeMap[val] > maxCount) { | |
modes = [val]; | |
maxCount = modeMap[val]; | |
} | |
else if (modeMap[val] === maxCount) { | |
modes.push(val); | |
maxCount = modeMap[val]; | |
} | |
}); | |
return modes; | |
}, | |
variance: function(array) { | |
var mean = arr.mean(array); | |
return arr.mean(array.map(function(num) { | |
return Math.pow(num - mean, 2); | |
})); | |
}, | |
standardDeviation: function(array) { | |
return Math.sqrt(arr.variance(array)); | |
}, | |
meanAbsoluteDeviation: function(array) { | |
var mean = arr.mean(array); | |
return arr.mean(array.map(function(num) { | |
return Math.abs(num - mean); | |
})); | |
}, | |
zScores: function(array) { | |
var mean = arr.mean(array); | |
var standardDeviation = arr.standardDeviation(array); | |
return array.map(function(num) { | |
return (num - mean) / standardDeviation; | |
}); | |
} | |
}; | |
// Function aliases: | |
arr.average = arr.mean; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment