Last active
December 11, 2023 14:46
-
-
Save Daniel-Hug/7273430 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; |
Thanks for sharing.
Personally I think it's better to underline that array must be one dimension and all values must be numeric values.
In my code I create this method inside your object:
_onlyNumericValues: function(array) {
return array.filter(el => !isNaN(parseFloat(el)) && isFinite(el) );
},
and then I call it whenever is necessary.
Of course one could purge array even before running this functions.
But I still think that you need to be sure that there aren't values such as undefined
, true/false
, 'string'
, ...
It really helps me a lot.
Thanks
For the variance, it should be divided by length - 1
arr.variance: function(array) {
var mean = arr.mean(array);
return arr.sum(array.map(function(num) {
return Math.pow(num - mean, 2);
})) / (array.length - 1)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great job! Really usefull! Thanks!