forked from simple-statistics/simple-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_covariance.test.js
48 lines (41 loc) · 1.23 KB
/
sample_covariance.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
/* eslint no-shadow: 0 */
const test = require("tap").test;
const ss = require("../dist/simple-statistics.js");
function rnd(x) {
return Math.round(x * 1000) / 1000;
}
test("sample covariance", function (t) {
t.test("can get perfect negative covariance", function (t) {
const x = [1, 2, 3, 4, 5, 6];
const y = [6, 5, 4, 3, 2, 1];
t.equal(rnd(ss.sampleCovariance(x, y)), -3.5);
t.end();
});
t.test("covariance of something with itself is its variance", function (t) {
const x = [1, 2, 3, 4, 5, 6];
t.equal(rnd(ss.sampleCovariance(x, x)), 3.5);
t.end();
});
t.test(
"covariance is zero for something with no correlation",
function (t) {
const x = [1, 2, 3, 4, 5, 6];
const y = [1, 1, 2, 2, 1, 1];
t.equal(rnd(ss.sampleCovariance(x, y)), 0);
t.end();
}
);
t.test("unequal-length corner case", function (t) {
t.throws(function () {
ss.sampleCovariance([1], []);
});
t.end();
});
t.test("zero-length corner case", function (t) {
t.throws(function () {
ss.sampleCovariance([], []);
});
t.end();
});
t.end();
});