@ac-kit/math-stats 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +123 -0
- package/README.md +43 -2
- package/dist/_digamma.d.ts +10 -0
- package/dist/_digamma.d.ts.map +1 -0
- package/dist/_digamma.js +38 -0
- package/dist/_digamma.js.map +1 -0
- package/dist/_dilogarithm.d.ts +14 -0
- package/dist/_dilogarithm.d.ts.map +1 -0
- package/dist/_dilogarithm.js +77 -0
- package/dist/_dilogarithm.js.map +1 -0
- package/dist/_incomplete-gamma-shape-derivative.d.ts +17 -0
- package/dist/_incomplete-gamma-shape-derivative.d.ts.map +1 -0
- package/dist/_incomplete-gamma-shape-derivative.js +90 -0
- package/dist/_incomplete-gamma-shape-derivative.js.map +1 -0
- package/dist/_logistic.d.ts +14 -0
- package/dist/_logistic.d.ts.map +1 -0
- package/dist/_logistic.js +29 -0
- package/dist/_logistic.js.map +1 -0
- package/dist/_normal-tail.d.ts +43 -0
- package/dist/_normal-tail.d.ts.map +1 -0
- package/dist/_normal-tail.js +124 -0
- package/dist/_normal-tail.js.map +1 -0
- package/dist/_rank-test.d.ts +71 -0
- package/dist/_rank-test.d.ts.map +1 -0
- package/dist/_rank-test.js +130 -0
- package/dist/_rank-test.js.map +1 -0
- package/dist/average.js +2 -2
- package/dist/average.js.map +1 -1
- package/dist/bootstrap.d.ts +67 -0
- package/dist/bootstrap.d.ts.map +1 -0
- package/dist/bootstrap.js +154 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/censored-logistic-distribution.d.ts +23 -0
- package/dist/censored-logistic-distribution.d.ts.map +1 -0
- package/dist/censored-logistic-distribution.js +63 -0
- package/dist/censored-logistic-distribution.js.map +1 -0
- package/dist/censored-shifted-gamma-distribution.d.ts +23 -0
- package/dist/censored-shifted-gamma-distribution.d.ts.map +1 -0
- package/dist/censored-shifted-gamma-distribution.js +73 -0
- package/dist/censored-shifted-gamma-distribution.js.map +1 -0
- package/dist/confidence-interval.d.ts +53 -0
- package/dist/confidence-interval.d.ts.map +1 -1
- package/dist/confidence-interval.js +73 -0
- package/dist/confidence-interval.js.map +1 -1
- package/dist/correlation.d.ts +41 -0
- package/dist/correlation.d.ts.map +1 -1
- package/dist/correlation.js +168 -2
- package/dist/correlation.js.map +1 -1
- package/dist/descriptive.d.ts +129 -0
- package/dist/descriptive.d.ts.map +1 -0
- package/dist/descriptive.js +289 -0
- package/dist/descriptive.js.map +1 -0
- package/dist/forecast/_forecast-checks.d.ts +30 -0
- package/dist/forecast/_forecast-checks.d.ts.map +1 -0
- package/dist/forecast/_forecast-checks.js +119 -0
- package/dist/forecast/_forecast-checks.js.map +1 -0
- package/dist/forecast/_score-kernels.d.ts +63 -0
- package/dist/forecast/_score-kernels.d.ts.map +1 -0
- package/dist/forecast/_score-kernels.js +264 -0
- package/dist/forecast/_score-kernels.js.map +1 -0
- package/dist/forecast/brier-score.d.ts +102 -0
- package/dist/forecast/brier-score.d.ts.map +1 -0
- package/dist/forecast/brier-score.js +163 -0
- package/dist/forecast/brier-score.js.map +1 -0
- package/dist/forecast/crps-closed-form.d.ts +64 -0
- package/dist/forecast/crps-closed-form.d.ts.map +1 -0
- package/dist/forecast/crps-closed-form.js +106 -0
- package/dist/forecast/crps-closed-form.js.map +1 -0
- package/dist/forecast/crps-ensemble.d.ts +53 -0
- package/dist/forecast/crps-ensemble.d.ts.map +1 -0
- package/dist/forecast/crps-ensemble.js +121 -0
- package/dist/forecast/crps-ensemble.js.map +1 -0
- package/dist/forecast/emos.d.ts +158 -0
- package/dist/forecast/emos.d.ts.map +1 -0
- package/dist/forecast/emos.js +397 -0
- package/dist/forecast/emos.js.map +1 -0
- package/dist/forecast/exceedance-probability.d.ts +54 -0
- package/dist/forecast/exceedance-probability.d.ts.map +1 -0
- package/dist/forecast/exceedance-probability.js +125 -0
- package/dist/forecast/exceedance-probability.js.map +1 -0
- package/dist/forecast/log-score.d.ts +18 -0
- package/dist/forecast/log-score.d.ts.map +1 -0
- package/dist/forecast/log-score.js +19 -0
- package/dist/forecast/log-score.js.map +1 -0
- package/dist/forecast/logistic-regression.d.ts +123 -0
- package/dist/forecast/logistic-regression.d.ts.map +1 -0
- package/dist/forecast/logistic-regression.js +282 -0
- package/dist/forecast/logistic-regression.js.map +1 -0
- package/dist/forecast/rank-histogram.d.ts +32 -0
- package/dist/forecast/rank-histogram.d.ts.map +1 -0
- package/dist/forecast/rank-histogram.js +63 -0
- package/dist/forecast/rank-histogram.js.map +1 -0
- package/dist/forecast/reliability-diagram.d.ts +70 -0
- package/dist/forecast/reliability-diagram.d.ts.map +1 -0
- package/dist/forecast/reliability-diagram.js +126 -0
- package/dist/forecast/reliability-diagram.js.map +1 -0
- package/dist/forecast/roc-curve.d.ts +58 -0
- package/dist/forecast/roc-curve.d.ts.map +1 -0
- package/dist/forecast/roc-curve.js +124 -0
- package/dist/forecast/roc-curve.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -1
- package/dist/mann-whitney-u.d.ts +27 -0
- package/dist/mann-whitney-u.d.ts.map +1 -0
- package/dist/mann-whitney-u.js +94 -0
- package/dist/mann-whitney-u.js.map +1 -0
- package/dist/multiple-regression.d.ts +123 -0
- package/dist/multiple-regression.d.ts.map +1 -0
- package/dist/multiple-regression.js +208 -0
- package/dist/multiple-regression.js.map +1 -0
- package/dist/principal-components.d.ts +66 -0
- package/dist/principal-components.d.ts.map +1 -0
- package/dist/principal-components.js +105 -0
- package/dist/principal-components.js.map +1 -0
- package/dist/quantile.d.ts +97 -9
- package/dist/quantile.d.ts.map +1 -1
- package/dist/quantile.js +298 -44
- package/dist/quantile.js.map +1 -1
- package/dist/regression.d.ts +46 -0
- package/dist/regression.d.ts.map +1 -1
- package/dist/regression.js +99 -1
- package/dist/regression.js.map +1 -1
- package/dist/sample.d.ts +29 -0
- package/dist/sample.d.ts.map +1 -0
- package/dist/sample.js +39 -0
- package/dist/sample.js.map +1 -0
- package/dist/t-digest.d.ts +136 -0
- package/dist/t-digest.d.ts.map +1 -0
- package/dist/t-digest.js +424 -0
- package/dist/t-digest.js.map +1 -0
- package/dist/truncated-normal-distribution.d.ts +28 -0
- package/dist/truncated-normal-distribution.d.ts.map +1 -0
- package/dist/truncated-normal-distribution.js +198 -0
- package/dist/truncated-normal-distribution.js.map +1 -0
- package/dist/wilcoxon-signed-rank.d.ts +42 -0
- package/dist/wilcoxon-signed-rank.d.ts.map +1 -0
- package/dist/wilcoxon-signed-rank.js +117 -0
- package/dist/wilcoxon-signed-rank.js.map +1 -0
- package/package.json +14 -10
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { erfc } from "@ac-kit/math-analysis";
|
|
2
|
+
import { LN_TWO_PI, SQRT1_2, SQRT_TWO_PI } from "@ac-kit/math-scalar";
|
|
3
|
+
const TINY_ = 1e-300;
|
|
4
|
+
const MAX_TERMS_ = 500;
|
|
5
|
+
/** Below this, `erfc` of `−x/√2` underflows soon: Laplace's fraction instead. */
|
|
6
|
+
const FRACTION_FROM_ = 30;
|
|
7
|
+
/**
|
|
8
|
+
* The tail `Tₖ(t) = k / (t + (k + 1) / (t + (k + 2) / (t + …)))` of Laplace's
|
|
9
|
+
* continued fraction for Mills' ratio `R(t) = Φᶜ(t)/φ(t) = 1/(t + T₁(t))`, `t >
|
|
10
|
+
* 0`, by modified Lentz. `T₁` gives `R`; `T₂` the difference `R⁻¹ − t − T₁` the
|
|
11
|
+
* truncated-normal moments need without cancellation. Converges for every `t >
|
|
12
|
+
* 0`, in a few dozen terms from `t ≈ 5`.
|
|
13
|
+
*/
|
|
14
|
+
export function millsFractionTail_(t, first) {
|
|
15
|
+
// f = 0 + a₁/(t + a₂/(t + …)), aⱼ = first + j − 1.
|
|
16
|
+
let f = TINY_;
|
|
17
|
+
let c = f;
|
|
18
|
+
let d = 0;
|
|
19
|
+
for (let j = 1; j < MAX_TERMS_; j++) {
|
|
20
|
+
const a = first + j - 1;
|
|
21
|
+
d = t + a * d;
|
|
22
|
+
if (Math.abs(d) < TINY_) {
|
|
23
|
+
d = TINY_;
|
|
24
|
+
}
|
|
25
|
+
c = t + a / c;
|
|
26
|
+
if (Math.abs(c) < TINY_) {
|
|
27
|
+
c = TINY_;
|
|
28
|
+
}
|
|
29
|
+
d = 1 / d;
|
|
30
|
+
const delta = c * d;
|
|
31
|
+
f *= delta;
|
|
32
|
+
if (Math.abs(delta - 1) <= Number.EPSILON) {
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return f;
|
|
37
|
+
}
|
|
38
|
+
/** From here Laplace's fraction converges within about a hundred terms. */
|
|
39
|
+
const MILLS_FRACTION_FROM_ = 2;
|
|
40
|
+
/**
|
|
41
|
+
* Mills' ratio `R(t) = Φᶜ(t) / φ(t)` for `t ≥ 0`: `erfc` below `t = 2`, where
|
|
42
|
+
* `exp(t²/2)` is harmless, then `1 / (t + T₁(t))`, which never underflows.
|
|
43
|
+
* `R(∞) = 0`.
|
|
44
|
+
*/
|
|
45
|
+
export function millsRatio_(t) {
|
|
46
|
+
if (t < MILLS_FRACTION_FROM_) {
|
|
47
|
+
return 0.5 * erfc(t * SQRT1_2) * SQRT_TWO_PI * Math.exp(0.5 * t * t);
|
|
48
|
+
}
|
|
49
|
+
if (t === Number.POSITIVE_INFINITY) {
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
return 1 / (t + millsFractionTail_(t, 1));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* `T₁(t) = 1/R(t) − t` for `t ≥ 0`, without the cancellation of that
|
|
56
|
+
* difference: the excess of the mean of the standard normal truncated below `t`
|
|
57
|
+
* over `t`. `T₁(∞) = 0`.
|
|
58
|
+
*/
|
|
59
|
+
export function millsExcess_(t) {
|
|
60
|
+
if (t < MILLS_FRACTION_FROM_) {
|
|
61
|
+
return 1 / millsRatio_(t) - t;
|
|
62
|
+
}
|
|
63
|
+
if (t === Number.POSITIVE_INFINITY) {
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
return millsFractionTail_(t, 1);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The variance of the standard normal truncated below `t ≥ 0`, `1 − T₁(t +
|
|
70
|
+
* T₁)`, from the fraction as `(T₂ − T₁) / (t + T₂)` where the direct form
|
|
71
|
+
* cancels (it falls as `1/t²`). `0` at `t = ∞`.
|
|
72
|
+
*/
|
|
73
|
+
export function lowerTruncatedVariance_(t) {
|
|
74
|
+
if (t < MILLS_FRACTION_FROM_) {
|
|
75
|
+
const excess = millsExcess_(t);
|
|
76
|
+
return 1 - excess * (t + excess);
|
|
77
|
+
}
|
|
78
|
+
if (t === Number.POSITIVE_INFINITY) {
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
const first = millsFractionTail_(t, 1);
|
|
82
|
+
const second = millsFractionTail_(t, 2);
|
|
83
|
+
return (second - first) / (t + second);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The hazard `φ(x) / Φᶜ(x)` of the standard normal, for every `x`: `1/R(x)`
|
|
87
|
+
* from zero up, the direct ratio below (where `Φᶜ ≥ ½`).
|
|
88
|
+
*/
|
|
89
|
+
export function normalHazard_(x) {
|
|
90
|
+
if (x >= 0) {
|
|
91
|
+
return x + millsExcess_(x);
|
|
92
|
+
}
|
|
93
|
+
return Math.exp(logNormalDensity_(x)) / (0.5 * erfc(x * SQRT1_2));
|
|
94
|
+
}
|
|
95
|
+
/** `ln φ(x)`, the log of the standard normal density. */
|
|
96
|
+
export function logNormalDensity_(x) {
|
|
97
|
+
return -0.5 * x * x - 0.5 * LN_TWO_PI;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* `ln Φᶜ(x) = ln P(Z > x)` for the standard normal, finite wherever the
|
|
101
|
+
* argument is: `erfc` while it keeps its digits, `ln φ(x) − ln(x + T₁(x))` from
|
|
102
|
+
* Laplace's fraction past `x = 30`, `log1p` of the small lower tail below
|
|
103
|
+
* zero.
|
|
104
|
+
*/
|
|
105
|
+
export function logNormalSurvival_(x) {
|
|
106
|
+
if (x !== x) {
|
|
107
|
+
return x;
|
|
108
|
+
}
|
|
109
|
+
if (x >= FRACTION_FROM_) {
|
|
110
|
+
if (x === Number.POSITIVE_INFINITY) {
|
|
111
|
+
return Number.NEGATIVE_INFINITY;
|
|
112
|
+
}
|
|
113
|
+
return logNormalDensity_(x) - Math.log(x + millsFractionTail_(x, 1));
|
|
114
|
+
}
|
|
115
|
+
if (x < -1) {
|
|
116
|
+
return Math.log1p(-0.5 * erfc(-x * SQRT1_2));
|
|
117
|
+
}
|
|
118
|
+
return Math.log(0.5 * erfc(x * SQRT1_2));
|
|
119
|
+
}
|
|
120
|
+
/** `ln Φ(x)` for the standard normal: {@link logNormalSurvival_} of `−x`. */
|
|
121
|
+
export function logNormalCdf_(x) {
|
|
122
|
+
return logNormalSurvival_(-x);
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=_normal-tail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_normal-tail.js","sourceRoot":"","sources":["../src/_normal-tail.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEtE,MAAM,KAAK,GAAG,MAAM,CAAC;AACrB,MAAM,UAAU,GAAG,GAAG,CAAC;AAEvB,iFAAiF;AACjF,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAS,EAAE,KAAa;IAC1D,mDAAmD;IACnD,IAAI,CAAC,GAAG,KAAK,CAAC;IACd,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,KAAK,IAAI,CAAC,G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M,UAAU,iBAAiB,CAAC,CAAS;IAC1C,OAAO,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,SAAS,CAAC;AACvC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAS;IAC3C,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACb,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,CAAC,IAAI,cAAc,EAAE,CAAC;QACzB,IAAI,CAAC,KAAK,MAAM,CAAC,iBAAiB,EAAE,CAAC;YACpC,OAAO,MAAM,CAAC,iBAAiB,CAAC;QACjC,CAAC;QACD,OAAO,iBAAiB,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACtE,CAAC;IACD,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAC9C,CAAC;IAED,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;AAC1C,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,aAAa,CAAC,CAAS;IACtC,OAAO,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { TTestAlternative } from "./t-test.js";
|
|
2
|
+
/**
|
|
3
|
+
* How a rank test finds its p-value:
|
|
4
|
+
*
|
|
5
|
+
* - `"exact"` — the permutation distribution of the statistic, conditional on the
|
|
6
|
+
* observed ranks, so it stays exact with ties and zeros (Hollander, Wolfe and
|
|
7
|
+
* Chicken, _Nonparametric Statistical Methods_, 3rd ed., §3.1 and §4.1
|
|
8
|
+
* comments): O(n³) time for the signed-rank test, O(n₁ · n³) for the rank-sum
|
|
9
|
+
* test, by dynamic programming over the doubled ranks.
|
|
10
|
+
* - `"asymptotic"` — the normal approximation with the tie-corrected variance,
|
|
11
|
+
* and the continuity correction unless `continuity` is `false`.
|
|
12
|
+
* - `"auto"` (default) — exact below 50 observations per sample (R's threshold,
|
|
13
|
+
* which R applies only without ties), asymptotic from there.
|
|
14
|
+
*/
|
|
15
|
+
export type RankTestMethod = "auto" | "exact" | "asymptotic";
|
|
16
|
+
/** Options of the rank tests. */
|
|
17
|
+
export type RankTestOptions = {
|
|
18
|
+
/** Defaults to `"two-sided"`. */
|
|
19
|
+
readonly alternative?: TTestAlternative;
|
|
20
|
+
/** Defaults to `"auto"`. */
|
|
21
|
+
readonly method?: RankTestMethod;
|
|
22
|
+
/**
|
|
23
|
+
* Shift the asymptotic statistic by half a unit towards the null, which keeps
|
|
24
|
+
* the normal p-value from understating the discrete one. Defaults to `true`
|
|
25
|
+
* (R's `correct = TRUE`). Ignored by the exact method.
|
|
26
|
+
*/
|
|
27
|
+
readonly continuity?: boolean;
|
|
28
|
+
};
|
|
29
|
+
/** A rank test's statistic and the evidence against the null hypothesis. */
|
|
30
|
+
export type RankTestResult = {
|
|
31
|
+
/** The statistic (see each test). */
|
|
32
|
+
readonly statistic: number;
|
|
33
|
+
/**
|
|
34
|
+
* The standardised statistic of the normal approximation, with the continuity
|
|
35
|
+
* correction when it applies; computed for either method.
|
|
36
|
+
*/
|
|
37
|
+
readonly zScore: number;
|
|
38
|
+
/** Probability of a statistic at least this extreme under the null. */
|
|
39
|
+
readonly pValue: number;
|
|
40
|
+
/** The method the p-value came from, once `"auto"` is resolved. */
|
|
41
|
+
readonly method: "exact" | "asymptotic";
|
|
42
|
+
};
|
|
43
|
+
/** Below this many observations per sample, `"auto"` is exact. */
|
|
44
|
+
export declare const RANK_TEST_EXACT_LIMIT_ = 50;
|
|
45
|
+
export declare function assertRankTestSample_(values: readonly number[], name: string, test: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* The z-score `(statistic − mean − ½·sign) / deviation`, with the continuity
|
|
48
|
+
* shift towards the null.
|
|
49
|
+
*/
|
|
50
|
+
export declare function rankTestZ_(statistic: number, mean: number, deviation: number, alternative: TTestAlternative, continuity: boolean): number;
|
|
51
|
+
export declare function normalPValue_(z: number, alternative: TTestAlternative): number;
|
|
52
|
+
/**
|
|
53
|
+
* The p-value of an integer statistic `observed` under the distribution
|
|
54
|
+
* `probabilities` over `0 … length − 1`: `P(S ≥ observed)` for `"greater"`,
|
|
55
|
+
* `P(S ≤ observed)` for `"less"`, twice the smaller (at most 1) otherwise.
|
|
56
|
+
*/
|
|
57
|
+
export declare function exactPValue_(probabilities: Float64Array, observed: number, alternative: TTestAlternative): number;
|
|
58
|
+
/**
|
|
59
|
+
* The distribution of `Σ εᵢ·weights[i]` for independent fair `εᵢ ∈ {0, 1}`: the
|
|
60
|
+
* signed-rank statistic's under the null, the weights being its integer
|
|
61
|
+
* (doubled) ranks. Positive terms only, so no cancellation.
|
|
62
|
+
*/
|
|
63
|
+
export declare function signFlipDistribution_(weights: ArrayLike<number>): Float64Array;
|
|
64
|
+
/**
|
|
65
|
+
* The distribution of the sum of `chosen` of the integer `weights` drawn
|
|
66
|
+
* without replacement, every subset equally likely: the rank-sum statistic's
|
|
67
|
+
* under the null. Probabilities rather than counts, so the binomial
|
|
68
|
+
* coefficients never overflow, and positive terms only.
|
|
69
|
+
*/
|
|
70
|
+
export declare function subsetSumDistribution_(weights: ArrayLike<number>, chosen: number): Float64Array;
|
|
71
|
+
//# sourceMappingURL=_rank-test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_rank-test.d.ts","sourceRoot":"","sources":["../src/_rank-test.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,OAAO,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YAAY,CAmBd;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACrC,OAAO,EAAE,SAAS,CAAC,MAAM,CAAC,EAC1B,MAAM,EAAE,MAAM,GACZ,YAAY,CAoCd"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { normalDistribution } from "./normal-distribution.js";
|
|
2
|
+
/** Below this many observations per sample, `"auto"` is exact. */
|
|
3
|
+
export const RANK_TEST_EXACT_LIMIT_ = 50;
|
|
4
|
+
const STANDARD_NORMAL_ = normalDistribution(0, 1);
|
|
5
|
+
export function assertRankTestSample_(values, name, test) {
|
|
6
|
+
for (let index = 0; index < values.length; index++) {
|
|
7
|
+
const value = values[index];
|
|
8
|
+
if (value !== value) {
|
|
9
|
+
throw new RangeError(`${test}: ${name}[${index}] is NaN`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The z-score `(statistic − mean − ½·sign) / deviation`, with the continuity
|
|
15
|
+
* shift towards the null.
|
|
16
|
+
*/
|
|
17
|
+
export function rankTestZ_(statistic, mean, deviation, alternative, continuity) {
|
|
18
|
+
const excess = statistic - mean;
|
|
19
|
+
const sign = !continuity
|
|
20
|
+
? 0
|
|
21
|
+
: alternative === "greater"
|
|
22
|
+
? 1
|
|
23
|
+
: alternative === "less"
|
|
24
|
+
? -1
|
|
25
|
+
: Math.sign(excess);
|
|
26
|
+
return (excess - 0.5 * sign) / deviation;
|
|
27
|
+
}
|
|
28
|
+
export function normalPValue_(z, alternative) {
|
|
29
|
+
if (alternative === "greater") {
|
|
30
|
+
return STANDARD_NORMAL_.survival(z);
|
|
31
|
+
}
|
|
32
|
+
if (alternative === "less") {
|
|
33
|
+
return STANDARD_NORMAL_.cdf(z);
|
|
34
|
+
}
|
|
35
|
+
return Math.min(1, 2 * STANDARD_NORMAL_.survival(Math.abs(z)));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The p-value of an integer statistic `observed` under the distribution
|
|
39
|
+
* `probabilities` over `0 … length − 1`: `P(S ≥ observed)` for `"greater"`,
|
|
40
|
+
* `P(S ≤ observed)` for `"less"`, twice the smaller (at most 1) otherwise.
|
|
41
|
+
*/
|
|
42
|
+
export function exactPValue_(probabilities, observed, alternative) {
|
|
43
|
+
let lower = 0;
|
|
44
|
+
let upper = 0;
|
|
45
|
+
for (let value = 0; value < probabilities.length; value++) {
|
|
46
|
+
const probability = probabilities[value];
|
|
47
|
+
if (value <= observed) {
|
|
48
|
+
lower += probability;
|
|
49
|
+
}
|
|
50
|
+
if (value >= observed) {
|
|
51
|
+
upper += probability;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
lower = Math.min(1, lower);
|
|
55
|
+
upper = Math.min(1, upper);
|
|
56
|
+
if (alternative === "greater") {
|
|
57
|
+
return upper;
|
|
58
|
+
}
|
|
59
|
+
if (alternative === "less") {
|
|
60
|
+
return lower;
|
|
61
|
+
}
|
|
62
|
+
return Math.min(1, 2 * Math.min(lower, upper));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The distribution of `Σ εᵢ·weights[i]` for independent fair `εᵢ ∈ {0, 1}`: the
|
|
66
|
+
* signed-rank statistic's under the null, the weights being its integer
|
|
67
|
+
* (doubled) ranks. Positive terms only, so no cancellation.
|
|
68
|
+
*/
|
|
69
|
+
export function signFlipDistribution_(weights) {
|
|
70
|
+
let total = 0;
|
|
71
|
+
for (let index = 0; index < weights.length; index++) {
|
|
72
|
+
total += weights[index];
|
|
73
|
+
}
|
|
74
|
+
const probabilities = new Float64Array(total + 1);
|
|
75
|
+
probabilities[0] = 1;
|
|
76
|
+
let reach = 0;
|
|
77
|
+
for (let index = 0; index < weights.length; index++) {
|
|
78
|
+
const weight = weights[index];
|
|
79
|
+
reach += weight;
|
|
80
|
+
for (let value = reach; value >= 0; value--) {
|
|
81
|
+
const kept = probabilities[value] * 0.5;
|
|
82
|
+
probabilities[value] =
|
|
83
|
+
value >= weight ? kept + probabilities[value - weight] * 0.5 : kept;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return probabilities;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The distribution of the sum of `chosen` of the integer `weights` drawn
|
|
90
|
+
* without replacement, every subset equally likely: the rank-sum statistic's
|
|
91
|
+
* under the null. Probabilities rather than counts, so the binomial
|
|
92
|
+
* coefficients never overflow, and positive terms only.
|
|
93
|
+
*/
|
|
94
|
+
export function subsetSumDistribution_(weights, chosen) {
|
|
95
|
+
const count = weights.length;
|
|
96
|
+
let total = 0;
|
|
97
|
+
for (let index = 0; index < count; index++) {
|
|
98
|
+
total += weights[index];
|
|
99
|
+
}
|
|
100
|
+
const width = total + 1;
|
|
101
|
+
// Row k: the distribution of the sum given k chosen so far.
|
|
102
|
+
const table = new Float64Array((chosen + 1) * width);
|
|
103
|
+
table[0] = 1;
|
|
104
|
+
let reach = 0;
|
|
105
|
+
for (let item = 0; item < count; item++) {
|
|
106
|
+
const weight = weights[item];
|
|
107
|
+
const remaining = count - item;
|
|
108
|
+
reach += weight;
|
|
109
|
+
for (let taken = Math.min(item, chosen); taken >= 0; taken--) {
|
|
110
|
+
const take = (chosen - taken) / remaining;
|
|
111
|
+
const row = taken * width;
|
|
112
|
+
if (take > 0) {
|
|
113
|
+
const next = row + width;
|
|
114
|
+
for (let value = reach - weight; value >= 0; value--) {
|
|
115
|
+
const probability = table[row + value];
|
|
116
|
+
if (probability !== 0) {
|
|
117
|
+
table[next + value + weight] =
|
|
118
|
+
table[next + value + weight] + probability * take;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const leave = 1 - take;
|
|
123
|
+
for (let value = reach; value >= 0; value--) {
|
|
124
|
+
table[row + value] = table[row + value] * leave;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return table.slice(chosen * width, (chosen + 1) * width);
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=_rank-test.js.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E,GAAG,WAAW,GAAG,IAAI,CAAC;oBACrD,CAAC;gBACF,CAAC;YACF,CAAC;YACD,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC;YACvB,KAAK,IAAI,KAAK,GAAG,KAAK,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;gBAC7C,KAAK,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,GAAG,KAAK,CAAE,GAAG,KAAK,CAAC;YAClD,CAAC;QACF,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,KAAK,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;AAC1D,CAAC"}
|
package/dist/average.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { quickselectInPlace } from "@ac-kit/algo";
|
|
2
2
|
import { compareNaturalAscending, fromVariadicArgs, map, minmax, sumPrecise, } from "@ac-kit/core";
|
|
3
3
|
export function mean(...args) {
|
|
4
4
|
const values = fromVariadicArgs(args);
|
|
@@ -63,7 +63,7 @@ export function median(...args) {
|
|
|
63
63
|
// index, and partitioning down to it is linear where sorting is not.
|
|
64
64
|
const working = values.slice();
|
|
65
65
|
const middle = count >> 1;
|
|
66
|
-
const upper =
|
|
66
|
+
const upper = quickselectInPlace(working, middle, compareNaturalAscending);
|
|
67
67
|
if (count % 2 === 1) {
|
|
68
68
|
return upper;
|
|
69
69
|
}
|
package/dist/average.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"average.js","sourceRoot":"","sources":["../src/average.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"average.js","sourceRoot":"","sources":["../src/average.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EACN,uBAAuB,EACvB,gBA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,CAAC;YAC9B,YAAY,GAAG,SAAS,CAAC;QAC1B,CAAC;IACF,CAAC;IAED,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC;QACzD,IAAI,SAAS,KAAK,YAAY,EAAE,CAAC;YAChC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnB,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC;AAcD,MAAM,UAAU,QAAQ,CAAC,GAAG,IAA0B;IACrD,MAAM,MAAM,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACtC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IACpC,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type Callable, type RandomFn } from "@ac-kit/core";
|
|
2
|
+
import type { ConfidenceInterval } from "./confidence-interval.js";
|
|
3
|
+
/**
|
|
4
|
+
* How {@link bootstrap} turns the resampled statistics into an interval (Efron
|
|
5
|
+
* and Tibshirani, _An Introduction to the Bootstrap_, 1993):
|
|
6
|
+
*
|
|
7
|
+
* - `"percentile"` — their `α/2` and `1 − α/2` quantiles.
|
|
8
|
+
* - `"basic"` — those quantiles reflected about the estimate, `2θ̂ − q`.
|
|
9
|
+
* - `"bca"` — bias-corrected and accelerated (Efron 1987, ch. 14): the percentile
|
|
10
|
+
* interval at levels moved by the median bias `z₀` of the resamples and the
|
|
11
|
+
* jackknife acceleration `a`; second-order accurate, and the default, as
|
|
12
|
+
* SciPy's `bootstrap`.
|
|
13
|
+
*/
|
|
14
|
+
export type BootstrapMethod = "percentile" | "basic" | "bca";
|
|
15
|
+
/** Options of {@link bootstrap}. */
|
|
16
|
+
export type BootstrapOptions = {
|
|
17
|
+
/** Resamples drawn, a positive integer. Defaults to `9999`. */
|
|
18
|
+
readonly resamples?: number;
|
|
19
|
+
/** Confidence level in `(0, 1)`. Defaults to `0.95`. */
|
|
20
|
+
readonly level?: number;
|
|
21
|
+
/** Defaults to `"bca"`. */
|
|
22
|
+
readonly method?: BootstrapMethod;
|
|
23
|
+
/**
|
|
24
|
+
* Uniform source over `[0, 1)`. Defaults to `Math.random`; pass a seeded
|
|
25
|
+
* generator from `@ac-kit/math-random` for a reproducible interval.
|
|
26
|
+
*/
|
|
27
|
+
readonly random?: RandomFn | null;
|
|
28
|
+
};
|
|
29
|
+
/** A bootstrap interval, with the resampling distribution behind it. */
|
|
30
|
+
export type BootstrapResult = ConfidenceInterval & {
|
|
31
|
+
/** The method the interval was computed by. */
|
|
32
|
+
readonly method: BootstrapMethod;
|
|
33
|
+
/** The standard deviation of the resampled statistics. */
|
|
34
|
+
readonly standardError: number;
|
|
35
|
+
/** The mean of the resampled statistics minus the estimate. */
|
|
36
|
+
readonly bias: number;
|
|
37
|
+
/** The statistic of each resample, in draw order. */
|
|
38
|
+
readonly distribution: Float64Array;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* A nonparametric bootstrap confidence interval for `statistic` of `values`:
|
|
42
|
+
* the statistic recomputed on `resamples` samples drawn from `values` with
|
|
43
|
+
* replacement, whose spread stands in for its sampling distribution — no
|
|
44
|
+
* distributional assumption, and any statistic (a trimmed mean, a ratio, a
|
|
45
|
+
* correlation of pairs indexed by `values`).
|
|
46
|
+
*
|
|
47
|
+
* `statistic` receives one array that is refilled for every resample (and for
|
|
48
|
+
* the `n` leave-one-out samples of the BCa acceleration): read it, never keep
|
|
49
|
+
* it. O(resamples · cost of `statistic`), plus O(n · cost) for `"bca"`.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const { lower, upper } = bootstrap(timings, median, { random: mulberry32(1) });
|
|
54
|
+
* ```;
|
|
55
|
+
*
|
|
56
|
+
* @param values The sample, at least two values. Not modified.
|
|
57
|
+
* @param statistic The statistic of a sample.
|
|
58
|
+
* @param options Resamples, level, method and random source.
|
|
59
|
+
* @returns The interval about `statistic(values)`, the standard error and bias
|
|
60
|
+
* of the resamples, and the resampled statistics. `NaN` bounds for `"bca"`
|
|
61
|
+
* when every resample lies on one side of the estimate.
|
|
62
|
+
* @throws {RangeError} When `values` has fewer than two values, `resamples` is
|
|
63
|
+
* not a positive integer, `level` is outside `(0, 1)` or `method` is not a
|
|
64
|
+
* {@link BootstrapMethod}.
|
|
65
|
+
*/
|
|
66
|
+
export declare function bootstrap(values: readonly number[], statistic: Callable<[readonly number[]], number>, options?: BootstrapOptions): BootstrapResult;
|
|
67
|
+
//# sourceMappingURL=bootstrap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAiB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAG3E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAKnE;;;;;;;;;;GAUG;AACH,MAAM,MAAM,eAAe,GAAG,YAAY,GAAG,OAAO,GAAG,KAAK,CAAC;AAE7D,oCAAoC;AACpC,MAAM,MAAM,gBAAgB,GAAG;IAC9B,+DAA+D;IAC/D,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAE5B,wDAAwD;IACxD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAExB,2BAA2B;IAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,eAAe,CAAC;IAElC;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC;CAClC,CAAC;AAEF,wEAAwE;AACxE,MAAM,MAAM,eAAe,GAAG,kBAAkB,GAAG;IAClD,+CAA+C;IAC/C,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IAEjC,0DAA0D;IAC1D,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAE/B,+DAA+D;IAC/D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,qDAAqD;IACrD,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;CACpC,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,SAAS,CACxB,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,SAAS,EAAE,QAAQ,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,EAChD,OAAO,CAAC,EAAE,gBAAgB,GACxB,eAAe,CAoGjB"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
import { randomInteger } from "@ac-kit/math-random";
|
|
3
|
+
import { normalDistribution } from "./normal-distribution.js";
|
|
4
|
+
import { normalQuantile } from "./normal-quantile.js";
|
|
5
|
+
import { quantile } from "./quantile.js";
|
|
6
|
+
const STANDARD_NORMAL_ = normalDistribution(0, 1);
|
|
7
|
+
/**
|
|
8
|
+
* A nonparametric bootstrap confidence interval for `statistic` of `values`:
|
|
9
|
+
* the statistic recomputed on `resamples` samples drawn from `values` with
|
|
10
|
+
* replacement, whose spread stands in for its sampling distribution — no
|
|
11
|
+
* distributional assumption, and any statistic (a trimmed mean, a ratio, a
|
|
12
|
+
* correlation of pairs indexed by `values`).
|
|
13
|
+
*
|
|
14
|
+
* `statistic` receives one array that is refilled for every resample (and for
|
|
15
|
+
* the `n` leave-one-out samples of the BCa acceleration): read it, never keep
|
|
16
|
+
* it. O(resamples · cost of `statistic`), plus O(n · cost) for `"bca"`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const { lower, upper } = bootstrap(timings, median, { random: mulberry32(1) });
|
|
21
|
+
* ```;
|
|
22
|
+
*
|
|
23
|
+
* @param values The sample, at least two values. Not modified.
|
|
24
|
+
* @param statistic The statistic of a sample.
|
|
25
|
+
* @param options Resamples, level, method and random source.
|
|
26
|
+
* @returns The interval about `statistic(values)`, the standard error and bias
|
|
27
|
+
* of the resamples, and the resampled statistics. `NaN` bounds for `"bca"`
|
|
28
|
+
* when every resample lies on one side of the estimate.
|
|
29
|
+
* @throws {RangeError} When `values` has fewer than two values, `resamples` is
|
|
30
|
+
* not a positive integer, `level` is outside `(0, 1)` or `method` is not a
|
|
31
|
+
* {@link BootstrapMethod}.
|
|
32
|
+
*/
|
|
33
|
+
export function bootstrap(values, statistic, options) {
|
|
34
|
+
const count = values.length;
|
|
35
|
+
if (count < 2) {
|
|
36
|
+
throw new RangeError(`bootstrap: the sample must have at least two values, got ${count}`);
|
|
37
|
+
}
|
|
38
|
+
const resamples = options?.resamples ?? 9999;
|
|
39
|
+
if (!Number.isSafeInteger(resamples) || resamples < 1) {
|
|
40
|
+
throw new RangeError(`bootstrap: resamples must be a positive integer, got ${describeValue(resamples)}`);
|
|
41
|
+
}
|
|
42
|
+
const level = options?.level ?? 0.95;
|
|
43
|
+
if (!(level > 0 && level < 1)) {
|
|
44
|
+
throw new RangeError(`bootstrap: the confidence level must be in (0, 1), got ${describeValue(level)}`);
|
|
45
|
+
}
|
|
46
|
+
const method = options?.method ?? "bca";
|
|
47
|
+
if (method !== "percentile" && method !== "basic" && method !== "bca") {
|
|
48
|
+
throw new RangeError(`bootstrap: method must be "percentile", "basic" or "bca", got ${describeValue(method)}`);
|
|
49
|
+
}
|
|
50
|
+
const random = options?.random ?? Math.random;
|
|
51
|
+
const estimate = statistic(values);
|
|
52
|
+
const buffer = Array.from({ length: count });
|
|
53
|
+
const distribution = new Float64Array(resamples);
|
|
54
|
+
let sum = 0;
|
|
55
|
+
for (let resample = 0; resample < resamples; resample++) {
|
|
56
|
+
for (let index = 0; index < count; index++) {
|
|
57
|
+
buffer[index] = values[randomInteger(0, count, random)];
|
|
58
|
+
}
|
|
59
|
+
const value = statistic(buffer);
|
|
60
|
+
distribution[resample] = value;
|
|
61
|
+
sum += value;
|
|
62
|
+
}
|
|
63
|
+
const resampledMean = sum / resamples;
|
|
64
|
+
let squares = 0;
|
|
65
|
+
for (let resample = 0; resample < resamples; resample++) {
|
|
66
|
+
const deviation = distribution[resample] - resampledMean;
|
|
67
|
+
squares += deviation * deviation;
|
|
68
|
+
}
|
|
69
|
+
const standardError = resamples > 1 ? Math.sqrt(squares / (resamples - 1)) : Number.NaN;
|
|
70
|
+
const sorted = Float64Array.from(distribution).sort();
|
|
71
|
+
const alpha = (1 - level) / 2;
|
|
72
|
+
let lowerLevel = alpha;
|
|
73
|
+
let upperLevel = 1 - alpha;
|
|
74
|
+
if (method === "bca") {
|
|
75
|
+
// z₀ from the share of resamples below the estimate, ties counted half.
|
|
76
|
+
let below = 0;
|
|
77
|
+
let atMost = 0;
|
|
78
|
+
for (let resample = 0; resample < resamples; resample++) {
|
|
79
|
+
const value = sorted[resample];
|
|
80
|
+
if (value < estimate) {
|
|
81
|
+
below++;
|
|
82
|
+
}
|
|
83
|
+
if (value <= estimate) {
|
|
84
|
+
atMost++;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const share = (below + atMost) / (2 * resamples);
|
|
88
|
+
const bias = share <= 0 || share >= 1 ? Number.NaN : normalQuantile(share);
|
|
89
|
+
const acceleration = jackknifeAcceleration_(values, statistic, buffer);
|
|
90
|
+
const shifted = (z) => {
|
|
91
|
+
const moved = bias + z;
|
|
92
|
+
return STANDARD_NORMAL_.cdf(bias + moved / (1 - acceleration * moved));
|
|
93
|
+
};
|
|
94
|
+
lowerLevel = shifted(normalQuantile(alpha));
|
|
95
|
+
upperLevel = shifted(normalQuantile(1 - alpha));
|
|
96
|
+
}
|
|
97
|
+
let lower;
|
|
98
|
+
let upper;
|
|
99
|
+
if (lowerLevel !== lowerLevel || upperLevel !== upperLevel) {
|
|
100
|
+
lower = Number.NaN;
|
|
101
|
+
upper = Number.NaN;
|
|
102
|
+
}
|
|
103
|
+
else if (method === "basic") {
|
|
104
|
+
lower = 2 * estimate - quantile(sorted, upperLevel);
|
|
105
|
+
upper = 2 * estimate - quantile(sorted, lowerLevel);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
lower = quantile(sorted, lowerLevel);
|
|
109
|
+
upper = quantile(sorted, upperLevel);
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
estimate,
|
|
113
|
+
lower,
|
|
114
|
+
upper,
|
|
115
|
+
halfWidth: (upper - lower) / 2,
|
|
116
|
+
level,
|
|
117
|
+
method,
|
|
118
|
+
standardError,
|
|
119
|
+
bias: resampledMean - estimate,
|
|
120
|
+
distribution,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Efron's acceleration `a = Σ(θ̄ − θ₍ᵢ₎)³ / (6 · (Σ(θ̄ − θ₍ᵢ₎)²)^{3/2})` from
|
|
125
|
+
* the leave-one-out statistics; `0` when they do not vary.
|
|
126
|
+
*/
|
|
127
|
+
function jackknifeAcceleration_(values, statistic, buffer) {
|
|
128
|
+
const count = values.length;
|
|
129
|
+
const leaveOneOut = new Float64Array(count);
|
|
130
|
+
buffer.length = count - 1;
|
|
131
|
+
let sum = 0;
|
|
132
|
+
for (let left = 0; left < count; left++) {
|
|
133
|
+
let position = 0;
|
|
134
|
+
for (let index = 0; index < count; index++) {
|
|
135
|
+
if (index !== left) {
|
|
136
|
+
buffer[position++] = values[index];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const value = statistic(buffer);
|
|
140
|
+
leaveOneOut[left] = value;
|
|
141
|
+
sum += value;
|
|
142
|
+
}
|
|
143
|
+
buffer.length = count;
|
|
144
|
+
const mean = sum / count;
|
|
145
|
+
let squares = 0;
|
|
146
|
+
let cubes = 0;
|
|
147
|
+
for (let index = 0; index < count; index++) {
|
|
148
|
+
const deviation = mean - leaveOneOut[index];
|
|
149
|
+
squares += deviation * deviation;
|
|
150
|
+
cubes += deviation * deviation * deviation;
|
|
151
|
+
}
|
|
152
|
+
return squares === 0 ? 0 : cubes / (6 * squares ** 1.5);
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=bootstrap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,aAAa,EAAiB,MAAM,cAAc,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAgDzC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,SAAS,CACxB,MAAyB,EACzB,SAAgD,EAChD,OAA0B;IAE1B,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACf,MAAM,IAAI,UAAU,CACnB,4DAA4D,KAAK,EAAE,CACnE,CAAC;IACH,CAAC;IACD,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,IAAI,CAAC;IAC7C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;QACvD,MAAM,IAAI,UAAU,CACnB,wDAAwD,aAAa,CAAC,SAAS,CAAC,EAAE,CAClF,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,EAAE,KAAK,IAAI,IAA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WAAW,CAAC,KAAK,CAAE,CAAC;QAC7C,OAAO,IAAI,SAAS,GAAG,SAAS,CAAC;QACjC,KAAK,IAAI,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;IAC5C,CAAC;IAED,OAAO,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,OAAO,IAAI,GAAG,CAAC,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Distribution } from "./distribution.js";
|
|
2
|
+
/**
|
|
3
|
+
* The logistic distribution of `location` and `scale` censored below at
|
|
4
|
+
* `lower`: `Y = max(lower, X)`, every draw of `X` under `lower` recorded as
|
|
5
|
+
* `lower` itself. The law of precipitation and other quantities with a point
|
|
6
|
+
* mass at a bound in ensemble post-processing (Messner, Mayr and Zeileis
|
|
7
|
+
* 2016).
|
|
8
|
+
*
|
|
9
|
+
* It is a mixed distribution: `cdf` jumps by `P(X ≤ lower)` at `lower`, and
|
|
10
|
+
* `density(lower)` returns that point mass — the density with respect to
|
|
11
|
+
* Lebesgue measure plus a unit atom at the bound, the likelihood of censored
|
|
12
|
+
* regression, so `−ln density(y)` is the log score at every `y`. `quantile(p)`
|
|
13
|
+
* is `lower` for every `p` up to the mass. `mean` and `variance` are exact, the
|
|
14
|
+
* second moment through the Fermi–Dirac integral `F₁` (a dilogarithm).
|
|
15
|
+
*
|
|
16
|
+
* @param location Location of the parent logistic, finite.
|
|
17
|
+
* @param scale Scale of the parent logistic; positive and finite.
|
|
18
|
+
* @param lower The censoring point, finite; `0` by default.
|
|
19
|
+
* @returns The distribution.
|
|
20
|
+
* @throws {RangeError} When a parameter is out of its range.
|
|
21
|
+
*/
|
|
22
|
+
export declare function censoredLogisticDistribution(location: number, scale: number, lower?: number): Distribution;
|
|
23
|
+
//# sourceMappingURL=censored-logistic-distribution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"censored-logistic-distribution.d.ts","sourceRoot":"","sources":["../src/censored-logistic-distribution.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,4BAA4B,CAC3C,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,KAAK,SAAI,GACP,YAAY,CA+Cd"}
|