Skip to content

Instantly share code, notes, and snippets.

@derickson derickson/stddev.js
Created Mar 11, 2014

Embed
What would you like to do?
standard deviation MongoDB aggregation pipline using method from http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
// standard deviation MongoDB aggregation pipline
// using method from http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
var parts = db.stat.aggregate( { $group: { _id: null,
s0: {$sum:1},
s1:{$sum:"$stat"},
s2:{$sum:{$multiply:["$stat","$stat"]}} }} ).result[0];
var stddev = Math.sqrt( parts.s0 * parts.s2 - Math.pow( parts.s1, 2) ) / parts.s0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.