Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Last active December 11, 2023 14:46
Show Gist options
  • Save Daniel-Hug/7273430 to your computer and use it in GitHub Desktop.
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
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;
@bitfool
Copy link

bitfool commented Feb 17, 2017

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.

@MOZGIII
Copy link

MOZGIII commented Oct 8, 2017

median has a mistake. mid % 1 is always 0.
Edit: nope, it's ok.

@beerbauf
Copy link

Great job! Really usefull! Thanks!

@lucamarogna
Copy link

lucamarogna commented Jan 8, 2019

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', ...

@patelnwd
Copy link

It really helps me a lot.
Thanks

@bobharley
Copy link

bobharley commented Dec 20, 2020

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