@danielsimonjr/mathts-functions 0.14.0 → 0.16.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/dist/descriptive-stats.d.ts +64 -0
- package/dist/descriptive-stats.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +975 -34
- package/dist/typed/dist-objects.d.ts +66 -0
- package/dist/typed/dist-objects.d.ts.map +1 -1
- package/dist/typed/hypothesis.d.ts +194 -0
- package/dist/typed/hypothesis.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -33,9 +33,11 @@ __export(typed_exports, {
|
|
|
33
33
|
airyAi: () => airyAi,
|
|
34
34
|
airyBi: () => airyBi,
|
|
35
35
|
and: () => and,
|
|
36
|
+
andersonDarlingTest: () => andersonDarlingTest,
|
|
36
37
|
angle2D: () => angle2D,
|
|
37
38
|
angle3D: () => angle3D,
|
|
38
39
|
anova: () => anova,
|
|
40
|
+
anova2: () => anova2,
|
|
39
41
|
apart: () => apart,
|
|
40
42
|
area: () => area,
|
|
41
43
|
arg: () => arg,
|
|
@@ -48,6 +50,7 @@ __export(typed_exports, {
|
|
|
48
50
|
autoCorrelation: () => autoCorrelation,
|
|
49
51
|
bandpassFilter: () => bandpassFilter,
|
|
50
52
|
bartlettPSD: () => bartlettPSD,
|
|
53
|
+
bartlettTest: () => bartlettTest,
|
|
51
54
|
bernoulli: () => bernoulli,
|
|
52
55
|
bernoulliPMF: () => bernoulliPMF,
|
|
53
56
|
besselI: () => besselI,
|
|
@@ -67,10 +70,12 @@ __export(typed_exports, {
|
|
|
67
70
|
bin: () => bin,
|
|
68
71
|
binomialDist: () => binomialDist,
|
|
69
72
|
binomialPMF: () => binomialPMF,
|
|
73
|
+
binomialTest: () => binomialTest,
|
|
70
74
|
bitAnd: () => bitAnd,
|
|
71
75
|
bitNot: () => bitNot,
|
|
72
76
|
bitOr: () => bitOr,
|
|
73
77
|
bitXor: () => bitXor,
|
|
78
|
+
bootstrapCI: () => bootstrapCI,
|
|
74
79
|
bspline: () => bspline,
|
|
75
80
|
cancel: () => cancel,
|
|
76
81
|
carlsonRC: () => carlsonRC,
|
|
@@ -120,12 +125,14 @@ __export(typed_exports, {
|
|
|
120
125
|
cube: () => cube,
|
|
121
126
|
cubicSpline: () => cubicSpline,
|
|
122
127
|
curvefit: () => curvefit,
|
|
128
|
+
dagostinoTest: () => dagostinoTest,
|
|
123
129
|
dct: () => dct,
|
|
124
130
|
deepEqual: () => deepEqual,
|
|
125
131
|
degree: () => degree,
|
|
126
132
|
delaunayTriangulation: () => delaunayTriangulation,
|
|
127
133
|
differences: () => differences,
|
|
128
134
|
digamma: () => digamma,
|
|
135
|
+
discreteUniformDist: () => discreteUniformDist,
|
|
129
136
|
discriminant: () => discriminant,
|
|
130
137
|
distance2D: () => distance2D,
|
|
131
138
|
distance3D: () => distance3D,
|
|
@@ -178,6 +185,7 @@ __export(typed_exports, {
|
|
|
178
185
|
fourier: () => fourier,
|
|
179
186
|
fresnelC: () => fresnelC,
|
|
180
187
|
fresnelS: () => fresnelS,
|
|
188
|
+
friedmanTest: () => friedmanTest,
|
|
181
189
|
fullSimplify: () => fullSimplify,
|
|
182
190
|
functionExpand: () => functionExpand,
|
|
183
191
|
fuseUnaryChain: () => fuseUnaryChain,
|
|
@@ -198,6 +206,7 @@ __export(typed_exports, {
|
|
|
198
206
|
graphDistance: () => graphDistance,
|
|
199
207
|
griddata: () => griddata,
|
|
200
208
|
groupDelay: () => groupDelay,
|
|
209
|
+
gumbelDist: () => gumbelDist,
|
|
201
210
|
harmonicNumber: () => harmonicNumber,
|
|
202
211
|
hermiteH: () => hermiteH,
|
|
203
212
|
hermiteInterp: () => hermiteInterp,
|
|
@@ -205,6 +214,8 @@ __export(typed_exports, {
|
|
|
205
214
|
hex: () => hex,
|
|
206
215
|
highpassFilter: () => highpassFilter,
|
|
207
216
|
hilbertTransform: () => hilbertTransform,
|
|
217
|
+
hotellingT2: () => hotellingT2,
|
|
218
|
+
hypergeometricDist: () => hypergeometricDist,
|
|
208
219
|
hypot: () => hypot,
|
|
209
220
|
idct: () => idct,
|
|
210
221
|
idst: () => idst,
|
|
@@ -217,12 +228,14 @@ __export(typed_exports, {
|
|
|
217
228
|
intersectLines2D: () => intersectLines2D,
|
|
218
229
|
intersectSegments2D: () => intersectSegments2D,
|
|
219
230
|
invFourier: () => invFourier,
|
|
231
|
+
invGaussDist: () => invGaussDist,
|
|
220
232
|
isConnected: () => isConnected,
|
|
221
233
|
jacobiSymbol: () => jacobiSymbol,
|
|
222
234
|
jordanForm: () => jordanForm,
|
|
223
235
|
jsDivergence: () => jsDivergence,
|
|
224
236
|
kdTree: () => kdTree,
|
|
225
237
|
kdTreeNearest: () => kdTreeNearest,
|
|
238
|
+
kolmogorovSmirnov2Test: () => kolmogorovSmirnov2Test,
|
|
226
239
|
kolmogorovSmirnovTest: () => kolmogorovSmirnovTest,
|
|
227
240
|
lagrangeInterp: () => lagrangeInterp,
|
|
228
241
|
laguerreL: () => laguerreL,
|
|
@@ -234,6 +247,7 @@ __export(typed_exports, {
|
|
|
234
247
|
leftShift: () => leftShift,
|
|
235
248
|
legendreFit: () => legendreFit,
|
|
236
249
|
legendreP: () => legendreP,
|
|
250
|
+
leveneTest: () => leveneTest,
|
|
237
251
|
lgamma: () => lgamma,
|
|
238
252
|
linearInterp: () => linearInterp,
|
|
239
253
|
linprog: () => linprog,
|
|
@@ -250,6 +264,7 @@ __export(typed_exports, {
|
|
|
250
264
|
lowpassFilter: () => lowpassFilter,
|
|
251
265
|
lucas: () => lucas,
|
|
252
266
|
lucasL: () => lucasL,
|
|
267
|
+
mahalanobis: () => mahalanobis,
|
|
253
268
|
manhattanDistance: () => manhattanDistance,
|
|
254
269
|
mannWhitneyTest: () => mannWhitneyTest,
|
|
255
270
|
matmul: () => matmul,
|
|
@@ -264,6 +279,7 @@ __export(typed_exports, {
|
|
|
264
279
|
maxSelect: () => maxSelect,
|
|
265
280
|
maximize: () => maximize,
|
|
266
281
|
mean: () => mean,
|
|
282
|
+
meanCI: () => meanCI,
|
|
267
283
|
medfilt: () => medfilt,
|
|
268
284
|
medianSelect: () => medianSelect,
|
|
269
285
|
min: () => min,
|
|
@@ -275,8 +291,11 @@ __export(typed_exports, {
|
|
|
275
291
|
moebiusMu: () => moebiusMu,
|
|
276
292
|
multiTaperPSD: () => multiTaperPSD,
|
|
277
293
|
multinomial: () => multinomial,
|
|
294
|
+
multipleComparison: () => multipleComparison,
|
|
278
295
|
multiply: () => multiply,
|
|
296
|
+
multivariateNormal: () => multivariateNormal,
|
|
279
297
|
nearestNeighbor: () => nearestNeighbor,
|
|
298
|
+
negativeBinomialDist: () => negativeBinomialDist,
|
|
280
299
|
newtonInterp: () => newtonInterp,
|
|
281
300
|
nextPrime: () => nextPrime,
|
|
282
301
|
nintegrate: () => nintegrate,
|
|
@@ -323,10 +342,12 @@ __export(typed_exports, {
|
|
|
323
342
|
parallelStatSum: () => parallelStatSum,
|
|
324
343
|
parallelStatVariance: () => parallelStatVariance,
|
|
325
344
|
parallelXCorr: () => parallelXCorr,
|
|
345
|
+
paretoDist: () => paretoDist,
|
|
326
346
|
partitions: () => partitions,
|
|
327
347
|
pchip: () => pchip,
|
|
328
348
|
pchipInterp: () => pchipInterp,
|
|
329
349
|
periodogram: () => periodogram,
|
|
350
|
+
permutationTest: () => permutationTest,
|
|
330
351
|
permutations: () => permutations,
|
|
331
352
|
pickRandom: () => pickRandom,
|
|
332
353
|
pinv: () => pinv,
|
|
@@ -354,11 +375,14 @@ __export(typed_exports, {
|
|
|
354
375
|
principalComponentAnalysis: () => principalComponentAnalysis,
|
|
355
376
|
print: () => print,
|
|
356
377
|
projectVector: () => projectVector,
|
|
378
|
+
proportionCI: () => proportionCI,
|
|
379
|
+
proportionZTest: () => proportionZTest,
|
|
357
380
|
quadprog: () => quadprog,
|
|
358
381
|
quickSelect: () => quickSelect,
|
|
359
382
|
random: () => random,
|
|
360
383
|
randomInt: () => randomInt,
|
|
361
384
|
rank: () => rank,
|
|
385
|
+
rayleighDist: () => rayleighDist,
|
|
362
386
|
rbfInterpolate: () => rbfInterpolate,
|
|
363
387
|
re: () => re,
|
|
364
388
|
reduce: () => reduce,
|
|
@@ -410,6 +434,7 @@ __export(typed_exports, {
|
|
|
410
434
|
stronglyConnectedComponents: () => stronglyConnectedComponents,
|
|
411
435
|
studentTPDF: () => studentTPDF,
|
|
412
436
|
studentTTest: () => studentTTest,
|
|
437
|
+
studentTTestPaired: () => studentTTestPaired,
|
|
413
438
|
subfactorial: () => subfactorial,
|
|
414
439
|
substitute: () => substitute,
|
|
415
440
|
subtract: () => subtract,
|
|
@@ -432,6 +457,7 @@ __export(typed_exports, {
|
|
|
432
457
|
trapz: () => trapz,
|
|
433
458
|
trapzF64: () => trapzF64,
|
|
434
459
|
triangleArea: () => triangleArea,
|
|
460
|
+
triangularDist: () => triangularDist,
|
|
435
461
|
trigExpand: () => trigExpand,
|
|
436
462
|
trigReduce: () => trigReduce,
|
|
437
463
|
trigToExp: () => trigToExp,
|
|
@@ -12813,12 +12839,7 @@ function _normalQuantileStd(p) {
|
|
|
12813
12839
|
4.374664141464968,
|
|
12814
12840
|
2.938163982698783
|
|
12815
12841
|
];
|
|
12816
|
-
const d = [
|
|
12817
|
-
0.007784695709041462,
|
|
12818
|
-
0.3224671290700398,
|
|
12819
|
-
2.445134137142996,
|
|
12820
|
-
3.754408661907416
|
|
12821
|
-
];
|
|
12842
|
+
const d = [0.007784695709041462, 0.3224671290700398, 2.445134137142996, 3.754408661907416];
|
|
12822
12843
|
const pLow = 0.02425;
|
|
12823
12844
|
let x;
|
|
12824
12845
|
if (p < pLow) {
|
|
@@ -13489,9 +13510,337 @@ function weibullDist(k, lambda = 1) {
|
|
|
13489
13510
|
})
|
|
13490
13511
|
};
|
|
13491
13512
|
}
|
|
13513
|
+
function _logChoose(n, r) {
|
|
13514
|
+
if (r < 0 || r > n) return -Infinity;
|
|
13515
|
+
return _logFactorial2(n) - _logFactorial2(r) - _logFactorial2(n - r);
|
|
13516
|
+
}
|
|
13517
|
+
function _choose(n, r) {
|
|
13518
|
+
if (r < 0 || r > n) return 0;
|
|
13519
|
+
const k = Math.min(r, n - r);
|
|
13520
|
+
let c = 1;
|
|
13521
|
+
for (let i = 0; i < k; i++) c = c * (n - i) / (i + 1);
|
|
13522
|
+
return c;
|
|
13523
|
+
}
|
|
13524
|
+
function _discreteSample(cdf, rng, cap) {
|
|
13525
|
+
const u = rng();
|
|
13526
|
+
for (let k = 0; k <= cap; k++) {
|
|
13527
|
+
if (cdf(k) >= u) return k;
|
|
13528
|
+
}
|
|
13529
|
+
return cap;
|
|
13530
|
+
}
|
|
13531
|
+
function hypergeometricDist(population, successes, draws) {
|
|
13532
|
+
if (![population, successes, draws].every((v) => Number.isInteger(v) && v >= 0)) {
|
|
13533
|
+
throw new Error(
|
|
13534
|
+
"hypergeometricDist: population, successes, draws must be non-negative integers"
|
|
13535
|
+
);
|
|
13536
|
+
}
|
|
13537
|
+
if (successes > population || draws > population) {
|
|
13538
|
+
throw new Error("hypergeometricDist: successes and draws cannot exceed population");
|
|
13539
|
+
}
|
|
13540
|
+
const M = population;
|
|
13541
|
+
const K = successes;
|
|
13542
|
+
const N = draws;
|
|
13543
|
+
const kMin = Math.max(0, N - (M - K));
|
|
13544
|
+
const kMax = Math.min(K, N);
|
|
13545
|
+
const useExact = M <= 1e3;
|
|
13546
|
+
const pmf = (k) => {
|
|
13547
|
+
if (!Number.isInteger(k) || k < kMin || k > kMax) return 0;
|
|
13548
|
+
return useExact ? _choose(K, k) * _choose(M - K, N - k) / _choose(M, N) : Math.exp(_logChoose(K, k) + _logChoose(M - K, N - k) - _logChoose(M, N));
|
|
13549
|
+
};
|
|
13550
|
+
const cdf = (k) => {
|
|
13551
|
+
if (k < kMin) return 0;
|
|
13552
|
+
const kk = Math.min(Math.floor(k), kMax);
|
|
13553
|
+
let sum3 = 0;
|
|
13554
|
+
for (let i = kMin; i <= kk; i++) sum3 += pmf(i);
|
|
13555
|
+
return Math.min(1, sum3);
|
|
13556
|
+
};
|
|
13557
|
+
const mean6 = N * K / M;
|
|
13558
|
+
const variance2 = N * (K / M) * ((M - K) / M) * ((M - N) / (M - 1));
|
|
13559
|
+
return {
|
|
13560
|
+
pdf: pmf,
|
|
13561
|
+
cdf,
|
|
13562
|
+
quantile: (p) => {
|
|
13563
|
+
if (p <= 0) return kMin;
|
|
13564
|
+
if (p >= 1) return kMax;
|
|
13565
|
+
for (let k = kMin; k <= kMax; k++) if (cdf(k) >= p) return k;
|
|
13566
|
+
return kMax;
|
|
13567
|
+
},
|
|
13568
|
+
mean: mean6,
|
|
13569
|
+
variance: variance2,
|
|
13570
|
+
sample: () => _discreteSample(cdf, Math.random, kMax),
|
|
13571
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13572
|
+
const out = new Float64Array(count2);
|
|
13573
|
+
for (let j = 0; j < count2; j++) out[j] = _discreteSample(cdf, rng, kMax);
|
|
13574
|
+
return out;
|
|
13575
|
+
})
|
|
13576
|
+
};
|
|
13577
|
+
}
|
|
13578
|
+
function negativeBinomialDist(r, p) {
|
|
13579
|
+
if (!Number.isInteger(r) || r < 1)
|
|
13580
|
+
throw new Error("negativeBinomialDist: r must be a positive integer");
|
|
13581
|
+
if (p <= 0 || p > 1) throw new Error("negativeBinomialDist: p must be in (0, 1]");
|
|
13582
|
+
const pmf = (k) => {
|
|
13583
|
+
if (!Number.isInteger(k) || k < 0) return 0;
|
|
13584
|
+
return Math.exp(_logChoose(k + r - 1, k) + r * Math.log(p) + k * Math.log(1 - p));
|
|
13585
|
+
};
|
|
13586
|
+
const cdf = (k) => {
|
|
13587
|
+
if (k < 0) return 0;
|
|
13588
|
+
return Math.min(1, _betainc(p, r, Math.floor(k) + 1));
|
|
13589
|
+
};
|
|
13590
|
+
const mean6 = r * (1 - p) / p;
|
|
13591
|
+
const variance2 = r * (1 - p) / (p * p);
|
|
13592
|
+
return {
|
|
13593
|
+
pdf: pmf,
|
|
13594
|
+
cdf,
|
|
13595
|
+
quantile: (prob) => {
|
|
13596
|
+
if (prob <= 0) return 0;
|
|
13597
|
+
if (prob >= 1) return Infinity;
|
|
13598
|
+
let cumul = 0;
|
|
13599
|
+
for (let k = 0; k < 1e5; k++) {
|
|
13600
|
+
cumul += pmf(k);
|
|
13601
|
+
if (cumul >= prob) return k;
|
|
13602
|
+
}
|
|
13603
|
+
return Infinity;
|
|
13604
|
+
},
|
|
13605
|
+
mean: mean6,
|
|
13606
|
+
variance: variance2,
|
|
13607
|
+
sample: () => _discreteSample(cdf, Math.random, Math.ceil(mean6 + 20 * Math.sqrt(variance2) + 20)),
|
|
13608
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13609
|
+
const out = new Float64Array(count2);
|
|
13610
|
+
const cap = Math.ceil(mean6 + 20 * Math.sqrt(variance2) + 20);
|
|
13611
|
+
for (let j = 0; j < count2; j++) out[j] = _discreteSample(cdf, rng, cap);
|
|
13612
|
+
return out;
|
|
13613
|
+
})
|
|
13614
|
+
};
|
|
13615
|
+
}
|
|
13616
|
+
var EULER_GAMMA = 0.5772156649015329;
|
|
13617
|
+
function _contSample(quantile, rng) {
|
|
13618
|
+
return quantile(rng());
|
|
13619
|
+
}
|
|
13620
|
+
function paretoDist(b, xm) {
|
|
13621
|
+
if (b <= 0 || xm <= 0) throw new Error("paretoDist: shape b and scale xm must be positive");
|
|
13622
|
+
const quantile = (p) => p <= 0 ? xm : p >= 1 ? Infinity : xm / Math.pow(1 - p, 1 / b);
|
|
13623
|
+
return {
|
|
13624
|
+
pdf: (x) => x < xm ? 0 : b * Math.pow(xm, b) / Math.pow(x, b + 1),
|
|
13625
|
+
cdf: (x) => x < xm ? 0 : 1 - Math.pow(xm / x, b),
|
|
13626
|
+
quantile,
|
|
13627
|
+
mean: b > 1 ? b * xm / (b - 1) : Infinity,
|
|
13628
|
+
variance: b > 2 ? xm * xm * b / ((b - 1) * (b - 1) * (b - 2)) : Infinity,
|
|
13629
|
+
sample: () => _contSample(quantile, Math.random),
|
|
13630
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13631
|
+
const out = new Float64Array(count2);
|
|
13632
|
+
for (let j = 0; j < count2; j++) out[j] = _contSample(quantile, rng);
|
|
13633
|
+
return out;
|
|
13634
|
+
})
|
|
13635
|
+
};
|
|
13636
|
+
}
|
|
13637
|
+
function rayleighDist(sigma) {
|
|
13638
|
+
if (sigma <= 0) throw new Error("rayleighDist: sigma must be positive");
|
|
13639
|
+
const s2 = sigma * sigma;
|
|
13640
|
+
const quantile = (p) => p <= 0 ? 0 : p >= 1 ? Infinity : sigma * Math.sqrt(-2 * Math.log(1 - p));
|
|
13641
|
+
return {
|
|
13642
|
+
pdf: (x) => x < 0 ? 0 : x / s2 * Math.exp(-(x * x) / (2 * s2)),
|
|
13643
|
+
cdf: (x) => x < 0 ? 0 : 1 - Math.exp(-(x * x) / (2 * s2)),
|
|
13644
|
+
quantile,
|
|
13645
|
+
mean: sigma * Math.sqrt(Math.PI / 2),
|
|
13646
|
+
variance: (4 - Math.PI) / 2 * s2,
|
|
13647
|
+
sample: () => _contSample(quantile, Math.random),
|
|
13648
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13649
|
+
const out = new Float64Array(count2);
|
|
13650
|
+
for (let j = 0; j < count2; j++) out[j] = _contSample(quantile, rng);
|
|
13651
|
+
return out;
|
|
13652
|
+
})
|
|
13653
|
+
};
|
|
13654
|
+
}
|
|
13655
|
+
function triangularDist(a, c, b) {
|
|
13656
|
+
if (!(a <= c && c <= b && a < b))
|
|
13657
|
+
throw new Error("triangularDist: require a <= mode <= b, a < b");
|
|
13658
|
+
const quantile = (p) => {
|
|
13659
|
+
if (p <= 0) return a;
|
|
13660
|
+
if (p >= 1) return b;
|
|
13661
|
+
const fc = (c - a) / (b - a);
|
|
13662
|
+
return p < fc ? a + Math.sqrt(p * (b - a) * (c - a)) : b - Math.sqrt((1 - p) * (b - a) * (b - c));
|
|
13663
|
+
};
|
|
13664
|
+
return {
|
|
13665
|
+
pdf: (x) => {
|
|
13666
|
+
if (x < a || x > b) return 0;
|
|
13667
|
+
if (x < c) return 2 * (x - a) / ((b - a) * (c - a));
|
|
13668
|
+
if (x > c) return 2 * (b - x) / ((b - a) * (b - c));
|
|
13669
|
+
return 2 / (b - a);
|
|
13670
|
+
},
|
|
13671
|
+
cdf: (x) => {
|
|
13672
|
+
if (x <= a) return 0;
|
|
13673
|
+
if (x >= b) return 1;
|
|
13674
|
+
return x < c ? (x - a) * (x - a) / ((b - a) * (c - a)) : 1 - (b - x) * (b - x) / ((b - a) * (b - c));
|
|
13675
|
+
},
|
|
13676
|
+
quantile,
|
|
13677
|
+
mean: (a + c + b) / 3,
|
|
13678
|
+
variance: (a * a + c * c + b * b - a * c - a * b - c * b) / 18,
|
|
13679
|
+
sample: () => _contSample(quantile, Math.random),
|
|
13680
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13681
|
+
const out = new Float64Array(count2);
|
|
13682
|
+
for (let j = 0; j < count2; j++) out[j] = _contSample(quantile, rng);
|
|
13683
|
+
return out;
|
|
13684
|
+
})
|
|
13685
|
+
};
|
|
13686
|
+
}
|
|
13687
|
+
function discreteUniformDist(lo, hi) {
|
|
13688
|
+
if (!Number.isInteger(lo) || !Number.isInteger(hi) || hi < lo) {
|
|
13689
|
+
throw new Error("discreteUniformDist: require integer lo <= hi");
|
|
13690
|
+
}
|
|
13691
|
+
const k = hi - lo + 1;
|
|
13692
|
+
const cdf = (x) => {
|
|
13693
|
+
if (x < lo) return 0;
|
|
13694
|
+
if (x >= hi) return 1;
|
|
13695
|
+
return (Math.floor(x) - lo + 1) / k;
|
|
13696
|
+
};
|
|
13697
|
+
return {
|
|
13698
|
+
pdf: (x) => Number.isInteger(x) && x >= lo && x <= hi ? 1 / k : 0,
|
|
13699
|
+
cdf,
|
|
13700
|
+
quantile: (p) => {
|
|
13701
|
+
if (p <= 0) return lo;
|
|
13702
|
+
if (p >= 1) return hi;
|
|
13703
|
+
return Math.min(hi, lo + Math.ceil(p * k) - 1);
|
|
13704
|
+
},
|
|
13705
|
+
mean: (lo + hi) / 2,
|
|
13706
|
+
variance: (k * k - 1) / 12,
|
|
13707
|
+
sample: () => lo + Math.floor(Math.random() * k),
|
|
13708
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13709
|
+
const out = new Float64Array(count2);
|
|
13710
|
+
for (let j = 0; j < count2; j++) out[j] = lo + Math.floor(rng() * k);
|
|
13711
|
+
return out;
|
|
13712
|
+
})
|
|
13713
|
+
};
|
|
13714
|
+
}
|
|
13715
|
+
function gumbelDist(mu, beta2) {
|
|
13716
|
+
if (beta2 <= 0) throw new Error("gumbelDist: scale beta must be positive");
|
|
13717
|
+
const quantile = (p) => p <= 0 ? -Infinity : p >= 1 ? Infinity : mu - beta2 * Math.log(-Math.log(p));
|
|
13718
|
+
return {
|
|
13719
|
+
pdf: (x) => {
|
|
13720
|
+
const z = (x - mu) / beta2;
|
|
13721
|
+
return 1 / beta2 * Math.exp(-(z + Math.exp(-z)));
|
|
13722
|
+
},
|
|
13723
|
+
cdf: (x) => Math.exp(-Math.exp(-(x - mu) / beta2)),
|
|
13724
|
+
quantile,
|
|
13725
|
+
mean: mu + beta2 * EULER_GAMMA,
|
|
13726
|
+
variance: Math.PI * Math.PI / 6 * beta2 * beta2,
|
|
13727
|
+
sample: () => _contSample(quantile, Math.random),
|
|
13728
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13729
|
+
const out = new Float64Array(count2);
|
|
13730
|
+
for (let j = 0; j < count2; j++) out[j] = _contSample(quantile, rng);
|
|
13731
|
+
return out;
|
|
13732
|
+
})
|
|
13733
|
+
};
|
|
13734
|
+
}
|
|
13735
|
+
function invGaussDist(mu, lambda) {
|
|
13736
|
+
if (mu <= 0 || lambda <= 0) throw new Error("invGaussDist: mu and lambda must be positive");
|
|
13737
|
+
const quantile = (p) => {
|
|
13738
|
+
if (p <= 0) return 0;
|
|
13739
|
+
if (p >= 1) return Infinity;
|
|
13740
|
+
let lo = 1e-9;
|
|
13741
|
+
let hi = mu * 100;
|
|
13742
|
+
for (let it = 0; it < 200; it++) {
|
|
13743
|
+
const mid = (lo + hi) / 2;
|
|
13744
|
+
if (cdf(mid) < p) lo = mid;
|
|
13745
|
+
else hi = mid;
|
|
13746
|
+
}
|
|
13747
|
+
return (lo + hi) / 2;
|
|
13748
|
+
};
|
|
13749
|
+
const cdf = (x) => {
|
|
13750
|
+
if (x <= 0) return 0;
|
|
13751
|
+
const a = Math.sqrt(lambda / x) * (x / mu - 1);
|
|
13752
|
+
const b = -Math.sqrt(lambda / x) * (x / mu + 1);
|
|
13753
|
+
return _normalCdfStd(a) + Math.exp(2 * lambda / mu) * _normalCdfStd(b);
|
|
13754
|
+
};
|
|
13755
|
+
return {
|
|
13756
|
+
pdf: (x) => {
|
|
13757
|
+
if (x <= 0) return 0;
|
|
13758
|
+
return Math.sqrt(lambda / (2 * Math.PI * x * x * x)) * Math.exp(-lambda * (x - mu) * (x - mu) / (2 * mu * mu * x));
|
|
13759
|
+
},
|
|
13760
|
+
cdf,
|
|
13761
|
+
quantile,
|
|
13762
|
+
mean: mu,
|
|
13763
|
+
variance: mu * mu * mu / lambda,
|
|
13764
|
+
sample: () => _contSample(quantile, Math.random),
|
|
13765
|
+
sampleN: (n, opts) => _sampleNDispatch(null, [], n, opts, (count2, rng) => {
|
|
13766
|
+
const out = new Float64Array(count2);
|
|
13767
|
+
for (let j = 0; j < count2; j++) out[j] = _contSample(quantile, rng);
|
|
13768
|
+
return out;
|
|
13769
|
+
})
|
|
13770
|
+
};
|
|
13771
|
+
}
|
|
13772
|
+
function _cholesky(m) {
|
|
13773
|
+
const n = m.length;
|
|
13774
|
+
const L = Array.from({ length: n }, () => new Array(n).fill(0));
|
|
13775
|
+
for (let i = 0; i < n; i++) {
|
|
13776
|
+
for (let j = 0; j <= i; j++) {
|
|
13777
|
+
let s = m[i][j];
|
|
13778
|
+
for (let k = 0; k < j; k++) s -= L[i][k] * L[j][k];
|
|
13779
|
+
if (i === j) {
|
|
13780
|
+
if (s <= 0) throw new Error("multivariateNormal: covariance is not positive-definite");
|
|
13781
|
+
L[i][j] = Math.sqrt(s);
|
|
13782
|
+
} else {
|
|
13783
|
+
L[i][j] = s / L[j][j];
|
|
13784
|
+
}
|
|
13785
|
+
}
|
|
13786
|
+
}
|
|
13787
|
+
return L;
|
|
13788
|
+
}
|
|
13789
|
+
function multivariateNormal(mean6, cov2) {
|
|
13790
|
+
const k = mean6.length;
|
|
13791
|
+
if (cov2.length !== k || cov2.some((row2) => row2.length !== k)) {
|
|
13792
|
+
throw new Error("multivariateNormal: cov must be a k\xD7k matrix matching mean");
|
|
13793
|
+
}
|
|
13794
|
+
const L = _cholesky(cov2);
|
|
13795
|
+
let logDet = 0;
|
|
13796
|
+
for (let i = 0; i < k; i++) logDet += 2 * Math.log(L[i][i]);
|
|
13797
|
+
return {
|
|
13798
|
+
mean: mean6,
|
|
13799
|
+
cov: cov2,
|
|
13800
|
+
pdf: (x) => {
|
|
13801
|
+
if (x.length !== k) throw new Error("multivariateNormal.pdf: x must have length k");
|
|
13802
|
+
const z = new Array(k).fill(0);
|
|
13803
|
+
for (let i = 0; i < k; i++) {
|
|
13804
|
+
let s = x[i] - mean6[i];
|
|
13805
|
+
for (let j = 0; j < i; j++) s -= L[i][j] * z[j];
|
|
13806
|
+
z[i] = s / L[i][i];
|
|
13807
|
+
}
|
|
13808
|
+
let quad = 0;
|
|
13809
|
+
for (let i = 0; i < k; i++) quad += z[i] * z[i];
|
|
13810
|
+
return Math.exp(-0.5 * (k * Math.log(2 * Math.PI) + logDet + quad));
|
|
13811
|
+
}
|
|
13812
|
+
};
|
|
13813
|
+
}
|
|
13492
13814
|
|
|
13493
13815
|
// src/typed/hypothesis.ts
|
|
13494
13816
|
import { computePool as computePool10 } from "@danielsimonjr/mathts-parallel";
|
|
13817
|
+
|
|
13818
|
+
// src/distribution-functions.ts
|
|
13819
|
+
var normalQuantile = (p, mu = 0, sigma = 1) => normalDist(mu, sigma).quantile(p);
|
|
13820
|
+
var studentTCDF = (x, df) => tDist(df).cdf(x);
|
|
13821
|
+
var studentTQuantile = (p, df) => tDist(df).quantile(p);
|
|
13822
|
+
var chiSquaredCDF = (x, df) => chiSquaredDist(df).cdf(x);
|
|
13823
|
+
var chiSquaredQuantile = (p, df) => chiSquaredDist(df).quantile(p);
|
|
13824
|
+
var fCDF = (x, d1, d2) => fDist(d1, d2).cdf(x);
|
|
13825
|
+
var fQuantile = (p, d1, d2) => fDist(d1, d2).quantile(p);
|
|
13826
|
+
var gammaCDF = (x, shape, rate = 1) => gammaDist(shape, rate).cdf(x);
|
|
13827
|
+
var gammaQuantile = (p, shape, rate = 1) => gammaDist(shape, rate).quantile(p);
|
|
13828
|
+
var betaCDF = (x, a, b) => betaDist(a, b).cdf(x);
|
|
13829
|
+
var betaQuantile = (p, a, b) => betaDist(a, b).quantile(p);
|
|
13830
|
+
var cauchyPDF = (x, x0 = 0, gamma2 = 1) => 1 / (Math.PI * gamma2 * (1 + ((x - x0) / gamma2) ** 2));
|
|
13831
|
+
var cauchyCDF = (x, x0 = 0, gamma2 = 1) => 0.5 + Math.atan((x - x0) / gamma2) / Math.PI;
|
|
13832
|
+
var cauchyQuantile = (p, x0 = 0, gamma2 = 1) => x0 + gamma2 * Math.tan(Math.PI * (p - 0.5));
|
|
13833
|
+
var laplacePDF = (x, mu = 0, b = 1) => Math.exp(-Math.abs(x - mu) / b) / (2 * b);
|
|
13834
|
+
var laplaceCDF = (x, mu = 0, b = 1) => x < mu ? 0.5 * Math.exp((x - mu) / b) : 1 - 0.5 * Math.exp(-(x - mu) / b);
|
|
13835
|
+
var laplaceQuantile = (p, mu = 0, b = 1) => p < 0.5 ? mu + b * Math.log(2 * p) : mu - b * Math.log(2 - 2 * p);
|
|
13836
|
+
var logisticPDF = (x, mu = 0, s = 1) => {
|
|
13837
|
+
const e = Math.exp(-(x - mu) / s);
|
|
13838
|
+
return e / (s * (1 + e) ** 2);
|
|
13839
|
+
};
|
|
13840
|
+
var logisticCDF = (x, mu = 0, s = 1) => 1 / (1 + Math.exp(-(x - mu) / s));
|
|
13841
|
+
var logisticQuantile = (p, mu = 0, s = 1) => mu + s * Math.log(p / (1 - p));
|
|
13842
|
+
|
|
13843
|
+
// src/typed/hypothesis.ts
|
|
13495
13844
|
var HYPOTHESIS_THRESHOLD = 4096;
|
|
13496
13845
|
function _makeMulberry322(seed) {
|
|
13497
13846
|
let s = seed | 0;
|
|
@@ -14252,6 +14601,407 @@ function principalComponentAnalysis(data, k) {
|
|
|
14252
14601
|
});
|
|
14253
14602
|
return { components: eigenvectors, explained, scores };
|
|
14254
14603
|
}
|
|
14604
|
+
function _kstwobignSf(x) {
|
|
14605
|
+
if (x <= 0) return 1;
|
|
14606
|
+
let sum3 = 0;
|
|
14607
|
+
for (let k = 1; k <= 100; k++) {
|
|
14608
|
+
const term = 2 * (k % 2 === 1 ? 1 : -1) * Math.exp(-2 * k * k * x * x);
|
|
14609
|
+
sum3 += term;
|
|
14610
|
+
if (Math.abs(term) < 1e-15) break;
|
|
14611
|
+
}
|
|
14612
|
+
return Math.max(0, Math.min(1, sum3));
|
|
14613
|
+
}
|
|
14614
|
+
function kolmogorovSmirnov2Test(sample1, sample2) {
|
|
14615
|
+
const n16 = sample1.length;
|
|
14616
|
+
const n26 = sample2.length;
|
|
14617
|
+
if (n16 < 1 || n26 < 1) {
|
|
14618
|
+
throw new Error("kolmogorovSmirnov2Test: both samples must be non-empty");
|
|
14619
|
+
}
|
|
14620
|
+
const a = sample1.slice().sort((x, y) => x - y);
|
|
14621
|
+
const b = sample2.slice().sort((x, y) => x - y);
|
|
14622
|
+
let i = 0;
|
|
14623
|
+
let j = 0;
|
|
14624
|
+
let d = 0;
|
|
14625
|
+
while (i < n16 && j < n26) {
|
|
14626
|
+
const x = Math.min(a[i], b[j]);
|
|
14627
|
+
while (i < n16 && a[i] <= x) i++;
|
|
14628
|
+
while (j < n26 && b[j] <= x) j++;
|
|
14629
|
+
d = Math.max(d, Math.abs(i / n16 - j / n26));
|
|
14630
|
+
}
|
|
14631
|
+
const en = Math.sqrt(n16 * n26 / (n16 + n26));
|
|
14632
|
+
return { statistic: d, pValue: _kstwobignSf(en * d) };
|
|
14633
|
+
}
|
|
14634
|
+
function _median(arr7) {
|
|
14635
|
+
const s = arr7.slice().sort((a, b) => a - b);
|
|
14636
|
+
const n = s.length;
|
|
14637
|
+
const m = n >> 1;
|
|
14638
|
+
return n % 2 === 1 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
14639
|
+
}
|
|
14640
|
+
function leveneTest(groups, center = "median") {
|
|
14641
|
+
const k = groups.length;
|
|
14642
|
+
if (k < 2) throw new Error("leveneTest: need at least 2 groups");
|
|
14643
|
+
let N = 0;
|
|
14644
|
+
for (const g of groups) {
|
|
14645
|
+
if (g.length < 1) throw new Error("leveneTest: every group must be non-empty");
|
|
14646
|
+
N += g.length;
|
|
14647
|
+
}
|
|
14648
|
+
const z = groups.map((g) => {
|
|
14649
|
+
const c = center === "median" ? _median(g) : _mean(g);
|
|
14650
|
+
return g.map((x) => Math.abs(x - c));
|
|
14651
|
+
});
|
|
14652
|
+
const allZ = [];
|
|
14653
|
+
for (const zi of z) for (const v of zi) allZ.push(v);
|
|
14654
|
+
const zBarAll = _mean(allZ);
|
|
14655
|
+
let num2 = 0;
|
|
14656
|
+
let den = 0;
|
|
14657
|
+
for (const zi of z) {
|
|
14658
|
+
const zBarI = _mean(zi);
|
|
14659
|
+
num2 += zi.length * (zBarI - zBarAll) * (zBarI - zBarAll);
|
|
14660
|
+
for (const v of zi) den += (v - zBarI) * (v - zBarI);
|
|
14661
|
+
}
|
|
14662
|
+
const W = (N - k) / (k - 1) * (num2 / den);
|
|
14663
|
+
return { statistic: W, pValue: _fPValue(W, k - 1, N - k), degreesOfFreedom: [k - 1, N - k] };
|
|
14664
|
+
}
|
|
14665
|
+
function bartlettTest(groups) {
|
|
14666
|
+
const k = groups.length;
|
|
14667
|
+
if (k < 2) throw new Error("bartlettTest: need at least 2 groups");
|
|
14668
|
+
let N = 0;
|
|
14669
|
+
for (const g of groups) {
|
|
14670
|
+
if (g.length < 2) throw new Error("bartlettTest: every group needs \u22652 observations");
|
|
14671
|
+
N += g.length;
|
|
14672
|
+
}
|
|
14673
|
+
let pooledNum = 0;
|
|
14674
|
+
let sumLnVar = 0;
|
|
14675
|
+
let sumInv = 0;
|
|
14676
|
+
for (const g of groups) {
|
|
14677
|
+
const ni = g.length;
|
|
14678
|
+
const si2 = _variance(g, 1);
|
|
14679
|
+
pooledNum += (ni - 1) * si2;
|
|
14680
|
+
sumLnVar += (ni - 1) * Math.log(si2);
|
|
14681
|
+
sumInv += 1 / (ni - 1);
|
|
14682
|
+
}
|
|
14683
|
+
const sp2 = pooledNum / (N - k);
|
|
14684
|
+
const numerator = (N - k) * Math.log(sp2) - sumLnVar;
|
|
14685
|
+
const C = 1 + (sumInv - 1 / (N - k)) / (3 * (k - 1));
|
|
14686
|
+
const T = numerator / C;
|
|
14687
|
+
return { statistic: T, pValue: _chiSquaredPValue(T, k - 1), degreesOfFreedom: k - 1 };
|
|
14688
|
+
}
|
|
14689
|
+
function studentTTestPaired(sample1, sample2) {
|
|
14690
|
+
const n = sample1.length;
|
|
14691
|
+
if (n < 2 || sample2.length !== n) {
|
|
14692
|
+
throw new Error("studentTTestPaired: samples must be the same length \u2265 2");
|
|
14693
|
+
}
|
|
14694
|
+
const d = sample1.map((x, i) => x - sample2[i]);
|
|
14695
|
+
const dBar = _mean(d);
|
|
14696
|
+
const sd = Math.sqrt(_variance(d, 1));
|
|
14697
|
+
const df = n - 1;
|
|
14698
|
+
const t = dBar / (sd / Math.sqrt(n));
|
|
14699
|
+
return { statistic: t, pValue: _tPValue(t, df), degreesOfFreedom: df };
|
|
14700
|
+
}
|
|
14701
|
+
function proportionZTest(count2, nobs, value) {
|
|
14702
|
+
let z;
|
|
14703
|
+
if (Array.isArray(count2) && Array.isArray(nobs)) {
|
|
14704
|
+
const [c1, c2] = count2;
|
|
14705
|
+
const [n16, n26] = nobs;
|
|
14706
|
+
const p1 = c1 / n16;
|
|
14707
|
+
const p2 = c2 / n26;
|
|
14708
|
+
const pPool = (c1 + c2) / (n16 + n26);
|
|
14709
|
+
z = (p1 - p2) / Math.sqrt(pPool * (1 - pPool) * (1 / n16 + 1 / n26));
|
|
14710
|
+
} else if (typeof count2 === "number" && typeof nobs === "number") {
|
|
14711
|
+
const p0 = value ?? 0.5;
|
|
14712
|
+
const pHat = count2 / nobs;
|
|
14713
|
+
z = (pHat - p0) / Math.sqrt(p0 * (1 - p0) / nobs);
|
|
14714
|
+
} else {
|
|
14715
|
+
throw new Error("proportionZTest: count and nobs must both be numbers or both be [a, b] pairs");
|
|
14716
|
+
}
|
|
14717
|
+
const pValue = erfcScalar(Math.abs(z) / Math.SQRT2);
|
|
14718
|
+
return { statistic: z, pValue };
|
|
14719
|
+
}
|
|
14720
|
+
function _binomPmf(k, n, p) {
|
|
14721
|
+
if (k < 0 || k > n) return 0;
|
|
14722
|
+
const logC = _lgamma3(n + 1) - _lgamma3(k + 1) - _lgamma3(n - k + 1);
|
|
14723
|
+
return Math.exp(logC + k * Math.log(p) + (n - k) * Math.log(1 - p));
|
|
14724
|
+
}
|
|
14725
|
+
function binomialTest(successes, n, p = 0.5) {
|
|
14726
|
+
if (!Number.isInteger(successes) || !Number.isInteger(n) || successes < 0 || successes > n) {
|
|
14727
|
+
throw new Error("binomialTest: successes must be an integer in [0, n]");
|
|
14728
|
+
}
|
|
14729
|
+
const dObs = _binomPmf(successes, n, p);
|
|
14730
|
+
const rel = 1 + 1e-7;
|
|
14731
|
+
let pValue = 0;
|
|
14732
|
+
for (let k = 0; k <= n; k++) {
|
|
14733
|
+
const pk = _binomPmf(k, n, p);
|
|
14734
|
+
if (pk <= dObs * rel) pValue += pk;
|
|
14735
|
+
}
|
|
14736
|
+
return { statistic: successes / n, pValue: Math.min(1, pValue) };
|
|
14737
|
+
}
|
|
14738
|
+
function _skewKurt(a) {
|
|
14739
|
+
const n = a.length;
|
|
14740
|
+
const m = _mean(a);
|
|
14741
|
+
let m2 = 0;
|
|
14742
|
+
let m3 = 0;
|
|
14743
|
+
let m4 = 0;
|
|
14744
|
+
for (const v of a) {
|
|
14745
|
+
const d = v - m;
|
|
14746
|
+
m2 += d * d;
|
|
14747
|
+
m3 += d * d * d;
|
|
14748
|
+
m4 += d * d * d * d;
|
|
14749
|
+
}
|
|
14750
|
+
const mu2 = m2 / n;
|
|
14751
|
+
return { skew: m3 / n / Math.pow(mu2, 1.5), kurtPearson: m4 / n / (mu2 * mu2) };
|
|
14752
|
+
}
|
|
14753
|
+
function andersonDarlingTest(data) {
|
|
14754
|
+
const n = data.length;
|
|
14755
|
+
if (n < 8) throw new Error("andersonDarlingTest: need at least 8 observations");
|
|
14756
|
+
const sorted = data.slice().sort((p2, q) => p2 - q);
|
|
14757
|
+
const mean6 = _mean(sorted);
|
|
14758
|
+
const s = Math.sqrt(_variance(sorted, 1));
|
|
14759
|
+
const phi = (z) => 0.5 * erfcScalar(-z / Math.SQRT2);
|
|
14760
|
+
let sumTerm = 0;
|
|
14761
|
+
for (let i = 0; i < n; i++) {
|
|
14762
|
+
const wi = (sorted[i] - mean6) / s;
|
|
14763
|
+
const wj = (sorted[n - 1 - i] - mean6) / s;
|
|
14764
|
+
sumTerm += (2 * (i + 1) - 1) / n * (Math.log(phi(wi)) + Math.log(1 - phi(wj)));
|
|
14765
|
+
}
|
|
14766
|
+
const a2 = -n - sumTerm;
|
|
14767
|
+
const a2s = a2 * (1 + 0.75 / n + 2.25 / (n * n));
|
|
14768
|
+
let p;
|
|
14769
|
+
if (a2s < 0.2) p = 1 - Math.exp(-13.436 + 101.14 * a2s - 223.73 * a2s * a2s);
|
|
14770
|
+
else if (a2s < 0.34) p = 1 - Math.exp(-8.318 + 42.796 * a2s - 59.938 * a2s * a2s);
|
|
14771
|
+
else if (a2s < 0.6) p = Math.exp(0.9177 - 4.279 * a2s - 1.38 * a2s * a2s);
|
|
14772
|
+
else if (a2s < 10) p = Math.exp(1.2937 - 5.709 * a2s + 0.0186 * a2s * a2s);
|
|
14773
|
+
else p = 0;
|
|
14774
|
+
return { statistic: a2, pValue: Math.max(0, Math.min(1, p)) };
|
|
14775
|
+
}
|
|
14776
|
+
function dagostinoTest(data) {
|
|
14777
|
+
const n = data.length;
|
|
14778
|
+
if (n < 8) throw new Error("dagostinoTest: need at least 8 observations");
|
|
14779
|
+
const { skew, kurtPearson } = _skewKurt(data);
|
|
14780
|
+
const y = skew * Math.sqrt((n + 1) * (n + 3) / (6 * (n - 2)));
|
|
14781
|
+
const beta2 = 3 * (n * n + 27 * n - 70) * (n + 1) * (n + 3) / ((n - 2) * (n + 5) * (n + 7) * (n + 9));
|
|
14782
|
+
const w2 = -1 + Math.sqrt(2 * (beta2 - 1));
|
|
14783
|
+
const delta = 1 / Math.sqrt(0.5 * Math.log(w2));
|
|
14784
|
+
const alpha = Math.sqrt(2 / (w2 - 1));
|
|
14785
|
+
const yy = y === 0 ? 1 : y;
|
|
14786
|
+
const z1 = delta * Math.log(yy / alpha + Math.sqrt(yy / alpha * (yy / alpha) + 1));
|
|
14787
|
+
const E = 3 * (n - 1) / (n + 1);
|
|
14788
|
+
const varb2 = 24 * n * (n - 2) * (n - 3) / ((n + 1) * (n + 1) * (n + 3) * (n + 5));
|
|
14789
|
+
const xx = (kurtPearson - E) / Math.sqrt(varb2);
|
|
14790
|
+
const sqrtbeta1 = 6 * (n * n - 5 * n + 2) / ((n + 7) * (n + 9)) * Math.sqrt(6 * (n + 3) * (n + 5) / (n * (n - 2) * (n - 3)));
|
|
14791
|
+
const A = 6 + 8 / sqrtbeta1 * (2 / sqrtbeta1 + Math.sqrt(1 + 4 / (sqrtbeta1 * sqrtbeta1)));
|
|
14792
|
+
const term1 = 1 - 2 / (9 * A);
|
|
14793
|
+
const denom = 1 + xx * Math.sqrt(2 / (A - 4));
|
|
14794
|
+
const term2 = Math.sign(denom) * Math.cbrt((1 - 2 / A) / Math.abs(denom));
|
|
14795
|
+
const z2 = (term1 - term2) / Math.sqrt(2 / (9 * A));
|
|
14796
|
+
const k2 = z1 * z1 + z2 * z2;
|
|
14797
|
+
return { statistic: k2, pValue: Math.exp(-k2 / 2) };
|
|
14798
|
+
}
|
|
14799
|
+
function friedmanTest(groups) {
|
|
14800
|
+
const k = groups.length;
|
|
14801
|
+
if (k < 3) throw new Error("friedmanTest: need at least 3 related groups");
|
|
14802
|
+
const n = groups[0].length;
|
|
14803
|
+
for (const g of groups)
|
|
14804
|
+
if (g.length !== n) throw new Error("friedmanTest: all groups must be the same length");
|
|
14805
|
+
const rankSums = new Array(k).fill(0);
|
|
14806
|
+
let tieCorrection = 0;
|
|
14807
|
+
for (let block = 0; block < n; block++) {
|
|
14808
|
+
const row2 = groups.map((g) => g[block]);
|
|
14809
|
+
const order = row2.map((_, i) => i).sort((i, j) => row2[i] - row2[j]);
|
|
14810
|
+
const ranks = new Array(k);
|
|
14811
|
+
for (let i = 0; i < k; ) {
|
|
14812
|
+
let j = i;
|
|
14813
|
+
while (j < k && row2[order[j]] === row2[order[i]]) j++;
|
|
14814
|
+
const avg = (i + 1 + j) / 2;
|
|
14815
|
+
const t = j - i;
|
|
14816
|
+
if (t > 1) tieCorrection += t * t * t - t;
|
|
14817
|
+
for (let m = i; m < j; m++) ranks[order[m]] = avg;
|
|
14818
|
+
i = j;
|
|
14819
|
+
}
|
|
14820
|
+
for (let g = 0; g < k; g++) rankSums[g] += ranks[g];
|
|
14821
|
+
}
|
|
14822
|
+
let stat = 12 / (n * k * (k + 1)) * rankSums.reduce((acc, rr) => acc + rr * rr, 0) - 3 * n * (k + 1);
|
|
14823
|
+
const c = 1 - tieCorrection / (n * k * (k * k - 1));
|
|
14824
|
+
if (c !== 0) stat /= c;
|
|
14825
|
+
const df = k - 1;
|
|
14826
|
+
return { statistic: stat, pValue: _chiSquaredPValue(stat, df), degreesOfFreedom: df };
|
|
14827
|
+
}
|
|
14828
|
+
function anova2(data) {
|
|
14829
|
+
const nA = data.length;
|
|
14830
|
+
const nB = data[0].length;
|
|
14831
|
+
const r = data[0][0].length;
|
|
14832
|
+
if (nA < 2 || nB < 2 || r < 2)
|
|
14833
|
+
throw new Error("anova2: need at least 2 levels per factor and 2 replicates");
|
|
14834
|
+
const all = [];
|
|
14835
|
+
for (const row2 of data) for (const cell of row2) for (const v of cell) all.push(v);
|
|
14836
|
+
const grand = _mean(all);
|
|
14837
|
+
const aMeans = data.map((row2) => _mean(row2.flat()));
|
|
14838
|
+
const bMeans = Array.from({ length: nB }, (_, j) => _mean(data.map((row2) => row2[j]).flat()));
|
|
14839
|
+
const cellMeans = data.map((row2) => row2.map((cell) => _mean(cell)));
|
|
14840
|
+
let ssA = 0;
|
|
14841
|
+
for (const am of aMeans) ssA += (am - grand) * (am - grand);
|
|
14842
|
+
ssA *= nB * r;
|
|
14843
|
+
let ssB = 0;
|
|
14844
|
+
for (const bm of bMeans) ssB += (bm - grand) * (bm - grand);
|
|
14845
|
+
ssB *= nA * r;
|
|
14846
|
+
let ssAB = 0;
|
|
14847
|
+
let sse = 0;
|
|
14848
|
+
for (let i = 0; i < nA; i++) {
|
|
14849
|
+
for (let j = 0; j < nB; j++) {
|
|
14850
|
+
const d = cellMeans[i][j] - aMeans[i] - bMeans[j] + grand;
|
|
14851
|
+
ssAB += d * d;
|
|
14852
|
+
for (const v of data[i][j]) sse += (v - cellMeans[i][j]) * (v - cellMeans[i][j]);
|
|
14853
|
+
}
|
|
14854
|
+
}
|
|
14855
|
+
ssAB *= r;
|
|
14856
|
+
const dfA = nA - 1;
|
|
14857
|
+
const dfB = nB - 1;
|
|
14858
|
+
const dfAB = dfA * dfB;
|
|
14859
|
+
const dfE = nA * nB * (r - 1);
|
|
14860
|
+
const mse = sse / dfE;
|
|
14861
|
+
const fA = ssA / dfA / mse;
|
|
14862
|
+
const fB = ssB / dfB / mse;
|
|
14863
|
+
const fAB = ssAB / dfAB / mse;
|
|
14864
|
+
return {
|
|
14865
|
+
factorA: { F: fA, pValue: _fPValue(fA, dfA, dfE), degreesOfFreedom: [dfA, dfE] },
|
|
14866
|
+
factorB: { F: fB, pValue: _fPValue(fB, dfB, dfE), degreesOfFreedom: [dfB, dfE] },
|
|
14867
|
+
interaction: { F: fAB, pValue: _fPValue(fAB, dfAB, dfE), degreesOfFreedom: [dfAB, dfE] }
|
|
14868
|
+
};
|
|
14869
|
+
}
|
|
14870
|
+
function multipleComparison(pValues, method = "bh") {
|
|
14871
|
+
const m = pValues.length;
|
|
14872
|
+
if (m === 0) return [];
|
|
14873
|
+
if (method === "bonferroni") return pValues.map((p) => Math.min(p * m, 1));
|
|
14874
|
+
const order = pValues.map((_, i) => i).sort((i, j) => pValues[i] - pValues[j]);
|
|
14875
|
+
const out = new Array(m);
|
|
14876
|
+
if (method === "holm") {
|
|
14877
|
+
let running = 0;
|
|
14878
|
+
for (let rank2 = 0; rank2 < m; rank2++) {
|
|
14879
|
+
const idx = order[rank2];
|
|
14880
|
+
running = Math.max(running, (m - rank2) * pValues[idx]);
|
|
14881
|
+
out[idx] = Math.min(running, 1);
|
|
14882
|
+
}
|
|
14883
|
+
} else {
|
|
14884
|
+
let prev = 1;
|
|
14885
|
+
for (let rank2 = m - 1; rank2 >= 0; rank2--) {
|
|
14886
|
+
const idx = order[rank2];
|
|
14887
|
+
prev = Math.min(prev, pValues[idx] * m / (rank2 + 1));
|
|
14888
|
+
out[idx] = Math.min(prev, 1);
|
|
14889
|
+
}
|
|
14890
|
+
}
|
|
14891
|
+
return out;
|
|
14892
|
+
}
|
|
14893
|
+
function meanCI(data, confidence = 0.95) {
|
|
14894
|
+
const n = data.length;
|
|
14895
|
+
if (n < 2) throw new Error("meanCI: need at least 2 observations");
|
|
14896
|
+
const m = _mean(data);
|
|
14897
|
+
const se = Math.sqrt(_variance(data, 1) / n);
|
|
14898
|
+
const tcrit = studentTQuantile((1 + confidence) / 2, n - 1);
|
|
14899
|
+
return { estimate: m, lower: m - tcrit * se, upper: m + tcrit * se, confidence };
|
|
14900
|
+
}
|
|
14901
|
+
function proportionCI(successes, n, confidence = 0.95) {
|
|
14902
|
+
if (n <= 0 || successes < 0 || successes > n) {
|
|
14903
|
+
throw new Error("proportionCI: require 0 <= successes <= n, n > 0");
|
|
14904
|
+
}
|
|
14905
|
+
const p = successes / n;
|
|
14906
|
+
const z = normalQuantile((1 + confidence) / 2);
|
|
14907
|
+
const half = z * Math.sqrt(p * (1 - p) / n);
|
|
14908
|
+
return { estimate: p, lower: p - half, upper: p + half, confidence };
|
|
14909
|
+
}
|
|
14910
|
+
function bootstrapCI(data, statistic, opts = {}) {
|
|
14911
|
+
const n = data.length;
|
|
14912
|
+
if (n < 2) throw new Error("bootstrapCI: need at least 2 observations");
|
|
14913
|
+
const confidence = opts.confidence ?? 0.95;
|
|
14914
|
+
const B = opts.resamples ?? 2e3;
|
|
14915
|
+
const rng = opts.seed !== void 0 ? _makeMulberry322(opts.seed) : Math.random;
|
|
14916
|
+
const stats = new Float64Array(B);
|
|
14917
|
+
const resample2 = new Array(n);
|
|
14918
|
+
for (let b = 0; b < B; b++) {
|
|
14919
|
+
for (let i = 0; i < n; i++) resample2[i] = data[Math.floor(rng() * n)];
|
|
14920
|
+
stats[b] = statistic(resample2);
|
|
14921
|
+
}
|
|
14922
|
+
stats.sort();
|
|
14923
|
+
const loIdx = Math.floor((1 - confidence) / 2 * B);
|
|
14924
|
+
const hiIdx = Math.min(B - 1, Math.ceil((1 - (1 - confidence) / 2) * B) - 1);
|
|
14925
|
+
return { estimate: statistic(data), lower: stats[loIdx], upper: stats[hiIdx], confidence };
|
|
14926
|
+
}
|
|
14927
|
+
function permutationTest(a, b, statistic, opts = {}) {
|
|
14928
|
+
const na = a.length;
|
|
14929
|
+
const nb = b.length;
|
|
14930
|
+
const B = opts.resamples ?? 2e3;
|
|
14931
|
+
const rng = opts.seed !== void 0 ? _makeMulberry322(opts.seed) : Math.random;
|
|
14932
|
+
const observed = statistic(a, b);
|
|
14933
|
+
const pool = new Float64Array(na + nb);
|
|
14934
|
+
for (let i = 0; i < na; i++) pool[i] = a[i];
|
|
14935
|
+
for (let i = 0; i < nb; i++) pool[na + i] = b[i];
|
|
14936
|
+
let extreme = 1;
|
|
14937
|
+
for (let p = 0; p < B; p++) {
|
|
14938
|
+
_shuffle(pool, rng);
|
|
14939
|
+
const x = Array.from(pool.subarray(0, na));
|
|
14940
|
+
const y = Array.from(pool.subarray(na));
|
|
14941
|
+
if (Math.abs(statistic(x, y)) >= Math.abs(observed) - 1e-12) extreme++;
|
|
14942
|
+
}
|
|
14943
|
+
return { statistic: observed, pValue: extreme / (B + 1) };
|
|
14944
|
+
}
|
|
14945
|
+
function _matInverse(m) {
|
|
14946
|
+
const n = m.length;
|
|
14947
|
+
const a = m.map((row2, i) => [...row2, ...Array.from({ length: n }, (_, j) => i === j ? 1 : 0)]);
|
|
14948
|
+
for (let col = 0; col < n; col++) {
|
|
14949
|
+
let pivot = col;
|
|
14950
|
+
for (let r = col + 1; r < n; r++) if (Math.abs(a[r][col]) > Math.abs(a[pivot][col])) pivot = r;
|
|
14951
|
+
if (Math.abs(a[pivot][col]) < 1e-15) throw new Error("matrix inverse: singular matrix");
|
|
14952
|
+
[a[col], a[pivot]] = [a[pivot], a[col]];
|
|
14953
|
+
const d = a[col][col];
|
|
14954
|
+
for (let j = 0; j < 2 * n; j++) a[col][j] /= d;
|
|
14955
|
+
for (let r = 0; r < n; r++) {
|
|
14956
|
+
if (r === col) continue;
|
|
14957
|
+
const f = a[r][col];
|
|
14958
|
+
for (let j = 0; j < 2 * n; j++) a[r][j] -= f * a[col][j];
|
|
14959
|
+
}
|
|
14960
|
+
}
|
|
14961
|
+
return a.map((row2) => row2.slice(n));
|
|
14962
|
+
}
|
|
14963
|
+
function _quadForm(d, M) {
|
|
14964
|
+
const n = d.length;
|
|
14965
|
+
let s = 0;
|
|
14966
|
+
for (let i = 0; i < n; i++) {
|
|
14967
|
+
let mi = 0;
|
|
14968
|
+
for (let j = 0; j < n; j++) mi += M[i][j] * d[j];
|
|
14969
|
+
s += d[i] * mi;
|
|
14970
|
+
}
|
|
14971
|
+
return s;
|
|
14972
|
+
}
|
|
14973
|
+
function mahalanobis(u, v, cov2) {
|
|
14974
|
+
if (u.length !== v.length || cov2.length !== u.length) {
|
|
14975
|
+
throw new Error("mahalanobis: u, v, and cov dimensions must match");
|
|
14976
|
+
}
|
|
14977
|
+
const inv2 = _matInverse(cov2);
|
|
14978
|
+
const d = u.map((ui, i) => ui - v[i]);
|
|
14979
|
+
return Math.sqrt(_quadForm(d, inv2));
|
|
14980
|
+
}
|
|
14981
|
+
function hotellingT2(data, mu0) {
|
|
14982
|
+
const n = data.length;
|
|
14983
|
+
const p = mu0.length;
|
|
14984
|
+
if (n <= p) throw new Error("hotellingT2: need more observations than variables");
|
|
14985
|
+
if (data.some((row2) => row2.length !== p))
|
|
14986
|
+
throw new Error("hotellingT2: every row must have length p");
|
|
14987
|
+
const xbar = Array.from({ length: p }, (_, j) => _mean(data.map((row2) => row2[j])));
|
|
14988
|
+
const S = Array.from({ length: p }, () => new Array(p).fill(0));
|
|
14989
|
+
for (const row2 of data) {
|
|
14990
|
+
for (let i = 0; i < p; i++) {
|
|
14991
|
+
for (let j = 0; j < p; j++) S[i][j] += (row2[i] - xbar[i]) * (row2[j] - xbar[j]);
|
|
14992
|
+
}
|
|
14993
|
+
}
|
|
14994
|
+
for (let i = 0; i < p; i++) for (let j = 0; j < p; j++) S[i][j] /= n - 1;
|
|
14995
|
+
const diff2 = xbar.map((x, i) => x - mu0[i]);
|
|
14996
|
+
const t2 = n * _quadForm(diff2, _matInverse(S));
|
|
14997
|
+
const f = (n - p) / (p * (n - 1)) * t2;
|
|
14998
|
+
return {
|
|
14999
|
+
statistic: t2,
|
|
15000
|
+
fStatistic: f,
|
|
15001
|
+
pValue: _fPValue(f, p, n - p),
|
|
15002
|
+
degreesOfFreedom: [p, n - p]
|
|
15003
|
+
};
|
|
15004
|
+
}
|
|
14255
15005
|
|
|
14256
15006
|
// src/typed/matrix-ops.ts
|
|
14257
15007
|
import {
|
|
@@ -33488,7 +34238,7 @@ var createMedian = /* @__PURE__ */ factory(
|
|
|
33488
34238
|
name219,
|
|
33489
34239
|
dependencies219,
|
|
33490
34240
|
({ typed: typed3, add: add2, divide: divide2, compare: compare2, partitionSelect: partitionSelect2 }) => {
|
|
33491
|
-
function
|
|
34241
|
+
function _median2(array) {
|
|
33492
34242
|
try {
|
|
33493
34243
|
const flat = flatten2(array.valueOf());
|
|
33494
34244
|
const num2 = flat.length;
|
|
@@ -33525,7 +34275,7 @@ var createMedian = /* @__PURE__ */ factory(
|
|
|
33525
34275
|
});
|
|
33526
34276
|
return typed3(name219, {
|
|
33527
34277
|
// median([a, b, c, d, ...])
|
|
33528
|
-
"Array | Matrix":
|
|
34278
|
+
"Array | Matrix": _median2,
|
|
33529
34279
|
// median([a, b, c, d, ...], dim)
|
|
33530
34280
|
"Array | Matrix, number | BigNumber": function(_array, _dim) {
|
|
33531
34281
|
throw new Error("median(A, dim) is not yet supported");
|
|
@@ -33535,7 +34285,7 @@ var createMedian = /* @__PURE__ */ factory(
|
|
|
33535
34285
|
if (containsCollections(args)) {
|
|
33536
34286
|
throw new TypeError("Scalar values expected in function median");
|
|
33537
34287
|
}
|
|
33538
|
-
return
|
|
34288
|
+
return _median2(args);
|
|
33539
34289
|
}
|
|
33540
34290
|
});
|
|
33541
34291
|
}
|
|
@@ -42536,6 +43286,186 @@ function corrcoef(matrix2) {
|
|
|
42536
43286
|
(_, i) => Array.from({ length: n }, (_2, j) => c[i][j] / (d[i] * d[j]))
|
|
42537
43287
|
);
|
|
42538
43288
|
}
|
|
43289
|
+
function kendallTau(x, y) {
|
|
43290
|
+
const n = x.length;
|
|
43291
|
+
if (n !== y.length) throw new Error("kendallTau: inputs must have equal length");
|
|
43292
|
+
if (n < 2) return NaN;
|
|
43293
|
+
let P2 = 0;
|
|
43294
|
+
let Q2 = 0;
|
|
43295
|
+
for (let i = 0; i < n; i++) {
|
|
43296
|
+
for (let j = i + 1; j < n; j++) {
|
|
43297
|
+
const s = Math.sign(x[j] - x[i]) * Math.sign(y[j] - y[i]);
|
|
43298
|
+
if (s > 0) P2++;
|
|
43299
|
+
else if (s < 0) Q2++;
|
|
43300
|
+
}
|
|
43301
|
+
}
|
|
43302
|
+
const n0 = n * (n - 1) / 2;
|
|
43303
|
+
const tiePairs = (arr7) => {
|
|
43304
|
+
const counts = /* @__PURE__ */ new Map();
|
|
43305
|
+
for (const v of arr7) counts.set(v, (counts.get(v) ?? 0) + 1);
|
|
43306
|
+
let s = 0;
|
|
43307
|
+
for (const c of counts.values()) s += c * (c - 1) / 2;
|
|
43308
|
+
return s;
|
|
43309
|
+
};
|
|
43310
|
+
const n16 = tiePairs(x);
|
|
43311
|
+
const n26 = tiePairs(y);
|
|
43312
|
+
const denom = Math.sqrt((n0 - n16) * (n0 - n26));
|
|
43313
|
+
if (denom === 0) return NaN;
|
|
43314
|
+
return (P2 - Q2) / denom;
|
|
43315
|
+
}
|
|
43316
|
+
function _tTwoTail(t, df) {
|
|
43317
|
+
return 2 * (1 - studentTCDF(Math.abs(t), df));
|
|
43318
|
+
}
|
|
43319
|
+
function linregress(x, y) {
|
|
43320
|
+
const n = x.length;
|
|
43321
|
+
if (n !== y.length) throw new Error("linregress: x and y must have equal length");
|
|
43322
|
+
if (n < 3) throw new Error("linregress: need at least 3 points for inference");
|
|
43323
|
+
const xa = Array.from(x);
|
|
43324
|
+
const ya = Array.from(y);
|
|
43325
|
+
const xbar = mean2(xa);
|
|
43326
|
+
const ybar = mean2(ya);
|
|
43327
|
+
let sxx = 0;
|
|
43328
|
+
let sxy = 0;
|
|
43329
|
+
let syy = 0;
|
|
43330
|
+
for (let i = 0; i < n; i++) {
|
|
43331
|
+
const dx = xa[i] - xbar;
|
|
43332
|
+
const dy = ya[i] - ybar;
|
|
43333
|
+
sxx += dx * dx;
|
|
43334
|
+
sxy += dx * dy;
|
|
43335
|
+
syy += dy * dy;
|
|
43336
|
+
}
|
|
43337
|
+
const slope = sxy / sxx;
|
|
43338
|
+
const intercept = ybar - slope * xbar;
|
|
43339
|
+
const rValue = sxy / Math.sqrt(sxx * syy);
|
|
43340
|
+
const df = n - 2;
|
|
43341
|
+
const s2 = (syy - slope * sxy) / df;
|
|
43342
|
+
const stdErr = Math.sqrt(s2 / sxx);
|
|
43343
|
+
const interceptStdErr = Math.sqrt(s2 * (1 / n + xbar * xbar / sxx));
|
|
43344
|
+
return {
|
|
43345
|
+
slope,
|
|
43346
|
+
intercept,
|
|
43347
|
+
rValue,
|
|
43348
|
+
pValue: _tTwoTail(slope / stdErr, df),
|
|
43349
|
+
stdErr,
|
|
43350
|
+
interceptStdErr
|
|
43351
|
+
};
|
|
43352
|
+
}
|
|
43353
|
+
function _corrTPValue(r, n) {
|
|
43354
|
+
const df = n - 2;
|
|
43355
|
+
if (df <= 0 || Math.abs(r) >= 1) return Math.abs(r) >= 1 ? 0 : 1;
|
|
43356
|
+
const t = r * Math.sqrt(df / (1 - r * r));
|
|
43357
|
+
return _tTwoTail(t, df);
|
|
43358
|
+
}
|
|
43359
|
+
function _pearson(x, y) {
|
|
43360
|
+
const xa = Array.from(x);
|
|
43361
|
+
const ya = Array.from(y);
|
|
43362
|
+
const n = xa.length;
|
|
43363
|
+
if (n !== ya.length) throw new Error("pearsonr: inputs must have equal length");
|
|
43364
|
+
const mx = mean2(xa);
|
|
43365
|
+
const my = mean2(ya);
|
|
43366
|
+
let sxy = 0;
|
|
43367
|
+
let sxx = 0;
|
|
43368
|
+
let syy = 0;
|
|
43369
|
+
for (let i = 0; i < n; i++) {
|
|
43370
|
+
const dx = xa[i] - mx;
|
|
43371
|
+
const dy = ya[i] - my;
|
|
43372
|
+
sxy += dx * dy;
|
|
43373
|
+
sxx += dx * dx;
|
|
43374
|
+
syy += dy * dy;
|
|
43375
|
+
}
|
|
43376
|
+
return sxy / Math.sqrt(sxx * syy);
|
|
43377
|
+
}
|
|
43378
|
+
function pearsonr(x, y) {
|
|
43379
|
+
const r = _pearson(x, y);
|
|
43380
|
+
return { coefficient: r, pValue: _corrTPValue(r, x.length) };
|
|
43381
|
+
}
|
|
43382
|
+
function spearmanr(x, y) {
|
|
43383
|
+
const rho = spearman(x, y);
|
|
43384
|
+
return { coefficient: rho, pValue: _corrTPValue(rho, x.length) };
|
|
43385
|
+
}
|
|
43386
|
+
function kendalltau(x, y) {
|
|
43387
|
+
const tau2 = kendallTau(x, y);
|
|
43388
|
+
const n = x.length;
|
|
43389
|
+
const z = 3 * tau2 * Math.sqrt(n * (n - 1)) / Math.sqrt(2 * (2 * n + 5));
|
|
43390
|
+
return { coefficient: tau2, pValue: 2 * (1 - normalCDF(Math.abs(z))) };
|
|
43391
|
+
}
|
|
43392
|
+
function ptp(x) {
|
|
43393
|
+
const a = Array.from(x);
|
|
43394
|
+
if (a.length === 0) return NaN;
|
|
43395
|
+
let lo = a[0];
|
|
43396
|
+
let hi = a[0];
|
|
43397
|
+
for (const v of a) {
|
|
43398
|
+
if (v < lo) lo = v;
|
|
43399
|
+
if (v > hi) hi = v;
|
|
43400
|
+
}
|
|
43401
|
+
return hi - lo;
|
|
43402
|
+
}
|
|
43403
|
+
function variation(x) {
|
|
43404
|
+
const a = Array.from(x);
|
|
43405
|
+
return stdPop(a) / mean2(a);
|
|
43406
|
+
}
|
|
43407
|
+
function trimmedMean(x, proportion) {
|
|
43408
|
+
if (proportion < 0 || proportion >= 0.5) {
|
|
43409
|
+
throw new Error("trimmedMean: proportion must be in [0, 0.5)");
|
|
43410
|
+
}
|
|
43411
|
+
const a = Array.from(x).sort((p, q) => p - q);
|
|
43412
|
+
const k = Math.floor(a.length * proportion);
|
|
43413
|
+
return mean2(a.slice(k, a.length - k));
|
|
43414
|
+
}
|
|
43415
|
+
function describe(x) {
|
|
43416
|
+
const a = Array.from(x);
|
|
43417
|
+
const n = a.length;
|
|
43418
|
+
if (n < 1) throw new Error("describe: input must be non-empty");
|
|
43419
|
+
const m = mean2(a);
|
|
43420
|
+
let m2 = 0;
|
|
43421
|
+
let m3 = 0;
|
|
43422
|
+
let m4 = 0;
|
|
43423
|
+
let lo = a[0];
|
|
43424
|
+
let hi = a[0];
|
|
43425
|
+
for (const v of a) {
|
|
43426
|
+
const d = v - m;
|
|
43427
|
+
m2 += d * d;
|
|
43428
|
+
m3 += d * d * d;
|
|
43429
|
+
m4 += d * d * d * d;
|
|
43430
|
+
if (v < lo) lo = v;
|
|
43431
|
+
if (v > hi) hi = v;
|
|
43432
|
+
}
|
|
43433
|
+
const mu2 = m2 / n;
|
|
43434
|
+
return {
|
|
43435
|
+
nobs: n,
|
|
43436
|
+
min: lo,
|
|
43437
|
+
max: hi,
|
|
43438
|
+
mean: m,
|
|
43439
|
+
variance: m2 / (n - 1),
|
|
43440
|
+
skewness: m3 / n / Math.pow(mu2, 1.5),
|
|
43441
|
+
kurtosis: m4 / n / (mu2 * mu2) - 3
|
|
43442
|
+
};
|
|
43443
|
+
}
|
|
43444
|
+
function histogram(x, bins = 10) {
|
|
43445
|
+
const a = Array.from(x);
|
|
43446
|
+
if (a.length === 0) throw new Error("histogram: input must be non-empty");
|
|
43447
|
+
if (!Number.isInteger(bins) || bins < 1)
|
|
43448
|
+
throw new Error("histogram: bins must be a positive integer");
|
|
43449
|
+
let lo = a[0];
|
|
43450
|
+
let hi = a[0];
|
|
43451
|
+
for (const v of a) {
|
|
43452
|
+
if (v < lo) lo = v;
|
|
43453
|
+
if (v > hi) hi = v;
|
|
43454
|
+
}
|
|
43455
|
+
if (lo === hi) {
|
|
43456
|
+
lo -= 0.5;
|
|
43457
|
+
hi += 0.5;
|
|
43458
|
+
}
|
|
43459
|
+
const width = (hi - lo) / bins;
|
|
43460
|
+
const edges = Array.from({ length: bins + 1 }, (_, i) => lo + i * width);
|
|
43461
|
+
const counts = new Array(bins).fill(0);
|
|
43462
|
+
for (const v of a) {
|
|
43463
|
+
let idx = Math.floor((v - lo) / width);
|
|
43464
|
+
if (idx === bins) idx = bins - 1;
|
|
43465
|
+
if (idx >= 0 && idx < bins) counts[idx]++;
|
|
43466
|
+
}
|
|
43467
|
+
return { counts, edges };
|
|
43468
|
+
}
|
|
42539
43469
|
|
|
42540
43470
|
// src/numeric-extra.ts
|
|
42541
43471
|
var toArr = (x) => Array.isArray(x) ? x : Array.from(x);
|
|
@@ -42621,31 +43551,6 @@ function cumtrapz(y, x) {
|
|
|
42621
43551
|
return out;
|
|
42622
43552
|
}
|
|
42623
43553
|
|
|
42624
|
-
// src/distribution-functions.ts
|
|
42625
|
-
var normalQuantile = (p, mu = 0, sigma = 1) => normalDist(mu, sigma).quantile(p);
|
|
42626
|
-
var studentTCDF = (x, df) => tDist(df).cdf(x);
|
|
42627
|
-
var studentTQuantile = (p, df) => tDist(df).quantile(p);
|
|
42628
|
-
var chiSquaredCDF = (x, df) => chiSquaredDist(df).cdf(x);
|
|
42629
|
-
var chiSquaredQuantile = (p, df) => chiSquaredDist(df).quantile(p);
|
|
42630
|
-
var fCDF = (x, d1, d2) => fDist(d1, d2).cdf(x);
|
|
42631
|
-
var fQuantile = (p, d1, d2) => fDist(d1, d2).quantile(p);
|
|
42632
|
-
var gammaCDF = (x, shape, rate = 1) => gammaDist(shape, rate).cdf(x);
|
|
42633
|
-
var gammaQuantile = (p, shape, rate = 1) => gammaDist(shape, rate).quantile(p);
|
|
42634
|
-
var betaCDF = (x, a, b) => betaDist(a, b).cdf(x);
|
|
42635
|
-
var betaQuantile = (p, a, b) => betaDist(a, b).quantile(p);
|
|
42636
|
-
var cauchyPDF = (x, x0 = 0, gamma2 = 1) => 1 / (Math.PI * gamma2 * (1 + ((x - x0) / gamma2) ** 2));
|
|
42637
|
-
var cauchyCDF = (x, x0 = 0, gamma2 = 1) => 0.5 + Math.atan((x - x0) / gamma2) / Math.PI;
|
|
42638
|
-
var cauchyQuantile = (p, x0 = 0, gamma2 = 1) => x0 + gamma2 * Math.tan(Math.PI * (p - 0.5));
|
|
42639
|
-
var laplacePDF = (x, mu = 0, b = 1) => Math.exp(-Math.abs(x - mu) / b) / (2 * b);
|
|
42640
|
-
var laplaceCDF = (x, mu = 0, b = 1) => x < mu ? 0.5 * Math.exp((x - mu) / b) : 1 - 0.5 * Math.exp(-(x - mu) / b);
|
|
42641
|
-
var laplaceQuantile = (p, mu = 0, b = 1) => p < 0.5 ? mu + b * Math.log(2 * p) : mu - b * Math.log(2 - 2 * p);
|
|
42642
|
-
var logisticPDF = (x, mu = 0, s = 1) => {
|
|
42643
|
-
const e = Math.exp(-(x - mu) / s);
|
|
42644
|
-
return e / (s * (1 + e) ** 2);
|
|
42645
|
-
};
|
|
42646
|
-
var logisticCDF = (x, mu = 0, s = 1) => 1 / (1 + Math.exp(-(x - mu) / s));
|
|
42647
|
-
var logisticQuantile = (p, mu = 0, s = 1) => mu + s * Math.log(p / (1 - p));
|
|
42648
|
-
|
|
42649
43554
|
// src/hypothesis-extra.ts
|
|
42650
43555
|
var arr2 = (x) => Array.isArray(x) ? x : Array.from(x);
|
|
42651
43556
|
var sampleVar = (x) => variance(x);
|
|
@@ -43695,9 +44600,11 @@ export {
|
|
|
43695
44600
|
airyAi,
|
|
43696
44601
|
airyBi,
|
|
43697
44602
|
and,
|
|
44603
|
+
andersonDarlingTest,
|
|
43698
44604
|
angle2D,
|
|
43699
44605
|
angle3D,
|
|
43700
44606
|
anova,
|
|
44607
|
+
anova2,
|
|
43701
44608
|
apart,
|
|
43702
44609
|
mapSlices as apply,
|
|
43703
44610
|
area,
|
|
@@ -43716,6 +44623,7 @@ export {
|
|
|
43716
44623
|
avogadro,
|
|
43717
44624
|
bandpassFilter,
|
|
43718
44625
|
bartlettPSD,
|
|
44626
|
+
bartlettTest,
|
|
43719
44627
|
bellNumbers,
|
|
43720
44628
|
bernoulli,
|
|
43721
44629
|
bernoulliPMF,
|
|
@@ -43740,6 +44648,7 @@ export {
|
|
|
43740
44648
|
bin,
|
|
43741
44649
|
binomialDist,
|
|
43742
44650
|
binomialPMF,
|
|
44651
|
+
binomialTest,
|
|
43743
44652
|
bitAnd,
|
|
43744
44653
|
bitNot,
|
|
43745
44654
|
bitOr,
|
|
@@ -43748,6 +44657,7 @@ export {
|
|
|
43748
44657
|
bohrRadius,
|
|
43749
44658
|
boltzmann,
|
|
43750
44659
|
boolean,
|
|
44660
|
+
bootstrapCI,
|
|
43751
44661
|
bspline,
|
|
43752
44662
|
butter,
|
|
43753
44663
|
cancel,
|
|
@@ -43846,12 +44756,14 @@ export {
|
|
|
43846
44756
|
cumtrapz,
|
|
43847
44757
|
curl,
|
|
43848
44758
|
curvefit,
|
|
44759
|
+
dagostinoTest,
|
|
43849
44760
|
dct,
|
|
43850
44761
|
deepEqual,
|
|
43851
44762
|
degree,
|
|
43852
44763
|
delaunayTriangulation,
|
|
43853
44764
|
derivative,
|
|
43854
44765
|
derivativeAt,
|
|
44766
|
+
describe,
|
|
43855
44767
|
det,
|
|
43856
44768
|
detrend,
|
|
43857
44769
|
deuteronMass,
|
|
@@ -43860,6 +44772,7 @@ export {
|
|
|
43860
44772
|
differences,
|
|
43861
44773
|
digamma,
|
|
43862
44774
|
directionalDerivative,
|
|
44775
|
+
discreteUniformDist,
|
|
43863
44776
|
discriminant,
|
|
43864
44777
|
distance,
|
|
43865
44778
|
distance2D,
|
|
@@ -44009,6 +44922,7 @@ export {
|
|
|
44009
44922
|
freqz,
|
|
44010
44923
|
fresnelC,
|
|
44011
44924
|
fresnelS,
|
|
44925
|
+
friedmanTest,
|
|
44012
44926
|
fullSimplify,
|
|
44013
44927
|
functionExpand,
|
|
44014
44928
|
fuseUnaryChain,
|
|
@@ -44044,6 +44958,7 @@ export {
|
|
|
44044
44958
|
griddata,
|
|
44045
44959
|
groebnerBasis,
|
|
44046
44960
|
groupDelay,
|
|
44961
|
+
gumbelDist,
|
|
44047
44962
|
harmonicNumber,
|
|
44048
44963
|
hartreeEnergy,
|
|
44049
44964
|
hasNumericValue,
|
|
@@ -44056,7 +44971,10 @@ export {
|
|
|
44056
44971
|
hex,
|
|
44057
44972
|
highpassFilter,
|
|
44058
44973
|
hilbertTransform,
|
|
44974
|
+
histogram,
|
|
44059
44975
|
hmean,
|
|
44976
|
+
hotellingT2,
|
|
44977
|
+
hypergeometricDist,
|
|
44060
44978
|
hypot,
|
|
44061
44979
|
idct,
|
|
44062
44980
|
identity,
|
|
@@ -44077,6 +44995,7 @@ export {
|
|
|
44077
44995
|
intersectSegments2D,
|
|
44078
44996
|
inv,
|
|
44079
44997
|
invFourier,
|
|
44998
|
+
invGaussDist,
|
|
44080
44999
|
inverseConductanceQuantum,
|
|
44081
45000
|
inverseLaplace,
|
|
44082
45001
|
inverseLaplaceTransform,
|
|
@@ -44100,9 +45019,12 @@ export {
|
|
|
44100
45019
|
jsDivergence,
|
|
44101
45020
|
kdTree,
|
|
44102
45021
|
kdTreeNearest,
|
|
45022
|
+
kendallTau,
|
|
45023
|
+
kendalltau,
|
|
44103
45024
|
kldivergence,
|
|
44104
45025
|
klitzing,
|
|
44105
45026
|
kmeans,
|
|
45027
|
+
kolmogorovSmirnov2Test,
|
|
44106
45028
|
kolmogorovSmirnovTest,
|
|
44107
45029
|
kron,
|
|
44108
45030
|
kruskalWallis,
|
|
@@ -44125,6 +45047,7 @@ export {
|
|
|
44125
45047
|
legendreFit,
|
|
44126
45048
|
legendreP,
|
|
44127
45049
|
levenbergMarquardt,
|
|
45050
|
+
leveneTest,
|
|
44128
45051
|
lfilter,
|
|
44129
45052
|
lfilterZi,
|
|
44130
45053
|
lgamma,
|
|
@@ -44132,6 +45055,7 @@ export {
|
|
|
44132
45055
|
linearInterp,
|
|
44133
45056
|
linearRegression,
|
|
44134
45057
|
linprog,
|
|
45058
|
+
linregress,
|
|
44135
45059
|
linsolve,
|
|
44136
45060
|
loess,
|
|
44137
45061
|
log,
|
|
@@ -44159,6 +45083,7 @@ export {
|
|
|
44159
45083
|
mad,
|
|
44160
45084
|
magneticConstant,
|
|
44161
45085
|
magneticFluxQuantum,
|
|
45086
|
+
mahalanobis,
|
|
44162
45087
|
manhattanDistance,
|
|
44163
45088
|
mannWhitneyTest,
|
|
44164
45089
|
map,
|
|
@@ -44179,6 +45104,7 @@ export {
|
|
|
44179
45104
|
maxSelect,
|
|
44180
45105
|
maximize,
|
|
44181
45106
|
mean,
|
|
45107
|
+
meanCI,
|
|
44182
45108
|
medfilt,
|
|
44183
45109
|
median,
|
|
44184
45110
|
medianSelect,
|
|
@@ -44199,10 +45125,13 @@ export {
|
|
|
44199
45125
|
movingAverage,
|
|
44200
45126
|
multiTaperPSD,
|
|
44201
45127
|
multinomial,
|
|
45128
|
+
multipleComparison,
|
|
44202
45129
|
multiply,
|
|
44203
45130
|
multiplyScalar,
|
|
45131
|
+
multivariateNormal,
|
|
44204
45132
|
multivariateTaylor,
|
|
44205
45133
|
nearestNeighbor,
|
|
45134
|
+
negativeBinomialDist,
|
|
44206
45135
|
nelderMead,
|
|
44207
45136
|
neutronMass,
|
|
44208
45137
|
newtonInterp,
|
|
@@ -44259,6 +45188,7 @@ export {
|
|
|
44259
45188
|
parallelStatSum,
|
|
44260
45189
|
parallelStatVariance,
|
|
44261
45190
|
parallelXCorr,
|
|
45191
|
+
paretoDist,
|
|
44262
45192
|
parse,
|
|
44263
45193
|
parseNumberWithConfig,
|
|
44264
45194
|
parser,
|
|
@@ -44267,7 +45197,9 @@ export {
|
|
|
44267
45197
|
partitions,
|
|
44268
45198
|
pchip,
|
|
44269
45199
|
pchipInterp,
|
|
45200
|
+
pearsonr,
|
|
44270
45201
|
periodogram,
|
|
45202
|
+
permutationTest,
|
|
44271
45203
|
permutations,
|
|
44272
45204
|
pickRandom,
|
|
44273
45205
|
piecewise,
|
|
@@ -44304,7 +45236,10 @@ export {
|
|
|
44304
45236
|
print,
|
|
44305
45237
|
prod,
|
|
44306
45238
|
projectVector,
|
|
45239
|
+
proportionCI,
|
|
45240
|
+
proportionZTest,
|
|
44307
45241
|
protonMass,
|
|
45242
|
+
ptp,
|
|
44308
45243
|
qr,
|
|
44309
45244
|
quadprog,
|
|
44310
45245
|
quantileSeq,
|
|
@@ -44323,6 +45258,7 @@ export {
|
|
|
44323
45258
|
rank,
|
|
44324
45259
|
rankdata,
|
|
44325
45260
|
rationalize,
|
|
45261
|
+
rayleighDist,
|
|
44326
45262
|
rbfInterpolate,
|
|
44327
45263
|
re,
|
|
44328
45264
|
reduce,
|
|
@@ -44398,6 +45334,7 @@ export {
|
|
|
44398
45334
|
sort,
|
|
44399
45335
|
sparse,
|
|
44400
45336
|
spearman,
|
|
45337
|
+
spearmanr,
|
|
44401
45338
|
spectralClustering,
|
|
44402
45339
|
spectrogram,
|
|
44403
45340
|
speedOfLight,
|
|
@@ -44416,6 +45353,7 @@ export {
|
|
|
44416
45353
|
studentTPDF,
|
|
44417
45354
|
studentTQuantile,
|
|
44418
45355
|
studentTTest,
|
|
45356
|
+
studentTTestPaired,
|
|
44419
45357
|
studentizedRangeCDF,
|
|
44420
45358
|
studentizedRangeQuantile,
|
|
44421
45359
|
subfactorial,
|
|
@@ -44452,10 +45390,12 @@ export {
|
|
|
44452
45390
|
trapz,
|
|
44453
45391
|
trapzF64,
|
|
44454
45392
|
triangleArea,
|
|
45393
|
+
triangularDist,
|
|
44455
45394
|
trigExpand,
|
|
44456
45395
|
trigReduce,
|
|
44457
45396
|
trigToExp,
|
|
44458
45397
|
tril,
|
|
45398
|
+
trimmedMean,
|
|
44459
45399
|
triu,
|
|
44460
45400
|
tukeyHSD,
|
|
44461
45401
|
typeOf2 as typeOf,
|
|
@@ -44489,6 +45429,7 @@ export {
|
|
|
44489
45429
|
vander,
|
|
44490
45430
|
variables,
|
|
44491
45431
|
variance,
|
|
45432
|
+
variation,
|
|
44492
45433
|
voronoiDiagram,
|
|
44493
45434
|
weakMixingAngle,
|
|
44494
45435
|
weibullDist,
|