@datagrok-libraries/statistics 0.1.3 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tests.ts DELETED
@@ -1,101 +0,0 @@
1
- //@ts-ignore: no types
2
- import * as jStat from 'jstat';
3
-
4
- type testStats = {
5
- 'p-value': number,
6
- 'Mean difference'?: number,
7
- 'Median difference'?: number,
8
- 'p-value more': number,
9
- 'p-value less': number,
10
- };
11
-
12
- export function tTest(arr1: number[], arr2: number[], devKnown=false, devEqual=false): testStats {
13
- const m1: number = jStat.mean(arr1);
14
- const m2: number = jStat.mean(arr2);
15
- const v1: number = jStat.variance(arr1);
16
- const v2: number = jStat.variance(arr2);
17
- const n1 = arr1.length;
18
- const n2 = arr2.length;
19
-
20
- let wv1;
21
- let wv2;
22
- let wv;
23
- let Z;
24
- let K;
25
- let pMore;
26
- let pLess;
27
- let pTot;
28
-
29
- if (!devKnown) {
30
- if (!devEqual) {
31
- wv1 = v1 / n1;
32
- wv2 = v2 / n2;
33
- Z = (m1 - m2) / Math.sqrt(wv1 + wv2);
34
- K = Math.pow((wv1 + wv2), 2) / (wv1 * wv1 / (n1 - 1) + wv2 * wv2 / (n2 - 1));
35
-
36
- pLess = jStat.studentt.cdf(Z, K);
37
- pMore = 1 - pLess;
38
- pTot = 2 * (pLess < pMore ? pLess : pMore);
39
- } else {
40
- K = n1 + n2 - 2;
41
- wv = (v1 * (n1 - 1) + v2 * (n2 - 1)) / K;
42
- Z = Math.sqrt(n1 * n2 / (n1 + n2)) * (m1 - m2) / wv;
43
-
44
- pMore = 1 - jStat.studentt.cdf(Z, K);
45
- pLess = jStat.studentt.cdf(Z, K);
46
- pTot = 2 * (pLess < pMore ? pLess : pMore);
47
- }
48
- } else {
49
- wv1 = v1 / n1;
50
- wv2 = v2 / n2;
51
- Z = (m1 - m2) / Math.sqrt(wv1 + wv2);
52
-
53
- pLess = jStat.normal.pdf(Z, 0, 1);
54
- pMore = 1 - pLess;
55
- pTot = 2 * (pLess < pMore ? pLess : pMore);
56
- }
57
- return {'p-value': pTot, 'Mean difference': m1 - m2, 'p-value more': pMore, 'p-value less': pLess};
58
- }
59
-
60
- export function uTest(x: number[], y: number[], continuity=true): testStats {
61
- const xy = x.concat(y);
62
- const n1 = x.length;
63
- const n2 = y.length;
64
- const med1 = jStat.median(x);
65
- const med2 = jStat.median(y);
66
-
67
- const ranks = jStat.rank(xy);
68
-
69
- const R1 = jStat.sum(ranks.slice(0, n1));
70
- const U1 = R1 - n1 * (n1 + 1) / 2;
71
- const U2 = n1 * n2 - U1;
72
- const U = U1 > U2 ? U1 : U2;
73
-
74
- const mu = n1 * n2 / 2;
75
- const n = n1 + n2;
76
-
77
- const tieTerm = _tieTerm(ranks);
78
- const s = Math.sqrt(n1 * n2 / 12 * ((n + 1) - tieTerm / (n* (n - 1))));
79
-
80
- let numerator = U - mu;
81
-
82
- if (continuity) {
83
- numerator -= 0.5;
84
- }
85
-
86
- const z = numerator / s;
87
-
88
- const p = 2 * (1 - jStat.normal.cdf(z, 0, 1));
89
-
90
- return {'p-value': p, 'Median difference': med1 - med2, 'p-value more': p, 'p-value less': p};
91
- }
92
-
93
- function _tieTerm(ranks: number[]): number {
94
- const ties: {[key: number]: number} = {};
95
-
96
- ranks.forEach((num) => {
97
- ties[num] = (ties[num] || 0) + 1;
98
- });
99
-
100
- return jStat.sum(Object.values(ties));
101
- }