Opened 11 years ago

Closed 11 years ago

#5583 closed enhancement (duplicate)

dojox.stats

Reported by: Sameul.Haque@… Owned by: Robert Coup
Priority: low Milestone:
Component: Dojox Version: 1.0
Keywords: statistics Cc:
Blocked By: Blocking:

Description

A basic stats module with population statistics and regression equations.


dojox.stats=new Object;


//Example: dojox.stats.minimum([-1,1,2,3,-2]]) : -2
dojox.stats.minimum=function (arr) {
var min=arr[0]; for (i=0;i<arr.length;i++) if (arr[i]<min) min=arr[i]; return min; 
}


//Example: dojox.stats.maximum([-1,1,2,3,-2]]) : 3
dojox.stats.maximum=function (arr) {
var max=arr[0]; for (i=0;i<arr.length;i++) if (arr[i]>max) min=arr[i]; return max;
}


//Example: dojox.stats.sum([-1,1,2,2,3,-2]]) : 3
dojox.stats.sum=function (arr) {
var sum = 0;
for (i=0;i<arr.length;i++) { sum+=arr[i]}
return sum;
}


//Example: dojox.stats.mean([-1,1,2,2,3,-2]]) : 0.8333333333333334
dojox.stats.mean=function (arr) {
return dojox.statssum(arr)/arr.length;
}

//Example: dojox.stats.mode([-1,1,2,2,3,-2]]) : 2
dojox.stats.mode=function (arr) {
var arrsort=arr.sort();
var count=1; var position=0; var frequencies=[];
for (i=0;i<arrsort.length;i++) { if (arrsort[i]==arrsort[i+1]) {count++} else {frequencies[position]=count; position++; count=1; } }
freq_max=dojox.stats.maximum(frequencies); var count=1; var position=0;
for (i=0;i<arrsort.length;i++) { if (arrsort[i]==arrsort[i+1]) {count++} else {if (count==freq_max) return arrsort[i];; count=1; } }
}


//Example: dojox.stats.quartile1([-1,1,2,2,3,-2]]) : -1
dojox.stats.quartile1=function (arr) {
return arr.sort()[Math.round((arr.length)/4)-1]
}

//Example: dojox.stats.median([-1,1,2,2,3,-2]]) : 1
dojox.stats.median=function (arr) {
return arr.sort()[Math.round((arr.length)/2)-1]
}

//Example: dojox.stats.quartile3([-1,1,2,2,3,-2]) : 2
dojox.stats.quartile3=function (arr) {
return arr.sort()[Math.round((arr.length)*3/4)-1]
}

//Example: dojox.stats.variance([-1,1,2,2,3,-2]) : 3.138888888888889
dojox.stats.variance=function (arr) {
var sq_dev=[]; var u=dojox.stats.mean(arr);
for (i=0;i<arr.length;i++) {sq_dev[i]=Math.pow(arr[i]-u,2);}
return dojox.stats.sum(sq_dev)/arr.length;
}

//Example: dojox.stats.meandev([-1,1,2,2,3,-2]) : 1.5555555555555553
dojox.stats.meandev=function (arr) {
var dev=[];u=dojox.stats.mean(arr);
for (i=0;i<arr.length;i++) {dev[i]=Math.abs(arr[i]-u);}
return dojox.stats.sum(dev)/arr.length;
}

//Example: dojox.stats.stdev([-1,1,2,2,3,-2]) : 1.9407902170679516
dojox.stats.stdev=function (arr) {
return Math.sqrt(dojox.stats.variance(arr));
}


//Example: dojox.stats.covariance([-1,1,2,2,3,-2],[1,2,3,4,5,6]) : 2.75
dojox.stats.covariance=function (arr1, arr2) { u = dojox.stats.mean(arr1); v = dojox.stats.mean(arr2); var sq_dev = []; for (i = 0; i < arr1.length; i++) { sq_dev[i] = (arr1[i] - u) * (arr2[i] - v); } return dojox.stats.sum(sq_dev) / arr1.length; }


//Example: dojox.stats.correlationcoefficient([-1,1,2,2,3,-2],[1,2,3,4,5,6]) : 0.9088690684389625
dojox.stats.corrcoeff=function (arr1, arr2) { return dojox.stats.covariance(arr1,arr2)/dojox.stats.stdev(arr1) / dojox.stats.stdev(arr2); }


//Example: dojox.stats.linear_reg_eq([-1,1,2,2,3,-2],[1,2,3,4,5,6]) : function anonymous(x) {return 0.9428571428571428*x+-2.4666666666666663}
dojox.stats.linear_reg_eq=function  (arrf, arrx) { var u = dojox.stats.mean(arrf); var v = dojox.stats.mean(arrx); var sq_dev = []; var devx = []; for (i = 0; i < arrf.length; i++) { sq_dev[i] = (arrf[i] - u) * (arrx[i] - v); devx[i] = Math.pow(arrx[i] - v, 2); } var linear_eq_coeff = dojox.stats.sum(sq_dev) / dojox.stats.sum(devx); var linear_eq_const = u - linear_eq_coeff * v; var linear_reg_eq = new Function("x", "return " + linear_eq_coeff + "*x+" + linear_eq_const); return linear_reg_eq; }

//Example: dojox.stats.exp_reg_eq([1,1,2,2,3,2],[1,2,3,4,5,6]) : function anonymous(x) {return Math.exp(0.19318779339440156*x)*0.8637415917898287}
dojox.stats.exp_reg_eq=function  (arrf, arrx) { 
var logarr=[]; for (i = 0; i < arrf.length; i++) { (logarr[i]=Math.log(arrf[i])); } var u = dojox.stats.mean(arrf); var v = dojox.stats.mean(arrx); var sq_dev = []; var devx = []; for (i = 0; i < arrf.length; i++) { sq_dev[i] = (logarr[i] - u) * (arrx[i] - v); devx[i] = Math.pow(arrx[i] - v, 2); } var exp_coeff = dojox.stats.sum(sq_dev) / dojox.stats.sum(devx); var exp_const = Math.exp(u - exp_coeff * v); var exp_reg_eq = new Function("x", "return Math.exp(" + exp_coeff + "*x)*" + exp_const); return exp_reg_eq; }


Attachments (1)

dojostatistics.js (4.2 KB) - added by guest 11 years ago.

Download all attachments as: .zip

Change History (5)

Changed 11 years ago by guest

Attachment: dojostatistics.js added

comment:1 Changed 11 years ago by Adam Peller

Milestone: 1.0.3

I'd suggest a discussion with the community on this first, perhaps in the forums, to solicit feedback on APIs and such. Also, please don't post code without CLA info.

comment:2 Changed 11 years ago by Adam Peller

oh, is this rcoup?

comment:3 Changed 11 years ago by Robert Coup

Cc: Robert Coup removed
Owner: changed from Adam Peller to Robert Coup
Priority: normallow
Reporter: changed from guest to Sameul.Haque@…
Status: newassigned

comment:4 Changed 11 years ago by Robert Coup

Resolution: duplicate
Status: assignedclosed

See #5584

Note: See TracTickets for help on using tickets.