Created
June 8, 2019 02:17
-
-
Save akaron/aac6aca533be10ded92c2d216bf4da5e to your computer and use it in GitHub Desktop.
mean and standard deviation for node.js
This file contains hidden or 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
#!/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