@ac-kit/math-stats 0.3.0 → 0.4.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 CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 065cf8d: `histogram(values, edges)` counts an iterable sample into the `edges.length + 1` bins that finite, strictly ascending edges cut: one below the first edge, each further bin closed on its lower edge and open on its upper one, the last from the last edge up. Infinities fall in the outer bins and `NaN` is not counted. Empty, non-finite or not strictly ascending edges throw `RangeError`. O(k + n log k) time for n values and k edges.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [065cf8d]
12
+ - Updated dependencies [065cf8d]
13
+ - Updated dependencies [065cf8d]
14
+ - @ac-kit/core@0.11.0
15
+ - @ac-kit/algo@0.4.1
16
+ - @ac-kit/math-analysis@0.1.10
17
+ - @ac-kit/math-scalar@0.2.3
18
+
3
19
  ## 0.3.0
4
20
 
5
21
  ### Minor Changes
package/README.md CHANGED
@@ -35,6 +35,19 @@ The three means answer different questions and are not interchangeable:
35
35
  They satisfy `harmonic ≤ geometric ≤ arithmetic` for positive values, with
36
36
  equality only when every value is identical.
37
37
 
38
+ ## Histograms
39
+
40
+ `histogram(values, edges)` counts a sample into the bins that strictly
41
+ ascending, finite `edges` cut, with one bin below the first edge and one from
42
+ the last: `edges.length + 1` counts, each bin closed on its lower edge and open
43
+ on its upper one. Infinities land in the outer bins; `NaN` is not counted.
44
+
45
+ ```ts
46
+ import { histogram } from "@ac-kit/math-stats";
47
+
48
+ histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20]); // [1, 2, 2, 2]
49
+ ```
50
+
38
51
  ## Sliding windows
39
52
 
