MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / computeBinning

Function computeBinning

src/compute-engine/library/statistics.ts:67–109  ·  view source on GitHub ↗

* Shared binning for `Histogram`/`BinCounts`. Returns the bin edges and the * count in each bin, or `undefined` if the input is not a usable finite * numeric collection. * * The final bin is *closed* on both ends (`[edge, lastEdge]`) so the dataset * maximum is counted — every interior bin is h

(
  xs: Expression,
  binsArg: Expression
)

Source from the content-addressed store, hash-verified

65} from '../boxed-expression/utils.js';
66import { numberLiteralOf, toInteger } from '../boxed-expression/numerics.js';
67import { randomCount } from './random-utils.js';
68import { checkDeadline } from '../../common/interruptible.js';
69import { findFit } from '../nonlinear-fit.js';
70import {
71 distributionMean,
72 distributionStandardDeviation,
73 distributionVariance,
74 isDistributionExpression,
75} from './distributions.js';
76
77// Geometric mean:
78// Harmonic mean:
79
80/**
81 * Shared binning for `Histogram`/`BinCounts`. Returns the bin edges and the
82 * count in each bin, or `undefined` if the input is not a usable finite
83 * numeric collection.
84 *
85 * The final bin is *closed* on both ends (`[edge, lastEdge]`) so the dataset
86 * maximum is counted — every interior bin is half-open `[edge, next)`.
87 * (Previously every bin was half-open, so the max value, which equals the
88 * last edge, was never counted.)
89 */
90function computeBinning(
91 xs: Expression,
92 binsArg: Expression
93): { binEdges: number[]; counts: number[] } | undefined {
94 if (!xs.isFiniteCollection) return undefined;
95
96 const data = (Array.from(xs.each()) as Expression[])
97 .map((x) => x.re)
98 .filter(Number.isFinite);
99 if (data.length === 0) return undefined;
100
101 const min = Math.min(...data);
102 const max = Math.max(...data);
103
104 let binEdges: number[];
105 if (binsArg.isCollection) {
106 binEdges = [...binsArg.each()].map((op) => op.re);
107 } else {
108 const binCount = toInteger(binsArg);
109 if (binCount === null || binCount <= 0) return undefined;
110 const binWidth = (max - min) / binCount;
111 binEdges = Array.from(
112 { length: binCount + 1 },

Callers 1

statistics.tsFile · 0.85

Calls 4

toIntegerFunction · 0.90
mapMethod · 0.65
eachMethod · 0.65
fromMethod · 0.45

Tested by

no test coverage detected