-
-
Save clara-shin/6ad5a3c38970488452422e3a69f9edb7 to your computer and use it in GitHub Desktop.
Standard deviation, in JavaScript
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
var sum = values.reduce(function(sum, value){ | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; |
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
var diffs = values.map(function(value){ | |
var diff = value - avg; | |
return diff; | |
}); |
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
var squareDiffs = values.map(function(value){ | |
var diff = value - avg; | |
var sqr = diff * diff; | |
return sqr; | |
}); |
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
function average(data){ | |
var sum = data.reduce(function(sum, value){ | |
return sum + value; | |
}, 0); | |
var avg = sum / data.length; | |
return avg; | |
} |
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
var avgSquareDiff = average(squareDiffs); |
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
var stdDev = Math.sqrt(avgSquareDiff); |
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment