Skip to content

Instantly share code, notes, and snippets.

@ailtonbsj
Created February 7, 2018 17:06
Show Gist options
  • Save ailtonbsj/96e6fad841366766e520dd18249d3ea9 to your computer and use it in GitHub Desktop.
Save ailtonbsj/96e6fad841366766e520dd18249d3ea9 to your computer and use it in GitHub Desktop.
Standard Deviation in JS
function stdDeviation(input, isAmostra){
let amostral = 0;
if(isAmostra != null) amostral = 1;
let sum = input.reduce((acc,val) => acc+val);
let mean = sum/input.length;
let diff2 = input.map(val => Math.pow(val - mean,2));
let sumDiff = diff2.reduce((acc,val) => acc+val);
let desv = Math.sqrt(sumDiff/(input.length-amostral));
console.log(mean);
console.log(desv);
}
stdDeviation([14,15,9,1],true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment