@numero-uno-jpn/rstats 0.5.0 → 0.7.0
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/CHANGELOG.md +125 -0
- package/README.ja.md +36 -20
- package/README.md +34 -18
- package/THIRD_PARTY_NOTICES.md +25 -5
- package/dist/chunks/chunk-6R7YDHA2.js +2 -0
- package/dist/chunks/chunk-AFKYWGKN.js +2 -0
- package/dist/chunks/chunk-ANYGLFT4.js +2 -0
- package/dist/chunks/chunk-BI5RSUZ2.js +2 -0
- package/dist/chunks/chunk-J3SNI7KY.js +2 -0
- package/dist/chunks/chunk-JCZAB6C5.js +2 -0
- package/dist/chunks/chunk-MVG5LUZU.js +2 -0
- package/dist/chunks/chunk-PTPPP4TI.js +2 -0
- package/dist/chunks/chunk-QNQJA6KZ.js +2 -0
- package/dist/chunks/chunk-SXSJXDD3.js +2 -0
- package/dist/chunks/chunk-UIOAWLAN.js +2 -0
- package/dist/chunks/chunk-XYXHHF5L.js +2 -0
- package/dist/chunks/chunk-YKN6OGJL.js +2 -0
- package/dist/chunks/chunk-ZW52Z3GY.js +2 -0
- package/dist/chunks/chunk-ZYA4ISAT.js +2 -0
- package/dist/descriptive/basic.d.ts +73 -0
- package/dist/descriptive/basic.js +256 -0
- package/dist/descriptive/cov.d.ts +39 -0
- package/dist/descriptive/cov.js +105 -0
- package/dist/descriptive/hist.d.ts +64 -0
- package/dist/descriptive/hist.js +304 -0
- package/dist/descriptive/moments.d.ts +63 -0
- package/dist/descriptive/moments.js +158 -0
- package/dist/descriptive/quantile.d.ts +50 -0
- package/dist/descriptive/quantile.js +131 -0
- package/dist/descriptive/rank.d.ts +23 -0
- package/dist/descriptive/rank.js +72 -0
- package/dist/descriptive/table.d.ts +54 -0
- package/dist/descriptive/table.js +160 -0
- package/dist/descriptive.js +2 -0
- package/dist/distributions/arcsine.d.ts +24 -0
- package/dist/distributions/arcsine.js +73 -0
- package/dist/distributions/invgamma.d.ts +29 -0
- package/dist/distributions/invgamma.js +85 -0
- package/dist/distributions/kumaraswamy.d.ts +17 -0
- package/dist/distributions/kumaraswamy.js +96 -0
- package/dist/distributions/laplace.d.ts +23 -0
- package/dist/distributions/laplace.js +69 -0
- package/dist/distributions/multinomial.d.ts +24 -0
- package/dist/distributions/multinomial.js +70 -0
- package/dist/distributions/nbeta.js +28 -10
- package/dist/distributions/nchisq.js +20 -3
- package/dist/distributions/pareto.d.ts +23 -0
- package/dist/distributions/pareto.js +76 -0
- package/dist/distributions/summary.d.ts +25 -0
- package/dist/distributions/summary.js +289 -0
- package/dist/distributions/triangular.d.ts +25 -0
- package/dist/distributions/triangular.js +87 -0
- package/dist/distributions.js +2 -0
- package/dist/entries/descriptive.d.ts +7 -0
- package/dist/entries/descriptive.js +8 -0
- package/dist/entries/distributions.d.ts +42 -0
- package/dist/entries/distributions.js +42 -0
- package/dist/entries/linalg.d.ts +11 -0
- package/dist/entries/linalg.js +11 -0
- package/dist/entries/numerics.d.ts +8 -0
- package/dist/entries/numerics.js +6 -0
- package/dist/entries/special.d.ts +10 -0
- package/dist/entries/special.js +10 -0
- package/dist/entries/tests.d.ts +21 -0
- package/dist/entries/tests.js +20 -0
- package/dist/htest/ansariTest.d.ts +30 -0
- package/dist/htest/ansariTest.js +253 -0
- package/dist/htest/aov.d.ts +85 -0
- package/dist/htest/aov.js +178 -0
- package/dist/htest/binomTest.d.ts +36 -0
- package/dist/htest/binomTest.js +121 -0
- package/dist/htest/chisqTest.d.ts +34 -2
- package/dist/htest/chisqTest.js +140 -37
- package/dist/htest/cor.d.ts +14 -0
- package/dist/htest/cor.js +4 -3
- package/dist/htest/core/ansari.d.ts +25 -0
- package/dist/htest/core/ansari.js +186 -0
- package/dist/htest/core/kolmogorov.d.ts +26 -0
- package/dist/htest/core/kolmogorov.js +221 -0
- package/dist/htest/core/mantelhaen.d.ts +26 -0
- package/dist/htest/core/mantelhaen.js +145 -0
- package/dist/htest/core/shapiro.d.ts +24 -0
- package/dist/htest/core/shapiro.js +142 -0
- package/dist/htest/core/simulate.d.ts +2 -0
- package/dist/htest/core/simulate.js +16 -0
- package/dist/htest/core/smirnov.d.ts +20 -0
- package/dist/htest/core/smirnov.js +75 -0
- package/dist/htest/fisherTest.d.ts +12 -1
- package/dist/htest/fisherTest.js +187 -127
- package/dist/htest/groups.d.ts +20 -0
- package/dist/htest/groups.js +62 -0
- package/dist/htest/ksTest.d.ts +31 -0
- package/dist/htest/ksTest.js +106 -0
- package/dist/htest/mantelhaenTest.d.ts +26 -0
- package/dist/htest/mantelhaenTest.js +196 -0
- package/dist/htest/mcnemarTest.d.ts +26 -0
- package/dist/htest/mcnemarTest.js +99 -0
- package/dist/htest/onewayTest.d.ts +26 -0
- package/dist/htest/onewayTest.js +97 -0
- package/dist/htest/pairwise.d.ts +64 -0
- package/dist/htest/pairwise.js +152 -0
- package/dist/htest/poissonTest.d.ts +35 -0
- package/dist/htest/poissonTest.js +170 -0
- package/dist/htest/propTest.d.ts +37 -0
- package/dist/htest/propTest.js +164 -0
- package/dist/htest/rankTests.d.ts +52 -0
- package/dist/htest/rankTests.js +234 -0
- package/dist/htest/types.d.ts +2 -0
- package/dist/htest/types.js +6 -0
- package/dist/htest/varTest.d.ts +48 -0
- package/dist/htest/varTest.js +118 -0
- package/dist/htest/wilcoxTest.js +136 -174
- package/dist/index.d.ts +6 -50
- package/dist/index.js +8 -50
- package/dist/integrate/quadpack.d.ts +29 -0
- package/dist/integrate/quadpack.js +525 -0
- package/dist/internal/seq.d.ts +5 -0
- package/dist/internal/seq.js +14 -0
- package/dist/internal/uniroot.js +10 -71
- package/dist/linalg/lu.d.ts +45 -0
- package/dist/linalg/lu.js +88 -0
- package/dist/linalg/ops.d.ts +90 -0
- package/dist/linalg/ops.js +320 -0
- package/dist/linalg.js +2 -0
- package/dist/models/lm.d.ts +98 -0
- package/dist/models/lm.js +187 -0
- package/dist/numerics/deriv.d.ts +37 -0
- package/dist/numerics/deriv.js +150 -0
- package/dist/numerics/integrate.d.ts +91 -0
- package/dist/numerics/integrate.js +264 -0
- package/dist/numerics/interp.d.ts +103 -0
- package/dist/numerics/interp.js +581 -0
- package/dist/numerics/ode.d.ts +23 -0
- package/dist/numerics/ode.js +59 -0
- package/dist/numerics.js +2 -0
- package/dist/optim/localmin.d.ts +16 -0
- package/dist/optim/localmin.js +97 -0
- package/dist/random/beta.js +1 -1
- package/dist/random/binomial.js +1 -1
- package/dist/random/continuous.js +1 -1
- package/dist/random/extra.d.ts +33 -0
- package/dist/random/extra.js +62 -0
- package/dist/random/gamma.js +1 -1
- package/dist/random/hyper.js +1 -1
- package/dist/random/mt.js +0 -1
- package/dist/random/poisson.js +0 -1
- package/dist/random/r2dtable.d.ts +26 -0
- package/dist/random/r2dtable.js +153 -0
- package/dist/random/rank.js +1 -1
- package/dist/random/rng.js +1 -1
- package/dist/random/sample.js +1 -1
- package/dist/rstats.js +3 -2
- package/dist/special/besselIK.d.ts +38 -0
- package/dist/special/besselIK.js +315 -0
- package/dist/special/besselJY.d.ts +32 -0
- package/dist/special/besselJY.js +508 -0
- package/dist/special/besselSeries.d.ts +8 -0
- package/dist/special/besselSeries.js +24 -0
- package/dist/special/beta.js +22 -5
- package/dist/special/choose.d.ts +75 -0
- package/dist/special/choose.js +233 -0
- package/dist/special/erf.d.ts +28 -2
- package/dist/special/erf.js +37 -10
- package/dist/special/extra.d.ts +47 -0
- package/dist/special/extra.js +220 -0
- package/dist/special/igam.d.ts +4 -0
- package/dist/special/igam.js +57 -2
- package/dist/special/incbet.js +77 -22
- package/dist/special/lgammaDiff.d.ts +7 -0
- package/dist/special/lgammaDiff.js +16 -3
- package/dist/special/log1p.d.ts +53 -0
- package/dist/special/log1p.js +93 -0
- package/dist/special/psigamma.d.ts +48 -0
- package/dist/special/psigamma.js +318 -0
- package/dist/special/rCompatible.d.ts +8 -0
- package/dist/special/rCompatible.js +1 -0
- package/dist/special.js +2 -0
- package/dist/tests.js +3 -0
- package/package.json +26 -1
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// Histogram counts and pretty break points: R's hist (with plot = FALSE), pretty and ecdf.
|
|
2
|
+
//
|
|
3
|
+
// Written for rStats.js to reproduce R's documented behaviour and output. pretty picks a unit of 1, 2, 5 or 10 times
|
|
4
|
+
// a power of ten close to range / n, preferring the larger units by the biases high.u.bias and u5.bias described in
|
|
5
|
+
// R's ?pretty, then extends to integer multiples of the unit that cover the data (with a 1e-10 relative rounding
|
|
6
|
+
// allowance) and keeps at least min.n cells; a range that is tiny relative to its magnitude gets a scale derived
|
|
7
|
+
// from the magnitude instead. hist chooses the number of cells by Sturges' (1926), Scott's (1979) or Freedman and
|
|
8
|
+
// Diaconis' (1981) rule, or as given, takes pretty break points over the data range, and counts after moving the
|
|
9
|
+
// breaks by 1e-7 times a typical cell width, so that values lying on a break up to rounding fall on the intended side.
|
|
10
|
+
// The order of floating-point operations follows R where the results are compared bit for bit.
|
|
11
|
+
import { median, range, sd } from "./basic.js";
|
|
12
|
+
import { IQR, quantile } from "./quantile.js";
|
|
13
|
+
import { seqLength } from "../internal/seq.js";
|
|
14
|
+
/**
|
|
15
|
+
* About n + 1 equally spaced "round" values (1, 2 or 5 times a power of 10) covering the range of `x`. Equivalent to
|
|
16
|
+
* R's `pretty()`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* pretty([0.3, 9.6]); // [0, 2, 4, 6, 8, 10]
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function pretty(x, options = {}) {
|
|
24
|
+
const finite = Array.from(x).filter(Number.isFinite);
|
|
25
|
+
if (finite.length === 0)
|
|
26
|
+
return [];
|
|
27
|
+
const n = options.n ?? 5;
|
|
28
|
+
const minN = options.minN ?? Math.floor(n / 3);
|
|
29
|
+
const shrink = options.shrinkSml ?? 0.75;
|
|
30
|
+
const h = options.highUBias ?? 1.5;
|
|
31
|
+
const h5 = options.u5Bias ?? 0.5 + 1.5 * h;
|
|
32
|
+
let lo = Infinity;
|
|
33
|
+
let up = -Infinity;
|
|
34
|
+
for (const v of finite) {
|
|
35
|
+
if (v < lo)
|
|
36
|
+
lo = v;
|
|
37
|
+
if (v > up)
|
|
38
|
+
up = v;
|
|
39
|
+
}
|
|
40
|
+
const d = up - lo;
|
|
41
|
+
// Step 1: the scale (target cell width)
|
|
42
|
+
let c;
|
|
43
|
+
let small;
|
|
44
|
+
if (d === 0 && up === 0) {
|
|
45
|
+
c = 1;
|
|
46
|
+
small = true;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
c = Math.max(Math.abs(lo), Math.abs(up));
|
|
50
|
+
const t = h5 >= 1.5 * h + 0.5 ? 1 / (1 + h) : 1.5 / (1 + h5);
|
|
51
|
+
const bound = (1 + t) * Math.max(1, n) * Number.EPSILON;
|
|
52
|
+
small = d < c * bound * 3;
|
|
53
|
+
}
|
|
54
|
+
if (small) {
|
|
55
|
+
if (c > 10)
|
|
56
|
+
c = 9 + c / 10;
|
|
57
|
+
c *= shrink;
|
|
58
|
+
if (minN > 1)
|
|
59
|
+
c /= minN;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
c = n > 1 ? d / n : d;
|
|
63
|
+
if (d === Infinity && n >= 2)
|
|
64
|
+
c = up / n - lo / n;
|
|
65
|
+
}
|
|
66
|
+
const tiny = 2 ** -20 * DBL_MIN;
|
|
67
|
+
if (c < tiny)
|
|
68
|
+
c = tiny;
|
|
69
|
+
else if (c > Number.MAX_VALUE / 1.25)
|
|
70
|
+
c = Number.MAX_VALUE / 1.25;
|
|
71
|
+
// Step 2: the unit, walking up b, 2b, 5b, 10b while the larger unit is close enough
|
|
72
|
+
const b = 10 ** Math.floor(Math.log10(c));
|
|
73
|
+
const steps = [
|
|
74
|
+
[2 * b, h],
|
|
75
|
+
[5 * b, h5],
|
|
76
|
+
[10 * b, h],
|
|
77
|
+
];
|
|
78
|
+
let u = b;
|
|
79
|
+
for (const [next, w] of steps) {
|
|
80
|
+
if (next - c < w * (c - u))
|
|
81
|
+
u = next;
|
|
82
|
+
else
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
// Step 3: integer multiples of the unit covering the data
|
|
86
|
+
const rho = 1e-10;
|
|
87
|
+
let s = Math.floor(lo / u + rho);
|
|
88
|
+
while (s * u > lo + rho * u)
|
|
89
|
+
s--;
|
|
90
|
+
while (!Number.isFinite(s * u))
|
|
91
|
+
s++;
|
|
92
|
+
let t = Math.ceil(up / u - rho);
|
|
93
|
+
while (t * u < up - rho * u)
|
|
94
|
+
t++;
|
|
95
|
+
while (!Number.isFinite(t * u))
|
|
96
|
+
t--;
|
|
97
|
+
// Step 4: at least minN cells
|
|
98
|
+
let k = Math.trunc(0.5 + t - s);
|
|
99
|
+
if (k < minN) {
|
|
100
|
+
const e = minN - k;
|
|
101
|
+
const half = Math.floor(e / 2);
|
|
102
|
+
const odd = e % 2;
|
|
103
|
+
if (lo === 0 && s === 0 && up !== 0)
|
|
104
|
+
t += e;
|
|
105
|
+
else if (up === 0 && t === 0 && lo !== 0)
|
|
106
|
+
s -= e;
|
|
107
|
+
else if (s >= 0) {
|
|
108
|
+
t += half;
|
|
109
|
+
s -= half + odd;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
s -= half;
|
|
113
|
+
t += half + odd;
|
|
114
|
+
}
|
|
115
|
+
k = minN;
|
|
116
|
+
}
|
|
117
|
+
// Step 5: end points (kept at the data ends when the multiples are within the rounding allowance)
|
|
118
|
+
const L = s * u < lo ? s * u : lo;
|
|
119
|
+
const U = t * u > up ? t * u : up;
|
|
120
|
+
// Step 6: the sequence, with values that are zero up to rounding set to 0
|
|
121
|
+
if (k <= 0)
|
|
122
|
+
return [L];
|
|
123
|
+
let out;
|
|
124
|
+
if (Number.isFinite(U - L))
|
|
125
|
+
out = seqLength(L, U, k + 1);
|
|
126
|
+
else {
|
|
127
|
+
// U - L overflows: step by U/k - L/k instead
|
|
128
|
+
const step = U / k - L / k;
|
|
129
|
+
out = Array.from({ length: k + 1 }, (_, i) => L + i * step);
|
|
130
|
+
out[k] = U;
|
|
131
|
+
}
|
|
132
|
+
const delta = U / k - L / k;
|
|
133
|
+
return out.map((v) => (Math.abs(v) < 1e-14 * delta ? 0 : v));
|
|
134
|
+
}
|
|
135
|
+
/** Smallest positive normal double */
|
|
136
|
+
const DBL_MIN = 2.2250738585072014e-308;
|
|
137
|
+
/** Rounding to 5 significant decimal digits (0 and non-finite values unchanged) */
|
|
138
|
+
function round5(v) {
|
|
139
|
+
return v === 0 || !Number.isFinite(v) ? v : Number(v.toPrecision(5));
|
|
140
|
+
}
|
|
141
|
+
/** Suggested number of classes for the data `xs` (finite values) by the named rule */
|
|
142
|
+
function classCount(xs, rule) {
|
|
143
|
+
const n = xs.length;
|
|
144
|
+
const [lo, hi] = range(xs);
|
|
145
|
+
const r = hi - lo;
|
|
146
|
+
switch (rule.toLowerCase()) {
|
|
147
|
+
case "sturges":
|
|
148
|
+
return Math.ceil(Math.log2(n) + 1);
|
|
149
|
+
case "scott": {
|
|
150
|
+
const h = 3.5 * sd(xs) * n ** (-1 / 3);
|
|
151
|
+
return h > 0 ? Math.max(1, Math.ceil(r / h)) : 1;
|
|
152
|
+
}
|
|
153
|
+
case "fd":
|
|
154
|
+
case "freedman-diaconis": {
|
|
155
|
+
const xr = xs.map(round5);
|
|
156
|
+
let h = 2 * IQR(xr);
|
|
157
|
+
for (let alpha = 1 / 8; h === 0 && alpha >= 1 / 512; alpha /= 2) {
|
|
158
|
+
const [qa, qb] = quantile(xr, [alpha, 1 - alpha]);
|
|
159
|
+
h = (qb - qa) / (1 - 2 * alpha);
|
|
160
|
+
}
|
|
161
|
+
if (h === 0)
|
|
162
|
+
h = 3.5 * sd(xs);
|
|
163
|
+
return h > 0 ? Math.ceil((r / h) * n ** (1 / 3)) : 1;
|
|
164
|
+
}
|
|
165
|
+
default:
|
|
166
|
+
throw new RangeError("unknown 'breaks' algorithm");
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** Reference cell size for the counting fuzz: median width (> 5 breaks), data range (<= 3), smallest positive width */
|
|
170
|
+
function fuzzScale(br, dataRange) {
|
|
171
|
+
const widths = br.slice(1).map((v, i) => v - br[i]);
|
|
172
|
+
if (br.length > 5)
|
|
173
|
+
return median(widths);
|
|
174
|
+
if (br.length <= 3)
|
|
175
|
+
return dataRange;
|
|
176
|
+
return Math.min(...widths.filter((w) => w > 0));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Histogram counts. Equivalent to R's `hist(x, plot = FALSE)`: break points (by default pretty points over the range
|
|
180
|
+
* with about Sturges' number of cells), counts, densities and cell midpoints. Non-finite values are dropped.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* hist([1, 2, 2, 3, 3, 3, 4]).counts; // [3, 3, 1] for breaks [1, 2, 3, 4]
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export function hist(x, options = {}) {
|
|
188
|
+
const xs = Array.from(x).filter(Number.isFinite);
|
|
189
|
+
const n = xs.length;
|
|
190
|
+
const right = options.right ?? true;
|
|
191
|
+
let includeLowest = options.includeLowest ?? true;
|
|
192
|
+
const spec = options.breaks ?? "Sturges";
|
|
193
|
+
let br;
|
|
194
|
+
if (typeof spec !== "string" && typeof spec !== "number" && spec.length > 1) {
|
|
195
|
+
br = [...spec].sort((a, b) => a - b);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
includeLowest = true;
|
|
199
|
+
let k = typeof spec === "string" ? classCount(xs, spec) : typeof spec === "number" ? spec : spec[0];
|
|
200
|
+
if (!Number.isFinite(k) || k < 1)
|
|
201
|
+
throw new RangeError("invalid number of 'breaks'");
|
|
202
|
+
k = Math.min(k, 1e6);
|
|
203
|
+
const [lo, hi] = range(xs);
|
|
204
|
+
br = pretty([lo, hi], { n: k, minN: 1 });
|
|
205
|
+
for (let i = 1; i < br.length; i++) {
|
|
206
|
+
if (!(br[i] - br[i - 1] > 0))
|
|
207
|
+
throw new RangeError("'breaks' are not strictly increasing");
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const nb = br.length;
|
|
211
|
+
// Breaks moved by a small tolerance so that values on a break up to rounding fall on the intended side
|
|
212
|
+
const [dlo, dhi] = n > 0 ? range(xs) : [0, 0];
|
|
213
|
+
const tau = 1e-7 * fuzzScale(br, dhi - dlo);
|
|
214
|
+
const f = br.map((v, i) => {
|
|
215
|
+
if (right)
|
|
216
|
+
return i === 0 && includeLowest ? v - tau : v + tau;
|
|
217
|
+
if (i < nb - 1)
|
|
218
|
+
return v - tau;
|
|
219
|
+
return includeLowest ? v + tau : v - tau;
|
|
220
|
+
});
|
|
221
|
+
const counts = new Array(Math.max(0, nb - 1)).fill(0);
|
|
222
|
+
let total = 0;
|
|
223
|
+
for (const v of xs) {
|
|
224
|
+
const cell = cellOf(v, f, right, includeLowest);
|
|
225
|
+
if (cell >= 0) {
|
|
226
|
+
counts[cell]++;
|
|
227
|
+
total++;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (total < n)
|
|
231
|
+
throw new RangeError("some 'x' not counted; maybe 'breaks' do not span range of 'x'");
|
|
232
|
+
const density = counts.map((c, i) => c / (n * (br[i + 1] - br[i])));
|
|
233
|
+
const mids = counts.map((_, i) => 0.5 * (br[i + 1] + br[i]));
|
|
234
|
+
return { breaks: br, counts, density, mids };
|
|
235
|
+
}
|
|
236
|
+
/** 0-based cell of `v` among the (shifted) breaks `f`, or -1 when it is in no cell */
|
|
237
|
+
function cellOf(v, f, right, includeLowest) {
|
|
238
|
+
const last = f.length - 1;
|
|
239
|
+
if (last < 1)
|
|
240
|
+
return -1;
|
|
241
|
+
if (right) {
|
|
242
|
+
if (v === f[0] && includeLowest)
|
|
243
|
+
return 0;
|
|
244
|
+
if (!(v > f[0] && v <= f[last]))
|
|
245
|
+
return -1;
|
|
246
|
+
// smallest j with v <= f[j]; the cell is j - 1
|
|
247
|
+
let a = 1;
|
|
248
|
+
let b = last;
|
|
249
|
+
while (a < b) {
|
|
250
|
+
const m = (a + b) >> 1;
|
|
251
|
+
if (v <= f[m])
|
|
252
|
+
b = m;
|
|
253
|
+
else
|
|
254
|
+
a = m + 1;
|
|
255
|
+
}
|
|
256
|
+
return a - 1;
|
|
257
|
+
}
|
|
258
|
+
if (v === f[last] && includeLowest)
|
|
259
|
+
return last - 1;
|
|
260
|
+
if (!(v >= f[0] && v < f[last]))
|
|
261
|
+
return -1;
|
|
262
|
+
// largest j with f[j] <= v; the cell is j
|
|
263
|
+
let a = 0;
|
|
264
|
+
let b = last - 1;
|
|
265
|
+
while (a < b) {
|
|
266
|
+
const m = (a + b + 1) >> 1;
|
|
267
|
+
if (f[m] <= v)
|
|
268
|
+
a = m;
|
|
269
|
+
else
|
|
270
|
+
b = m - 1;
|
|
271
|
+
}
|
|
272
|
+
return a;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Empirical distribution function of `x`: a function returning the fraction of values at or below its argument.
|
|
276
|
+
* Equivalent to R's `ecdf()`. `NaN` values are dropped.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* ecdf([3, 1, 2])(2); // 2/3
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
export function ecdf(x) {
|
|
284
|
+
const s = Array.from(x)
|
|
285
|
+
.filter((v) => !Number.isNaN(v))
|
|
286
|
+
.sort((a, b) => a - b);
|
|
287
|
+
const n = s.length;
|
|
288
|
+
if (n < 1)
|
|
289
|
+
throw new RangeError("'x' must have 1 or more non-missing values");
|
|
290
|
+
return (q) => {
|
|
291
|
+
if (Number.isNaN(q))
|
|
292
|
+
return NaN;
|
|
293
|
+
let lo = 0;
|
|
294
|
+
let hi = n;
|
|
295
|
+
while (lo < hi) {
|
|
296
|
+
const mid = (lo + hi) >> 1;
|
|
297
|
+
if (s[mid] <= q)
|
|
298
|
+
lo = mid + 1;
|
|
299
|
+
else
|
|
300
|
+
hi = mid;
|
|
301
|
+
}
|
|
302
|
+
return lo / n;
|
|
303
|
+
};
|
|
304
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type NaRmOptions } from "./basic.js";
|
|
2
|
+
import type { Sample } from "../htest/types.js";
|
|
3
|
+
/** Options for {@link skewness} and {@link kurtosis}. */
|
|
4
|
+
export interface MomentOptions {
|
|
5
|
+
/** e1071's type: 1, 2 or 3. Default `3`. */
|
|
6
|
+
type?: 1 | 2 | 3;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Sample skewness. Equivalent to `e1071::skewness()` (missing values dropped); `{ type: 1 }` is jStat's skewness.
|
|
10
|
+
*/
|
|
11
|
+
export declare function skewness(x: Sample, options?: MomentOptions): number;
|
|
12
|
+
/**
|
|
13
|
+
* Sample excess kurtosis. Equivalent to `e1071::kurtosis()` (missing values dropped); `{ type: 1 }` is jStat's
|
|
14
|
+
* kurtosis.
|
|
15
|
+
*/
|
|
16
|
+
export declare function kurtosis(x: Sample, options?: MomentOptions): number;
|
|
17
|
+
/**
|
|
18
|
+
* Geometric mean exp(mean(log x)). Equivalent to `DescTools::Gmean()`: NaN with a negative value, 0 with a zero.
|
|
19
|
+
*/
|
|
20
|
+
export declare function geometricMean(x: Sample, options?: NaRmOptions): number;
|
|
21
|
+
/**
|
|
22
|
+
* The most frequent values (all of them, in increasing order) and their frequency. Equivalent to `DescTools::Mode()`
|
|
23
|
+
* (missing values dropped): `value` is `[NaN]` and `frequency` NaN when every value occurs once.
|
|
24
|
+
*/
|
|
25
|
+
export declare function modes(x: Sample): {
|
|
26
|
+
value: number[];
|
|
27
|
+
frequency: number;
|
|
28
|
+
};
|
|
29
|
+
/** Options for {@link meanAD}. */
|
|
30
|
+
export interface MeanADOptions {
|
|
31
|
+
/** Centre: the mean (default) or `"median"`, or a number. */
|
|
32
|
+
center?: "mean" | "median" | number;
|
|
33
|
+
}
|
|
34
|
+
/** Mean absolute deviation from the centre. Equivalent to `DescTools::MeanAD()` (missing values dropped). */
|
|
35
|
+
export declare function meanAD(x: Sample, options?: MeanADOptions): number;
|
|
36
|
+
/** Options for {@link cv}. */
|
|
37
|
+
export interface CvOptions extends NaRmOptions {
|
|
38
|
+
/** Small-sample correction. Default `false`. */
|
|
39
|
+
unbiased?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Coefficient of variation sd / mean (sample standard deviation). Equivalent to `DescTools::CoefVar()`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function cv(x: Sample, options?: CvOptions): number;
|
|
45
|
+
/** Options for {@link scale}. */
|
|
46
|
+
export interface ScaleOptions {
|
|
47
|
+
/** Subtract the column means (`true`, default), nothing (`false`) or the given values. */
|
|
48
|
+
center?: boolean | readonly number[];
|
|
49
|
+
/**
|
|
50
|
+
* Divide by the root mean square of each (centred) column, sqrt(sum v^2 / (n - 1)) (`true`, default: the standard
|
|
51
|
+
* deviation when centred), by nothing (`false`) or by the given values.
|
|
52
|
+
*/
|
|
53
|
+
scale?: boolean | readonly number[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Centre and scale the columns of a matrix (array of rows; a vector is one column). Equivalent to R's `scale()`:
|
|
57
|
+
* `center` and `scale` report the values used (R's attributes), when any.
|
|
58
|
+
*/
|
|
59
|
+
export declare function scale(x: Sample | readonly (readonly number[])[], options?: ScaleOptions): {
|
|
60
|
+
scaled: number[][];
|
|
61
|
+
center?: number[];
|
|
62
|
+
scale?: number[];
|
|
63
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Shape and location summaries that R's base lacks, with the definitions of the packages they are checked against:
|
|
2
|
+
// skewness and kurtosis (e1071, types 1 to 3), geometricMean, modes, meanAD and cv (DescTools' Gmean, Mode, MeanAD
|
|
3
|
+
// and CoefVar). Also scale (R's base).
|
|
4
|
+
//
|
|
5
|
+
// Written for rStats.js from the published definitions (no ported code). With m_k the central moments (divisor n),
|
|
6
|
+
// g1 = m3 / m2^1.5 and g2 = m4 / m2^2 - 3; e1071's type 1 is (g1, g2), type 2 the adjusted estimators of SAS and SPSS
|
|
7
|
+
// (Joanes and Gill, The Statistician 47, 1998), type 3 (the default, as in MINITAB and BMDP) b1 = g1 ((n-1)/n)^1.5 and
|
|
8
|
+
// b2 = (g2 + 3) ((n-1)/n)^2 - 3. The central moments are summed after subtracting the double-double mean.
|
|
9
|
+
import { values } from "./basic.js";
|
|
10
|
+
import { meanDD, mean as meanOf, variance } from "../internal/summary.js";
|
|
11
|
+
import { median } from "./basic.js";
|
|
12
|
+
/** Central sums sum (x - mean)^2, ^3, ^4 of values without NaN */
|
|
13
|
+
function centralSums(xs) {
|
|
14
|
+
const m = meanDD(xs);
|
|
15
|
+
let s2 = 0;
|
|
16
|
+
let s3 = 0;
|
|
17
|
+
let s4 = 0;
|
|
18
|
+
for (const v of xs) {
|
|
19
|
+
const d = v - m[0] - m[1];
|
|
20
|
+
const d2 = d * d;
|
|
21
|
+
s2 += d2;
|
|
22
|
+
s3 += d2 * d;
|
|
23
|
+
s4 += d2 * d2;
|
|
24
|
+
}
|
|
25
|
+
return [s2, s3, s4];
|
|
26
|
+
}
|
|
27
|
+
function checkType(type) {
|
|
28
|
+
if (![1, 2, 3].includes(type))
|
|
29
|
+
throw new RangeError("Invalid 'type' argument.");
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Sample skewness. Equivalent to `e1071::skewness()` (missing values dropped); `{ type: 1 }` is jStat's skewness.
|
|
33
|
+
*/
|
|
34
|
+
export function skewness(x, options = {}) {
|
|
35
|
+
const type = options.type ?? 3;
|
|
36
|
+
checkType(type);
|
|
37
|
+
const xs = values(x, true);
|
|
38
|
+
const n = xs.length;
|
|
39
|
+
const [s2, s3] = centralSums(xs);
|
|
40
|
+
const g1 = (Math.sqrt(n) * s3) / s2 ** 1.5;
|
|
41
|
+
if (type === 1)
|
|
42
|
+
return g1;
|
|
43
|
+
if (type === 2) {
|
|
44
|
+
if (n < 3)
|
|
45
|
+
throw new RangeError("Need at least 3 complete observations.");
|
|
46
|
+
return (g1 * Math.sqrt(n * (n - 1))) / (n - 2);
|
|
47
|
+
}
|
|
48
|
+
return g1 * (1 - 1 / n) ** 1.5;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Sample excess kurtosis. Equivalent to `e1071::kurtosis()` (missing values dropped); `{ type: 1 }` is jStat's
|
|
52
|
+
* kurtosis.
|
|
53
|
+
*/
|
|
54
|
+
export function kurtosis(x, options = {}) {
|
|
55
|
+
const type = options.type ?? 3;
|
|
56
|
+
checkType(type);
|
|
57
|
+
const xs = values(x, true);
|
|
58
|
+
const n = xs.length;
|
|
59
|
+
const [s2, , s4] = centralSums(xs);
|
|
60
|
+
const r = (n * s4) / (s2 * s2);
|
|
61
|
+
if (type === 1)
|
|
62
|
+
return r - 3;
|
|
63
|
+
if (type === 2) {
|
|
64
|
+
if (n < 4)
|
|
65
|
+
throw new RangeError("Need at least 4 complete observations.");
|
|
66
|
+
return (((n + 1) * (r - 3) + 6) * (n - 1)) / ((n - 2) * (n - 3));
|
|
67
|
+
}
|
|
68
|
+
return r * (1 - 1 / n) ** 2 - 3;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Geometric mean exp(mean(log x)). Equivalent to `DescTools::Gmean()`: NaN with a negative value, 0 with a zero.
|
|
72
|
+
*/
|
|
73
|
+
export function geometricMean(x, options = {}) {
|
|
74
|
+
const xs = values(x, options.naRm);
|
|
75
|
+
if (xs === undefined)
|
|
76
|
+
return NaN;
|
|
77
|
+
if (xs.some((v) => v < 0))
|
|
78
|
+
return NaN;
|
|
79
|
+
if (xs.includes(0))
|
|
80
|
+
return 0;
|
|
81
|
+
return Math.exp(meanOf(xs.map(Math.log)));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The most frequent values (all of them, in increasing order) and their frequency. Equivalent to `DescTools::Mode()`
|
|
85
|
+
* (missing values dropped): `value` is `[NaN]` and `frequency` NaN when every value occurs once.
|
|
86
|
+
*/
|
|
87
|
+
export function modes(x) {
|
|
88
|
+
const xs = values(x, true);
|
|
89
|
+
const counts = new Map();
|
|
90
|
+
for (const v of xs)
|
|
91
|
+
counts.set(v === 0 ? 0 : v, (counts.get(v === 0 ? 0 : v) ?? 0) + 1);
|
|
92
|
+
let best = 0;
|
|
93
|
+
for (const c of counts.values())
|
|
94
|
+
best = Math.max(best, c);
|
|
95
|
+
if (best <= 1)
|
|
96
|
+
return { value: [NaN], frequency: NaN };
|
|
97
|
+
const value = [...counts.entries()].filter(([, c]) => c === best).map(([v]) => v);
|
|
98
|
+
return { value: value.sort((a, b) => a - b), frequency: best };
|
|
99
|
+
}
|
|
100
|
+
/** Mean absolute deviation from the centre. Equivalent to `DescTools::MeanAD()` (missing values dropped). */
|
|
101
|
+
export function meanAD(x, options = {}) {
|
|
102
|
+
const xs = values(x, true);
|
|
103
|
+
const c = options.center ?? "mean";
|
|
104
|
+
const center = c === "mean" ? meanOf(xs) : c === "median" ? median(xs) : c;
|
|
105
|
+
return meanOf(xs.map((v) => Math.abs(v - center)));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Coefficient of variation sd / mean (sample standard deviation). Equivalent to `DescTools::CoefVar()`.
|
|
109
|
+
*/
|
|
110
|
+
export function cv(x, options = {}) {
|
|
111
|
+
const xs = values(x, options.naRm);
|
|
112
|
+
if (xs === undefined)
|
|
113
|
+
return NaN;
|
|
114
|
+
const n = xs.length;
|
|
115
|
+
let res = Math.sqrt(variance(xs)) / meanOf(xs);
|
|
116
|
+
if (options.unbiased)
|
|
117
|
+
res *= 1 - 1 / (4 * (n - 1)) + (res * res) / n + 1 / (2 * (n - 1) ** 2);
|
|
118
|
+
return res;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Centre and scale the columns of a matrix (array of rows; a vector is one column). Equivalent to R's `scale()`:
|
|
122
|
+
* `center` and `scale` report the values used (R's attributes), when any.
|
|
123
|
+
*/
|
|
124
|
+
export function scale(x, options = {}) {
|
|
125
|
+
const rows = x.length > 0 && Array.isArray(x[0]) ? x.map((r) => [...r]) : Array.from(x, (v) => [v]);
|
|
126
|
+
const nc = rows[0]?.length ?? 0;
|
|
127
|
+
const col = (j) => rows.map((r) => r[j]);
|
|
128
|
+
const result = { scaled: rows };
|
|
129
|
+
const c = options.center ?? true;
|
|
130
|
+
if (c !== false) {
|
|
131
|
+
const center = c === true ? Array.from({ length: nc }, (_, j) => meanOf(col(j).filter((v) => !Number.isNaN(v)))) : [...c];
|
|
132
|
+
if (center.length !== nc)
|
|
133
|
+
throw new RangeError("length of 'center' must equal the number of columns of 'x'");
|
|
134
|
+
for (const r of rows)
|
|
135
|
+
for (let j = 0; j < nc; j++)
|
|
136
|
+
r[j] = r[j] - center[j];
|
|
137
|
+
result.center = center;
|
|
138
|
+
}
|
|
139
|
+
const s = options.scale ?? true;
|
|
140
|
+
if (s !== false) {
|
|
141
|
+
const sc = s === true
|
|
142
|
+
? Array.from({ length: nc }, (_, j) => {
|
|
143
|
+
const v = col(j).filter((t) => !Number.isNaN(t));
|
|
144
|
+
let ss = 0;
|
|
145
|
+
for (const t of v)
|
|
146
|
+
ss += t * t;
|
|
147
|
+
return Math.sqrt(ss / Math.max(1, v.length - 1));
|
|
148
|
+
})
|
|
149
|
+
: [...s];
|
|
150
|
+
if (sc.length !== nc)
|
|
151
|
+
throw new RangeError("length of 'scale' must equal the number of columns of 'x'");
|
|
152
|
+
for (const r of rows)
|
|
153
|
+
for (let j = 0; j < nc; j++)
|
|
154
|
+
r[j] = r[j] / sc[j];
|
|
155
|
+
result.scale = sc;
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type NaRmOptions } from "./basic.js";
|
|
2
|
+
import type { Sample } from "../htest/types.js";
|
|
3
|
+
/** Options for {@link quantile}. */
|
|
4
|
+
export interface QuantileOptions extends NaRmOptions {
|
|
5
|
+
/** Hyndman and Fan's definition, 1 to 9. Default `7`. */
|
|
6
|
+
type?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Sample quantiles. Equivalent to R's `quantile()` (with `names = FALSE`).
|
|
10
|
+
*
|
|
11
|
+
* @param x The data.
|
|
12
|
+
* @param probs Probabilities in [0, 1]. Default `[0, 0.25, 0.5, 0.75, 1]`. A single number gives a single number.
|
|
13
|
+
* @param options `type` (1 to 9, default 7) and `naRm`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* quantile([1, 2, 3, 4, 5, 6], 0.25); // 2.25 (type 7)
|
|
18
|
+
* quantile([1, 2, 3, 4, 5, 6], 0.25, { type: 9 }); // 1.9375 (jStat's quantiles)
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function quantile(x: Sample, probs: number, options?: QuantileOptions): number;
|
|
22
|
+
export declare function quantile(x: Sample, probs?: readonly number[], options?: QuantileOptions): number[];
|
|
23
|
+
/** Interquartile range, the difference of the 0.75 and 0.25 quantiles. Equivalent to R's `IQR()`. */
|
|
24
|
+
export declare function IQR(x: Sample, options?: QuantileOptions): number;
|
|
25
|
+
/**
|
|
26
|
+
* Tukey's five-number summary (minimum, lower hinge, median, upper hinge, maximum). Equivalent to R's `fivenum()`;
|
|
27
|
+
* `NaN` values are dropped.
|
|
28
|
+
*/
|
|
29
|
+
export declare function fivenum(x: Sample): [number, number, number, number, number];
|
|
30
|
+
/** Options for {@link mad}. */
|
|
31
|
+
export interface MadOptions extends NaRmOptions {
|
|
32
|
+
/** Centre. Default: the median. */
|
|
33
|
+
center?: number;
|
|
34
|
+
/** Scale factor. Default `1.4826` (consistent for the normal standard deviation). */
|
|
35
|
+
constant?: number;
|
|
36
|
+
/** For an even count, take the lower of the two middle absolute deviations (the "lo-median"). */
|
|
37
|
+
low?: boolean;
|
|
38
|
+
/** For an even count, take the higher one (the "hi-median"). */
|
|
39
|
+
high?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Median absolute deviation, scaled. Equivalent to R's `mad()`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* mad([1, 2, 3, 4, 100]); // 1.4826
|
|
47
|
+
* mad([1, 2, 3, 4, 100], { constant: 1 }); // 1 (jStat's meddev)
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function mad(x: Sample, options?: MadOptions): number;
|