Skip to content

Instantly share code, notes, and snippets.

@akaron
Created June 8, 2019 02:17
Show Gist options
  • Save akaron/aac6aca533be10ded92c2d216bf4da5e to your computer and use it in GitHub Desktop.
Save akaron/aac6aca533be10ded92c2d216bf4da5e to your computer and use it in GitHub Desktop.
mean and standard deviation for node.js
#!/usr/bin/env node
'use strict';
// basic functions for mean and standard deviation
let getMean = (data) => {
return data.reduce(function (a, b) {
return Number(a) + Number(b);
}) / data.length;
};
let getStandardDevitation = (data) => {
let m = getMean(data);
return Math.sqrt(data.reduce(function (sq, n) {
return sq + Math.pow(n - m, 2);
}, 0) / (data.length - 1));
};
// example
var a = [60, 70, 80, 90];
console.log(getMean(a)); // returns: 75
console.log(getStandardDevitation(a)); // returns: 12.909944487358056
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment