Skip to content

Instantly share code, notes, and snippets.

@PatheticMustan
Created September 7, 2023 15:07
Show Gist options
  • Save PatheticMustan/ec65235c34df1808791b8731fadcc890 to your computer and use it in GitHub Desktop.
Save PatheticMustan/ec65235c34df1808791b8731fadcc890 to your computer and use it in GitHub Desktop.
calculate mean, std dev, and std err
function magic(a) {
let N = a.length;
let mean = a.reduce((a,b)=>a+b,0) / N;
let sd = 0;
for (let i=0; i<a.length; i++) {
sd += ((a[i] - mean) ** 2);
}
sd /= N - 1;
sd = Math.sqrt(sd);
let sde = sd / Math.sqrt(N);
console.log(`mean: ${mean.toFixed(8)}`);
console.log(`standard deviation: ${sd.toFixed(8)}`);
console.log(`standard error: ${sde.toFixed(8)}`);
}
magic([
1.69, 1.67, 1.69, 1.70, 1.74, 1.70, 1.74, 1.65, 1.79, 1.74
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment