stats.js
1.75 KB
//>>built
define(["dojo","../main"],function(g,n){g.getObject("math.stats",!0,n);var h=n.math.stats;g.mixin(h,{sd:function(a){return Math.sqrt(h.variance(a))},variance:function(a){var b=0,c=0;g.forEach(a,function(a){b+=a;c+=Math.pow(a,2)});return c/a.length-Math.pow(b/a.length,2)},bestFit:function(a,b,c){b=b||"x";c=c||"y";void 0!==a[0]&&"number"==typeof a[0]&&(a=g.map(a,function(a,b){return{x:b,y:a}}));for(var d=0,f=0,k=0,h=0,l=0,n=0,q=0,m=a.length,p,e=0;e<m;e++)d+=a[e][b],f+=a[e][c],k+=Math.pow(a[e][b],2),
h+=Math.pow(a[e][c],2),l+=a[e][b]*a[e][c];for(e=0;e<m;e++)p=a[e][b]-d/m,n+=p*p,q+=p*a[e][c];a=q/(n||1);k=Math.sqrt((k-Math.pow(d,2)/m)*(h-Math.pow(f,2)/m));if(0===k)throw Error("dojox.math.stats.bestFit: the denominator for Pearson's R is 0.");l=(l-d*f/m)/k;k=Math.pow(l,2);0>a&&(l=-l);return{slope:a,intercept:(f-d*a)/(m||1),r:l,r2:k}},forecast:function(a,b,c,d){a=h.bestFit(a,c,d);return a.slope*b+a.intercept},mean:function(a){var b=0;g.forEach(a,function(a){b+=a});return b/Math.max(a.length,1)},min:function(a){return Math.min.apply(null,
a)},max:function(a){return Math.max.apply(null,a)},median:function(a){var b=a.slice(0).sort(function(a,b){return a-b});return(b[Math.floor(a.length/2)]+b[Math.ceil(a.length/2)])/2},mode:function(a){var b={},c=0,d=Number.MIN_VALUE;g.forEach(a,function(a){void 0!==b[a]?b[a]++:b[a]=1});for(var f in b)d<b[f]&&(d=b[f],c=f);return c},sum:function(a){var b=0;g.forEach(a,function(a){b+=a});return b},approxLin:function(a,b){b*=a.length-1;var c=Math.ceil(b),d=c-1;return 0>d?a[0]:c>=a.length?a[a.length-1]:a[d]*
(c-b)+a[c]*(b-d)},summary:function(a,b){b||(a=a.slice(0),a.sort(function(a,b){return a-b}));b=h.approxLin;return{min:a[0],p25:b(a,.25),med:b(a,.5),p75:b(a,.75),max:a[a.length-1],p10:b(a,.1),p90:b(a,.9)}}});return n.math.stats});