// AMD-ID "dojox/math/stats" define("dojox/math/stats", ["dojo", "../main"], function(dojo, dojox) { dojo.getObject("math.stats", true, dojox); var st = dojox.math.stats; dojo.mixin(st, { sd: function(/* Number[] */a){ // summary: // Returns the standard deviation of the passed arguments. return Math.sqrt(st.variance(a)); // Number }, variance: function(/* Number[] */a){ // summary: // Find the variance in the passed array of numbers. var mean=0, squares=0; dojo.forEach(a, function(item){ mean+=item; squares+=Math.pow(item,2); }); return (squares/a.length)-Math.pow(mean/a.length, 2); // Number }, bestFit: function(/* Object[]|Number[] */ a, /* String? */ xProp, /* String? */ yProp){ // summary: // Calculate the slope and intercept in a linear fashion. An array // of objects is expected; optionally you can pass in the property // names for "x" and "y", else x/y is used as the default. If you // pass an array of numbers, it will be mapped to a set of {x,y} objects // where x = the array index. xProp = xProp || "x", yProp = yProp || "y"; if(a[0] !== undefined && typeof(a[0]) == "number"){ // this is an array of numbers, so use the index as x. a = dojo.map(a, function(item, idx){ return { x: idx, y: item }; }); } var sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0, stt = 0, sts = 0, n = a.length, t; for(var i=0; i= a.length){ return a[a.length - 1]; } return a[f] * (t - p) + a[t] * (p - f); // Number }, summary: function(a, alreadySorted){ // summary: // Returns a non-parametric collection of summary statistics: // the classic five-number summary extended to the Bowley's // seven-figure summary. // a: Number[] // a numeric array to be appraised. // alreadySorted: Boolean? // a Boolean flag to indicated that the array is already sorted. // This is an optional flag purely to improve the performance. // If skipped, the array will be assumed unsorted. // returns: Object if(!alreadySorted){ a = a.slice(0); // copy the array a.sort(function(a, b){ return a - b; }); // sort it properly } var l = st.approxLin, result = { // the five-number summary min: a[0], // minimum p25: l(a, 0.25), // lower quartile med: l(a, 0.5), // median p75: l(a, 0.75), // upper quartile max: a[a.length - 1], // maximum // extended to the Bowley's seven-figure summary p10: l(a, 0.1), // first decile p90: l(a, 0.9) // last decile }; return result; // Object } }); return dojox.math.stats; });