-
-
Save Daniel-Hug/7273430 to your computer and use it in GitHub Desktop.
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, simple and sweet. i added meanSample, varianceSample, and standardDevationSample to my own fork, where meanSample simply divides by (array.length-1) instead of array.length, and the others reference meanSample or varianceSample as appropriate. feel free to add to yours if desired.
median
has a mistake. mid % 1
is always 0
.
Edit: nope, it's ok.
Great job! Really usefull! Thanks!
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)
}
JavaScript statistical functions for arrays:
max
min
range
midrange
sum
mean
/average
median
modes
variance
standardDeviation
meanAbsoluteDeviation
zScores
They are all methods of the
arr
object. They all accept an array as their only argument.arr.modes
andarr.zScores
return an array, and the rest will return a number.