Skip to content

Instantly share code, notes, and snippets.

@suhovius
Forked from RedBeard0531/functions.js
Created December 31, 2012 16:05
Show Gist options
  • Select an option

  • Save suhovius/4420919 to your computer and use it in GitHub Desktop.

Select an option

Save suhovius/4420919 to your computer and use it in GitHub Desktop.

Revisions

  1. @RedBeard0531 RedBeard0531 revised this gist Feb 22, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion functions.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,6 @@ function reduce(key, values) {
    a.min = Math.min(a.min, b.min);
    a.max = Math.max(a.max, b.max);
    }
    printjson(a);

    return a;
    }
  2. @invalid-email-address Anonymous created this gist Feb 22, 2012.
    40 changes: 40 additions & 0 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    // derived from http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm

    function map() {
    emit(1, // Or put a GROUP BY key here
    {sum: this.value, // the field you want stats for
    min: this.value,
    max: this.value,
    count:1,
    diff: 0, // M2,n: sum((val-mean)^2)
    });
    }

    function reduce(key, values) {
    var a = values[0]; // will reduce into here
    for (var i=1/*!*/; i < values.length; i++){
    var b = values[i]; // will merge 'b' into 'a'


    // temp helpers
    var delta = a.sum/a.count - b.sum/b.count; // a.mean - b.mean
    var weight = (a.count * b.count)/(a.count + b.count);

    // do the reducing
    a.diff += b.diff + delta*delta*weight;
    a.sum += b.sum;
    a.count += b.count;
    a.min = Math.min(a.min, b.min);
    a.max = Math.max(a.max, b.max);
    }
    printjson(a);

    return a;
    }

    function finalize(key, value){
    value.avg = value.sum / value.count;
    value.variance = value.diff / value.count;
    value.stddev = Math.sqrt(value.variance);
    return value;
    }
    22 changes: 22 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    > load('functions.js')
    > db.stuff.drop()
    false
    > db.stuff.insert({value:1})
    > db.stuff.insert({value:2})
    > db.stuff.insert({value:2})
    > db.stuff.insert({value:2})
    > db.stuff.insert({value:3})
    > db.stuff.mapReduce(map, reduce, {finalize:finalize, out:{inline:1}}).results[0]
    {
    "_id" : 1,
    "value" : {
    "sum" : 10,
    "min" : 1,
    "max" : 3,
    "count" : 5,
    "diff" : 2,
    "avg" : 2,
    "variance" : 0.4,
    "stddev" : 0.6324555320336759
    }
    }