forked from simple-statistics/simple-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcumulative.test.js
92 lines (89 loc) · 2.79 KB
/
cumulative.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
/* eslint no-shadow: 0 */
const test = require("tap").test;
const ss = require("../dist/simple-statistics.js");
test("cumulativeStdNormalProbability", function (t) {
// https://en.wikipedia.org/wiki/Standard_normal_table#Examples_of_use
t.test("wikipedia test example works", function (t) {
t.equal(ss.cumulativeStdNormalProbability(0.4), 0.6554);
t.end();
});
t.test("nondecreasing", function (t) {
for (let i = 0; i < ss.standardNormalTable.length; i++) {
if (
!ss.cumulativeStdNormalProbability(i / 100) >=
ss.cumulativeStdNormalProbability((i - 1) / 100)
) {
t.fail("non-decreasing failure on " + i);
}
}
t.end();
});
t.test("matches errorFunction", function (t) {
for (let i = 0; i < ss.standardNormalTable.length; i++) {
if (
!(
Math.abs(
ss.cumulativeStdNormalProbability(i / 100) -
(0.5 +
0.5 * ss.errorFunction(i / 100 / Math.sqrt(2)))
) < ss.epsilon
)
) {
t.fail("error-fn failure on " + i);
}
}
t.end();
});
t.test("symmetry", function (t) {
t.equal(
Math.abs(
ss.cumulativeStdNormalProbability(-1) -
(1 - ss.cumulativeStdNormalProbability(1))
) < ss.epsilon,
true
);
t.end();
});
t.test("inverse", function (t) {
for (let i = 0; i <= 1 + ss.epsilon; i += 0.01) {
t.equal(
Math.abs(ss.cumulativeStdNormalProbability(ss.probit(i)) - i) <
21 * ss.epsilon,
true
);
}
t.end();
});
t.end();
});
test("cumulativeStdLogisticProbability", function (t) {
t.test("median is zero", function (t) {
t.equal(ss.cumulativeStdLogisticProbability(0), 0.5);
t.end();
});
t.test("increasing", function (t) {
for (let i = -3; i <= 3; i += 0.01) {
if (
ss.cumulativeStdLogisticProbability(i + ss.epsilon) <=
ss.cumulativeStdLogisticProbability(i)
) {
t.fail("increasing failure at " + i);
}
}
t.end();
});
t.test("symmetry", function (t) {
for (let i = 0; i <= 3; i += 0.01) {
if (
Math.abs(
ss.cumulativeStdLogisticProbability(i) -
(1 - ss.cumulativeStdLogisticProbability(-i))
) > ss.epsilon
) {
t.fail("not symmetric about zero");
}
}
t.end();
});
t.end();
});