@ac-kit/math-stats 0.4.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 +229 -0
- package/README.md +106 -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/_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/_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/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 +80 -4
- package/dist/histogram.d.ts.map +1 -1
- package/dist/histogram.js +86 -9
- 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 +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/kmeans.d.ts.map +1 -1
- package/dist/kmeans.js +12 -7
- package/dist/kmeans.js.map +1 -1
- package/dist/kmedoids.js +2 -1
- package/dist/kmedoids.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 +26 -2
- package/dist/quantile.d.ts.map +1 -1
- package/dist/quantile.js +69 -9
- 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/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/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
|
@@ -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"}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { assertInteger, assertPositive, describeValue, nextAfter, } from "@ac-kit/core";
|
|
2
|
+
import { tickIncrement } from "@ac-kit/math-scalar";
|
|
3
|
+
import { differenceOver_, linspaceEdge_, ruleWidth_, } from "./_histogram-rules.js";
|
|
4
|
+
// More bins than this cannot be held (their edges would fill 32 GiB).
|
|
5
|
+
const MAX_BINS_ = 2 ** 32 - 2;
|
|
6
|
+
// A rule's width within this relative distance of a round step takes that step:
|
|
7
|
+
// the width is computed in floating point, the step exact.
|
|
8
|
+
const ROUND_TOLERANCE_ = 1e-12;
|
|
9
|
+
const RULES_ = new Set([
|
|
10
|
+
"auto",
|
|
11
|
+
"freedman-diaconis",
|
|
12
|
+
"scott",
|
|
13
|
+
"sturges",
|
|
14
|
+
"doane",
|
|
15
|
+
"rice",
|
|
16
|
+
"sqrt",
|
|
17
|
+
"stone",
|
|
18
|
+
]);
|
|
19
|
+
function gridOfIncrement_(increment) {
|
|
20
|
+
return increment < 0
|
|
21
|
+
? { step: 1 / -increment, divisor: -increment }
|
|
22
|
+
: { step: increment, divisor: 0 };
|
|
23
|
+
}
|
|
24
|
+
// A width given as is; one within 10⁻⁹ of the reciprocal of an integer puts its
|
|
25
|
+
// edges at exact quotients.
|
|
26
|
+
function gridOfWidth_(width) {
|
|
27
|
+
const divisor = Math.round(1 / width);
|
|
28
|
+
return width < 1 && Math.abs(1 / width - divisor) < 1e-9 * divisor
|
|
29
|
+
? { step: 1 / divisor, divisor }
|
|
30
|
+
: { step: width, divisor: 0 };
|
|
31
|
+
}
|
|
32
|
+
function gridEdge_(grid, index) {
|
|
33
|
+
return grid.divisor === 0 ? index * grid.step : index / grid.divisor;
|
|
34
|
+
}
|
|
35
|
+
// The 1, 2 or 5 times a power of ten nearest `width`, on a logarithmic scale
|
|
36
|
+
// (`[0, width]` holds a multiple of every step, 0, so none is skipped).
|
|
37
|
+
function nearestRoundGrid_(width) {
|
|
38
|
+
const increment = tickIncrement(0, width, 1);
|
|
39
|
+
return increment === increment ? gridOfIncrement_(increment) : undefined;
|
|
40
|
+
}
|
|
41
|
+
// The next of 1, 2, 5, 10, … after a step of that sequence.
|
|
42
|
+
function nextRoundGrid_(grid) {
|
|
43
|
+
return nearestRoundGrid_(grid.step * 2.5);
|
|
44
|
+
}
|
|
45
|
+
// The smallest 1, 2 or 5 times a power of ten at least `width`.
|
|
46
|
+
function roundGridAtLeast_(width) {
|
|
47
|
+
let grid = nearestRoundGrid_(width);
|
|
48
|
+
while (grid !== undefined && grid.step < width * (1 - ROUND_TOLERANCE_)) {
|
|
49
|
+
grid = nextRoundGrid_(grid);
|
|
50
|
+
}
|
|
51
|
+
return grid;
|
|
52
|
+
}
|
|
53
|
+
// The indices of the multiples of `grid` at or below `low` and at or above
|
|
54
|
+
// `high`, checked against the edges themselves; at least one bin apart.
|
|
55
|
+
function cover_(grid, low, high) {
|
|
56
|
+
const scale = (value) => grid.divisor === 0 ? value / grid.step : value * grid.divisor;
|
|
57
|
+
let first = Math.floor(scale(low));
|
|
58
|
+
if (gridEdge_(grid, first + 1) <= low) {
|
|
59
|
+
first++;
|
|
60
|
+
}
|
|
61
|
+
else if (gridEdge_(grid, first) > low) {
|
|
62
|
+
first--;
|
|
63
|
+
}
|
|
64
|
+
let last = Math.ceil(scale(high));
|
|
65
|
+
if (gridEdge_(grid, last - 1) >= high) {
|
|
66
|
+
last--;
|
|
67
|
+
}
|
|
68
|
+
else if (gridEdge_(grid, last) < high) {
|
|
69
|
+
last++;
|
|
70
|
+
}
|
|
71
|
+
return { first, last: Math.max(last, first + 1) };
|
|
72
|
+
}
|
|
73
|
+
function assertBinCount_(bins) {
|
|
74
|
+
if (!(bins <= MAX_BINS_)) {
|
|
75
|
+
throw new RangeError(`histogramEdges: ${describeValue(bins)} bins, more than ${MAX_BINS_}; pass a maxCount`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Merges edges a range too narrow for its bin count rounds onto one double,
|
|
79
|
+
// so the edges stay strictly ascending. The first and last differ, so both
|
|
80
|
+
// stay.
|
|
81
|
+
function strictlyAscending_(edges) {
|
|
82
|
+
let kept = 1;
|
|
83
|
+
for (let index = 1; index < edges.length; index++) {
|
|
84
|
+
if (edges[index] > edges[kept - 1]) {
|
|
85
|
+
edges[kept++] = edges[index];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return kept === edges.length ? edges : edges.slice(0, kept);
|
|
89
|
+
}
|
|
90
|
+
function checkOptions_(options) {
|
|
91
|
+
const { rule, width, count, maxCount, range } = options;
|
|
92
|
+
if (width !== undefined) {
|
|
93
|
+
assertPositive(width, "histogramEdges: width");
|
|
94
|
+
if (rule !== undefined || count !== undefined || maxCount !== undefined) {
|
|
95
|
+
throw new RangeError("histogramEdges: width excludes rule, count and maxCount");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (count !== undefined) {
|
|
99
|
+
assertInteger(count, "histogramEdges: count", 1);
|
|
100
|
+
if (rule !== undefined) {
|
|
101
|
+
throw new RangeError("histogramEdges: count excludes rule");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (maxCount !== undefined) {
|
|
105
|
+
assertInteger(maxCount, "histogramEdges: maxCount", 1);
|
|
106
|
+
}
|
|
107
|
+
if (rule !== undefined && !RULES_.has(rule)) {
|
|
108
|
+
throw new RangeError(`histogramEdges: unknown rule ${describeValue(rule)}`);
|
|
109
|
+
}
|
|
110
|
+
if (range !== undefined) {
|
|
111
|
+
const [low, high] = range;
|
|
112
|
+
if (!(low - low === 0 && high - high === 0 && low <= high)) {
|
|
113
|
+
throw new RangeError(`histogramEdges: range must be finite with low ≤ high, got [${describeValue(low)}, ${describeValue(high)}]`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// The width the rule gives the sample; Sturges' where the rule finds none
|
|
118
|
+
// though there is a spread (a zero interquartile range, fewer than three
|
|
119
|
+
// values for Doane's).
|
|
120
|
+
function chosenWidth_(values, rule, low, high) {
|
|
121
|
+
const width = ruleWidth_(values, rule, low, high);
|
|
122
|
+
return width === width || rule === "sturges"
|
|
123
|
+
? width
|
|
124
|
+
: ruleWidth_(values, "sturges", low, high);
|
|
125
|
+
}
|
|
126
|
+
function plainEdges_(values, options, low, high) {
|
|
127
|
+
const { width, count, maxCount } = options;
|
|
128
|
+
if (width !== undefined) {
|
|
129
|
+
let bins = Math.max(1, Math.ceil(differenceOver_(high, low, width)));
|
|
130
|
+
assertBinCount_(bins);
|
|
131
|
+
while (low + bins * width < high) {
|
|
132
|
+
bins++;
|
|
133
|
+
}
|
|
134
|
+
const edges = new Float64Array(bins + 1);
|
|
135
|
+
for (let index = 0; index <= bins; index++) {
|
|
136
|
+
edges[index] = low + index * width;
|
|
137
|
+
}
|
|
138
|
+
if (edges[bins] === Infinity) {
|
|
139
|
+
throw new RangeError(`histogramEdges: bins of width ${width} from ${low} leave the double range`);
|
|
140
|
+
}
|
|
141
|
+
return strictlyAscending_(edges);
|
|
142
|
+
}
|
|
143
|
+
let bins = count;
|
|
144
|
+
if (bins === undefined) {
|
|
145
|
+
const ruleWidth = chosenWidth_(values, options.rule ?? "auto", low, high);
|
|
146
|
+
bins =
|
|
147
|
+
ruleWidth === ruleWidth
|
|
148
|
+
? Math.max(1, Math.ceil(differenceOver_(high, low, ruleWidth)))
|
|
149
|
+
: 1;
|
|
150
|
+
}
|
|
151
|
+
if (maxCount !== undefined) {
|
|
152
|
+
bins = Math.min(bins, maxCount);
|
|
153
|
+
}
|
|
154
|
+
assertBinCount_(bins);
|
|
155
|
+
const edges = new Float64Array(bins + 1);
|
|
156
|
+
for (let index = 0; index <= bins; index++) {
|
|
157
|
+
edges[index] = linspaceEdge_(low, high, bins, index);
|
|
158
|
+
}
|
|
159
|
+
return strictlyAscending_(edges);
|
|
160
|
+
}
|
|
161
|
+
function niceEdges_(values, options, low, high) {
|
|
162
|
+
const { width, count, maxCount } = options;
|
|
163
|
+
// A single value takes its own magnitude as the span, as Vega's does.
|
|
164
|
+
const spanOver = (divisor) => low === high
|
|
165
|
+
? (Math.abs(low) || 1) / divisor
|
|
166
|
+
: differenceOver_(high, low, divisor);
|
|
167
|
+
let grid;
|
|
168
|
+
if (width === undefined) {
|
|
169
|
+
let target;
|
|
170
|
+
if (count === undefined) {
|
|
171
|
+
target = chosenWidth_(values, options.rule ?? "auto", low, high);
|
|
172
|
+
if (target !== target) {
|
|
173
|
+
target = spanOver(maxCount ?? 1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
target = spanOver(count);
|
|
178
|
+
}
|
|
179
|
+
grid = roundGridAtLeast_(target);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
grid = gridOfWidth_(width);
|
|
183
|
+
}
|
|
184
|
+
const magnitude = Math.max(Math.abs(low), Math.abs(high));
|
|
185
|
+
for (;;) {
|
|
186
|
+
if (grid === undefined) {
|
|
187
|
+
throw new RangeError(`histogramEdges: no round width covers [${low}, ${high}] within the double range`);
|
|
188
|
+
}
|
|
189
|
+
// A grid finer than the doubles at a single value, whose multiples'
|
|
190
|
+
// indices may not even be finite.
|
|
191
|
+
if (low === high &&
|
|
192
|
+
low !== 0 &&
|
|
193
|
+
grid.step <= Math.abs(low - nextAfter(low, 0))) {
|
|
194
|
+
return singleValueEdges_(low);
|
|
195
|
+
}
|
|
196
|
+
const { first, last } = cover_(grid, low, high);
|
|
197
|
+
const bins = last - first;
|
|
198
|
+
if (maxCount === undefined || bins <= maxCount) {
|
|
199
|
+
assertBinCount_(bins);
|
|
200
|
+
const edges = gridEdges_(grid, first, last, 1);
|
|
201
|
+
// across a binade boundary, multiples a little coarser still merge
|
|
202
|
+
return edges.length > 1 ? edges : singleValueEdges_(edges[0]);
|
|
203
|
+
}
|
|
204
|
+
// A step past every value leaves a range across 0 two bins, [−s, 0]
|
|
205
|
+
// and [0, s]; with one bin allowed, they make one.
|
|
206
|
+
if (grid.step >= magnitude) {
|
|
207
|
+
return gridEdges_(grid, first, last, last - first);
|
|
208
|
+
}
|
|
209
|
+
grid = nextRoundGrid_(grid);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// A single value where the width is finer than the doubles' spacing has its
|
|
213
|
+
// enclosing multiples round onto itself. Its neighbouring double is then the
|
|
214
|
+
// rounding of a multiple too (the multiples are closer than the doubles), so
|
|
215
|
+
// the bin runs from the value to the next double up, or from the one below
|
|
216
|
+
// at `Number.MAX_VALUE`.
|
|
217
|
+
function singleValueEdges_(value) {
|
|
218
|
+
const above = nextAfter(value, Infinity);
|
|
219
|
+
return above === Infinity
|
|
220
|
+
? Float64Array.of(nextAfter(value, -Infinity), value)
|
|
221
|
+
: Float64Array.of(value, above);
|
|
222
|
+
}
|
|
223
|
+
// The multiples of `grid` from index `first` to `last`, every `stride`th.
|
|
224
|
+
function gridEdges_(grid, first, last, stride) {
|
|
225
|
+
const edges = new Float64Array((last - first) / stride + 1);
|
|
226
|
+
for (let position = 0; position < edges.length; position++) {
|
|
227
|
+
edges[position] = gridEdge_(grid, first + position * stride);
|
|
228
|
+
}
|
|
229
|
+
if (edges[0] === -Infinity || edges[edges.length - 1] === Infinity) {
|
|
230
|
+
throw new RangeError(`histogramEdges: the multiples of ${grid.step} covering the range leave the double range`);
|
|
231
|
+
}
|
|
232
|
+
return strictlyAscending_(edges);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* The edges of a histogram's bins over the finite values of `values`: equal
|
|
236
|
+
* bins as NumPy's `histogram_bin_edges` cuts them, or, with `nice`, round edges
|
|
237
|
+
* as Vega's `bin` places them. The bins are half-open, `[edges[i], edges[i +
|
|
238
|
+
* 1])`, but for the last, which is closed; {@link histogram} counts into them
|
|
239
|
+
* with `lastClosed`.
|
|
240
|
+
*
|
|
241
|
+
* **The range** is `range`, or the extent of the finite values; `[0, 1]` when
|
|
242
|
+
* there are none. `NaN` and the infinities are ignored, there and in the rule's
|
|
243
|
+
* sample, where NumPy refuses a range that is not finite. An empty range (a
|
|
244
|
+
* single value) is widened to `[x − 0.5, x + 0.5]`, as NumPy does (to the
|
|
245
|
+
* neighbouring doubles where 0.5 is below their spacing); with `nice`, it takes
|
|
246
|
+
* the value's magnitude (or 1 for 0) as its span, as Vega does.
|
|
247
|
+
*
|
|
248
|
+
* **The width** is `width`; or the range over `count`; or the `rule`'s over the
|
|
249
|
+
* values within the range ({@link histogramBinWidth}, default `"auto"`). Where
|
|
250
|
+
* a rule finds no width though the values spread (Freedman–Diaconis' zero
|
|
251
|
+
* interquartile range, Doane's fewer than three values, Stone's), Sturges'
|
|
252
|
+
* stands in, where NumPy would make one bin; where none can (fewer than two
|
|
253
|
+
* values in the range, or all equal), there is one bin, or with `nice` as many
|
|
254
|
+
* as `maxCount` allows.
|
|
255
|
+
*
|
|
256
|
+
* **Equal bins** (the default) run from the low end to the high end, their
|
|
257
|
+
* count the range over the width rounded up and at most `maxCount`; edge `i` is
|
|
258
|
+
* `low + i · (high − low) / count`, the last `high` itself, as NumPy's
|
|
259
|
+
* `linspace`. A `width` instead gives edges `low + i · width`, the last at or
|
|
260
|
+
* past `high`.
|
|
261
|
+
*
|
|
262
|
+
* **Nice bins** have a width of 1, 2 or 5 times a power of ten, the smallest at
|
|
263
|
+
* least the rule's or `count`'s (within 10⁻¹²), widened along that sequence
|
|
264
|
+
* until there are at most `maxCount` bins; edges are its multiples, from the
|
|
265
|
+
* one at or below the range to the one at or above it, a step below 1 dividing
|
|
266
|
+
* an integer index so that 0.3 is 0.3. A `width` is kept as given, its edges on
|
|
267
|
+
* its multiples (exact quotients when it is within 10⁻⁹ of the reciprocal of an
|
|
268
|
+
* integer). A range across 0 makes two bins at least; a `maxCount` of 1 joins
|
|
269
|
+
* them into one, `[−s, s]`.
|
|
270
|
+
*
|
|
271
|
+
* Edges are strictly ascending and finite: a range holding fewer doubles than
|
|
272
|
+
* bins has the edges that round together merged. With `nice`, a single value
|
|
273
|
+
* where the multiples of the width are closer than the doubles (a `width` of
|
|
274
|
+
* 0.1 at 10¹⁶) gets one bin from the value to the next double up (from the one
|
|
275
|
+
* below at `Number.MAX_VALUE`), both roundings of multiples: always two edges
|
|
276
|
+
* around it, as without `nice`.
|
|
277
|
+
*
|
|
278
|
+
* Time O(n + k) for `n` values and `k` bins, plus the rule's
|
|
279
|
+
* ({@link histogramBinWidth}); memory O(k), plus the rule's.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* histogramEdges([1, 2, 2, 3, 7], { count: 3 }); // [1, 3, 5, 7]
|
|
283
|
+
* histogramEdges([0.13, 4.2, 9.7], { count: 4, nice: true }); // [0, 5, 10]
|
|
284
|
+
*
|
|
285
|
+
* @param values - The sample, in any order; a typed array is read as is. Not
|
|
286
|
+
* modified.
|
|
287
|
+
* @param options - How to choose the bins.
|
|
288
|
+
* @returns At least two edges, strictly ascending.
|
|
289
|
+
* @throws {RangeError} For a `width` that is not finite and positive, a `count`
|
|
290
|
+
* or `maxCount` that is not an integer ≥ 1, an unknown `rule`, a `range` that
|
|
291
|
+
* is not finite and ordered, options that exclude each other, more than 2³² −
|
|
292
|
+
* 2 bins, and edges that would leave the double range.
|
|
293
|
+
*/
|
|
294
|
+
export function histogramEdges(values, options = {}) {
|
|
295
|
+
checkOptions_(options);
|
|
296
|
+
let low = Infinity;
|
|
297
|
+
let high = -Infinity;
|
|
298
|
+
if (options.range === undefined) {
|
|
299
|
+
for (let index = 0; index < values.length; index++) {
|
|
300
|
+
const value = values[index];
|
|
301
|
+
if (value - value === 0) {
|
|
302
|
+
low = Math.min(low, value);
|
|
303
|
+
high = Math.max(high, value);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (low > high) {
|
|
307
|
+
low = 0;
|
|
308
|
+
high = 1;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
[low, high] = options.range;
|
|
313
|
+
}
|
|
314
|
+
if (options.nice === true) {
|
|
315
|
+
return niceEdges_(values, options, low, high);
|
|
316
|
+
}
|
|
317
|
+
if (low === high) {
|
|
318
|
+
const below = low - 0.5;
|
|
319
|
+
const above = high + 0.5;
|
|
320
|
+
low = below === low ? nextAfter(low, -Infinity) : below;
|
|
321
|
+
high = above === high ? nextAfter(high, Infinity) : above;
|
|
322
|
+
}
|
|
323
|
+
return plainEdges_(values, options, low, high);
|
|
324
|
+
}
|
|
325
|
+
//# sourceMappingURL=histogram-edges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"histogram-edges.js","sourceRoot":"","sources":["../src/histogram-edges.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,aAAa,EACb,cAAc,EACd,aAAa,EACb,SAAS,GACT,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EACN,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BACzB,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;gBAC3B,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAC9B,CAAC;QACF,CAAC;QACD,IAAI,GAAG,GAAG,IAAI,EAAE,CAAC;YAChB,GAAG,GAAG,CAAC,CAAC;YACR,IAAI,GAAG,CAAC,CAAC;QACV,CAAC;IACF,CAAC;SAAM,CAAC;QACP,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC;IAC7B,CAAC;IACD,IAAI,OAAO,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QAC3B,OAAO,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC;IACD,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QAClB,MAAM,KAAK,GAAG,GAAG,GAAG,GAAG,CAAC;QACxB,MAAM,KAAK,GAAG,IAAI,GAAG,GAAG,CAAC;QACzB,GAAG,GAAG,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACxD,IAAI,GAAG,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAC3D,CAAC;IACD,OAAO,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;AAChD,CAAC"}
|
package/dist/histogram.d.ts
CHANGED
|
@@ -1,19 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rules of thumb for a histogram's bin width, NumPy's `histogram_bin_edges`
|
|
3
|
+
* estimators of the same names (`'fd'` for `"freedman-diaconis"`).
|
|
4
|
+
*/
|
|
5
|
+
export type HistogramBinRule = "auto" | "freedman-diaconis" | "scott" | "sturges" | "doane" | "rice" | "sqrt" | "stone";
|
|
6
|
+
/**
|
|
7
|
+
* The width of a histogram bin by a rule of thumb, over the finite values of
|
|
8
|
+
* `values` (infinities and `NaN` are ignored), with `n` the count of those:
|
|
9
|
+
*
|
|
10
|
+
* - `"sturges"`: `range / (log2(n) + 1)` (Sturges, 1926), the bin count growing
|
|
11
|
+
* with the logarithm of the size; suits near-normal samples.
|
|
12
|
+
* - `"doane"`: `range / (1 + log2(n) + log2(1 + |g₁| / σ_g₁))` (Doane, 1976),
|
|
13
|
+
* Sturges' with more bins for a skewed sample, `g₁` the sample skewness and
|
|
14
|
+
* `σ_g₁ = √(6 (n − 2) / ((n + 1)(n + 3)))`; needs three values.
|
|
15
|
+
* - `"rice"`: `range / (2 · n^(1/3))`.
|
|
16
|
+
* - `"sqrt"`: `range / √n`, Excel's and many tools' default.
|
|
17
|
+
* - `"scott"`: `(24·√π / n)^(1/3) · σ` (Scott, 1979), the width minimising the
|
|
18
|
+
* integrated squared error for a normal density, with `σ` the population
|
|
19
|
+
* standard deviation.
|
|
20
|
+
* - `"freedman-diaconis"`: `2 · IQR · n^(-1/3)` (Freedman and Diaconis, 1981),
|
|
21
|
+
* with the interquartile range by linear interpolation; robust to outliers.
|
|
22
|
+
* - `"stone"`: `range / k` for the bin count `k` in `1 … max(100, ⌊√n⌋)` that
|
|
23
|
+
* minimises the leave-one-out estimate of the integrated squared error
|
|
24
|
+
* (Stone, 1984), the first such count on a tie.
|
|
25
|
+
* - `"auto"`: NumPy's default, the Freedman–Diaconis width raised to at least
|
|
26
|
+
* half the square-root rule's (so a near-zero interquartile range cannot ask
|
|
27
|
+
* for a great many bins), then capped by Sturges'.
|
|
28
|
+
*
|
|
29
|
+
* These are the definitions of NumPy's `histogram_bin_edges` (`'fd'` for
|
|
30
|
+
* Freedman–Diaconis), checked against NumPy 2.5.
|
|
31
|
+
*
|
|
32
|
+
* `NaN` for fewer than two finite values (three for Doane's), for zero spread
|
|
33
|
+
* (range, standard deviation or interquartile range being zero, which a
|
|
34
|
+
* Freedman–Diaconis sample of mostly equal values can have despite a non-zero
|
|
35
|
+
* range), for a width that underflows to zero and for an unknown rule: where
|
|
36
|
+
* NumPy's estimator answers 0.
|
|
37
|
+
*
|
|
38
|
+
* The spread is measured without overflow or underflow over the whole double
|
|
39
|
+
* range: the range and the interquartile range from the values themselves, the
|
|
40
|
+
* standard deviation and skewness on the sample scaled near 1 by a power of
|
|
41
|
+
* two. The width is the representable result, rounded; `Infinity` only when it
|
|
42
|
+
* exceeds `Number.MAX_VALUE`, which Scott's, Freedman–Diaconis' and Stone's
|
|
43
|
+
* (whose best may be one bin) can on a spread near the double range, and the
|
|
44
|
+
* other range-based rules never do.
|
|
45
|
+
*
|
|
46
|
+
* Time O(n) for the range-based rules (Sturges, Doane, Rice, square root) and
|
|
47
|
+
* Scott's, O(n log n) for Freedman–Diaconis' and `"auto"`, O(n · max(100, √n))
|
|
48
|
+
* for Stone's. Memory O(1) for Sturges', Rice's and the square-root rule; O(n)
|
|
49
|
+
* otherwise, a filtered copy.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* histogramBinWidth([1, 2, 3, 4, 5, 6, 7, 8], "sturges"); // 7 / 4
|
|
53
|
+
*
|
|
54
|
+
* @param values - The sample, in any order; a typed array is read as is. Not
|
|
55
|
+
* modified.
|
|
56
|
+
* @param rule - The rule to apply.
|
|
57
|
+
* @returns The bin width, positive, `Infinity` past the largest double, or
|
|
58
|
+
* `NaN`.
|
|
59
|
+
* @see {@link histogramEdges} for the edges of bins of that width.
|
|
60
|
+
* @see {@link histogram} to count a sample into bins.
|
|
61
|
+
*/
|
|
62
|
+
export declare function histogramBinWidth(values: ArrayLike<number>, rule: HistogramBinRule): number;
|
|
63
|
+
/** Options of {@link histogram}. */
|
|
64
|
+
export type HistogramOptions = {
|
|
65
|
+
/**
|
|
66
|
+
* Whether the last edge closes the last bin between edges, as
|
|
67
|
+
* {@link histogramEdges}' bins and NumPy's are: a value equal to it is then
|
|
68
|
+
* counted in `counts[edges.length - 1]` rather than in the overflow bin.
|
|
69
|
+
* Without a second edge there is no such bin, and it changes nothing. Default
|
|
70
|
+
* `false`.
|
|
71
|
+
*/
|
|
72
|
+
readonly lastClosed?: boolean | undefined;
|
|
73
|
+
};
|
|
1
74
|
/**
|
|
2
75
|
* Counts `values` into the bins that `edges` cut: bin 0 holds the values below
|
|
3
76
|
* `edges[0]`, bin `i` those at or above `edges[i - 1]` and below `edges[i]`,
|
|
4
|
-
* and the last bin those at or above the last edge
|
|
5
|
-
* the first or the last bin; a
|
|
77
|
+
* and the last bin those at or above the last edge (above it with
|
|
78
|
+
* `lastClosed`). An infinite value falls in the first or the last bin; a
|
|
79
|
+
* not-a-number value is not counted.
|
|
6
80
|
*
|
|
7
81
|
* Time O(k + n log k) for `n` values and `k` edges; memory O(k) for the counts.
|
|
8
82
|
*
|
|
9
83
|
* @example
|
|
10
84
|
* histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20]); // [1, 2, 2, 2]
|
|
85
|
+
* histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20], { lastClosed: true }); // [1, 2, 3, 1]
|
|
11
86
|
*
|
|
12
87
|
* @param values - The sample, in any order; read once.
|
|
13
|
-
* @param edges - The bin boundaries: at least one, finite, strictly ascending
|
|
88
|
+
* @param edges - The bin boundaries: at least one, finite, strictly ascending;
|
|
89
|
+
* a typed array is read as is.
|
|
14
90
|
* @returns `edges.length + 1` counts.
|
|
15
91
|
* @throws {RangeError} When `edges` is empty, holds a value that is not finite
|
|
16
92
|
* or is not strictly ascending.
|
|
17
93
|
*/
|
|
18
|
-
export declare function histogram(values: Iterable<number>, edges:
|
|
94
|
+
export declare function histogram(values: Iterable<number>, edges: ArrayLike<number>, options?: HistogramOptions): number[];
|
|
19
95
|
//# sourceMappingURL=histogram.d.ts.map
|
package/dist/histogram.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"histogram.d.ts","sourceRoot":"","sources":["../src/histogram.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"histogram.d.ts","sourceRoot":"","sources":["../src/histogram.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GACzB,MAAM,GACN,mBAAmB,GACnB,OAAO,GACP,SAAS,GACT,OAAO,GACP,MAAM,GACN,MAAM,GACN,OAAO,CAAC;AAEX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,EACzB,IAAI,EAAE,gBAAgB,GACpB,MAAM,CAYR;AAED,oCAAoC;AACpC,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;;;;;OAMG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC1C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,CACxB,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,EACxB,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EACxB,OAAO,GAAE,gBAAqB,GAC5B,MAAM,EAAE,CAgCV"}
|