40
53
  `SlidingMedian` keeps the exact median of the last `n` values pushed, without
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Counts `values` into the bins that `edges` cut: bin 0 holds the values below
3
+ * `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. An infinite value falls in
5
+ * the first or the last bin; a not-a-number value is not counted.
6
+ *
7
+ * Time O(k + n log k) for `n` values and `k` edges; memory O(k) for the counts.
8
+ *
9
+ * @example
10
+ * histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20]); // [1, 2, 2, 2]
11
+ *
12
+ * @param values - The sample, in any order; read once.
13
+ * @param edges - The bin boundaries: at least one, finite, strictly ascending.
14
+ * @returns `edges.length + 1` counts.
15
+ * @throws {RangeError} When `edges` is empty, holds a value that is not finite
16
+ * or is not strictly ascending.
17
+ */
18
+ export declare function histogram(values: Iterable<number>, edges: readonly number[]): number[];
19
+ //# sourceMappingURL=histogram.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"histogram.d.ts","sourceRoot":"","sources":["../src/histogram.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,SAAS,CACxB,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,EACxB,KAAK,EAAE,SAAS,MAAM,EAAE,GACtB,MAAM,EAAE,CA2BV"}
@@ -0,0 +1,40 @@
1
+ import { bisectRight } from "@ac-kit/algo";
2
+ import { compareNaturalAscending } from "@ac-kit/core";
3
+ /**
4
+ * Counts `values` into the bins that `edges` cut: bin 0 holds the values below
5
+ * `edges[0]`, bin `i` those at or above `edges[i - 1]` and below `edges[i]`,
6
+ * and the last bin those at or above the last edge. An infinite value falls in
7
+ * the first or the last bin; a not-a-number value is not counted.
8
+ *
9
+ * Time O(k + n log k) for `n` values and `k` edges; memory O(k) for the counts.
10
+ *
11
+ * @example
12
+ * histogram([-5, 0, 9, 10, 19, 20, 500], [0, 10, 20]); // [1, 2, 2, 2]
13
+ *
14
+ * @param values - The sample, in any order; read once.
15
+ * @param edges - The bin boundaries: at least one, finite, strictly ascending.
16
+ * @returns `edges.length + 1` counts.
17
+ * @throws {RangeError} When `edges` is empty, holds a value that is not finite
18
+ * or is not strictly ascending.
19
+ */
20
+ export function histogram(values, edges) {
21
+ if (edges.length === 0) {
22
+ throw new RangeError("edges must not be empty");
23
+ }
24
+ for (let index = 0; index < edges.length; index++) {
25
+ const edge = edges[index];
26
+ if (!Number.isFinite(edge) || (index > 0 && !(edge > edges[index - 1]))) {
27
+ throw new RangeError(`edges must be finite and strictly ascending, got ${edge} at index ${index}`);
28
+ }
29
+ }
30
+ const counts = new Array(edges.length + 1).fill(0);
31
+ for (const value of values) {
32
+ if (Number.isNaN(value)) {
33
+ continue;
34
+ }
35
+ const bin = bisectRight(edges, value, compareNaturalAscending);
36
+ counts[bin]++;
37
+ }
38
+ return counts;
39
+ }
40
+ //# sourceMappingURL=histogram.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"histogram.js","sourceRoot":"","sources":["../src/histogram.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEvD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,SAAS,CACxB,MAAwB,EACxB,KAAwB;IAExB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,UAAU,CAAC,yBAAyB,CAAC,CAAC;IACjD,CAAC;IAED,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAE,CAAC;QAE3B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,GAAG,CAAC,CAAE,CAAC,CAAC,EAAE,CAAC;YAC1E,MAAM,IAAI,UAAU,CACnB,oDAAoD,IAAI,aAAa,KAAK,EAAE,CAC5E,CAAC;QACH,CAAC;IACF,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,KAAK,CAAS,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAE3D,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,SAAS;QACV,CAAC;QAED,MAAM,GAAG,GAAG,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,uBAAuB,CAAC,CAAC;QAC/D,MAAM,CAAC,GAAG,CAAE,EAAE,CAAC;IAChB,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export * from "./f-distribution.js";
12
12
  export * from "./gamma-distribution.js";
13
13
  export * from "./geometric-distribution.js";
14
14
  export * from "./hierarchical-cluster.js";
15
+ export * from "./histogram.js";
15
16
  export * from "./hypergeometric-distribution.js";
16
17
  export * from "./kmeans.js";
17
18
  export * from "./kmedoids.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kCAAkC,CAAC;AACjD,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,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,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC"}
package/dist/index.js CHANGED
@@ -12,6 +12,7 @@ export * from "./f-distribution.js";
12
12
  export * from "./gamma-distribution.js";
13
13
  export * from "./geometric-distribution.js";
14
14
  export * from "./hierarchical-cluster.js";
15
+ export * from "./histogram.js";
15
16
  export * from "./hypergeometric-distribution.js";
16
17
  export * from "./kmeans.js";
17
18
  export * from "./kmedoids.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kCAAkC,CAAC;AACjD,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qCAAqC,CAAC;AACpD,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,eAAe,CAA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jC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/math-stats",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Descriptive statistics, probability distributions and statistical inference over samples of number.",
5
5
  "keywords": [
6
6
  "distribution",
@@ -44,14 +44,15 @@
44
44
  "prepack": "yarn build"
45
45
  },
46
46
  "dependencies": {
47
- "@ac-kit/algo": "^0.4.0",
48
- "@ac-kit/core": "^0.10.0",
49
- "@ac-kit/math-analysis": "^0.1.9",
50
- "@ac-kit/math-scalar": "^0.2.2",
47
+ "@ac-kit/algo": "^0.4.1",
48
+ "@ac-kit/core": "^0.11.0",
49
+ "@ac-kit/math-analysis": "^0.1.10",
50
+ "@ac-kit/math-scalar": "^0.2.3",
51
51
  "@types/web": "^0.0.367"
52
52
  },
53
53
  "devDependencies": {
54
- "@ac-kit/test-util": "0.3.0",
54
+ "@ac-kit/math-random": "0.3.0",
55
+ "@ac-kit/test-util": "0.3.1",
55
56
  "@ac-kit/tsconfig": "0.1.0",
56
57
  "@ac-kit/typedoc-config": "0.1.0",
57
58
  "@ac-kit/vitest-config": "0.1.1",