Created
September 22, 2014 20:21
-
-
Save roman01la/7a5982016a8b7ecc9a47 to your computer and use it in GitHub Desktop.
Standard Deviation
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 standardDeviation (values) { | |
var avg = average(values); | |
var squareDiffs = values.map(function (value) { | |
var diff = value - avg, | |
sqrDiff = diff * diff; | |
return sqrDiff; | |
}); | |
var avgSquareDiff = average(squareDiffs); | |
var stdDev = Math.sqrt(avgSquareDiff); | |
return stdDev; | |
} | |
function average (data) { | |
var sum = data.reduce(function (sum, value) { | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; | |
return avg; | |
} | |
var stdDev = standardDeviation([1, 2, 3, 4, 5, 6, 7, 8, 9, 25]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment