Last active
July 8, 2016 17:58
-
-
Save stekhn/2e998249b393a6418ec0bbbc3fc85f54 to your computer and use it in GitHub Desktop.
Standard deviation in JavaScript
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
function stdDev(arr) { | |
var avg = mean(arr); | |
var squareDiffs = arr.map(function (value) { | |
var diff = value - avg; | |
var sqrDiff = diff * diff; | |
return sqrDiff; | |
}); | |
return Math.sqrt(mean(squareDiffs)); | |
} | |
function mean(arr) { | |
return arr.reduce(function (previous, current) { | |
return previous + current; | |
}, 0) / arr.length; | |
} | |
stdDev([251, 360, 210, 900, 425, 12]); | |
// => 274.25940680717264 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment