root / trunk / web / dojo / dojox / math / stats.js @ 11
History | View | Annotate | Download (2.3 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.math.stats"]){ |
||
9 | dojo._hasResource["dojox.math.stats"]=true; |
||
10 | dojo.provide("dojox.math.stats");
|
||
11 | (function(){
|
||
12 | var st=dojox.math.stats;
|
||
13 | dojo.mixin(st,{sd:function(a){ |
||
14 | return Math.sqrt(st.variance(a));
|
||
15 | },variance:function(a){ |
||
16 | var _1=0,_2=0; |
||
17 | dojo.forEach(a,function(_3){
|
||
18 | _1+=_3; |
||
19 | _2+=Math.pow(_3,2);
|
||
20 | }); |
||
21 | return (_2/a.length)-Math.pow(_1/a.length,2); |
||
22 | },bestFit:function(a,_4,_5){ |
||
23 | _4=_4||"x",_5=_5||"y"; |
||
24 | if(a[0]!==undefined&&typeof (a[0])=="number"){ |
||
25 | a=dojo.map(a,function(_6,_7){
|
||
26 | return {x:_7,y:_6}; |
||
27 | }); |
||
28 | } |
||
29 | var sx=0,sy=0,_8=0,_9=0,_a=0,_b=0,_c=0,n=a.length,t; |
||
30 | for(var i=0;i<n;i++){ |
||
31 | sx+=a[i][_4]; |
||
32 | sy+=a[i][_5]; |
||
33 | _8+=Math.pow(a[i][_4],2);
|
||
34 | _9+=Math.pow(a[i][_5],2);
|
||
35 | _a+=a[i][_4]*a[i][_5]; |
||
36 | } |
||
37 | for(i=0;i<n;i++){ |
||
38 | t=a[i][_4]-sx/n; |
||
39 | _b+=t*t; |
||
40 | _c+=t*a[i][_5]; |
||
41 | } |
||
42 | var _d=_c/(_b||1); |
||
43 | var d=Math.sqrt((_8-Math.pow(sx,2)/n)*(_9-Math.pow(sy,2)/n)); |
||
44 | if(d===0){ |
||
45 | throw new Error("dojox.math.stats.bestFit: the denominator for Pearson's R is 0."); |
||
46 | } |
||
47 | var r=(_a-(sx*sy/n))/d;
|
||
48 | var r2=Math.pow(r,2); |
||
49 | if(_d<0){ |
||
50 | r=-r; |
||
51 | } |
||
52 | return {slope:_d,intercept:(sy-sx*_d)/(n||1),r:r,r2:r2}; |
||
53 | },forecast:function(a,x,_e,_f){ |
||
54 | var fit=st.bestFit(a,_e,_f);
|
||
55 | return (fit.slope*x)+fit.intercept;
|
||
56 | },mean:function(a){ |
||
57 | var t=0; |
||
58 | dojo.forEach(a,function(v){
|
||
59 | t+=v; |
||
60 | }); |
||
61 | return t/Math.max(a.length,1); |
||
62 | },min:function(a){ |
||
63 | return Math.min.apply(null,a); |
||
64 | },max:function(a){ |
||
65 | return Math.max.apply(null,a); |
||
66 | },median:function(a){ |
||
67 | var t=a.slice(0).sort(function(a,b){ |
||
68 | return a-b;
|
||
69 | }); |
||
70 | return (t[Math.floor(a.length/2)]+t[Math.ceil(a.length/2)])/2; |
||
71 | },mode:function(a){ |
||
72 | var o={},r=0,m=Number.MIN_VALUE; |
||
73 | dojo.forEach(a,function(v){
|
||
74 | (o[v]!==undefined)?o[v]++:o[v]=1; |
||
75 | }); |
||
76 | for(var p in o){ |
||
77 | if(m<o[p]){
|
||
78 | m=o[p],r=p; |
||
79 | } |
||
80 | } |
||
81 | return r;
|
||
82 | },sum:function(a){ |
||
83 | var sum=0; |
||
84 | dojo.forEach(a,function(n){
|
||
85 | sum+=n; |
||
86 | }); |
||
87 | return sum;
|
||
88 | },approxLin:function(a,pos){ |
||
89 | var p=pos*(a.length-1),t=Math.ceil(p),f=t-1; |
||
90 | if(f<0){ |
||
91 | return a[0]; |
||
92 | } |
||
93 | if(t>=a.length){
|
||
94 | return a[a.length-1]; |
||
95 | } |
||
96 | return a[f]*(t-p)+a[t]*(p-f);
|
||
97 | },summary:function(a,_10){ |
||
98 | if(!_10){
|
||
99 | a=a.slice(0);
|
||
100 | a.sort(function(a,b){
|
||
101 | return a-b;
|
||
102 | }); |
||
103 | } |
||
104 | var l=st.approxLin,_11={min:a[0],p25:l(a,0.25),med:l(a,0.5),p75:l(a,0.75),max:a[a.length-1],p10:l(a,0.1),p90:l(a,0.9)}; |
||
105 | return _11;
|
||
106 | }}); |
||
107 | })(); |
||
108 | } |