Dear Wiki user, You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.
The "View_Snippets" page has been changed by SebastianCohnen. The comment on this change is: Inserted the standard deviation example from the couchdb test suite. http://wiki.apache.org/couchdb/View_Snippets?action=diff&rev1=27&rev2=28 -------------------------------------------------- * [[#list_unique_values|Generating a list of unique values]] * [[#top_n_tags|Retrieve the top N tags]] * [[#aggregate_sum|Joining an aggregate sum along with related data ]] + * [[#standard_deviation|Computing the standard deviation]] * [[#summary_stats|Computing simple summary statistics (min,max,mean,standard deviation) ]] * [[#interactive_couchdb|Interactive CouchDB Tutorial]] @@ -257, +258 @@ return output; } }}} + + + <<Anchor(standard_deviation)>> + == Computing the standard deviation == + This example is from the couchdb test-suite. It is '''much''' easier and less complex then following example ([[#summary_stats|Computing simple summary statistics (min,max,mean,standard deviation)]]) although it does not calculate min,max and mean (but this should be an easy exercise). + + {{{ + // Map + function (doc) { + emit(doc.val, doc.val) + }; + }}} + + {{{ + // Reduce + function (keys, values, rereduce) { + // This computes the standard deviation of the mapped results + var stdDeviation=0.0; + var count=0; + var total=0.0; + var sqrTotal=0.0; + + if (!rereduce) { + // This is the reduce phase, we are reducing over emitted values from + // the map functions. + for(var i in values) { + total = total + values[i]; + sqrTotal = sqrTotal + (values[i] * values[i]); + } + count = values.length; + } + else { + // This is the rereduce phase, we are re-reducing previosuly + // reduced values. + for(var i in values) { + count = count + values[i].count; + total = total + values[i].total; + sqrTotal = sqrTotal + values[i].sqrTotal; + } + } + + var variance = (sqrTotal - ((total * total)/count)) / count; + stdDeviation = Math.sqrt(variance); + + // the reduce result. It contains enough information to be rereduced + // with other reduce results. + return {"stdDeviation":stdDeviation,"count":count, + "total":total,"sqrTotal":sqrTotal}; + }; + }}} + <<Anchor(summary_stats)>> == Computing simple summary statistics (min,max,mean,standard deviation) ==
