Created
June 12, 2018 14:01
-
-
Save jimkang/ef944fcc570bb05007764e73f3435bb8 to your computer and use it in GitHub Desktop.
Simple stddev example
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
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