Skip to content

Instantly share code, notes, and snippets.

@chrisveness
Created September 21, 2016 22:13
Show Gist options
  • Save chrisveness/5fa9c6b9aeb0a39eeef89bcd2d4ed5fb to your computer and use it in GitHub Desktop.
Save chrisveness/5fa9c6b9aeb0a39eeef89bcd2d4ed5fb to your computer and use it in GitHub Desktop.
Standard deviation of set of values
/**
* 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