@ac-kit/math-stats 0.4.0 → 0.5.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 +147 -0
- package/README.md +67 -0
- package/dist/_assert-probability.d.ts.map +1 -1
- package/dist/_assert-probability.js +2 -1
- package/dist/_assert-probability.js.map +1 -1
- package/dist/_satterthwaite.d.ts +13 -0
- package/dist/_satterthwaite.d.ts.map +1 -0
- package/dist/_satterthwaite.js +25 -0
- package/dist/_satterthwaite.js.map +1 -0
- package/dist/adjust-p-values.d.ts +41 -0
- package/dist/adjust-p-values.d.ts.map +1 -0
- package/dist/adjust-p-values.js +75 -0
- package/dist/adjust-p-values.js.map +1 -0
- package/dist/ashman-d.d.ts +25 -0
- package/dist/ashman-d.d.ts.map +1 -0
- package/dist/ashman-d.js +24 -0
- package/dist/ashman-d.js.map +1 -0
- package/dist/beta-distribution.d.ts.map +1 -1
- package/dist/beta-distribution.js +3 -2
- package/dist/beta-distribution.js.map +1 -1
- package/dist/bimodality-coefficient.d.ts +32 -0
- package/dist/bimodality-coefficient.d.ts.map +1 -0
- package/dist/bimodality-coefficient.js +63 -0
- package/dist/bimodality-coefficient.js.map +1 -0
- package/dist/binomial-distribution.d.ts.map +1 -1
- package/dist/binomial-distribution.js +2 -1
- package/dist/binomial-distribution.js.map +1 -1
- package/dist/chi-squared-distribution.d.ts.map +1 -1
- package/dist/chi-squared-distribution.js +2 -1
- package/dist/chi-squared-distribution.js.map +1 -1
- package/dist/chi-squared-test.js +2 -2
- package/dist/chi-squared-test.js.map +1 -1
- package/dist/combine-independent-estimates.d.ts +53 -0
- package/dist/combine-independent-estimates.d.ts.map +1 -0
- package/dist/combine-independent-estimates.js +71 -0
- package/dist/combine-independent-estimates.js.map +1 -0
- package/dist/confidence-interval.d.ts.map +1 -1
- package/dist/confidence-interval.js +2 -1
- package/dist/confidence-interval.js.map +1 -1
- package/dist/exponential-distribution.d.ts.map +1 -1
- package/dist/exponential-distribution.js +2 -1
- package/dist/exponential-distribution.js.map +1 -1
- package/dist/f-distribution.d.ts.map +1 -1
- package/dist/f-distribution.js +3 -2
- package/dist/f-distribution.js.map +1 -1
- package/dist/gamma-distribution.d.ts.map +1 -1
- package/dist/gamma-distribution.js +3 -2
- package/dist/gamma-distribution.js.map +1 -1
- package/dist/geometric-distribution.d.ts.map +1 -1
- package/dist/geometric-distribution.js +2 -1
- package/dist/geometric-distribution.js.map +1 -1
- package/dist/hartigan-dip.d.ts +65 -0
- package/dist/hartigan-dip.d.ts.map +1 -0
- package/dist/hartigan-dip.js +273 -0
- package/dist/hartigan-dip.js.map +1 -0
- package/dist/histogram.d.ts +42 -0
- package/dist/histogram.d.ts.map +1 -1
- package/dist/histogram.js +106 -2
- package/dist/histogram.js.map +1 -1
- package/dist/hypergeometric-distribution.js +2 -2
- package/dist/hypergeometric-distribution.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/kmeans.d.ts.map +1 -1
- package/dist/kmeans.js +1 -1
- package/dist/kmeans.js.map +1 -1
- package/dist/log-normal-distribution.d.ts.map +1 -1
- package/dist/log-normal-distribution.js +2 -1
- package/dist/log-normal-distribution.js.map +1 -1
- package/dist/negative-binomial-distribution.d.ts.map +1 -1
- package/dist/negative-binomial-distribution.js +3 -2
- package/dist/negative-binomial-distribution.js.map +1 -1
- package/dist/normal-distribution.d.ts.map +1 -1
- package/dist/normal-distribution.js +2 -1
- package/dist/normal-distribution.js.map +1 -1
- package/dist/normal-quantile.d.ts.map +1 -1
- package/dist/normal-quantile.js +2 -1
- package/dist/normal-quantile.js.map +1 -1
- package/dist/poisson-distribution.d.ts.map +1 -1
- package/dist/poisson-distribution.js +2 -1
- package/dist/poisson-distribution.js.map +1 -1
- package/dist/quantile.d.ts +6 -2
- package/dist/quantile.d.ts.map +1 -1
- package/dist/quantile.js +17 -2
- package/dist/quantile.js.map +1 -1
- package/dist/random-effects.d.ts +56 -9
- package/dist/random-effects.d.ts.map +1 -1
- package/dist/random-effects.js +59 -12
- package/dist/random-effects.js.map +1 -1
- package/dist/regression.d.ts +26 -2
- package/dist/regression.d.ts.map +1 -1
- package/dist/regression.js +175 -12
- package/dist/regression.js.map +1 -1
- package/dist/satterthwaite-degrees-of-freedom.d.ts +44 -0
- package/dist/satterthwaite-degrees-of-freedom.d.ts.map +1 -0
- package/dist/satterthwaite-degrees-of-freedom.js +41 -0
- package/dist/satterthwaite-degrees-of-freedom.js.map +1 -0
- package/dist/sliding-median.d.ts +1 -2
- package/dist/sliding-median.d.ts.map +1 -1
- package/dist/sliding-median.js +28 -24
- package/dist/sliding-median.js.map +1 -1
- package/dist/student-t-distribution.d.ts.map +1 -1
- package/dist/student-t-distribution.js +2 -1
- package/dist/student-t-distribution.js.map +1 -1
- package/dist/two-cluster-split.d.ts +34 -0
- package/dist/two-cluster-split.d.ts.map +1 -0
- package/dist/two-cluster-split.js +78 -0
- package/dist/two-cluster-split.js.map +1 -0
- package/dist/variance-of-sample-median.d.ts +40 -0
- package/dist/variance-of-sample-median.d.ts.map +1 -0
- package/dist/variance-of-sample-median.js +162 -0
- package/dist/variance-of-sample-median.js.map +1 -0
- package/dist/welch-t-statistic.d.ts.map +1 -1
- package/dist/welch-t-statistic.js +7 -3
- package/dist/welch-t-statistic.js.map +1 -1
- package/package.json +9 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,152 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 34e8cfa: `combineIndependentEstimates(terms)` combines independent estimates linearly
|
|
8
|
+
(`Σ cᵢ θᵢ`, coefficient `1` by default): the estimate, summed exactly rounded,
|
|
9
|
+
its variance `Σ cᵢ² Vᵢ`, its standard error, and the Welch–Satterthwaite
|
|
10
|
+
degrees of freedom, accurate even where the variance leaves the double range.
|
|
11
|
+
Coefficients `1` and `−1` give Welch's difference of two means.
|
|
12
|
+
|
|
13
|
+
`varianceOfSampleMedian(sorted)` estimates the variance of a sample's median
|
|
14
|
+
from the sorted sample: its exact bootstrap variance in closed form, the
|
|
15
|
+
Maritz–Jarrett estimator for an odd sample and Hutson and Ernst's exact
|
|
16
|
+
bootstrap variance of the two middle values' mean for an even one. It needs no
|
|
17
|
+
distributional shape, is exact at every sample size (no switch to a
|
|
18
|
+
large-sample formula), handles ties, and costs O(n) beyond the sort.
|
|
19
|
+
- 34e8cfa: `histogramBinWidth(values, rule)` returns the width of a histogram bin by
|
|
20
|
+
`"sturges"` (`range / (log2 n + 1)`), `"scott"` (`(24√π / n)^(1/3) · σ`, with
|
|
21
|
+
the population standard deviation) or `"freedman-diaconis"`
|
|
22
|
+
(`2 · IQR · n^(-1/3)`, linear-interpolated quartiles), the definitions behind
|
|
23
|
+
NumPy's `histogram_bin_edges`. Only finite values count; the result is `NaN`
|
|
24
|
+
for fewer than two of them, for zero spread, for a width that underflows to
|
|
25
|
+
zero and for an unknown rule. Each spread is measured without overflow or
|
|
26
|
+
underflow over the whole double range, so the width is the representable
|
|
27
|
+
result, rounded; it is `Infinity` only when it exceeds `Number.MAX_VALUE`.
|
|
28
|
+
|
|
29
|
+
`quantile` now accepts any sorted array-like (a `Float64Array` included),
|
|
30
|
+
where it took a `readonly number[]`, and interpolates without overflow between
|
|
31
|
+
neighbours further apart than `Number.MAX_VALUE` (it returned `Infinity`);
|
|
32
|
+
other results are unchanged.
|
|
33
|
+
- 34e8cfa: `bimodalityCoefficient(values)`: Sarle's bimodality coefficient with the SAS
|
|
34
|
+
small-sample corrections (about 1/3 for a normal sample, 5/9 for a uniform or
|
|
35
|
+
exponential one, 1 for two equal point masses) and
|
|
36
|
+
`BIMODALITY_COEFFICIENT_THRESHOLD` (5/9). A screen with known caveats — skewed
|
|
37
|
+
or heavy-tailed unimodal samples can exceed 5/9 and one extreme value pushes it
|
|
38
|
+
towards 1 — documented on the function.
|
|
39
|
+
|
|
40
|
+
`hartiganDip(sorted)`: Hartigan's dip, the distance from a sorted sample's
|
|
41
|
+
distribution function to the nearest unimodal one, with the modal interval, or
|
|
42
|
+
`null` for an empty sample. `dipTestCriticalValue(count, significance)` gives
|
|
43
|
+
the dip above which a sample of at least 10 values is called multimodal at
|
|
44
|
+
significance 0.1, 0.05 or 0.01, from a simulated table of uniform samples (the
|
|
45
|
+
least favourable unimodal case), so the test's false-alarm rate on any unimodal
|
|
46
|
+
sample, however skewed, is at most the significance.
|
|
47
|
+
|
|
48
|
+
`twoClusterSplit(sorted)` splits a sorted sample into the two groups of least
|
|
49
|
+
within-group variance (Otsu's criterion, exact in one dimension, deterministic),
|
|
50
|
+
returning the low group's size and both groups' means and variances, computed
|
|
51
|
+
from each group's own deviations so groups far from zero keep them, or `null`
|
|
52
|
+
when no cut exists. `ashmanD(groups)` is Ashman's D for such a split: how far
|
|
53
|
+
apart two groups are, not whether there are two.
|
|
54
|
+
- 34e8cfa: `poolRandomEffects(groups, level, { method: "hartung-knapp" })` gives the
|
|
55
|
+
Hartung–Knapp–Sidik–Jonkman interval: Student's t on `k − 1` degrees of freedom,
|
|
56
|
+
scaled by the observed scatter of the groups and never narrower than
|
|
57
|
+
DerSimonian–Laird's. It never under-covers with two to nine groups, where the
|
|
58
|
+
default DerSimonian–Laird normal interval is too narrow, at the price of being
|
|
59
|
+
conservative with few or agreeing groups; it needs at least two groups (one
|
|
60
|
+
throws a `RangeError`). The default method is unchanged. Both methods keep a
|
|
61
|
+
finite interval for a level within an ulp of 1. `PooledEstimate`
|
|
62
|
+
gains `standardError` and `degreesOfFreedom` (`Infinity` for the normal
|
|
63
|
+
interval), so a caller can test the estimate against a value. A non-finite group
|
|
64
|
+
estimate or a `NaN` level now throws a `RangeError` instead of producing `NaN`.
|
|
65
|
+
|
|
66
|
+
`adjustPValues(pValues, method)` adjusts a family of p-values for multiple
|
|
67
|
+
comparisons with `"holm"`, `"bonferroni"` or `"benjamini-hochberg"`; the values
|
|
68
|
+
match R's `p.adjust`, and a `NaN` or out-of-range p-value throws.
|
|
69
|
+
|
|
70
|
+
`satterthwaiteDegreesOfFreedom(components)` gives the Welch–Satterthwaite
|
|
71
|
+
effective degrees of freedom of a sum of independent variance estimates,
|
|
72
|
+
accurate for variances anywhere in the double range; invalid components throw a
|
|
73
|
+
`RangeError`. `welchTStatistic` computes its degrees of freedom the same way: its
|
|
74
|
+
results are unchanged, a `NaN` or infinite variance still gives `NaN` degrees of
|
|
75
|
+
freedom rather than a throw, and very large finite variances no longer overflow
|
|
76
|
+
to `NaN`.
|
|
77
|
+
|
|
78
|
+
### Patch Changes
|
|
79
|
+
|
|
80
|
+
- 34e8cfa: `randomInteger` and `randomBoolean` move from `@ac-kit/algo` to
|
|
81
|
+
`@ac-kit/math-random`, with unchanged behaviour. **Breaking for
|
|
82
|
+
`@ac-kit/algo`:** import them from `@ac-kit/math-random` instead
|
|
83
|
+
(`import { randomBoolean, randomInteger } from "@ac-kit/math-random"`);
|
|
84
|
+
`choice` and `shuffle` stay in `@ac-kit/algo`. `@ac-kit/math-stats` and
|
|
85
|
+
`@ac-kit/model-markup` now depend on `@ac-kit/math-random`.
|
|
86
|
+
- 34e8cfa: An error message that quotes a numeric argument describes it with
|
|
87
|
+
`@ac-kit/core`'s `describeValue`, so building the message can no longer throw
|
|
88
|
+
on a symbol. The text changes: `-0` keeps its sign (`got -0`), a bigint shows
|
|
89
|
+
its digits (a digit-count bound when it is too large to show) and any other
|
|
90
|
+
kind of value is named by its kind. Affected:
|
|
91
|
+
the argument checks of `factorial`, the biquad, window, DCT, FFT size,
|
|
92
|
+
log-frequency map, spectral flux, level meter and novelty peak picker
|
|
93
|
+
functions, every probability distribution's parameters and `quantile`,
|
|
94
|
+
`normalQuantile`, `chiSquaredTest`, `confidenceInterval`, `histogram`,
|
|
95
|
+
`SlidingMedian`, the incomplete beta and gamma functions (and their inverses),
|
|
96
|
+
`trapezoidalRule`, the decimal and rational formatting digit counts and radix,
|
|
97
|
+
the fixed-width integer bit width and shift count, `linspace`, `quantize`,
|
|
98
|
+
`bigIntDigitAt`, `bigIntDigitCount`, `sampleRamp` and `KlActivations`.
|
|
99
|
+
- 34e8cfa: `linearRegressionSlope` answers for finite values of any magnitude instead of
|
|
100
|
+
`NaN` beyond about 1e306 or a lost slope when the terms are far apart in
|
|
101
|
+
magnitude. When every non-zero value lies within 2⁻³⁰⁰ to 2³⁰⁰ (about 1e-90 to
|
|
102
|
+
1e90), the slope is within four units in the last place of the exact slope of
|
|
103
|
+
the values (a relative error below 2⁻⁵¹ unless the slope is subnormal), however
|
|
104
|
+
much the deviations cancel:
|
|
105
|
+
`linearRegressionSlope([0, 0, 1, -1], [2 ** 100, 2 ** 100, 1, -1])` is `1`,
|
|
106
|
+
where it answered `0`. Beyond that range it is the binary64 nearest the exact
|
|
107
|
+
slope, `±Infinity` only when that is past `Number.MAX_VALUE`.
|
|
108
|
+
- Updated dependencies [34e8cfa]
|
|
109
|
+
- Updated dependencies [34e8cfa]
|
|
110
|
+
- Updated dependencies [34e8cfa]
|
|
111
|
+
- Updated dependencies [34e8cfa]
|
|
112
|
+
- Updated dependencies [34e8cfa]
|
|
113
|
+
- Updated dependencies [34e8cfa]
|
|
114
|
+
- Updated dependencies [34e8cfa]
|
|
115
|
+
- Updated dependencies [34e8cfa]
|
|
116
|
+
- Updated dependencies [34e8cfa]
|
|
117
|
+
- Updated dependencies [34e8cfa]
|
|
118
|
+
- Updated dependencies [34e8cfa]
|
|
119
|
+
- Updated dependencies [34e8cfa]
|
|
120
|
+
- Updated dependencies [34e8cfa]
|
|
121
|
+
- Updated dependencies [34e8cfa]
|
|
122
|
+
- Updated dependencies [34e8cfa]
|
|
123
|
+
- Updated dependencies [34e8cfa]
|
|
124
|
+
- Updated dependencies [34e8cfa]
|
|
125
|
+
- Updated dependencies [34e8cfa]
|
|
126
|
+
- Updated dependencies [34e8cfa]
|
|
127
|
+
- Updated dependencies [34e8cfa]
|
|
128
|
+
- Updated dependencies [34e8cfa]
|
|
129
|
+
- Updated dependencies [34e8cfa]
|
|
130
|
+
- Updated dependencies [34e8cfa]
|
|
131
|
+
- Updated dependencies [34e8cfa]
|
|
132
|
+
- Updated dependencies [34e8cfa]
|
|
133
|
+
- Updated dependencies [34e8cfa]
|
|
134
|
+
- Updated dependencies [34e8cfa]
|
|
135
|
+
- Updated dependencies [34e8cfa]
|
|
136
|
+
- Updated dependencies [34e8cfa]
|
|
137
|
+
- Updated dependencies [34e8cfa]
|
|
138
|
+
- Updated dependencies [34e8cfa]
|
|
139
|
+
- Updated dependencies [34e8cfa]
|
|
140
|
+
- Updated dependencies [34e8cfa]
|
|
141
|
+
- Updated dependencies [34e8cfa]
|
|
142
|
+
- Updated dependencies [34e8cfa]
|
|
143
|
+
- @ac-kit/algo@0.5.0
|
|
144
|
+
- @ac-kit/math-random@0.4.0
|
|
145
|
+
- @ac-kit/core@0.12.0
|
|
146
|
+
- @ac-kit/math-scalar@0.3.0
|
|
147
|
+
- @ac-kit/data@0.4.0
|
|
148
|
+
- @ac-kit/math-analysis@0.1.11
|
|
149
|
+
|
|
3
150
|
## 0.4.0
|
|
4
151
|
|
|
5
152
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -48,6 +48,56 @@ import { histogram } from "@ac-kit/math-stats";
|
|
|
48
48
|
histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20]); // [1, 2, 2, 2]
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
`histogramBinWidth(values, rule)` gives the bin width of a rule of thumb —
|
|
52
|
+
`"sturges"`, `"scott"` or `"freedman-diaconis"`, as in NumPy's
|
|
53
|
+
`histogram_bin_edges` — over the finite values, or `NaN` for fewer than two of
|
|
54
|
+
them or zero spread. O(n), O(n log n) for Freedman–Diaconis.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { histogramBinWidth } from "@ac-kit/math-stats";
|
|
58
|
+
|
|
59
|
+
histogramBinWidth([1, 2, 3, 4, 5, 6, 7, 8], "sturges"); // 1.75
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Two groups in a sample
|
|
63
|
+
|
|
64
|
+
`hartiganDip(sorted)` is Hartigan's dip: how far the sample's distribution
|
|
65
|
+
function is from the nearest unimodal one: between `1 / (2n)` and `1/4` for two
|
|
66
|
+
values or more, and 0.5 for a single value. Skew
|
|
67
|
+
and heavy tails do not move it, so it is the test to use when the question is
|
|
68
|
+
"one mode or more": `dipTestCriticalValue(count, significance)` gives the dip a
|
|
69
|
+
sample of that size exceeds with probability 0.1, 0.05 or 0.01 when it is
|
|
70
|
+
uniform, the least favourable unimodal shape, so any unimodal sample is called
|
|
71
|
+
multimodal at most that often.
|
|
72
|
+
|
|
73
|
+
`bimodalityCoefficient(values)` is Sarle's coefficient with the SAS small-sample
|
|
74
|
+
corrections: about 1/3 for a normal sample, 5/9 for a uniform or exponential
|
|
75
|
+
one, 1 for two equal point masses, so a value above
|
|
76
|
+
`BIMODALITY_COEFFICIENT_THRESHOLD` (5/9) suggests more than one mode. It is a
|
|
77
|
+
screen, not a proof: a skewed or heavy-tailed unimodal sample can exceed 5/9 and
|
|
78
|
+
one extreme value pushes it towards 1, so trim outliers first and, for
|
|
79
|
+
multiplicative data such as timings, take the logarithms.
|
|
80
|
+
|
|
81
|
+
`twoClusterSplit(sorted)` cuts a sorted sample into the low and high group of
|
|
82
|
+
least within-group variance (Otsu's criterion, exact in one dimension and
|
|
83
|
+
deterministic, unlike `kmeans`), returning the low group's size and both
|
|
84
|
+
groups' means and variances, or `null` when every value is equal. `ashmanD`
|
|
85
|
+
turns such a split into Ashman's D, `√2·|μ₁ − μ₂| / √(σ₁² + σ₂²)`: above 2 the
|
|
86
|
+
groups are cleanly apart, but a one-sided heavy tail also gives about 2.6, so
|
|
87
|
+
D describes how far apart two groups are, not whether there are two.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import {
|
|
91
|
+
dipTestCriticalValue,
|
|
92
|
+
hartiganDip,
|
|
93
|
+
twoClusterSplit,
|
|
94
|
+
} from "@ac-kit/math-stats";
|
|
95
|
+
|
|
96
|
+
const multimodal =
|
|
97
|
+
hartiganDip(sortedTimings)!.dip > dipTestCriticalValue(sortedTimings.length, 0.01);
|
|
98
|
+
const split = twoClusterSplit(sortedTimings); // where the two groups part
|
|
99
|
+
```
|
|
100
|
+
|
|
51
101
|
## Sliding windows
|
|
52
102
|
|
|
53
103
|
`SlidingMedian` keeps the exact median of the last `n` values pushed, without
|
|
@@ -96,10 +146,27 @@ passing a seeded generator from `@ac-kit/math-random` makes a draw reproducible.
|
|
|
96
146
|
| `welchTStatistic(a, b)` | The statistic alone, for callers thresholding on it |
|
|
97
147
|
| `chiSquaredTest(observed, expected, options)` | Pearson's goodness of fit |
|
|
98
148
|
| `oneWayAnova(groups)` | Whether several groups share a common mean |
|
|
149
|
+
| `poolRandomEffects(groups, level, { method })` | Random-effects pooling: DerSimonian–Laird or Hartung–Knapp interval |
|
|
150
|
+
| `adjustPValues(pValues, method)` | Holm, Bonferroni or Benjamini–Hochberg adjustment of a family of tests |
|
|
151
|
+
| `satterthwaiteDegreesOfFreedom(components)` | Effective degrees of freedom of a sum of estimated variances |
|
|
152
|
+
| `combineIndependentEstimates(terms)` | `Σ cᵢ θᵢ` of independent estimates, its variance and Welch–Satterthwaite degrees of freedom |
|
|
153
|
+
| `varianceOfSampleMedian(sorted)` | Variance of a sample median: its exact bootstrap variance (Maritz–Jarrett, Hutson–Ernst for an even sample), distribution-free at every size |
|
|
99
154
|
|
|
100
155
|
`tTest` reads its form from its arguments rather than from an option: a number
|
|
101
156
|
as the second argument is a hypothesised mean, an array is a second sample.
|
|
102
157
|
|
|
158
|
+
`poolRandomEffects` defaults to DerSimonian–Laird's normal interval, which is
|
|
159
|
+
too narrow below about ten groups. With few groups (separate processes,
|
|
160
|
+
machines, runs), pass `{ method: "hartung-knapp" }`: the interval then uses
|
|
161
|
+
Student's t on `k − 1` degrees of freedom, scaled by the observed scatter of the
|
|
162
|
+
groups and never narrower than DerSimonian–Laird's. It needs at least two
|
|
163
|
+
groups. That truncation (the "modified" Hartung–Knapp of Röver, Knapp and
|
|
164
|
+
Friede 2015) never lets the interval under-cover, but makes it conservative
|
|
165
|
+
wherever it binds: with groups that agree (τ² = 0) at any count, and with two
|
|
166
|
+
to five groups, a nominal 95 % interval holds the true value 97–100 % of the
|
|
167
|
+
time. Near-nominal coverage is reached with heterogeneous groups from about
|
|
168
|
+
nine up.
|
|
169
|
+
|
|
103
170
|
## Usage
|
|
104
171
|
|
|
105
172
|
```ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_assert-probability.d.ts","sourceRoot":"","sources":["../src/_assert-probability.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"_assert-probability.d.ts","sourceRoot":"","sources":["../src/_assert-probability.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI,CAM7E"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
1
2
|
/**
|
|
2
3
|
* Rejects a probability outside `[0, 1]`, naming the distribution rather than
|
|
3
4
|
* this module.
|
|
@@ -12,7 +13,7 @@
|
|
|
12
13
|
*/
|
|
13
14
|
export function assertProbability_(routine, probability) {
|
|
14
15
|
if (!(probability >= 0 && probability <= 1)) {
|
|
15
|
-
throw new RangeError(`${routine}: probability must be in [0, 1], got ${probability}`);
|
|
16
|
+
throw new RangeError(`${routine}: probability must be in [0, 1], got ${describeValue(probability)}`);
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
//# sourceMappingURL=_assert-probability.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_assert-probability.js","sourceRoot":"","sources":["../src/_assert-probability.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAe,EAAE,WAAmB;IACtE,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CACnB,GAAG,OAAO,wCAAwC,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"_assert-probability.js","sourceRoot":"","sources":["../src/_assert-probability.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAe,EAAE,WAAmB;IACtE,IAAI,CAAC,CAAC,WAAW,IAAI,CAAC,IAAI,WAAW,IAAI,CAAC,CAAC,EAAE,CAAC;QAC7C,MAAM,IAAI,UAAU,CACnB,GAAG,OAAO,wCAAwC,aAAa,CAAC,WAAW,CAAC,EAAE,CAC9E,CAAC;IACH,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `(Σ Vᵢ)² / Σ (Vᵢ² / νᵢ)` without validation: a `NaN` or infinite variance
|
|
3
|
+
* gives `NaN`, as does a sum whose variances are all zero.
|
|
4
|
+
*
|
|
5
|
+
* Every variance is divided by the largest before summing and squaring, so the
|
|
6
|
+
* result is finite and accurate across the whole double range — two variances
|
|
7
|
+
* of `1e308` give the same degrees of freedom as two of `1`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function satterthwaite_(components: readonly {
|
|
10
|
+
readonly variance: number;
|
|
11
|
+
readonly degreesOfFreedom: number;
|
|
12
|
+
}[]): number;
|
|
13
|
+
//# sourceMappingURL=_satterthwaite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_satterthwaite.d.ts","sourceRoot":"","sources":["../src/_satterthwaite.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC7B,UAAU,EAAE,SAAS;IACpB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CAClC,EAAE,GACD,MAAM,CAsBR"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { map, sumPrecise } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* `(Σ Vᵢ)² / Σ (Vᵢ² / νᵢ)` without validation: a `NaN` or infinite variance
|
|
4
|
+
* gives `NaN`, as does a sum whose variances are all zero.
|
|
5
|
+
*
|
|
6
|
+
* Every variance is divided by the largest before summing and squaring, so the
|
|
7
|
+
* result is finite and accurate across the whole double range — two variances
|
|
8
|
+
* of `1e308` give the same degrees of freedom as two of `1`.
|
|
9
|
+
*/
|
|
10
|
+
export function satterthwaite_(components) {
|
|
11
|
+
let largest = 0;
|
|
12
|
+
for (const { variance } of components) {
|
|
13
|
+
largest = Math.max(largest, variance);
|
|
14
|
+
}
|
|
15
|
+
if (largest === 0 || !Number.isFinite(largest)) {
|
|
16
|
+
return Number.NaN;
|
|
17
|
+
}
|
|
18
|
+
const total = sumPrecise(map(components, (component) => component.variance / largest));
|
|
19
|
+
const denominator = sumPrecise(map(components, ({ variance, degreesOfFreedom }) => {
|
|
20
|
+
const share = variance / largest;
|
|
21
|
+
return (share * share) / degreesOfFreedom;
|
|
22
|
+
}));
|
|
23
|
+
return (total * total) / denominator;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=_satterthwaite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_satterthwaite.js","sourceRoot":"","sources":["../src/_satterthwaite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAC7B,UAGG;IAEH,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,MAAM,EAAE,QAAQ,EAAE,IAAI,UAAU,EAAE,CAAC;QACvC,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACvC,CAAC;IAED,IAAI,OAAO,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QAChD,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,MAAM,KAAK,GAAG,UAAU,CACvB,GAAG,CAAC,UAAU,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,GAAG,OAAO,CAAC,CAC5D,CAAC;IACF,MAAM,WAAW,GAAG,UAAU,CAC7B,GAAG,CAAC,UAAU,EAAE,CAAC,EAAE,QAAQ,EAAE,gBAAgB,EAAE,EAAE,EAAE;QAClD,MAAM,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;QAEjC,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,gBAAgB,CAAC;IAC3C,CAAC,CAAC,CACF,CAAC;IAEF,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,WAAW,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A multiple-comparison adjustment of p-values.
|
|
3
|
+
*
|
|
4
|
+
* - `"holm"` — Holm's step-down procedure. Bounds the family-wise error rate, the
|
|
5
|
+
* probability of rejecting any true hypothesis, under any dependence between
|
|
6
|
+
* the tests; uniformly more powerful than Bonferroni.
|
|
7
|
+
* - `"bonferroni"` — multiplies each p-value by the number of tests. The same
|
|
8
|
+
* guarantee as Holm's, with less power; kept because it is the one every
|
|
9
|
+
* reader can check by hand.
|
|
10
|
+
* - `"benjamini-hochberg"` — the Benjamini–Hochberg step-up procedure. Bounds the
|
|
11
|
+
* false discovery rate, the expected share of rejections that are wrong, for
|
|
12
|
+
* independent or positively dependent tests. More rejections than Holm's, and
|
|
13
|
+
* a weaker claim about them.
|
|
14
|
+
*/
|
|
15
|
+
export type PValueAdjustment = "holm" | "bonferroni" | "benjamini-hochberg";
|
|
16
|
+
/**
|
|
17
|
+
* Adjusts a family of p-values for multiple comparisons, so that rejecting
|
|
18
|
+
* every hypothesis whose adjusted p-value is at most `α` controls the error
|
|
19
|
+
* rate `method` names at `α`.
|
|
20
|
+
*
|
|
21
|
+
* The adjusted values are those of R's `p.adjust` and statsmodels'
|
|
22
|
+
* `multipletests` (`holm`, `bonferroni`, `fdr_bh`): capped at 1, never smaller
|
|
23
|
+
* than the p-value they adjust, in the same order as the input, and monotone in
|
|
24
|
+
* it (a smaller p-value never receives a larger adjusted value). For Holm's
|
|
25
|
+
* procedure, "adjusted `p ≤ α`" is exactly "rejected by the step-down walk at
|
|
26
|
+
* `α`": the walk stops at the first hypothesis failing its step, and every
|
|
27
|
+
* later one keeps an adjusted value above `α`.
|
|
28
|
+
*
|
|
29
|
+
* O(m log m) for Holm and Benjamini–Hochberg, which sort; O(m) for Bonferroni.
|
|
30
|
+
*
|
|
31
|
+
* @param pValues The family's p-values, each in `[0, 1]`. Not modified. An
|
|
32
|
+
* empty family adjusts to an empty array.
|
|
33
|
+
* @param method The adjustment.
|
|
34
|
+
* @returns A new array of adjusted p-values, index for index with `pValues`.
|
|
35
|
+
* @throws {RangeError} When a p-value is outside `[0, 1]` or is `NaN` — a
|
|
36
|
+
* missing test cannot be dropped silently, since the family's size is what
|
|
37
|
+
* every adjustment scales by — or when `method` is not one of
|
|
38
|
+
* {@link PValueAdjustment}.
|
|
39
|
+
*/
|
|
40
|
+
export declare function adjustPValues(pValues: readonly number[], method: PValueAdjustment): number[];
|
|
41
|
+
//# sourceMappingURL=adjust-p-values.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adjust-p-values.d.ts","sourceRoot":"","sources":["../src/adjust-p-values.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,YAAY,GAAG,oBAAoB,CAAC;AAE5E;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAC5B,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,MAAM,EAAE,gBAAgB,GACtB,MAAM,EAAE,CA0BV"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { sortedIndices } from "@ac-kit/algo";
|
|
2
|
+
import { compareNaturalAscending, describeValue } from "@ac-kit/core";
|
|
3
|
+
/**
|
|
4
|
+
* Adjusts a family of p-values for multiple comparisons, so that rejecting
|
|
5
|
+
* every hypothesis whose adjusted p-value is at most `α` controls the error
|
|
6
|
+
* rate `method` names at `α`.
|
|
7
|
+
*
|
|
8
|
+
* The adjusted values are those of R's `p.adjust` and statsmodels'
|
|
9
|
+
* `multipletests` (`holm`, `bonferroni`, `fdr_bh`): capped at 1, never smaller
|
|
10
|
+
* than the p-value they adjust, in the same order as the input, and monotone in
|
|
11
|
+
* it (a smaller p-value never receives a larger adjusted value). For Holm's
|
|
12
|
+
* procedure, "adjusted `p ≤ α`" is exactly "rejected by the step-down walk at
|
|
13
|
+
* `α`": the walk stops at the first hypothesis failing its step, and every
|
|
14
|
+
* later one keeps an adjusted value above `α`.
|
|
15
|
+
*
|
|
16
|
+
* O(m log m) for Holm and Benjamini–Hochberg, which sort; O(m) for Bonferroni.
|
|
17
|
+
*
|
|
18
|
+
* @param pValues The family's p-values, each in `[0, 1]`. Not modified. An
|
|
19
|
+
* empty family adjusts to an empty array.
|
|
20
|
+
* @param method The adjustment.
|
|
21
|
+
* @returns A new array of adjusted p-values, index for index with `pValues`.
|
|
22
|
+
* @throws {RangeError} When a p-value is outside `[0, 1]` or is `NaN` — a
|
|
23
|
+
* missing test cannot be dropped silently, since the family's size is what
|
|
24
|
+
* every adjustment scales by — or when `method` is not one of
|
|
25
|
+
* {@link PValueAdjustment}.
|
|
26
|
+
*/
|
|
27
|
+
export function adjustPValues(pValues, method) {
|
|
28
|
+
for (const [index, pValue] of pValues.entries()) {
|
|
29
|
+
if (!(pValue >= 0 && pValue <= 1)) {
|
|
30
|
+
throw new RangeError(`adjustPValues: every p-value must be in [0, 1], got ${describeValue(pValue)} at index ${index}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const count = pValues.length;
|
|
34
|
+
switch (method) {
|
|
35
|
+
case "bonferroni":
|
|
36
|
+
return pValues.map((pValue) => Math.min(1, pValue * count));
|
|
37
|
+
case "holm":
|
|
38
|
+
return holm_(pValues);
|
|
39
|
+
case "benjamini-hochberg":
|
|
40
|
+
return benjaminiHochberg_(pValues);
|
|
41
|
+
default:
|
|
42
|
+
throw new RangeError(`adjustPValues: unknown method ${describeValue(method)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `p̃₍ᵢ₎ = max_{j ≤ i} min(1, (m − j + 1) · p₍ⱼ₎)` over the ascending order:
|
|
47
|
+
* the running maximum is what makes the walk stop at the first failure.
|
|
48
|
+
*/
|
|
49
|
+
function holm_(pValues) {
|
|
50
|
+
const count = pValues.length;
|
|
51
|
+
const adjusted = Array.from({ length: count });
|
|
52
|
+
let runningMaximum = 0;
|
|
53
|
+
for (const [rank, index] of sortedIndices(pValues, compareNaturalAscending).entries()) {
|
|
54
|
+
runningMaximum = Math.max(runningMaximum, Math.min(1, (count - rank) * pValues[index]));
|
|
55
|
+
adjusted[index] = runningMaximum;
|
|
56
|
+
}
|
|
57
|
+
return adjusted;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* `p̃₍ᵢ₎ = min_{j ≥ i} min(1, m / j · p₍ⱼ₎)` over the ascending order, the
|
|
61
|
+
* running minimum taken from the largest p-value down.
|
|
62
|
+
*/
|
|
63
|
+
function benjaminiHochberg_(pValues) {
|
|
64
|
+
const count = pValues.length;
|
|
65
|
+
const order = sortedIndices(pValues, compareNaturalAscending);
|
|
66
|
+
const adjusted = Array.from({ length: count });
|
|
67
|
+
let runningMinimum = 1;
|
|
68
|
+
for (let rank = count - 1; rank >= 0; rank--) {
|
|
69
|
+
const index = order[rank];
|
|
70
|
+
runningMinimum = Math.min(runningMinimum, (count / (rank + 1)) * pValues[index]);
|
|
71
|
+
adjusted[index] = runningMinimum;
|
|
72
|
+
}
|
|
73
|
+
return adjusted;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=adjust-p-values.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adjust-p-values.js","sourceRoot":"","sources":["../src/adjust-p-values.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAkBtE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,aAAa,CAC5B,OAA0B,EAC1B,MAAwB;IAExB,KAAK,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;QACjD,IAAI,CAAC,CAAC,MAAM,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC;YACnC,MAAM,IAAI,UAAU,CACnB,uDAAuD,aAAa,CAAC,MAAM,CAAC,aAAa,KAAK,EAAE,CAChG,CAAC;QACH,CAAC;IACF,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC;IAE7B,QAAQ,MAAM,EAAE,CAAC;QAChB,KAAK,YAAY;YAChB,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC;QAE7D,KAAK,MAAM;YACV,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC;QAEvB,KAAK,oBAAoB;YACxB,OAAO,kBAAkB,CAAC,OAAO,CAAC,CAAC;QAEpC;YACC,MAAM,IAAI,UAAU,CACnB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AC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAE,CACtC,CAAC;QACF,QAAQ,CAAC,KAAK,CAAC,GAAG,cAAc,CAAC;IAClC,CAAC;IAED,OAAO,QAAQ,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ashman's D: how cleanly two groups are separated, `√2 · |μ₁ − μ₂| / √(σ₁² +
|
|
3
|
+
* σ₂²)`. Above 2, the two normal components of a mixture are well enough apart
|
|
4
|
+
* to be told apart; a one-sided heavy tail split in two gives about 2.6, so a
|
|
5
|
+
* bimodality test wants a bar well above 2.
|
|
6
|
+
*
|
|
7
|
+
* Source: K. M. Ashman, C. M. Bird and S. E. Zepf, "Detecting bimodality in
|
|
8
|
+
* astronomical datasets", The Astronomical Journal 108, 1994, pp. 2348–2361.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ashmanD({ lowMean: 1, highMean: 3, lowVariance: 0.01, highVariance: 0.01 });
|
|
12
|
+
* // 14.1…
|
|
13
|
+
*
|
|
14
|
+
* @param groups - The two groups' means and variances, for instance a
|
|
15
|
+
* {@link twoClusterSplit} result.
|
|
16
|
+
* @returns D; `Infinity` when both variances are zero and the means differ,
|
|
17
|
+
* `NaN` when they are zero and the means are equal.
|
|
18
|
+
*/
|
|
19
|
+
export declare function ashmanD(groups: {
|
|
20
|
+
readonly lowMean: number;
|
|
21
|
+
readonly highMean: number;
|
|
22
|
+
readonly lowVariance: number;
|
|
23
|
+
readonly highVariance: number;
|
|
24
|
+
}): number;
|
|
25
|
+
//# sourceMappingURL=ashman-d.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ashman-d.d.ts","sourceRoot":"","sources":["../src/ashman-d.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE;IAC/B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CAC9B,GAAG,MAAM,CAKT"}
|
package/dist/ashman-d.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ashman's D: how cleanly two groups are separated, `√2 · |μ₁ − μ₂| / √(σ₁² +
|
|
3
|
+
* σ₂²)`. Above 2, the two normal components of a mixture are well enough apart
|
|
4
|
+
* to be told apart; a one-sided heavy tail split in two gives about 2.6, so a
|
|
5
|
+
* bimodality test wants a bar well above 2.
|
|
6
|
+
*
|
|
7
|
+
* Source: K. M. Ashman, C. M. Bird and S. E. Zepf, "Detecting bimodality in
|
|
8
|
+
* astronomical datasets", The Astronomical Journal 108, 1994, pp. 2348–2361.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ashmanD({ lowMean: 1, highMean: 3, lowVariance: 0.01, highVariance: 0.01 });
|
|
12
|
+
* // 14.1…
|
|
13
|
+
*
|
|
14
|
+
* @param groups - The two groups' means and variances, for instance a
|
|
15
|
+
* {@link twoClusterSplit} result.
|
|
16
|
+
* @returns D; `Infinity` when both variances are zero and the means differ,
|
|
17
|
+
* `NaN` when they are zero and the means are equal.
|
|
18
|
+
*/
|
|
19
|
+
export function ashmanD(groups) {
|
|
20
|
+
const apart = Math.abs(groups.highMean - groups.lowMean);
|
|
21
|
+
const spread = Math.sqrt(groups.lowVariance + groups.highVariance);
|
|
22
|
+
return (Math.SQRT2 * apart) / spread;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=ashman-d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ashman-d.js","sourceRoot":"","sources":["../src/ashman-d.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,OAAO,CAAC,MAKvB;IACA,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IACzD,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IAEnE,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC;AACtC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"beta-distribution.d.ts","sourceRoot":"","sources":["../src/beta-distribution.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"beta-distribution.d.ts","sourceRoot":"","sources":["../src/beta-distribution.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEtD;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAyD1E"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
1
2
|
import { inverseRegularizedIncompleteBeta, logBeta, regularizedIncompleteBeta, } from "@ac-kit/math-analysis";
|
|
2
3
|
import { assertProbability_ } from "./_assert-probability.js";
|
|
3
4
|
import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
|
|
@@ -15,10 +16,10 @@ import { inverseTransformSample_ } from "./_inverse-transform-sample.js";
|
|
|
15
16
|
*/
|
|
16
17
|
export function betaDistribution(alpha, beta) {
|
|
17
18
|
if (!(alpha > 0)) {
|
|
18
|
-
throw new RangeError(`betaDistribution: alpha must be positive, got ${alpha}`);
|
|
19
|
+
throw new RangeError(`betaDistribution: alpha must be positive, got ${describeValue(alpha)}`);
|
|
19
20
|
}
|
|
20
21
|
if (!(beta > 0)) {
|
|
21
|
-
throw new RangeError(`betaDistribution: beta must be positive, got ${beta}`);
|
|
22
|
+
throw new RangeError(`betaDistribution: beta must be positive, got ${describeValue(beta)}`);
|
|
22
23
|
}
|
|
23
24
|
const logNormaliser = logBeta(alpha, beta);
|
|
24
25
|
const total = alpha + beta;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"beta-distribution.js","sourceRoot":"","sources":["../src/beta-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,gCAAgC,EAChC,OAAO,EACP,yBAAyB,GACzB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAGzE;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa,EAAE,IAAY;IAC3D,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;QAClB,MAAM,IAAI,UAAU,CACnB,iDAAiD,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"beta-distribution.js","sourceRoot":"","sources":["../src/beta-distribution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EACN,gCAAgC,EAChC,OAAO,EACP,yBAAyB,GACzB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAGzE;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa,EAAE,IAAY;IAC3D,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;QAClB,MAAM,IAAI,UAAU,CACnB,iDAAiD,aAAa,CAAC,KAAK,CAAC,EAAE,CACvE,CAAC;IACH,CAAC;IACD,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;QACjB,MAAM,IAAI,UAAU,C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AE,CAAC;gBACZ,OAAO,CAAC,CAAC;YACV,CAAC;YAED,qEAAqE;YACrE,yEAAyE;YACzE,uEAAuE;YACvE,wEAAwE;YACxE,kEAAkE;YAClE,OAAO,CAAC,GAAG,SAAS;gBACnB,CAAC,CAAC,CAAC,GAAG,yBAAyB,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC/C,CAAC,CAAC,yBAAyB,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAClD,CAAC;QACD,QAAQ;QACR,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC;QAC7D,IAAI,EAAE,KAAK,GAAG,KAAK;QACnB,QAAQ,EAAE,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;KACxD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Sarle's threshold: a coefficient above 5/9 suggests more than one mode. */
|
|
2
|
+
export declare const BIMODALITY_COEFFICIENT_THRESHOLD: number;
|
|
3
|
+
/**
|
|
4
|
+
* Sarle's bimodality coefficient with the small-sample corrections of the SAS
|
|
5
|
+
* implementation,
|
|
6
|
+
*
|
|
7
|
+
* `b = (G₁² + 1) / (G₂ + 3 (n − 1)² / ((n − 2)(n − 3)))`
|
|
8
|
+
*
|
|
9
|
+
* Where `G₁` is the adjusted sample skewness and `G₂` the adjusted sample
|
|
10
|
+
* excess kurtosis. It lies in `(0, 1]`: about 1/3 for a normal sample, 5/9 for
|
|
11
|
+
* a uniform or exponential one, and 1 for two point masses of equal weight, so
|
|
12
|
+
* a value above {@link BIMODALITY_COEFFICIENT_THRESHOLD} suggests more than one
|
|
13
|
+
* mode.
|
|
14
|
+
*
|
|
15
|
+
* Known caveats (Pfister et al. 2013): it also exceeds 5/9 for a strongly
|
|
16
|
+
* skewed or heavy-tailed unimodal sample, a single extreme value pushes it
|
|
17
|
+
* towards 1, and below a few dozen values it is too noisy to read. Trim
|
|
18
|
+
* outliers first, and use it as a screen rather than a proof.
|
|
19
|
+
*
|
|
20
|
+
* Time O(n), memory O(1).
|
|
21
|
+
*
|
|
22
|
+
* Sources: SAS Institute, SAS/STAT User's Guide, "The MODECLUS procedure"
|
|
23
|
+
* (1990); R. Pfister, K. A. Schwarz, M. Janczyk, R. Dale and J. B. Freeman,
|
|
24
|
+
* "Good things peak in pairs: a note on the bimodality coefficient", Frontiers
|
|
25
|
+
* in Psychology 4, 2013, article 700.
|
|
26
|
+
*
|
|
27
|
+
* @param values - The sample, in any order; read three times.
|
|
28
|
+
* @returns The coefficient, or `NaN` for fewer than four values or a constant
|
|
29
|
+
* sample.
|
|
30
|
+
*/
|
|
31
|
+
export declare function bimodalityCoefficient(values: readonly number[]): number;
|
|
32
|
+
//# sourceMappingURL=bimodality-coefficient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bimodality-coefficient.d.ts","sourceRoot":"","sources":["../src/bimodality-coefficient.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,eAAO,MAAM,gCAAgC,QAAQ,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAyCvE"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** Sarle's threshold: a coefficient above 5/9 suggests more than one mode. */
|
|
2
|
+
export const BIMODALITY_COEFFICIENT_THRESHOLD = 5 / 9;
|
|
3
|
+
/**
|
|
4
|
+
* Sarle's bimodality coefficient with the small-sample corrections of the SAS
|
|
5
|
+
* implementation,
|
|
6
|
+
*
|
|
7
|
+
* `b = (G₁² + 1) / (G₂ + 3 (n − 1)² / ((n − 2)(n − 3)))`
|
|
8
|
+
*
|
|
9
|
+
* Where `G₁` is the adjusted sample skewness and `G₂` the adjusted sample
|
|
10
|
+
* excess kurtosis. It lies in `(0, 1]`: about 1/3 for a normal sample, 5/9 for
|
|
11
|
+
* a uniform or exponential one, and 1 for two point masses of equal weight, so
|
|
12
|
+
* a value above {@link BIMODALITY_COEFFICIENT_THRESHOLD} suggests more than one
|
|
13
|
+
* mode.
|
|
14
|
+
*
|
|
15
|
+
* Known caveats (Pfister et al. 2013): it also exceeds 5/9 for a strongly
|
|
16
|
+
* skewed or heavy-tailed unimodal sample, a single extreme value pushes it
|
|
17
|
+
* towards 1, and below a few dozen values it is too noisy to read. Trim
|
|
18
|
+
* outliers first, and use it as a screen rather than a proof.
|
|
19
|
+
*
|
|
20
|
+
* Time O(n), memory O(1).
|
|
21
|
+
*
|
|
22
|
+
* Sources: SAS Institute, SAS/STAT User's Guide, "The MODECLUS procedure"
|
|
23
|
+
* (1990); R. Pfister, K. A. Schwarz, M. Janczyk, R. Dale and J. B. Freeman,
|
|
24
|
+
* "Good things peak in pairs: a note on the bimodality coefficient", Frontiers
|
|
25
|
+
* in Psychology 4, 2013, article 700.
|
|
26
|
+
*
|
|
27
|
+
* @param values - The sample, in any order; read three times.
|
|
28
|
+
* @returns The coefficient, or `NaN` for fewer than four values or a constant
|
|
29
|
+
* sample.
|
|
30
|
+
*/
|
|
31
|
+
export function bimodalityCoefficient(values) {
|
|
32
|
+
const count = values.length;
|
|
33
|
+
if (count < 4) {
|
|
34
|
+
return Number.NaN;
|
|
35
|
+
}
|
|
36
|
+
let sum = 0;
|
|
37
|
+
for (let index = 0; index < count; index++) {
|
|
38
|
+
sum += values[index];
|
|
39
|
+
}
|
|
40
|
+
const mean = sum / count;
|
|
41
|
+
let second = 0;
|
|
42
|
+
let third = 0;
|
|
43
|
+
let fourth = 0;
|
|
44
|
+
for (let index = 0; index < count; index++) {
|
|
45
|
+
const deviation = values[index] - mean;
|
|
46
|
+
const squared = deviation * deviation;
|
|
47
|
+
second += squared;
|
|
48
|
+
third += squared * deviation;
|
|
49
|
+
fourth += squared * squared;
|
|
50
|
+
}
|
|
51
|
+
const variance = second / count;
|
|
52
|
+
if (!(variance > 0)) {
|
|
53
|
+
return Number.NaN;
|
|
54
|
+
}
|
|
55
|
+
const skewness = third / count / variance ** 1.5;
|
|
56
|
+
const excessKurtosis = fourth / count / variance ** 2 - 3;
|
|
57
|
+
const adjustedSkewness = (skewness * Math.sqrt(count * (count - 1))) / (count - 2);
|
|
58
|
+
const adjustedKurtosis = ((count - 1) / ((count - 2) * (count - 3))) *
|
|
59
|
+
((count + 1) * excessKurtosis + 6);
|
|
60
|
+
return ((adjustedSkewness ** 2 + 1) /
|
|
61
|
+
(adjustedKurtosis + (3 * (count - 1) ** 2) / ((count - 2) * (count - 3))));
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=bimodality-coefficient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bimodality-coefficient.js","sourceRoot":"","sources":["../src/bimodality-coefficient.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAAC,GAAG,CAAC,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAyB;IAC9D,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5B,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACf,OAAO,MAAM,CAAC,GAAG,CAAC;IACnB,CAAC;IAED,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;QAC5C,GAAG,IAAI,MAAM,CAAC,KAAK,CAAE,CAAC;IACvB,CAAC;IACD,MAAM,IAAI,GAAG,GAAG,GAAG,KAAK,CAAC;IAEzB,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,KAAK,GAAG,CA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