-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
113 lines (90 loc) · 2.9 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*jslint vars: true */
'use strict';
var regression = require('./'),
test = require('tap').test;
/**
* We're going to lose some precision when sampling lots of data, so just
* test if the value is close enough!
* @param {Test t} Tap test
* @param {Number a}
* @param {Number b}
* @param {String str} String to assert
*/
function closeEnoughDammit(t, a, b, str) {
if (typeof a !== 'number') {
t.type(a, 'number', 'is number ' + (str || ''));
} else {
t.equal(Math.round(a * 100000), Math.round(b * 100000), str);
}
}
function isEmpty(t, sample) {
t.equal(sample.count(), 0, 'enough');
t.equal(sample.enough(), false, 'enough');
t.equal(sample.coef(), false, 'coef');
t.equal(sample.coef('active'), false, 'active coef');
t.equal(sample.coef('idle'), false, 'idle coef');
t.equal(sample.calc({active: 1337, idle: 999}), false);
}
test('single coef', function (t) {
var i, sample = regression();
var a = 20.0001, b = 2, c = 0.5;
isEmpty(t, sample);
t.equal(sample.update(a + 5 * b + 10 * c, {
active: 5,
idle: 10
}), sample);
for (i = 0; i < 999; i += 1) {
var active = i * 2 + 50,
idle = i * 3 + 51;
sample.update(a + active * b + idle * c, {
active: active,
idle: idle
});
}
closeEnoughDammit(t, sample.coef(), a, 'coef');
closeEnoughDammit(t, sample.coef('active'), b, 'active coef');
closeEnoughDammit(t, sample.coef('idle'), c, 'idle coef');
closeEnoughDammit(
t,
sample.calc({active: 1337, idle: 999}),
a + 1337 * b + 999 * c
);
t.equal(sample.count(), 1000, 'sample count');
t.equal(sample.enough(), true, 'enough data');
t.equal(sample.empty(), sample);
isEmpty(t, sample);
t.end();
});
test('double coef', function (t) {
var i, sample = regression();
var a1 = 20.0001, b1 = 2, c1 = 0.5,
a2 = 22.5, b2 = 1.5, c2 = 8.8,
a = (a1 + a2) / 2, b = (b1 + b2) / 2, c = (c1 + c2) / 2;
isEmpty(t, sample);
for (i = 0; i < 10; i += 1) {
var active = i * 2 + 50,
idle = i * 3 + 51;
sample.update(a1 + active * b1 + idle * c1, {
active: active,
idle: idle
});
sample.update(a2 + active * b2 + idle * c2, {
active: active,
idle: idle
});
}
console.error('COEF', sample.coef());
closeEnoughDammit(t, sample.coef(), a1, 'coef');
closeEnoughDammit(t, sample.coef('active'), b1, 'active coef');
closeEnoughDammit(t, sample.coef('idle'), c1, 'idle coef');
// closeEnoughDammit(
// t,
// sample.calc({active: 1337, idle: 999}),
// a + 1337 * b + 999 * c
// );
// t.equal(sample.count(), 1000, 'sample count');
// t.equal(sample.enough(), true, 'enough data');
t.equal(sample.empty(), sample);
isEmpty(t, sample);
t.end();
});