forked from simple-statistics/simple-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantile.test.js
89 lines (77 loc) · 2.71 KB
/
quantile.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
/* eslint no-shadow: 0 */
const test = require("tap").test;
const ss = require("../dist/simple-statistics.js");
test("quantile", function (t) {
// Data and results from
// [Wikipedia](http://en.wikipedia.org/wiki/Quantile#Quantiles_of_a_population)
t.test("can get proper quantiles of an even-length list", function (t) {
const even = [3, 6, 7, 8, 8, 10, 13, 15, 16, 20];
t.equal(ss.quantile(even, 0.25), 7);
t.equal(ss.quantile(even, 0.5), 9);
t.equal(ss.quantile(even, 0.75), 15);
t.end();
});
t.test("can get proper quantiles of an odd-length list", function (t) {
const odd = [3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20];
t.equal(ss.quantile(odd, 0.25), 7);
t.equal(ss.quantile(odd, 0.5), 9);
t.equal(ss.quantile(odd, 0.75), 15);
t.end();
});
t.test("the median quantile is equal to the median", function (t) {
const rand = [1, 4, 5, 8];
t.equal(ss.quantile(rand, 0.5), ss.median(rand));
const rand2 = [10, 50, 2, 4, 4, 5, 8];
t.equal(ss.quantile(rand2, 0.5), ss.median(rand2));
t.end();
});
t.throws(function () {
ss.quantile([], 0.5);
}, "a zero-length list throws an error");
t.test("test odd-value case", function (t) {
t.equal(ss.quantile([0, 1, 2, 3, 4], 0.2), 1);
t.end();
});
t.test("bad bounds throw an error", function (t) {
t.throws(function () {
ss.quantile([1, 2, 3], 1.1);
});
t.throws(function () {
ss.quantile([1, 2, 3], -0.5);
});
t.end();
});
t.test("max quantile is equal to the max", function (t) {
t.equal(ss.quantile([1, 2, 3], 1), ss.max([1, 2, 3]));
t.end();
});
t.test("min quantile is equal to the min", function (t) {
t.equal(ss.quantile([1, 2, 3], 0), ss.min([1, 2, 3]));
t.end();
});
t.test(
"if quantile arg is an array, response is an array of quantiles",
function (t) {
const odd = [3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20];
t.same(
ss.quantile(odd, [0, 0.25, 0.5, 0.75, 1]),
[3, 7, 9, 15, 20]
);
t.same(ss.quantile(odd, [0.75, 0.5]), [15, 9]);
t.end();
}
);
t.test(
"can get an array of quantiles on a small number of elements",
function (t) {
const input = [500, 468, 454, 469];
t.same(ss.quantile(input, [0.25, 0.5, 0.75]), [461, 468.5, 484.5]);
t.same(
ss.quantile(input, [0.05, 0.25, 0.5, 0.75, 0.95]),
[454, 461, 468.5, 484.5, 500]
);
t.end();
}
);
t.end();
});