@ac-kit/math-stats 0.5.0 → 0.6.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 +82 -0
- package/README.md +43 -4
- package/dist/_histogram-rules.d.ts +15 -0
- package/dist/_histogram-rules.d.ts.map +1 -0
- package/dist/_histogram-rules.js +208 -0
- package/dist/_histogram-rules.js.map +1 -0
- package/dist/hierarchical-cluster.d.ts.map +1 -1
- package/dist/hierarchical-cluster.js +10 -5
- package/dist/hierarchical-cluster.js.map +1 -1
- package/dist/histogram-edges.d.ts +92 -0
- package/dist/histogram-edges.d.ts.map +1 -0
- package/dist/histogram-edges.js +325 -0
- package/dist/histogram-edges.js.map +1 -0
- package/dist/histogram.d.ts +55 -21
- package/dist/histogram.d.ts.map +1 -1
- package/dist/histogram.js +56 -83
- package/dist/histogram.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/kmeans.d.ts.map +1 -1
- package/dist/kmeans.js +11 -6
- package/dist/kmeans.js.map +1 -1
- package/dist/kmedoids.js +2 -1
- package/dist/kmedoids.js.map +1 -1
- package/dist/quantile.d.ts +23 -3
- package/dist/quantile.d.ts.map +1 -1
- package/dist/quantile.js +58 -13
- package/dist/quantile.js.map +1 -1
- package/dist/running-statistics.d.ts +103 -0
- package/dist/running-statistics.d.ts.map +1 -0
- package/dist/running-statistics.js +283 -0
- package/dist/running-statistics.js.map +1 -0
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,87 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 0b4fe43: `histogramEdges(values, options)` gives a histogram's bin edges: NumPy's
|
|
8
|
+
`histogram_bin_edges` equal bins from the low end of the range to the high end,
|
|
9
|
+
or with `nice: true` Vega's round edges, on the multiples of a width of 1, 2 or
|
|
10
|
+
5 times a power of ten. The width comes from a `rule` (default `"auto"`), a
|
|
11
|
+
`width` or a `count`, with at most `maxCount` bins, over `range` or the extent
|
|
12
|
+
of the finite values; `NaN` and ±Infinity are ignored, an empty sample covers
|
|
13
|
+
`[0, 1]`, a single value is widened by a half (by its own magnitude with
|
|
14
|
+
`nice`), and where a rule finds no width though the values spread (a zero
|
|
15
|
+
interquartile range) Sturges' width stands in instead of NumPy's single bin.
|
|
16
|
+
Edges are finite and strictly ascending, at least two around the values (a
|
|
17
|
+
single value under a `nice` width finer than the doubles there gets the value
|
|
18
|
+
and the next double); the last bin is closed.
|
|
19
|
+
|
|
20
|
+
`HistogramBinRule` and `histogramBinWidth` add NumPy's other rules: `"auto"`,
|
|
21
|
+
`"doane"`, `"rice"`, `"sqrt"` and `"stone"`, checked against NumPy 2.5.
|
|
22
|
+
`histogramBinWidth` and `histogram` accept any `ArrayLike<number>` (a typed
|
|
23
|
+
array is read as is), and `histogram(values, edges, { lastClosed: true })`
|
|
24
|
+
counts a value equal to the last edge in the last bin, as `histogramEdges`' bins
|
|
25
|
+
and NumPy's are.
|
|
26
|
+
- 0b4fe43: `RunningStatistics` accumulates the count, mean, variance, minimum and maximum
|
|
27
|
+
of numbers added one at a time (`add`), by range (`addRange`) or by indexed
|
|
28
|
+
range of a column (`addIndexed`), without copying them. `NaN` is ignored. The
|
|
29
|
+
variance is Welford's single-pass update, so a mean far above the spread loses
|
|
30
|
+
no precision, and `merge` combines partitions exactly as if their numbers had
|
|
31
|
+
been added to one accumulator. `sampleVariance` is `NaN` below two numbers,
|
|
32
|
+
where the function of the same name answers `0`. Its getters read each
|
|
33
|
+
statistic; `writeMean`, `writeMin`, `writeMax` and the variance and standard
|
|
34
|
+
deviation writers put it in an output array (a `Float64Array` or a `number[]`)
|
|
35
|
+
instead, without allocating, even once both kinds have been written.
|
|
36
|
+
|
|
37
|
+
`quantile(sorted, fraction, from, to)` reads the sorted range `[from, to)` in
|
|
38
|
+
place, one group's slice of a shared buffer, and throws a `RangeError` for a
|
|
39
|
+
range outside the array. `writeQuantile(target, index, sorted, fraction, from,
|
|
40
|
+
to)` writes the same quantile into `target[index]` without allocating.
|
|
41
|
+
|
|
42
|
+
### Patch Changes
|
|
43
|
+
|
|
44
|
+
- Updated dependencies [0b4fe43]
|
|
45
|
+
- Updated dependencies [0b4fe43]
|
|
46
|
+
- Updated dependencies [0b4fe43]
|
|
47
|
+
- Updated dependencies [0b4fe43]
|
|
48
|
+
- Updated dependencies [0b4fe43]
|
|
49
|
+
- Updated dependencies [0b4fe43]
|
|
50
|
+
- Updated dependencies [0b4fe43]
|
|
51
|
+
- Updated dependencies [0b4fe43]
|
|
52
|
+
- Updated dependencies [0b4fe43]
|
|
53
|
+
- Updated dependencies [0b4fe43]
|
|
54
|
+
- Updated dependencies [0b4fe43]
|
|
55
|
+
- Updated dependencies [0b4fe43]
|
|
56
|
+
- Updated dependencies [0b4fe43]
|
|
57
|
+
- Updated dependencies [0b4fe43]
|
|
58
|
+
- Updated dependencies [0b4fe43]
|
|
59
|
+
- Updated dependencies [0b4fe43]
|
|
60
|
+
- Updated dependencies [0b4fe43]
|
|
61
|
+
- Updated dependencies [0b4fe43]
|
|
62
|
+
- Updated dependencies [0b4fe43]
|
|
63
|
+
- Updated dependencies [0b4fe43]
|
|
64
|
+
- Updated dependencies [0b4fe43]
|
|
65
|
+
- Updated dependencies [0b4fe43]
|
|
66
|
+
- Updated dependencies [0b4fe43]
|
|
67
|
+
- Updated dependencies [0b4fe43]
|
|
68
|
+
- Updated dependencies [0b4fe43]
|
|
69
|
+
- Updated dependencies [0b4fe43]
|
|
70
|
+
- Updated dependencies [0b4fe43]
|
|
71
|
+
- Updated dependencies [0b4fe43]
|
|
72
|
+
- Updated dependencies [0b4fe43]
|
|
73
|
+
- Updated dependencies [0b4fe43]
|
|
74
|
+
- Updated dependencies [0b4fe43]
|
|
75
|
+
- Updated dependencies [0b4fe43]
|
|
76
|
+
- Updated dependencies [0b4fe43]
|
|
77
|
+
- Updated dependencies [0b4fe43]
|
|
78
|
+
- @ac-kit/algo@0.6.0
|
|
79
|
+
- @ac-kit/core@0.13.0
|
|
80
|
+
- @ac-kit/data@0.5.0
|
|
81
|
+
- @ac-kit/math-analysis@0.2.0
|
|
82
|
+
- @ac-kit/math-scalar@0.4.0
|
|
83
|
+
- @ac-kit/math-random@0.4.1
|
|
84
|
+
|
|
3
85
|
## 0.5.0
|
|
4
86
|
|
|
5
87
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -46,12 +46,14 @@ on its upper one. Infinities land in the outer bins; `NaN` is not counted.
|
|
|
46
46
|
import { histogram } from "@ac-kit/math-stats";
|
|
47
47
|
|
|
48
48
|
histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20]); // [1, 2, 2, 2]
|
|
49
|
+
histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20], { lastClosed: true }); // [1, 2, 3, 1]
|
|
49
50
|
```
|
|
50
51
|
|
|
51
|
-
`histogramBinWidth(values, rule)` gives the bin width of a rule of thumb
|
|
52
|
-
`
|
|
53
|
-
`
|
|
54
|
-
them or zero spread. O(n), O(n
|
|
52
|
+
`histogramBinWidth(values, rule)` gives the bin width of a rule of thumb, as in
|
|
53
|
+
NumPy's `histogram_bin_edges`: `"auto"` (NumPy's default), `"freedman-diaconis"`,
|
|
54
|
+
`"scott"`, `"sturges"`, `"doane"`, `"rice"`, `"sqrt"` or `"stone"`, over the
|
|
55
|
+
finite values, or `NaN` for fewer than two of them or zero spread. O(n), O(n
|
|
56
|
+
log n) for Freedman–Diaconis and `"auto"`, O(n · max(100, √n)) for Stone.
|
|
55
57
|
|
|
56
58
|
```ts
|
|
57
59
|
import { histogramBinWidth } from "@ac-kit/math-stats";
|
|
@@ -59,6 +61,23 @@ import { histogramBinWidth } from "@ac-kit/math-stats";
|
|
|
59
61
|
histogramBinWidth([1, 2, 3, 4, 5, 6, 7, 8], "sturges"); // 1.75
|
|
60
62
|
```
|
|
61
63
|
|
|
64
|
+
`histogramEdges(values, options)` gives the edges themselves, from a `rule`
|
|
65
|
+
(default `"auto"`), a `width` or a `count`, at most `maxCount` bins, over
|
|
66
|
+
`range` or the extent of the finite values. By default they are NumPy's equal
|
|
67
|
+
bins from the low end to the high end; `nice: true` puts them on the multiples
|
|
68
|
+
of a 1, 2 or 5 width instead, as Vega's `bin` does. `NaN` and the infinities
|
|
69
|
+
are ignored, an empty sample covers `[0, 1]`, a single value is widened, and
|
|
70
|
+
where a rule finds no width (a zero interquartile range) Sturges' stands in.
|
|
71
|
+
Its last bin is closed, as `histogram` counts with `lastClosed`.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { histogram, histogramEdges } from "@ac-kit/math-stats";
|
|
75
|
+
|
|
76
|
+
const values = [0.13, 4.2, 9.7, 5];
|
|
77
|
+
const edges = histogramEdges(values, { count: 4, nice: true }); // [0, 5, 10]
|
|
78
|
+
histogram(values, edges, { lastClosed: true }); // [0, 2, 2, 0]
|
|
79
|
+
```
|
|
80
|
+
|
|
62
81
|
## Two groups in a sample
|
|
63
82
|
|
|
64
83
|
`hartiganDip(sorted)` is Hartigan's dip: how far the sample's distribution
|
|
@@ -111,6 +130,26 @@ recent.push(sample);
|
|
|
111
130
|
const threshold = recent.median() * 1.5;
|
|
112
131
|
```
|
|
113
132
|
|
|
133
|
+
## Running statistics
|
|
134
|
+
|
|
135
|
+
`RunningStatistics` keeps the count, mean, variance, minimum and maximum of
|
|
136
|
+
numbers added one at a time, or by range of a column, without copying them. It
|
|
137
|
+
uses Welford's update, so a mean far above the spread (`1e9 + 0.1`) loses
|
|
138
|
+
nothing, and partitions `merge` as if added to one accumulator.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { RunningStatistics } from "@ac-kit/math-stats";
|
|
142
|
+
|
|
143
|
+
const group = new RunningStatistics().addIndexed(column, rows, from, to);
|
|
144
|
+
group.sampleStandardDeviation;
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Its `write…` methods put a statistic in an output array rather than returning
|
|
148
|
+
it, as `writeQuantile(target, index, sorted, fraction, from, to)` does for
|
|
149
|
+
`quantile`: a double returned by a call the engine does not inline is boxed,
|
|
150
|
+
one written to an array is not, so a column of per-group results fills without
|
|
151
|
+
allocating.
|
|
152
|
+
|
|
114
153
|
## Distributions
|
|
115
154
|
|
|
116
155
|
Every family is a factory returning a `Distribution` — `density`, `cdf`,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { HistogramBinRule } from "./histogram.js";
|
|
2
|
+
export declare function differenceOver_(high: number, low: number, divisor: number): number;
|
|
3
|
+
/**
|
|
4
|
+
* Edge `index` of `bins` equal bins over `[low, high]`, as NumPy's `linspace`
|
|
5
|
+
* computes it (`index · step + low`, the last edge `high` itself); a span past
|
|
6
|
+
* `Number.MAX_VALUE` is halved first, exactly.
|
|
7
|
+
*/
|
|
8
|
+
export declare function linspaceEdge_(low: number, high: number, bins: number, index: number): number;
|
|
9
|
+
/**
|
|
10
|
+
* The width `rule` gives the values of `values` within `[low, high]` (finite
|
|
11
|
+
* bounds), as NumPy's estimator of that name does, or `NaN` where NumPy's is 0
|
|
12
|
+
* (fewer than two values, no spread) and for an unknown rule.
|
|
13
|
+
*/
|
|
14
|
+
export declare function ruleWidth_(values: ArrayLike<number>, rule: HistogramBinRule, low: number, high: number): number;
|
|
15
|
+
//# sourceMappingURL=_histogram-rules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_histogram-rules.d.ts","sourceRoot":"","sources":["../src/_histogram-rules.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAMvD,wBAAgB,eAAe,CAC9B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,GACb,MAAM,CAMR;AAoCD;;;;GAIG;AACH,wBAAgB,aAAa,CAC5B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,GACX,MAAM,CAaR;AAwED;;;;GAIG;AACH,wBAAgB,UAAU,CACzB,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,EACzB,IAAI,EAAE,gBAAgB,EACtB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GACV,MAAM,CA+GR"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { scaleByPowerOfTwo } from "@ac-kit/core";
|
|
2
|
+
import { mean } from "./average.js";
|
|
3
|
+
import { populationStandardDeviation } from "./dispersion.js";
|
|
4
|
+
import { quantile } from "./quantile.js";
|
|
5
|
+
// `(high − low) / divisor`, with `high ≥ low`, rounded once unless the
|
|
6
|
+
// difference overflows; then the halves, exact at that magnitude, are
|
|
7
|
+
// subtracted instead.
|
|
8
|
+
export function differenceOver_(high, low, divisor) {
|
|
9
|
+
const difference = high - low;
|
|
10
|
+
if (difference !== Infinity) {
|
|
11
|
+
return difference / divisor;
|
|
12
|
+
}
|
|
13
|
+
return (high / 2 - low / 2) / (divisor / 2);
|
|
14
|
+
}
|
|
15
|
+
function positiveOrNaN_(width) {
|
|
16
|
+
return width > 0 ? width : Number.NaN;
|
|
17
|
+
}
|
|
18
|
+
// The bin `value` falls in among `bins` equal bins over `[low, high]`, as
|
|
19
|
+
// NumPy's `histogram` finds it: the scaled offset truncated, then corrected
|
|
20
|
+
// against the edges, which it can misjudge by one near an edge; the last bin
|
|
21
|
+
// is closed.
|
|
22
|
+
function uniformBin_(value, low, high, bins) {
|
|
23
|
+
const delta = high - low;
|
|
24
|
+
const scaled = delta === Infinity
|
|
25
|
+
? ((value / 2 - low / 2) / (high / 2 - low / 2)) * bins
|
|
26
|
+
: ((value - low) / delta) * bins;
|
|
27
|
+
let bin = Math.trunc(scaled);
|
|
28
|
+
if (bin >= bins) {
|
|
29
|
+
bin = bins - 1;
|
|
30
|
+
}
|
|
31
|
+
if (value < linspaceEdge_(low, high, bins, bin)) {
|
|
32
|
+
bin--;
|
|
33
|
+
}
|
|
34
|
+
else if (bin !== bins - 1 &&
|
|
35
|
+
value >= linspaceEdge_(low, high, bins, bin + 1)) {
|
|
36
|
+
bin++;
|
|
37
|
+
}
|
|
38
|
+
return bin;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Edge `index` of `bins` equal bins over `[low, high]`, as NumPy's `linspace`
|
|
42
|
+
* computes it (`index · step + low`, the last edge `high` itself); a span past
|
|
43
|
+
* `Number.MAX_VALUE` is halved first, exactly.
|
|
44
|
+
*/
|
|
45
|
+
export function linspaceEdge_(low, high, bins, index) {
|
|
46
|
+
if (index === bins) {
|
|
47
|
+
return high;
|
|
48
|
+
}
|
|
49
|
+
const delta = high - low;
|
|
50
|
+
if (delta === Infinity) {
|
|
51
|
+
return 2 * (index * ((high / 2 - low / 2) / bins) + low / 2);
|
|
52
|
+
}
|
|
53
|
+
const step = delta / bins;
|
|
54
|
+
// A step that underflows to zero: NumPy then divides first.
|
|
55
|
+
return step === 0 && delta !== 0
|
|
56
|
+
? (index / bins) * delta + low
|
|
57
|
+
: index * step + low;
|
|
58
|
+
}
|
|
59
|
+
// Stone's rule (Stone, 1984), as NumPy's `_hist_bin_stone`: the bin count in
|
|
60
|
+
// 1 … max(100, ⌊√n⌋) minimising the cross-validated estimate of the integrated
|
|
61
|
+
// squared error, `(2 − (n + 1) · Σ pₖ²) / h`, the first on a tie.
|
|
62
|
+
function stoneWidth_(sample, smallest, largest, low, high) {
|
|
63
|
+
const count = sample.length;
|
|
64
|
+
const upper = Math.max(100, Math.floor(Math.sqrt(count)));
|
|
65
|
+
const counts = new Float64Array(upper);
|
|
66
|
+
// `h` is the spread over the count; a spread past the largest double is
|
|
67
|
+
// halved, the same factor for every count, which keeps the scores' order.
|
|
68
|
+
const spread = largest - smallest;
|
|
69
|
+
const scaledSpread = spread === Infinity ? largest / 2 - smallest / 2 : spread;
|
|
70
|
+
let best = 1;
|
|
71
|
+
let bestScore = Infinity;
|
|
72
|
+
for (let bins = 1; bins <= upper; bins++) {
|
|
73
|
+
counts.fill(0, 0, bins);
|
|
74
|
+
for (const value of sample) {
|
|
75
|
+
counts[uniformBin_(value, low, high, bins)]++;
|
|
76
|
+
}
|
|
77
|
+
let squares = 0;
|
|
78
|
+
for (let bin = 0; bin < bins; bin++) {
|
|
79
|
+
const share = counts[bin] / count;
|
|
80
|
+
squares += share * share;
|
|
81
|
+
}
|
|
82
|
+
const score = (2 - (count + 1) * squares) / (scaledSpread / bins);
|
|
83
|
+
if (score < bestScore) {
|
|
84
|
+
bestScore = score;
|
|
85
|
+
best = bins;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return differenceOver_(largest, smallest, best);
|
|
89
|
+
}
|
|
90
|
+
// The finite sample brought near 1 by a power of two, exactly: the standard
|
|
91
|
+
// deviation overflows on raw values past about 1e154 (its squares) and 1e308
|
|
92
|
+
// (its sum). A value that rounds there moves by under 2⁻¹⁰⁷⁴ of that scale,
|
|
93
|
+
// far below the deviation of any sample whose largest value lies near 1.
|
|
94
|
+
// Returns the exponent, or `undefined` for a sample of zeros.
|
|
95
|
+
function scaleNearOne_(sample) {
|
|
96
|
+
let largest = 0;
|
|
97
|
+
for (const value of sample) {
|
|
98
|
+
largest = Math.max(largest, Math.abs(value));
|
|
99
|
+
}
|
|
100
|
+
if (largest === 0) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const exponent = Math.floor(Math.log2(largest));
|
|
104
|
+
for (let index = 0; index < sample.length; index++) {
|
|
105
|
+
sample[index] = scaleByPowerOfTwo(sample[index], -exponent);
|
|
106
|
+
}
|
|
107
|
+
return exponent;
|
|
108
|
+
}
|
|
109
|
+
function freedmanDiaconis_(sample) {
|
|
110
|
+
const sorted = Float64Array.from(sample).sort();
|
|
111
|
+
return positiveOrNaN_(differenceOver_(quantile(sorted, 0.75), quantile(sorted, 0.25), sample.length ** (1 / 3) / 2));
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The width `rule` gives the values of `values` within `[low, high]` (finite
|
|
115
|
+
* bounds), as NumPy's estimator of that name does, or `NaN` where NumPy's is 0
|
|
116
|
+
* (fewer than two values, no spread) and for an unknown rule.
|
|
117
|
+
*/
|
|
118
|
+
export function ruleWidth_(values, rule, low, high) {
|
|
119
|
+
let count = 0;
|
|
120
|
+
let smallest = Infinity;
|
|
121
|
+
let largest = -Infinity;
|
|
122
|
+
for (let index = 0; index < values.length; index++) {
|
|
123
|
+
const value = values[index];
|
|
124
|
+
if (value >= low && value <= high) {
|
|
125
|
+
count++;
|
|
126
|
+
smallest = Math.min(smallest, value);
|
|
127
|
+
largest = Math.max(largest, value);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (count < 2) {
|
|
131
|
+
return Number.NaN;
|
|
132
|
+
}
|
|
133
|
+
const sample = () => {
|
|
134
|
+
const kept = [];
|
|
135
|
+
for (let index = 0; index < values.length; index++) {
|
|
136
|
+
const value = values[index];
|
|
137
|
+
if (value >= low && value <= high) {
|
|
138
|
+
kept.push(value);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return kept;
|
|
142
|
+
};
|
|
143
|
+
switch (rule) {
|
|
144
|
+
case "sturges":
|
|
145
|
+
return positiveOrNaN_(differenceOver_(largest, smallest, Math.log2(count) + 1));
|
|
146
|
+
case "rice":
|
|
147
|
+
return positiveOrNaN_(differenceOver_(largest, smallest, 2 * count ** (1 / 3)));
|
|
148
|
+
case "sqrt":
|
|
149
|
+
return positiveOrNaN_(differenceOver_(largest, smallest, Math.sqrt(count)));
|
|
150
|
+
case "scott": {
|
|
151
|
+
const scaled = sample();
|
|
152
|
+
const exponent = scaleNearOne_(scaled);
|
|
153
|
+
if (exponent === undefined) {
|
|
154
|
+
return Number.NaN;
|
|
155
|
+
}
|
|
156
|
+
const width = ((24 * Math.sqrt(Math.PI)) / count) ** (1 / 3) *
|
|
157
|
+
populationStandardDeviation(scaled);
|
|
158
|
+
return positiveOrNaN_(scaleByPowerOfTwo(width, exponent));
|
|
159
|
+
}
|
|
160
|
+
case "doane": {
|
|
161
|
+
if (count < 3) {
|
|
162
|
+
return Number.NaN;
|
|
163
|
+
}
|
|
164
|
+
// The skewness does not change with the scale, so it is measured on
|
|
165
|
+
// the sample brought near 1.
|
|
166
|
+
const scaled = sample();
|
|
167
|
+
if (scaleNearOne_(scaled) === undefined) {
|
|
168
|
+
return Number.NaN;
|
|
169
|
+
}
|
|
170
|
+
const sigma = populationStandardDeviation(scaled);
|
|
171
|
+
if (!(sigma > 0)) {
|
|
172
|
+
return Number.NaN;
|
|
173
|
+
}
|
|
174
|
+
const center = mean(scaled);
|
|
175
|
+
let cubes = 0;
|
|
176
|
+
for (const value of scaled) {
|
|
177
|
+
const standardised = (value - center) / sigma;
|
|
178
|
+
cubes += standardised * standardised * standardised;
|
|
179
|
+
}
|
|
180
|
+
const skewness = cubes / count;
|
|
181
|
+
const skewnessError = Math.sqrt((6 * (count - 2)) / ((count + 1) * (count + 3)));
|
|
182
|
+
return positiveOrNaN_(differenceOver_(largest, smallest, 1 +
|
|
183
|
+
Math.log2(count) +
|
|
184
|
+
Math.log2(1 + Math.abs(skewness) / skewnessError)));
|
|
185
|
+
}
|
|
186
|
+
case "freedman-diaconis":
|
|
187
|
+
return freedmanDiaconis_(sample());
|
|
188
|
+
case "auto": {
|
|
189
|
+
// NumPy's: the Freedman–Diaconis width, but at least half the
|
|
190
|
+
// square-root rule's (so a near-zero interquartile range cannot ask
|
|
191
|
+
// for a great many bins), capped by Sturges'.
|
|
192
|
+
const sturges = differenceOver_(largest, smallest, Math.log2(count) + 1);
|
|
193
|
+
if (!(sturges > 0)) {
|
|
194
|
+
return Number.NaN;
|
|
195
|
+
}
|
|
196
|
+
const freedmanDiaconis = freedmanDiaconis_(sample());
|
|
197
|
+
const sqrtHalf = differenceOver_(largest, smallest, Math.sqrt(count)) / 2;
|
|
198
|
+
return Math.min(Math.max(freedmanDiaconis === freedmanDiaconis ? freedmanDiaconis : 0, sqrtHalf), sturges);
|
|
199
|
+
}
|
|
200
|
+
case "stone":
|
|
201
|
+
return smallest === largest
|
|
202
|
+
? Number.NaN
|
|
203
|
+
: positiveOrNaN_(stoneWidth_(sample(), smallest, largest, low, high));
|
|
204
|
+
default:
|
|
205
|
+
return Number.NaN;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=_histogram-rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_histogram-rules.js","sourceRoot":"","sources":["../src/_histogram-rules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AAE9D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,uEAAuE;AACvE,sEAAsE;AACtE,sBAAsB;AACtB,MAAM,UAAU,eAAe,CAC9B,IAAY,EACZ,GAAW,EACX,OAAe;IAEf,MAAM,UAAU,GAAG,IAAI,GAAG,GAAG,CAAC;IAC9B,IAAI,UAAU,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,UAAU,GAAG,OAAO,CAAC;IAC7B,CAAC;IACD,OAAO,CAAC,IAAI,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC;AAC7C,CAAC;AAED,SAAS,cAAc,CAAC,KAAa;IACpC,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;AACvC,CAAC;AAED,0EAA0E;AAC1E,4EAA4E;AAC5E,6EAA6E;AAC7E,aAAa;AACb,SAAS,WAAW,CACnB,KAAa,EACb,GAA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;;;;;;;;;GAUG;AACH,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,SAAS,GAAG,UAAU,GAAG,MAAM,CAAC;AAE7E,MAAM,MAAM,0BAA0B,GAAG;IACxC,OAAO,CAAC,EAAE,mBAAmB,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,mBAAmB,CAClC,CAAC,EAAE,MAAM,EACT,WAAW,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,0BAA0B,GAClC,MAAM,EAAE,EAAE,
|
|
1
|
+
{"version":3,"file":"hierarchical-cluster.d.ts","sourceRoot":"","sources":["../src/hierarchical-cluster.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,SAAS,GAAG,UAAU,GAAG,MAAM,CAAC;AAE7E,MAAM,MAAM,0BAA0B,GAAG;IACxC,OAAO,CAAC,EAAE,mBAAmB,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,mBAAmB,CAClC,CAAC,EAAE,MAAM,EACT,WAAW,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,0BAA0B,GAClC,MAAM,EAAE,EAAE,CAiGZ"}
|
|
@@ -18,8 +18,9 @@ import { dotPrecise } from "@ac-kit/math-scalar";
|
|
|
18
18
|
* @returns Array of clusters; each cluster is an array of original indices.
|
|
19
19
|
*/
|
|
20
20
|
export function hierarchicalCluster(n, getDistance, threshold, options) {
|
|
21
|
-
if (n === 0)
|
|
21
|
+
if (n === 0) {
|
|
22
22
|
return [];
|
|
23
|
+
}
|
|
23
24
|
const linkage = options?.linkage ?? "average";
|
|
24
25
|
// Build a symmetric n×n distance matrix.
|
|
25
26
|
// clusterDist[i][j] holds the current inter-cluster distance (same value at [j][i]).
|
|
@@ -42,11 +43,13 @@ export function hierarchicalCluster(n, getDistance, threshold, options) {
|
|
|
42
43
|
let mergeA = -1;
|
|
43
44
|
let mergeB = -1;
|
|
44
45
|
for (let i = 0; i < n; i++) {
|
|
45
|
-
if (!active[i])
|
|
46
|
+
if (!active[i]) {
|
|
46
47
|
continue;
|
|
48
|
+
}
|
|
47
49
|
for (let j = i + 1; j < n; j++) {
|
|
48
|
-
if (!active[j])
|
|
50
|
+
if (!active[j]) {
|
|
49
51
|
continue;
|
|
52
|
+
}
|
|
50
53
|
const d = clusterDist[i][j];
|
|
51
54
|
if (d < minDist) {
|
|
52
55
|
minDist = d;
|
|
@@ -55,16 +58,18 @@ export function hierarchicalCluster(n, getDistance, threshold, options) {
|
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
}
|
|
58
|
-
if (mergeA === -1 || minDist >= threshold)
|
|
61
|
+
if (mergeA === -1 || minDist >= threshold) {
|
|
59
62
|
break;
|
|
63
|
+
}
|
|
60
64
|
const nA = sizes[mergeA];
|
|
61
65
|
const nB = sizes[mergeB];
|
|
62
66
|
const dAB = clusterDist[mergeA][mergeB];
|
|
63
67
|
// Update distances from the merged cluster (mergeA absorbs mergeB) to all remaining
|
|
64
68
|
// active clusters using the Lance–Williams recurrence for the chosen linkage.
|
|
65
69
|
for (let c = 0; c < n; c++) {
|
|
66
|
-
if (!active[c] || c === mergeA || c === mergeB)
|
|
70
|
+
if (!active[c] || c === mergeA || c === mergeB) {
|
|
67
71
|
continue;
|
|
72
|
+
}
|
|
68
73
|
const nC = sizes[c];
|
|
69
74
|
const dAC = clusterDist[mergeA][c];
|
|
70
75
|
const dBC = clusterDist[mergeB][c];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hierarchical-cluster.js","sourceRoot":"","sources":["../src/hierarchical-cluster.ts"],"names":[],"mappings":"AAAA,OAA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GAAG,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;YAC7C,CAAC;iBAAM,CAAC;gBACP,8EAA8E;gBAC9E,mFAAmF;gBACnF,MAAM,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;gBAC1B,MAAM,MAAM,GACX,UAAU,CACT,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC,EAC/C,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CACf,GAAG,IAAI,CAAC;gBACV,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;YAC1C,CAAC;YAED,WAAW,CAAC,MAAM,CAAE,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;YAClC,WAAW,CAAC,CAAC,CAAE,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;QACnC,CAAC;QAED,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC;QACxB,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,CAAE,EAAE,GAAG,OAAO,CAAC,MAAM,CAAE,CAAC,CAAC;QAC7D,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { HistogramBinRule } from "./histogram.js";
|
|
2
|
+
/** Options of {@link histogramEdges}. */
|
|
3
|
+
export type HistogramEdgesOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* The rule of thumb that picks the bin width. Default `"auto"`, NumPy's.
|
|
6
|
+
* Excludes `width` and `count`.
|
|
7
|
+
*/
|
|
8
|
+
readonly rule?: HistogramBinRule | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The bin width itself, finite and positive. Excludes `rule`, `count` and
|
|
11
|
+
* `maxCount`.
|
|
12
|
+
*/
|
|
13
|
+
readonly width?: number | undefined;
|
|
14
|
+
/** The bin count itself, an integer ≥ 1. Excludes `rule` and `width`. */
|
|
15
|
+
readonly count?: number | undefined;
|
|
16
|
+
/** The most bins, an integer ≥ 1. Excludes `width`. */
|
|
17
|
+
readonly maxCount?: number | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Round edges, as Vega's `bin` places them: a width of 1, 2 or 5 times a
|
|
20
|
+
* power of ten, edges on its multiples. Default `false`, NumPy's equal bins
|
|
21
|
+
* from the low end of the range to the high end.
|
|
22
|
+
*/
|
|
23
|
+
readonly nice?: boolean | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The `[low, high]` the bins cover, finite with `low ≤ high`; values outside
|
|
26
|
+
* it are left out of the rule's sample. Default the extent of the finite
|
|
27
|
+
* values.
|
|
28
|
+
*/
|
|
29
|
+
readonly range?: readonly [low: number, high: number] | undefined;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The edges of a histogram's bins over the finite values of `values`: equal
|
|
33
|
+
* bins as NumPy's `histogram_bin_edges` cuts them, or, with `nice`, round edges
|
|
34
|
+
* as Vega's `bin` places them. The bins are half-open, `[edges[i], edges[i +
|
|
35
|
+
* 1])`, but for the last, which is closed; {@link histogram} counts into them
|
|
36
|
+
* with `lastClosed`.
|
|
37
|
+
*
|
|
38
|
+
* **The range** is `range`, or the extent of the finite values; `[0, 1]` when
|
|
39
|
+
* there are none. `NaN` and the infinities are ignored, there and in the rule's
|
|
40
|
+
* sample, where NumPy refuses a range that is not finite. An empty range (a
|
|
41
|
+
* single value) is widened to `[x − 0.5, x + 0.5]`, as NumPy does (to the
|
|
42
|
+
* neighbouring doubles where 0.5 is below their spacing); with `nice`, it takes
|
|
43
|
+
* the value's magnitude (or 1 for 0) as its span, as Vega does.
|
|
44
|
+
*
|
|
45
|
+
* **The width** is `width`; or the range over `count`; or the `rule`'s over the
|
|
46
|
+
* values within the range ({@link histogramBinWidth}, default `"auto"`). Where
|
|
47
|
+
* a rule finds no width though the values spread (Freedman–Diaconis' zero
|
|
48
|
+
* interquartile range, Doane's fewer than three values, Stone's), Sturges'
|
|
49
|
+
* stands in, where NumPy would make one bin; where none can (fewer than two
|
|
50
|
+
* values in the range, or all equal), there is one bin, or with `nice` as many
|
|
51
|
+
* as `maxCount` allows.
|
|
52
|
+
*
|
|
53
|
+
* **Equal bins** (the default) run from the low end to the high end, their
|
|
54
|
+
* count the range over the width rounded up and at most `maxCount`; edge `i` is
|
|
55
|
+
* `low + i · (high − low) / count`, the last `high` itself, as NumPy's
|
|
56
|
+
* `linspace`. A `width` instead gives edges `low + i · width`, the last at or
|
|
57
|
+
* past `high`.
|
|
58
|
+
*
|
|
59
|
+
* **Nice bins** have a width of 1, 2 or 5 times a power of ten, the smallest at
|
|
60
|
+
* least the rule's or `count`'s (within 10⁻¹²), widened along that sequence
|
|
61
|
+
* until there are at most `maxCount` bins; edges are its multiples, from the
|
|
62
|
+
* one at or below the range to the one at or above it, a step below 1 dividing
|
|
63
|
+
* an integer index so that 0.3 is 0.3. A `width` is kept as given, its edges on
|
|
64
|
+
* its multiples (exact quotients when it is within 10⁻⁹ of the reciprocal of an
|
|
65
|
+
* integer). A range across 0 makes two bins at least; a `maxCount` of 1 joins
|
|
66
|
+
* them into one, `[−s, s]`.
|
|
67
|
+
*
|
|
68
|
+
* Edges are strictly ascending and finite: a range holding fewer doubles than
|
|
69
|
+
* bins has the edges that round together merged. With `nice`, a single value
|
|
70
|
+
* where the multiples of the width are closer than the doubles (a `width` of
|
|
71
|
+
* 0.1 at 10¹⁶) gets one bin from the value to the next double up (from the one
|
|
72
|
+
* below at `Number.MAX_VALUE`), both roundings of multiples: always two edges
|
|
73
|
+
* around it, as without `nice`.
|
|
74
|
+
*
|
|
75
|
+
* Time O(n + k) for `n` values and `k` bins, plus the rule's
|
|
76
|
+
* ({@link histogramBinWidth}); memory O(k), plus the rule's.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* histogramEdges([1, 2, 2, 3, 7], { count: 3 }); // [1, 3, 5, 7]
|
|
80
|
+
* histogramEdges([0.13, 4.2, 9.7], { count: 4, nice: true }); // [0, 5, 10]
|
|
81
|
+
*
|
|
82
|
+
* @param values - The sample, in any order; a typed array is read as is. Not
|
|
83
|
+
* modified.
|
|
84
|
+
* @param options - How to choose the bins.
|
|
85
|
+
* @returns At least two edges, strictly ascending.
|
|
86
|
+
* @throws {RangeError} For a `width` that is not finite and positive, a `count`
|
|
87
|
+
* or `maxCount` that is not an integer ≥ 1, an unknown `rule`, a `range` that
|
|
88
|
+
* is not finite and ordered, options that exclude each other, more than 2³² −
|
|
89
|
+
* 2 bins, and edges that would leave the double range.
|
|
90
|
+
*/
|
|
91
|
+
export declare function histogramEdges(values: ArrayLike<number>, options?: HistogramEdgesOptions): Float64Array;
|
|
92
|
+
//# sourceMappingURL=histogram-edges.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"histogram-edges.d.ts","sourceRoot":"","sources":["../src/histogram-edges.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,yCAAyC;AACzC,MAAM,MAAM,qBAAqB,GAAG;IACnC;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAC7C;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,yEAAyE;IACzE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,uDAAuD;IACvD,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACpC;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;CAClE,CAAC;AAoSF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,EACzB,OAAO,GAAE,qBAA0B,GACjC,YAAY,CA6Bd"}
|