-
-
Save mxriverlynn/b7c6615ecf1fe9ad60a8 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
var sum = values.reduce(function(sum, value){ | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; |
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
var diffs = values.map(function(value){ | |
var diff = value - avg; | |
return diff; | |
}); |
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
var squareDiffs = values.map(function(value){ | |
var diff = value - avg; | |
var sqr = diff * diff; | |
return sqr; | |
}); |
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 average(data){ | |
var sum = data.reduce(function(sum, value){ | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; | |
return avg; | |
} |
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
var avgSquareDiff = average(squareDiffs); |
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
var stdDev = Math.sqrt(avgSquareDiff); |
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; | |
var 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; | |
} |
webdev23
commented
Nov 3, 2018
•
This doesn't compute standard deviation, it computes the uncorrected standard deviation, because it uses 1/N
. The denominator for stdev should be N-1
, not N
. The squared differences should not be averaged.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment