1 | dojo.provide("dojox.math.tests.stats"); |
---|
2 | dojo.require("dojox.math.stats"); |
---|
3 | |
---|
4 | (function(){ |
---|
5 | var epsilon = 1e-6; |
---|
6 | eq = function(t, a, b){ |
---|
7 | t.t(!isNaN(a) && ! isNaN(b)); |
---|
8 | var delta = Math.abs((a - b) / (a + b)); |
---|
9 | t.t(isNaN(delta) || delta < epsilon); |
---|
10 | }, |
---|
11 | a1 = [1, 2, 1], |
---|
12 | a2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; |
---|
13 | a3 = [0, 5, 4, 5, 8, 20, 21, 20, 20, 8, 9, 15, 12, 11, 18, 7, 19, 13, 13, 4]; |
---|
14 | tests.register("dojox.math.stats.tests", [ |
---|
15 | function sd(t){ |
---|
16 | t.assertEqual(6.335613624582861, dojox.math.stats.sd(a3)); |
---|
17 | }, |
---|
18 | function variance(t){ |
---|
19 | t.assertEqual(40.139999999999986, dojox.math.stats.variance(a3)); |
---|
20 | }, |
---|
21 | function mean(t){ |
---|
22 | t.assertEqual(11.6, dojox.math.stats.mean(a3)); |
---|
23 | }, |
---|
24 | function min(t){ |
---|
25 | t.assertEqual(0, dojox.math.stats.min(a3)); |
---|
26 | }, |
---|
27 | function max(t){ |
---|
28 | t.assertEqual(21, dojox.math.stats.max(a3)); |
---|
29 | }, |
---|
30 | function median(t){ |
---|
31 | t.assertEqual(12, dojox.math.stats.median(a3)); |
---|
32 | }, |
---|
33 | function mode(t){ |
---|
34 | t.assertEqual(20, dojox.math.stats.mode(a3)); |
---|
35 | }, |
---|
36 | function sum(t){ |
---|
37 | t.assertEqual(232, dojox.math.stats.sum(a3)); |
---|
38 | } |
---|
39 | ]); |
---|
40 | |
---|
41 | var points = [ |
---|
42 | {x:1, y:42}, {x:1, y:7}, {x:2, y:17}, {x:4, y:41}, |
---|
43 | {x:5, y:60}, {x:7, y:19}, {x:7, y:16}, {x:8, y:15}, |
---|
44 | {x:10, y:29}, {x:11, y:1}, {x:12, y:10}, {x:13, y:22}, |
---|
45 | {x:13, y:16}, {x:14, y:29}, {x:20, y:37}, {x:21, y:10}, |
---|
46 | {x:21, y:60}, {x:22, y:4}, {x:22, y:33}, {x:25, y:52}, |
---|
47 | {x:25, y:32}, {x:25, y:18}, {x:27, y:46}, {x:28, y:2}, |
---|
48 | {x:28, y:56}, {x:29, y:12}, {x:32, y:53}, {x:32, y:14}, |
---|
49 | {x:36, y:18}, {x:37, y:23}, {x:38, y:18}, {x:45, y:37}, |
---|
50 | {x:48, y:43}, {x:50, y:9}, {x:53, y:48}, {x:55, y:60}, |
---|
51 | {x:55, y:28}, {x:57, y:19}, {x:58, y:48}, {x:58, y:29} |
---|
52 | ]; |
---|
53 | tests.register("dojox.math.stats.tests.bestFit", [ |
---|
54 | function bf(t){ |
---|
55 | var result = dojox.math.stats.bestFit(points); |
---|
56 | console.log(result); |
---|
57 | t.assertEqual(0.208, Math.round(result.slope*1000)/1000); |
---|
58 | t.assertEqual(22.829, Math.round(result.intercept*1000)/1000); |
---|
59 | t.assertEqual(0.045, Math.round(result.r2*1000)/1000); |
---|
60 | t.assertEqual(0.212, Math.round(result.r*1000)/1000); |
---|
61 | } |
---|
62 | ]); |
---|
63 | tests.register("dojox.math.stats.tests.forecast", [ |
---|
64 | function _42(t){ |
---|
65 | t.assertEqual(31.580951899655346, dojox.math.stats.forecast(points, 42)); |
---|
66 | }, |
---|
67 | function _54(t){ |
---|
68 | t.assertEqual(34.08152295859065, dojox.math.stats.forecast(points, 54)); |
---|
69 | }, |
---|
70 | function _201(t){ |
---|
71 | t.assertEqual(64.71351843054812, dojox.math.stats.forecast(points, 201)); |
---|
72 | } |
---|
73 | ]); |
---|
74 | tests.register("dojox.math.stats.tests.approx", [ |
---|
75 | function approx1(t){ eq(t, dojox.math.stats.approxLin(a1, 0), 1); }, |
---|
76 | function approx2(t){ eq(t, dojox.math.stats.approxLin(a1, 0.5), 2); }, |
---|
77 | function approx3(t){ eq(t, dojox.math.stats.approxLin(a1, 1), 1); }, |
---|
78 | function approx4(t){ eq(t, dojox.math.stats.approxLin(a1, 0.25), 1.5); }, |
---|
79 | function approx5(t){ eq(t, dojox.math.stats.approxLin(a1, 0.75), 1.5); }, |
---|
80 | function approx6(t){ eq(t, dojox.math.stats.approxLin(a1, 0.1), 1.2); }, |
---|
81 | function summary1(t){ |
---|
82 | var s = dojox.math.stats.summary(a1); |
---|
83 | eq(t, s.min, 1); |
---|
84 | eq(t, s.p10, 1); |
---|
85 | eq(t, s.p25, 1); |
---|
86 | eq(t, s.med, 1); |
---|
87 | eq(t, s.p75, 1.5); |
---|
88 | eq(t, s.p90, 1.8); |
---|
89 | eq(t, s.max, 2); |
---|
90 | }, |
---|
91 | function summary2(t){ |
---|
92 | var s = dojox.math.stats.summary(a2, true); |
---|
93 | eq(t, s.min, 0); |
---|
94 | eq(t, s.p10, 2); |
---|
95 | eq(t, s.p25, 5); |
---|
96 | eq(t, s.med, 10); |
---|
97 | eq(t, s.p75, 15); |
---|
98 | eq(t, s.p90, 18); |
---|
99 | eq(t, s.max, 20); |
---|
100 | } |
---|
101 | ]); |
---|
102 | })(); |
---|