@ac-kit/math-stats 0.1.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 +22 -0
- package/LICENSE +28 -0
- package/README.md +91 -0
- package/dist/_assert-probability.d.ts +14 -0
- package/dist/_assert-probability.d.ts.map +1 -0
- package/dist/_assert-probability.js +18 -0
- package/dist/_assert-probability.js.map +1 -0
- package/dist/_discrete-quantile.d.ts +16 -0
- package/dist/_discrete-quantile.d.ts.map +1 -0
- package/dist/_discrete-quantile.js +43 -0
- package/dist/_discrete-quantile.js.map +1 -0
- package/dist/_inverse-transform-sample.d.ts +17 -0
- package/dist/_inverse-transform-sample.d.ts.map +1 -0
- package/dist/_inverse-transform-sample.js +18 -0
- package/dist/_inverse-transform-sample.js.map +1 -0
- package/dist/_log-binomial.d.ts +16 -0
- package/dist/_log-binomial.d.ts.map +1 -0
- package/dist/_log-binomial.js +19 -0
- package/dist/_log-binomial.js.map +1 -0
- package/dist/average.d.ts +95 -0
- package/dist/average.d.ts.map +1 -0
- package/dist/average.js +113 -0
- package/dist/average.js.map +1 -0
- package/dist/beta-distribution.d.ts +15 -0
- package/dist/beta-distribution.d.ts.map +1 -0
- package/dist/beta-distribution.js +61 -0
- package/dist/beta-distribution.js.map +1 -0
- package/dist/binomial-distribution.d.ts +18 -0
- package/dist/binomial-distribution.d.ts.map +1 -0
- package/dist/binomial-distribution.js +74 -0
- package/dist/binomial-distribution.js.map +1 -0
- package/dist/chi-squared-distribution.d.ts +15 -0
- package/dist/chi-squared-distribution.d.ts.map +1 -0
- package/dist/chi-squared-distribution.js +25 -0
- package/dist/chi-squared-distribution.js.map +1 -0
- package/dist/chi-squared-test.d.ts +39 -0
- package/dist/chi-squared-test.d.ts.map +1 -0
- package/dist/chi-squared-test.js +56 -0
- package/dist/chi-squared-test.js.map +1 -0
- package/dist/confidence-interval.d.ts +62 -0
- package/dist/confidence-interval.d.ts.map +1 -0
- package/dist/confidence-interval.js +93 -0
- package/dist/confidence-interval.js.map +1 -0
- package/dist/dispersion.d.ts +45 -0
- package/dist/dispersion.d.ts.map +1 -0
- package/dist/dispersion.js +80 -0
- package/dist/dispersion.js.map +1 -0
- package/dist/distribution.d.ts +61 -0
- package/dist/distribution.d.ts.map +1 -0
- package/dist/distribution.js +2 -0
- package/dist/distribution.js.map +1 -0
- package/dist/exponential-distribution.d.ts +15 -0
- package/dist/exponential-distribution.d.ts.map +1 -0
- package/dist/exponential-distribution.js +33 -0
- package/dist/exponential-distribution.js.map +1 -0
- package/dist/f-distribution.d.ts +17 -0
- package/dist/f-distribution.d.ts.map +1 -0
- package/dist/f-distribution.js +88 -0
- package/dist/f-distribution.js.map +1 -0
- package/dist/gamma-distribution.d.ts +15 -0
- package/dist/gamma-distribution.d.ts.map +1 -0
- package/dist/gamma-distribution.js +45 -0
- package/dist/gamma-distribution.js.map +1 -0
- package/dist/geometric-distribution.d.ts +19 -0
- package/dist/geometric-distribution.d.ts.map +1 -0
- package/dist/geometric-distribution.js +54 -0
- package/dist/geometric-distribution.js.map +1 -0
- package/dist/hierarchical-cluster.d.ts +35 -0
- package/dist/hierarchical-cluster.d.ts.map +1 -0
- package/dist/hierarchical-cluster.js +97 -0
- package/dist/hierarchical-cluster.js.map +1 -0
- package/dist/hypergeometric-distribution.d.ts +18 -0
- package/dist/hypergeometric-distribution.d.ts.map +1 -0
- package/dist/hypergeometric-distribution.js +92 -0
- package/dist/hypergeometric-distribution.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/kmeans.d.ts +87 -0
- package/dist/kmeans.d.ts.map +1 -0
- package/dist/kmeans.js +126 -0
- package/dist/kmeans.js.map +1 -0
- package/dist/kmedoids.d.ts +27 -0
- package/dist/kmedoids.d.ts.map +1 -0
- package/dist/kmedoids.js +47 -0
- package/dist/kmedoids.js.map +1 -0
- package/dist/log-normal-distribution.d.ts +15 -0
- package/dist/log-normal-distribution.d.ts.map +1 -0
- package/dist/log-normal-distribution.js +44 -0
- package/dist/log-normal-distribution.js.map +1 -0
- package/dist/negative-binomial-distribution.d.ts +18 -0
- package/dist/negative-binomial-distribution.d.ts.map +1 -0
- package/dist/negative-binomial-distribution.js +64 -0
- package/dist/negative-binomial-distribution.js.map +1 -0
- package/dist/normal-distribution.d.ts +16 -0
- package/dist/normal-distribution.d.ts.map +1 -0
- package/dist/normal-distribution.js +41 -0
- package/dist/normal-distribution.js.map +1 -0
- package/dist/normal-quantile.d.ts +19 -0
- package/dist/normal-quantile.d.ts.map +1 -0
- package/dist/normal-quantile.js +115 -0
- package/dist/normal-quantile.js.map +1 -0
- package/dist/one-way-anova.d.ts +31 -0
- package/dist/one-way-anova.d.ts.map +1 -0
- package/dist/one-way-anova.js +69 -0
- package/dist/one-way-anova.js.map +1 -0
- package/dist/outliers.d.ts +20 -0
- package/dist/outliers.d.ts.map +1 -0
- package/dist/outliers.js +49 -0
- package/dist/outliers.js.map +1 -0
- package/dist/poisson-distribution.d.ts +15 -0
- package/dist/poisson-distribution.d.ts.map +1 -0
- package/dist/poisson-distribution.js +44 -0
- package/dist/poisson-distribution.js.map +1 -0
- package/dist/quantile.d.ts +9 -0
- package/dist/quantile.d.ts.map +1 -0
- package/dist/quantile.js +23 -0
- package/dist/quantile.js.map +1 -0
- package/dist/random-effects.d.ts +45 -0
- package/dist/random-effects.d.ts.map +1 -0
- package/dist/random-effects.js +97 -0
- package/dist/random-effects.js.map +1 -0
- package/dist/regression.d.ts +12 -0
- package/dist/regression.d.ts.map +1 -0
- package/dist/regression.js +35 -0
- package/dist/regression.js.map +1 -0
- package/dist/student-t-distribution.d.ts +16 -0
- package/dist/student-t-distribution.d.ts.map +1 -0
- package/dist/student-t-distribution.js +54 -0
- package/dist/student-t-distribution.js.map +1 -0
- package/dist/sum.d.ts +31 -0
- package/dist/sum.d.ts.map +1 -0
- package/dist/sum.js +30 -0
- package/dist/sum.js.map +1 -0
- package/dist/t-test.d.ts +52 -0
- package/dist/t-test.d.ts.map +1 -0
- package/dist/t-test.js +81 -0
- package/dist/t-test.js.map +1 -0
- package/dist/trend.d.ts +55 -0
- package/dist/trend.d.ts.map +1 -0
- package/dist/trend.js +98 -0
- package/dist/trend.js.map +1 -0
- package/dist/uncertainty.d.ts +25 -0
- package/dist/uncertainty.d.ts.map +1 -0
- package/dist/uncertainty.js +26 -0
- package/dist/uncertainty.js.map +1 -0
- package/dist/uniform-distribution.d.ts +11 -0
- package/dist/uniform-distribution.d.ts.map +1 -0
- package/dist/uniform-distribution.js +33 -0
- package/dist/uniform-distribution.js.map +1 -0
- package/dist/welch-t-statistic.d.ts +24 -0
- package/dist/welch-t-statistic.d.ts.map +1 -0
- package/dist/welch-t-statistic.js +33 -0
- package/dist/welch-t-statistic.js.map +1 -0
- package/package.json +66 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { erfc } from "@ac-kit/math-analysis";
|
|
2
|
+
import { SQRT1_2, SQRT_TWO_PI } from "@ac-kit/math-scalar";
|
|
3
|
+
// Coefficients of Acklam's rational approximation to the inverse standard
|
|
4
|
+
// normal CDF, as published in "An algorithm for computing the inverse normal
|
|
5
|
+
// cumulative distribution function" (Peter J. Acklam). The approximation is
|
|
6
|
+
// split into a central region and a tail region, each with its own rational
|
|
7
|
+
// function.
|
|
8
|
+
const CENTRAL_NUMERATOR = [
|
|
9
|
+
-39.69683028665376, 220.9460984245205, -275.9285104469687,
|
|
10
|
+
138.357751867269, -30.66479806614716, 2.506628277459239,
|
|
11
|
+
];
|
|
12
|
+
const CENTRAL_DENOMINATOR = [
|
|
13
|
+
-54.47609879822406, 161.5858368580409, -155.6989798598866,
|
|
14
|
+
66.80131188771972, -13.28068155288572,
|
|
15
|
+
];
|
|
16
|
+
const TAIL_NUMERATOR = [
|
|
17
|
+
-0.007784894002430293, -0.3223964580411365, -2.400758277161838,
|
|
18
|
+
-2.549732539343734, 4.374664141464968, 2.938163982698783,
|
|
19
|
+
];
|
|
20
|
+
const TAIL_DENOMINATOR = [
|
|
21
|
+
0.007784695709041462, 0.3224671290700398, 2.445134137142996,
|
|
22
|
+
3.754408661907416,
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Boundary between the central and tail regions of the approximation, chosen by
|
|
26
|
+
* Acklam so both rational functions stay in their accurate range.
|
|
27
|
+
*/
|
|
28
|
+
const TAIL_CUTOFF = 0.02425;
|
|
29
|
+
/**
|
|
30
|
+
* Inverse of the standard normal cumulative distribution function (the probit
|
|
31
|
+
* function): the value `x` such that a standard normal variable falls at or
|
|
32
|
+
* below `x` with probability `probability`.
|
|
33
|
+
*
|
|
34
|
+
* Acklam's rational approximation, whose relative error is bounded by 1.15e-9,
|
|
35
|
+
* refined by a single Halley step against `erfc`. That step triples the correct
|
|
36
|
+
* digits, so the result is accurate to the last few bits rather than to nine
|
|
37
|
+
* decimals — which matters because the error is _relative_, and the absolute
|
|
38
|
+
* error of the bare approximation grows with the quantile, reaching about 7e-9
|
|
39
|
+
* out at `p = 1e-12`.
|
|
40
|
+
*
|
|
41
|
+
* @param probability Probability in `[0, 1]`.
|
|
42
|
+
* @returns The quantile; `-Infinity` for `0`, `Infinity` for `1`, `NaN` for
|
|
43
|
+
* `NaN`.
|
|
44
|
+
* @throws {RangeError} When `probability` is outside `[0, 1]`.
|
|
45
|
+
*/
|
|
46
|
+
export function normalQuantile(probability) {
|
|
47
|
+
if (Number.isNaN(probability)) {
|
|
48
|
+
return Number.NaN;
|
|
49
|
+
}
|
|
50
|
+
if (probability === 0) {
|
|
51
|
+
return Number.NEGATIVE_INFINITY;
|
|
52
|
+
}
|
|
53
|
+
if (probability === 1) {
|
|
54
|
+
return Number.POSITIVE_INFINITY;
|
|
55
|
+
}
|
|
56
|
+
if (probability < 0 || probability > 1) {
|
|
57
|
+
throw new RangeError(`normalQuantile: probability must be in [0, 1], got ${probability}`);
|
|
58
|
+
}
|
|
59
|
+
return polish(approximate(probability), probability);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* One Halley step on `Φ(x) − p`, whose first two derivatives are the density
|
|
63
|
+
* and `−x` times it — so the correction costs one `erfc` and no extra
|
|
64
|
+
* transcendental.
|
|
65
|
+
*
|
|
66
|
+
* The residual is taken against whichever tail is the small one, since `Φ(x)`
|
|
67
|
+
* itself rounds to 1 well before the upper tail stops mattering.
|
|
68
|
+
*/
|
|
69
|
+
function polish(guess, probability) {
|
|
70
|
+
const scaled = guess * SQRT1_2;
|
|
71
|
+
const residual = probability <= 0.5
|
|
72
|
+
? 0.5 * erfc(-scaled) - probability
|
|
73
|
+
: 1 - probability - 0.5 * erfc(scaled);
|
|
74
|
+
const density = Math.exp(-0.5 * guess * guess) / SQRT_TWO_PI;
|
|
75
|
+
if (density === 0) {
|
|
76
|
+
return guess;
|
|
77
|
+
}
|
|
78
|
+
const newton = residual / density;
|
|
79
|
+
return guess - newton / (1 - 0.5 * newton * guess);
|
|
80
|
+
}
|
|
81
|
+
/** Acklam's approximation, accurate to about nine digits on its own. */
|
|
82
|
+
function approximate(probability) {
|
|
83
|
+
if (probability < TAIL_CUTOFF) {
|
|
84
|
+
return tailQuantile(probability);
|
|
85
|
+
}
|
|
86
|
+
if (probability > 1 - TAIL_CUTOFF) {
|
|
87
|
+
return -tailQuantile(1 - probability);
|
|
88
|
+
}
|
|
89
|
+
// Central region: rational function in r = (p - 1/2)², multiplied by
|
|
90
|
+
// q = p - 1/2 to restore the sign the squaring removed.
|
|
91
|
+
const offset = probability - 0.5;
|
|
92
|
+
const squared = offset * offset;
|
|
93
|
+
const numerator = horner(CENTRAL_NUMERATOR, squared) * offset;
|
|
94
|
+
const denominator = horner(CENTRAL_DENOMINATOR, squared) * squared + 1;
|
|
95
|
+
return numerator / denominator;
|
|
96
|
+
}
|
|
97
|
+
/** Tail branch of Acklam's approximation, for `probability < TAIL_CUTOFF`. */
|
|
98
|
+
function tailQuantile(probability) {
|
|
99
|
+
const q = Math.sqrt(-2 * Math.log(probability));
|
|
100
|
+
const numerator = horner(TAIL_NUMERATOR, q);
|
|
101
|
+
const denominator = horner(TAIL_DENOMINATOR, q) * q + 1;
|
|
102
|
+
return numerator / denominator;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Evaluates the polynomial `coefficients[0]·xⁿ⁻¹ + … + coefficients[n-1]` by
|
|
106
|
+
* Horner's method, coefficients highest-degree first.
|
|
107
|
+
*/
|
|
108
|
+
function horner(coefficients, x) {
|
|
109
|
+
let result = coefficients[0];
|
|
110
|
+
for (let index = 1; index < coefficients.length; index++) {
|
|
111
|
+
result = result * x + coefficients[index];
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=normal-quantile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normal-quantile.js","sourceRoot":"","sources":["../src/normal-quantile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAE3D,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,4EAA4E;AAC5E,YAAY;AACZ,MAAM,iBAAiB,GAAG;IACzB,CAAC,iBAAuB,EAAE,iBAAuB,EAAE,CAAC,iBAAuB;IAC3E,gBAAsB,EAAE,CAAC,iBAAuB,EAAE,iBAAqB;CAC9D,CAAC;AACX,MAAM,mBAAmB,GAAG;IAC3B,CAAC,iBAAuB,EAAE,iBAAuB,EAAE,CAAC,iBAAuB;IAC3E,iBAAuB,EAAE,CAAC,iBAAuB;CACxC,CAAC;AACX,MAAM,cAAc,GAAG;IACtB,CAAC,oBAAwB,EAAE,CAAC,kBAAwB,EAAE,CAAC,iBAAqB;IAC5E,CAAC,iBAAqB,EAAE,iBAAqB,EAAE,iBAAqB;CAC3D,CAAC;AACX,MAAM,gBAAgB,GAAG;IACxB,oBAAwB,EA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C,EAAE,KAAK,GAAG,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC1D,MAAM,GAAG,MAAM,GAAG,CAAC,GAAG,YAAY,CAAC,KAAK,CAAE,CAAC;IAC5C,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** A one-way ANOVA's statistic and the evidence against the null. */
|
|
2
|
+
export type OneWayAnovaResult = {
|
|
3
|
+
/** The F ratio: between-group mean square over within-group mean square. */
|
|
4
|
+
statistic: number;
|
|
5
|
+
/** `groups − 1`. */
|
|
6
|
+
numeratorDegreesOfFreedom: number;
|
|
7
|
+
/** `observations − groups`. */
|
|
8
|
+
denominatorDegreesOfFreedom: number;
|
|
9
|
+
/** Probability of an F ratio at least this large under the null. */
|
|
10
|
+
pValue: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* One-way analysis of variance: whether several groups share a common mean.
|
|
14
|
+
*
|
|
15
|
+
* The two sums of squares are accumulated exactly. Their terms are deviations
|
|
16
|
+
* from a mean, so they straddle zero by construction and a naive running total
|
|
17
|
+
* can cancel away every significant bit of either accumulator — the same reason
|
|
18
|
+
* `linearRegressionSlope` compensates.
|
|
19
|
+
*
|
|
20
|
+
* Named `oneWayAnova` rather than `anova` because the one-way layout is a
|
|
21
|
+
* genuine restriction, not an implementation detail: a two-way or repeated
|
|
22
|
+
* measures design takes a different input shape and answers a different
|
|
23
|
+
* question, and would be a different function rather than an option here.
|
|
24
|
+
*
|
|
25
|
+
* @param groups One array of observations per group. Not modified.
|
|
26
|
+
* @returns The F ratio, its two degrees of freedom and the p-value.
|
|
27
|
+
* @throws {RangeError} When there are fewer than two groups, any group is
|
|
28
|
+
* empty, or there are no more observations than groups.
|
|
29
|
+
*/
|
|
30
|
+
export declare function oneWayAnova(groups: readonly (readonly number[])[]): OneWayAnovaResult;
|
|
31
|
+
//# sourceMappingURL=one-way-anova.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"one-way-anova.d.ts","sourceRoot":"","sources":["../src/one-way-anova.ts"],"names":[],"mappings":"AAKA,qEAAqE;AACrE,MAAM,MAAM,iBAAiB,GAAG;IAC/B,4EAA4E;IAC5E,SAAS,EAAE,MAAM,CAAC;IAElB,oBAAoB;IACpB,yBAAyB,EAAE,MAAM,CAAC;IAElC,+BAA+B;IAC/B,2BAA2B,EAAE,MAAM,CAAC;IAEpC,oEAAoE;IACpE,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,WAAW,CAC1B,MAAM,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,GACpC,iBAAiB,CAyDnB"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { PreciseSum } from "@ac-kit/core";
|
|
2
|
+
import { mean } from "./average.js";
|
|
3
|
+
import { fDistribution } from "./f-distribution.js";
|
|
4
|
+
/**
|
|
5
|
+
* One-way analysis of variance: whether several groups share a common mean.
|
|
6
|
+
*
|
|
7
|
+
* The two sums of squares are accumulated exactly. Their terms are deviations
|
|
8
|
+
* from a mean, so they straddle zero by construction and a naive running total
|
|
9
|
+
* can cancel away every significant bit of either accumulator — the same reason
|
|
10
|
+
* `linearRegressionSlope` compensates.
|
|
11
|
+
*
|
|
12
|
+
* Named `oneWayAnova` rather than `anova` because the one-way layout is a
|
|
13
|
+
* genuine restriction, not an implementation detail: a two-way or repeated
|
|
14
|
+
* measures design takes a different input shape and answers a different
|
|
15
|
+
* question, and would be a different function rather than an option here.
|
|
16
|
+
*
|
|
17
|
+
* @param groups One array of observations per group. Not modified.
|
|
18
|
+
* @returns The F ratio, its two degrees of freedom and the p-value.
|
|
19
|
+
* @throws {RangeError} When there are fewer than two groups, any group is
|
|
20
|
+
* empty, or there are no more observations than groups.
|
|
21
|
+
*/
|
|
22
|
+
export function oneWayAnova(groups) {
|
|
23
|
+
if (groups.length < 2) {
|
|
24
|
+
throw new RangeError("oneWayAnova: need at least two groups");
|
|
25
|
+
}
|
|
26
|
+
let observations = 0;
|
|
27
|
+
const grandTotal = new PreciseSum();
|
|
28
|
+
for (const [index, group] of groups.entries()) {
|
|
29
|
+
if (group.length === 0) {
|
|
30
|
+
throw new RangeError(`oneWayAnova: group ${index} is empty`);
|
|
31
|
+
}
|
|
32
|
+
observations += group.length;
|
|
33
|
+
for (const value of group) {
|
|
34
|
+
grandTotal.add(value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const denominatorDegreesOfFreedom = observations - groups.length;
|
|
38
|
+
if (denominatorDegreesOfFreedom < 1) {
|
|
39
|
+
throw new RangeError("oneWayAnova: need more observations than groups");
|
|
40
|
+
}
|
|
41
|
+
const grandMean = grandTotal.value / observations;
|
|
42
|
+
const between = new PreciseSum();
|
|
43
|
+
const within = new PreciseSum();
|
|
44
|
+
for (const group of groups) {
|
|
45
|
+
const groupMean = mean(group);
|
|
46
|
+
const shift = groupMean - grandMean;
|
|
47
|
+
between.add(group.length * shift * shift);
|
|
48
|
+
for (const value of group) {
|
|
49
|
+
const deviation = value - groupMean;
|
|
50
|
+
within.add(deviation * deviation);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const numeratorDegreesOfFreedom = groups.length - 1;
|
|
54
|
+
const withinMeanSquare = within.value / denominatorDegreesOfFreedom;
|
|
55
|
+
// Every observation in a group identical leaves no within-group variation,
|
|
56
|
+
// so the ratio is undefined rather than infinite.
|
|
57
|
+
const statistic = withinMeanSquare > 0
|
|
58
|
+
? between.value / numeratorDegreesOfFreedom / withinMeanSquare
|
|
59
|
+
: Number.NaN;
|
|
60
|
+
return {
|
|
61
|
+
statistic,
|
|
62
|
+
numeratorDegreesOfFreedom,
|
|
63
|
+
denominatorDegreesOfFreedom,
|
|
64
|
+
pValue: Number.isNaN(statistic)
|
|
65
|
+
? Number.NaN
|
|
66
|
+
: fDistribution(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom).survival(statistic),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=one-way-anova.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"one-way-anova.js","sourceRoot":"","sources":["../src/one-way-anova.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAiBpD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,WAAW,CAC1B,MAAsC;IAEtC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,UAAU,CAAC,uCAAuC,CAAC,CAAC;IAC/D,CAAC;IAED,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,MAAM,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;IACpC,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;QAC/C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,UAAU,CAAC,sBAAsB,KAAK,WAAW,CAAC,CAAC;QAC9D,CAAC;QACD,YAAY,IAAI,KAAK,CAAC,MAAM,CAAC;QAC7B,KAAK,MAAM,KAAK,IAAI,KAAK,EAAE,CAAC;YAC3B,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC;IACF,CAAC;IAED,MAAM,2BAA2B,GAAG,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC;IACjE,IAAI,2BAA2B,GAAG,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,UAAU,CAAC,iDAAiD,CAAC,CAAC;IACzE,CAAC;IAED,MAAM,SAAS,GAAG,UAAU,CAAC,KAAK,GAAG,YAAY,CAAC;IAClD,MAAM,OAAO,GAAG,IAAI,UAAU,EAAE,CAAC;IACjC,MAAM,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;IAEhC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;QAC9B,MAAM,KAAK,GAAG,SAAS,GAAG,SAAS,CAAC;QACpC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC,CAAC;QAE1C,KAAK,MAAM,KAAK,IAAI,KAAK,EAAE,CAAC;YAC3B,MAAM,SAAS,GAAG,KAAK,GAAG,SAAS,CAAC;YACpC,MAAM,CAAC,GAAG,CAAC,SAAS,GAAG,SAAS,CAAC,CAAC;QACnC,CAAC;IACF,CAAC;IAED,MAAM,yBAAyB,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IACpD,MAAM,gBAAgB,GAAG,MAAM,CAAC,KAAK,GAAG,2BAA2B,CAAC;IACpE,2EAA2E;IAC3E,kDAAkD;IAClD,MAAM,SAAS,GACd,gBAAgB,GAAG,CAAC;QACnB,CAAC,CAAC,OAAO,CAAC,KAAK,GAAG,yBAAyB,GAAG,gBAAgB;QAC9D,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;IAEf,OAAO;QACN,SAAS;QACT,yBAAyB;QACzB,2BAA2B;QAC3B,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC;YAC9B,CAAC,CAAC,MAAM,CAAC,GAAG;YACZ,CAAC,CAAC,aAAa,CACb,yBAAyB,EACzB,2BAA2B,CAC3B,CAAC,QAAQ,CAAC,SAAS,CAAC;KACvB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Consistency constant relating the median absolute deviation to the standard
|
|
3
|
+
* deviation of a normal distribution, used by the modified Z-score.
|
|
4
|
+
*/
|
|
5
|
+
export declare const MAD_TO_SIGMA = 0.6745;
|
|
6
|
+
/** Conventional modified Z-score cutoff for outlier classification. */
|
|
7
|
+
export declare const DEFAULT_OUTLIER_THRESHOLD = 3.5;
|
|
8
|
+
/**
|
|
9
|
+
* Flag values unlikely to belong to the same distribution as the rest.
|
|
10
|
+
*
|
|
11
|
+
* Uses the modified Z-score, built on the median and the median absolute
|
|
12
|
+
* deviation. A plain Z-score is unusable for this: a single extreme value
|
|
13
|
+
* inflates the standard deviation enough to hide itself.
|
|
14
|
+
*
|
|
15
|
+
* @param values The sample, in collection order. Not modified.
|
|
16
|
+
* @param threshold Modified Z-score cutoff.
|
|
17
|
+
* @returns Indices into `values`, in ascending order.
|
|
18
|
+
*/
|
|
19
|
+
export declare function findOutliers(values: readonly number[], threshold?: number): number[];
|
|
20
|
+
//# sourceMappingURL=outliers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outliers.d.ts","sourceRoot":"","sources":["../src/outliers.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,eAAO,MAAM,YAAY,SAAS,CAAC;AAEnC,uEAAuE;AACvE,eAAO,MAAM,yBAAyB,MAAM,CAAC;AAE7C;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC3B,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,SAAS,GAAE,MAAkC,GAC3C,MAAM,EAAE,CA6BV"}
|
package/dist/outliers.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { medianAbsoluteDeviation } from "./dispersion.js";
|
|
2
|
+
import { quantile } from "./quantile.js";
|
|
3
|
+
import { sumBy } from "./sum.js";
|
|
4
|
+
/**
|
|
5
|
+
* Consistency constant relating the median absolute deviation to the standard
|
|
6
|
+
* deviation of a normal distribution, used by the modified Z-score.
|
|
7
|
+
*/
|
|
8
|
+
export const MAD_TO_SIGMA = 0.6745;
|
|
9
|
+
/** Conventional modified Z-score cutoff for outlier classification. */
|
|
10
|
+
export const DEFAULT_OUTLIER_THRESHOLD = 3.5;
|
|
11
|
+
/**
|
|
12
|
+
* Flag values unlikely to belong to the same distribution as the rest.
|
|
13
|
+
*
|
|
14
|
+
* Uses the modified Z-score, built on the median and the median absolute
|
|
15
|
+
* deviation. A plain Z-score is unusable for this: a single extreme value
|
|
16
|
+
* inflates the standard deviation enough to hide itself.
|
|
17
|
+
*
|
|
18
|
+
* @param values The sample, in collection order. Not modified.
|
|
19
|
+
* @param threshold Modified Z-score cutoff.
|
|
20
|
+
* @returns Indices into `values`, in ascending order.
|
|
21
|
+
*/
|
|
22
|
+
export function findOutliers(values, threshold = DEFAULT_OUTLIER_THRESHOLD) {
|
|
23
|
+
if (values.length < 3) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const sorted = [...values].sort((left, right) => left - right);
|
|
27
|
+
const middle = quantile(sorted, 0.5);
|
|
28
|
+
let deviation = medianAbsoluteDeviation(sorted, middle);
|
|
29
|
+
if (deviation === 0) {
|
|
30
|
+
// A degenerate MAD means over half the values are identical; fall back to
|
|
31
|
+
// the mean absolute deviation so genuine spikes are still detected.
|
|
32
|
+
deviation =
|
|
33
|
+
sumBy(values, (value) => Math.abs(value - middle)) /
|
|
34
|
+
values.length /
|
|
35
|
+
MAD_TO_SIGMA;
|
|
36
|
+
}
|
|
37
|
+
if (deviation === 0) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
const indices = [];
|
|
41
|
+
for (let index = 0; index < values.length; index++) {
|
|
42
|
+
const score = (MAD_TO_SIGMA * (values[index] - middle)) / deviation;
|
|
43
|
+
if (Math.abs(score) > threshold) {
|
|
44
|
+
indices.push(index);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return indices;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=outliers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outliers.js","sourceRoot":"","sources":["../src/outliers.ts"],"names":[],"mappings":"AAAA,OAAO,EAA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AAM,CAAC,KAAK,CAAE,GAAG,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;QACrE,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,SAAS,EAAE,CAAC;YACjC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;IACF,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Distribution } from "./distribution.js";
|
|
2
|
+
/**
|
|
3
|
+
* The Poisson distribution — the number of events in a fixed interval when they
|
|
4
|
+
* arrive independently at a constant average `rate`.
|
|
5
|
+
*
|
|
6
|
+
* Both tails come from the incomplete gamma by the exact identity `P(X ≤ k) =
|
|
7
|
+
* Q(k + 1, λ)`, so neither is a sum of terms and neither is reached by
|
|
8
|
+
* subtracting the other.
|
|
9
|
+
*
|
|
10
|
+
* @param rate Expected number of events; must be positive.
|
|
11
|
+
* @returns The distribution.
|
|
12
|
+
* @throws {RangeError} When `rate` is not positive.
|
|
13
|
+
*/
|
|
14
|
+
export declare function poissonDistribution(rate: number): Distribution;
|
|
15
|
+
//# sourceMappingURL=poisson-distribution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poisson-distribution.d.ts","sourceRoot":"","sources":["../src/poisson-distribution.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAmC9D"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { logGamma, regularizedIncompleteGamma, regularizedIncompleteGammaUpper, } from "@ac-kit/math-analysis";
|
|
2
|
+
import { assertProbability_ } from "./_assert-probability.js";
|
|
3
|
+
import { discreteQuantile_ } from "./_discrete-quantile.js";
|
|
4
|
+
import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
|
|
5
|
+
/**
|
|
6
|
+
* The Poisson distribution — the number of events in a fixed interval when they
|
|
7
|
+
* arrive independently at a constant average `rate`.
|
|
8
|
+
*
|
|
9
|
+
* Both tails come from the incomplete gamma by the exact identity `P(X ≤ k) =
|
|
10
|
+
* Q(k + 1, λ)`, so neither is a sum of terms and neither is reached by
|
|
11
|
+
* subtracting the other.
|
|
12
|
+
*
|
|
13
|
+
* @param rate Expected number of events; must be positive.
|
|
14
|
+
* @returns The distribution.
|
|
15
|
+
* @throws {RangeError} When `rate` is not positive.
|
|
16
|
+
*/
|
|
17
|
+
export function poissonDistribution(rate) {
|
|
18
|
+
if (!(rate > 0)) {
|
|
19
|
+
throw new RangeError(`poissonDistribution: rate must be positive, got ${rate}`);
|
|
20
|
+
}
|
|
21
|
+
const cdf = (x) => {
|
|
22
|
+
const events = Math.floor(x);
|
|
23
|
+
return events < 0 ? 0 : regularizedIncompleteGammaUpper(events + 1, rate);
|
|
24
|
+
};
|
|
25
|
+
const quantile = (probability) => {
|
|
26
|
+
assertProbability_("poissonDistribution", probability);
|
|
27
|
+
return discreteQuantile_(cdf, probability, 0, Number.POSITIVE_INFINITY);
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
density: (x) => Number.isInteger(x) && x >= 0
|
|
31
|
+
? Math.exp(-rate + x * Math.log(rate) - logGamma(x + 1))
|
|
32
|
+
: 0,
|
|
33
|
+
cdf,
|
|
34
|
+
survival: (x) => {
|
|
35
|
+
const events = Math.floor(x);
|
|
36
|
+
return events < 0 ? 1 : regularizedIncompleteGamma(events + 1, rate);
|
|
37
|
+
},
|
|
38
|
+
quantile,
|
|
39
|
+
sample: (random) => inverseTransformSample_(quantile, random),
|
|
40
|
+
mean: rate,
|
|
41
|
+
variance: rate,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=poisson-distribution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poisson-distribution.js","sourceRo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CAAC;YACxD,CAAC,CAAC,CAAC;QACL,GAAG;QACH,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE;YACf,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAE7B,OAAO,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,0BAA0B,CAAC,MAAM,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;QACtE,CAAC;QACD,QAAQ;QACR,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC7D,IAAI,EAAE,IAAI;QACV,QAAQ,EAAE,IAAI;KACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Linear-interpolated quantile of an already-sorted sample.
|
|
3
|
+
*
|
|
4
|
+
* @param sorted Values in ascending order. Not modified, not re-checked.
|
|
5
|
+
* @param fraction Quantile to compute, in `[0, 1]`.
|
|
6
|
+
* @returns The quantile, or `NaN` for an empty sample.
|
|
7
|
+
*/
|
|
8
|
+
export declare function quantile(sorted: readonly number[], fraction: number): number;
|
|
9
|
+
//# sourceMappingURL=quantile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quantile.d.ts","sourceRoot":"","sources":["../src/quantile.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAiB5E"}
|
package/dist/quantile.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Linear-interpolated quantile of an already-sorted sample.
|
|
3
|
+
*
|
|
4
|
+
* @param sorted Values in ascending order. Not modified, not re-checked.
|
|
5
|
+
* @param fraction Quantile to compute, in `[0, 1]`.
|
|
6
|
+
* @returns The quantile, or `NaN` for an empty sample.
|
|
7
|
+
*/
|
|
8
|
+
export function quantile(sorted, fraction) {
|
|
9
|
+
if (sorted.length === 0) {
|
|
10
|
+
return Number.NaN;
|
|
11
|
+
}
|
|
12
|
+
if (sorted.length === 1) {
|
|
13
|
+
return sorted[0];
|
|
14
|
+
}
|
|
15
|
+
const position = (sorted.length - 1) * fraction;
|
|
16
|
+
const lower = Math.floor(position);
|
|
17
|
+
const upper = Math.ceil(position);
|
|
18
|
+
if (lower === upper) {
|
|
19
|
+
return sorted[lower];
|
|
20
|
+
}
|
|
21
|
+
return (sorted[lower] + (sorted[upper] - sorted[lower]) * (position - lower));
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=quantile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quantile.js","sourceRoot":"","sources":["../src/quantile.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAyB,EAAE,QAAgB;IACnE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC,CAAC,CAAE,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC;IAChD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAClC,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;QACrB,OAAO,MAAM,CAAC,KAAK,CAAE,CAAC;IACvB,CAAC;IACD,OAAO,CACN,MAAM,CAAC,KAAK,CAAE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAE,GAAG,MAAM,CAAC,KAAK,CAAE,CAAC,GAAG,CAAC,QAAQ,GAAG,KAAK,CAAC,CACvE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ConfidenceInterval } from "./confidence-interval.js";
|
|
2
|
+
/** One group's estimate and the variance of that estimate. */
|
|
3
|
+
export type RandomEffectsGroup = {
|
|
4
|
+
readonly estimate: number;
|
|
5
|
+
/** Variance **of the estimate**, not of the underlying samples. */
|
|
6
|
+
readonly variance: number;
|
|
7
|
+
};
|
|
8
|
+
export type PooledEstimate = {
|
|
9
|
+
readonly estimate: number;
|
|
10
|
+
readonly interval: ConfidenceInterval;
|
|
11
|
+
/** Between-group variance component, τ². Zero when groups agree. */
|
|
12
|
+
readonly betweenVariance: number;
|
|
13
|
+
/**
|
|
14
|
+
* Fraction of total variance attributable to between-group differences, I².
|
|
15
|
+
*
|
|
16
|
+
* Zero when the spread between groups is no more than their own uncertainty
|
|
17
|
+
* already explains; near one when the groups disagree far beyond it.
|
|
18
|
+
*/
|
|
19
|
+
readonly heterogeneity: number;
|
|
20
|
+
readonly groups: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Pool estimates from several groups under a random-effects model, by the
|
|
24
|
+
* DerSimonian–Laird method.
|
|
25
|
+
*
|
|
26
|
+
* Use this when each group is one draw from a population of contexts rather
|
|
27
|
+
* than a repeat of the same measurement: separate processes, machines or
|
|
28
|
+
* sessions. A fixed-effect average would treat the between-group spread as if
|
|
29
|
+
* it did not exist and report an interval far too narrow; this estimates that
|
|
30
|
+
* spread as τ² and widens the interval by it.
|
|
31
|
+
*
|
|
32
|
+
* With one group there is no between-group spread to estimate, and the result
|
|
33
|
+
* is that group's own estimate and interval.
|
|
34
|
+
*
|
|
35
|
+
* O(k) in the number of groups.
|
|
36
|
+
*
|
|
37
|
+
* @param groups One entry per group. Not modified.
|
|
38
|
+
* @param level Confidence level in `(0, 1)`. Default `0.95`.
|
|
39
|
+
* @returns The pooled estimate, its interval, and how much the groups
|
|
40
|
+
* disagreed.
|
|
41
|
+
* @throws {RangeError} When `groups` is empty, when any variance is not a
|
|
42
|
+
* positive finite number, or when `level` is outside `(0, 1)`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function poolRandomEffects(groups: readonly RandomEffectsGroup[], level?: number): PooledEstimate;
|
|
45
|
+
//# sourceMappingURL=random-effects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-effects.d.ts","sourceRoot":"","sources":["../src/random-effects.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAG9D,8DAA8D;AAC9D,MAAM,MAAM,kBAAkB,GAAG;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,mEAAmE;IACnE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC;IACtC,oEAAoE;IACpE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC;;;;;OAKG;IACH,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,SAAS,kBAAkB,EAAE,EACrC,KAAK,SAAO,GACV,cAAc,CA8ChB"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { map, sumPrecise } from "@ac-kit/core";
|
|
2
|
+
import { normalQuantile } from "./normal-quantile.js";
|
|
3
|
+
/**
|
|
4
|
+
* Pool estimates from several groups under a random-effects model, by the
|
|
5
|
+
* DerSimonian–Laird method.
|
|
6
|
+
*
|
|
7
|
+
* Use this when each group is one draw from a population of contexts rather
|
|
8
|
+
* than a repeat of the same measurement: separate processes, machines or
|
|
9
|
+
* sessions. A fixed-effect average would treat the between-group spread as if
|
|
10
|
+
* it did not exist and report an interval far too narrow; this estimates that
|
|
11
|
+
* spread as τ² and widens the interval by it.
|
|
12
|
+
*
|
|
13
|
+
* With one group there is no between-group spread to estimate, and the result
|
|
14
|
+
* is that group's own estimate and interval.
|
|
15
|
+
*
|
|
16
|
+
* O(k) in the number of groups.
|
|
17
|
+
*
|
|
18
|
+
* @param groups One entry per group. Not modified.
|
|
19
|
+
* @param level Confidence level in `(0, 1)`. Default `0.95`.
|
|
20
|
+
* @returns The pooled estimate, its interval, and how much the groups
|
|
21
|
+
* disagreed.
|
|
22
|
+
* @throws {RangeError} When `groups` is empty, when any variance is not a
|
|
23
|
+
* positive finite number, or when `level` is outside `(0, 1)`.
|
|
24
|
+
*/
|
|
25
|
+
export function poolRandomEffects(groups, level = 0.95) {
|
|
26
|
+
if (groups.length === 0) {
|
|
27
|
+
throw new RangeError("poolRandomEffects: at least one group is required");
|
|
28
|
+
}
|
|
29
|
+
if (level <= 0 || level >= 1) {
|
|
30
|
+
throw new RangeError("poolRandomEffects: level must be in (0, 1)");
|
|
31
|
+
}
|
|
32
|
+
for (const group of groups) {
|
|
33
|
+
// A zero-variance group would claim infinite precision and take the whole
|
|
34
|
+
// weight. It never means that: it means the estimate was quantised by a
|
|
35
|
+
// coarse clock, and the caller has to decide what to do about it.
|
|
36
|
+
if (!Number.isFinite(group.variance) || group.variance <= 0) {
|
|
37
|
+
throw new RangeError("poolRandomEffects: every group variance must be positive and finite");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const betweenVariance = betweenGroupVariance_(groups);
|
|
41
|
+
// Inverse-variance weights routinely span several orders of magnitude, which
|
|
42
|
+
// is where a naive running total drops the small ones entirely.
|
|
43
|
+
const weights = groups.map((group) => 1 / (group.variance + betweenVariance));
|
|
44
|
+
const weightSum = sumPrecise(weights);
|
|
45
|
+
const weightedSum = sumPrecise(map(groups, (group, index) => weights[index] * group.estimate));
|
|
46
|
+
const estimate = weightedSum / weightSum;
|
|
47
|
+
const halfWidth = normalQuantile((1 + level) / 2) / Math.sqrt(weightSum);
|
|
48
|
+
return {
|
|
49
|
+
estimate,
|
|
50
|
+
interval: {
|
|
51
|
+
estimate,
|
|
52
|
+
lower: estimate - halfWidth,
|
|
53
|
+
upper: estimate + halfWidth,
|
|
54
|
+
halfWidth,
|
|
55
|
+
level,
|
|
56
|
+
},
|
|
57
|
+
betweenVariance,
|
|
58
|
+
heterogeneity: heterogeneity_(groups),
|
|
59
|
+
groups: groups.length,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* τ², the excess of Cochran's Q over its expectation under homogeneity,
|
|
64
|
+
* rescaled to a variance. Truncated at zero: a negative estimate means the
|
|
65
|
+
* groups agree more closely than chance alone predicts, not that the variance
|
|
66
|
+
* is negative.
|
|
67
|
+
*/
|
|
68
|
+
function betweenGroupVariance_(groups) {
|
|
69
|
+
if (groups.length < 2) {
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
const { q, weightSum, weightSquaredSum } = cochranQ_(groups);
|
|
73
|
+
const scale = weightSum - weightSquaredSum / weightSum;
|
|
74
|
+
return scale <= 0 ? 0 : Math.max(0, (q - (groups.length - 1)) / scale);
|
|
75
|
+
}
|
|
76
|
+
/** I², the share of total variation that is between groups rather than within. */
|
|
77
|
+
function heterogeneity_(groups) {
|
|
78
|
+
if (groups.length < 2) {
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
const { q } = cochranQ_(groups);
|
|
82
|
+
return q <= 0 ? 0 : Math.max(0, (q - (groups.length - 1)) / q);
|
|
83
|
+
}
|
|
84
|
+
/** Weighted spread of the group estimates around their fixed-effect average. */
|
|
85
|
+
function cochranQ_(groups) {
|
|
86
|
+
const weights = groups.map((group) => 1 / group.variance);
|
|
87
|
+
const weightSum = sumPrecise(weights);
|
|
88
|
+
const weightSquaredSum = sumPrecise(map(weights, (weight) => weight * weight));
|
|
89
|
+
const weightedSum = sumPrecise(map(groups, (group, index) => weights[index] * group.estimate));
|
|
90
|
+
const fixedEffect = weightedSum / weightSum;
|
|
91
|
+
const q = sumPrecise(map(groups, (group) => {
|
|
92
|
+
const deviation = group.estimate - fixedEffect;
|
|
93
|
+
return (deviation * deviation) / group.variance;
|
|
94
|
+
}));
|
|
95
|
+
return { q, weightSum, weightSquaredSum };
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=random-effects.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-effects.js","sourceRoot":"","sources":["../src/random-effects.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAwBtD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,iBAAiB,CAChC,MAAqC,EACrC,KAAK,GAAG,IAAI;IAEZ,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,UAAU,CAAC,mDAAmD,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,UAAU,CAAC,4CAA4C,CAAC,CAAC;IACpE,CAAC;IAED,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,0EAA0E;QAC1E,wEAAwE;QACxE,kEAAkE;QAClE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,QAAQ,IAAI,CAAC,EAAE,CAAC;YAC7D,MAAM,IAAI,UAAU,CACnB,qEAAqE,CACrE,CAAC;QACH,CAAC;IACF,CAAC;IAED,MAAM,eAAe,GAAG,qBAAqB,CAAC,MAAM,CAAC,CA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C,QAAQ,GAAG,WAAW,CAAC;QAE/C,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC;IACjD,CAAC,CAAC,CACF,CAAC;IAEF,OAAO,EAAE,CAAC,EAAE,SAAS,EAAE,gBAAgB,EAAE,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slope of the ordinary-least-squares line through paired `xValues` and
|
|
3
|
+
* `yValues`.
|
|
4
|
+
*
|
|
5
|
+
* @param xValues Independent-variable samples. Not modified.
|
|
6
|
+
* @param yValues Dependent-variable samples paired by index. Not modified.
|
|
7
|
+
* @returns The slope, or `0` for fewer than two values — the same
|
|
8
|
+
* underdetermined-input convention as {@link sampleVariance}.
|
|
9
|
+
* @throws {RangeError} If `xValues` and `yValues` have different lengths.
|
|
10
|
+
*/
|
|
11
|
+
export declare function linearRegressionSlope(xValues: readonly number[], yValues: readonly number[]): number;
|
|
12
|
+
//# sourceMappingURL=regression.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regression.d.ts","sourceRoot":"","sources":["../src/regression.ts"],"names":[],"mappings":"AAIA;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,SAAS,MAAM,EAAE,GACxB,MAAM,CA0BR"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { PreciseSum } from "@ac-kit/core";
|
|
2
|
+
import { mean } from "./average.js";
|
|
3
|
+
/**
|
|
4
|
+
* Slope of the ordinary-least-squares line through paired `xValues` and
|
|
5
|
+
* `yValues`.
|
|
6
|
+
*
|
|
7
|
+
* @param xValues Independent-variable samples. Not modified.
|
|
8
|
+
* @param yValues Dependent-variable samples paired by index. Not modified.
|
|
9
|
+
* @returns The slope, or `0` for fewer than two values — the same
|
|
10
|
+
* underdetermined-input convention as {@link sampleVariance}.
|
|
11
|
+
* @throws {RangeError} If `xValues` and `yValues` have different lengths.
|
|
12
|
+
*/
|
|
13
|
+
export function linearRegressionSlope(xValues, yValues) {
|
|
14
|
+
if (xValues.length !== yValues.length) {
|
|
15
|
+
throw new RangeError("xValues and yValues must have the same length");
|
|
16
|
+
}
|
|
17
|
+
const count = xValues.length;
|
|
18
|
+
if (count < 2) {
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
const meanX = mean(xValues);
|
|
22
|
+
const meanY = mean(yValues);
|
|
23
|
+
// Exact: the deviations straddle their means by construction, so a naive
|
|
24
|
+
// running total can cancel away every significant bit of either accumulator.
|
|
25
|
+
const covariance = new PreciseSum();
|
|
26
|
+
const variance = new PreciseSum();
|
|
27
|
+
for (let index = 0; index < count; index++) {
|
|
28
|
+
const deltaX = xValues[index] - meanX;
|
|
29
|
+
covariance.add(deltaX * (yValues[index] - meanY));
|
|
30
|
+
variance.add(deltaX * deltaX);
|
|
31
|
+
}
|
|
32
|
+
const spread = variance.value;
|
|
33
|
+
return spread === 0 ? 0 : covariance.value / spread;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=regression.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regression.js","sourceRoot":"","sources":["../src/regression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC;;;;;;;;;GASG;AACH,MAAM,UAAU,qBAAqB,CACpC,OAA0B,EAC1B,OAA0B;IAE1B,IAAI,OAAO,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC;QACvC,MAAM,IAAI,UAAU,CAAC,+CAA+C,CAAC,CAAC;IACvE,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC;IAC7B,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACf,OAAO,CAAC,CAAC;IACV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