Skip to content

Instantly share code, notes, and snippets.

@jimkang
Created June 12, 2018 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimkang/ef944fcc570bb05007764e73f3435bb8 to your computer and use it in GitHub Desktop.
Save jimkang/ef944fcc570bb05007764e73f3435bb8 to your computer and use it in GitHub Desktop.
Simple stddev example
var curry = require('lodash.curry');
var counts = [9, 4, 4, 5, 49];
var avgCount = mean(counts);
var deviations = counts.map(curry(bMinusA)(avgCount));
var squaredDevs = deviations.map(square);
var variance = mean(squaredDevs);
var populationStdDev = Math.sqrt(variance);
console.log('avgCount', avgCount);
console.log('populationStdDev', populationStdDev);
function sum(count, total) {
return total + count;
}
function bMinusA(a, b) {
return b - a;
}
function square(x) {
return x * x;
}
function mean(values) {
return values.reduce(sum, 0)/values.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment