Created
September 21, 2016 22:13
-
-
Save chrisveness/5fa9c6b9aeb0a39eeef89bcd2d4ed5fb to your computer and use it in GitHub Desktop.
Standard deviation of set of values
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
/** | |
* Returns standard deviation of set of values. | |
* | |
* @param {number[]} values - Array of values. | |
* @returns {number} Standard devation of values. | |
*/ | |
function stdDeviation(values) { | |
const avgOfValues = average(values); | |
const squaresOfDiffs = values.map(value => (value-avgOfValues)**2); | |
const avgOfSquaresOfDiffs = average(squaresOfDiffs); | |
return Math.sqrt(avgOfSquaresOfDiffs); | |
function average(values) { | |
return values.reduce((sum, value) => sum + value, 0) / values.length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment