quantileBoundaries function
The bins-1 internal cut points splitting sorted values into bins
roughly equal-count groups (quantile boundaries).
Returns an empty list when bins < 2 or values is empty (no internal
cut exists). Each boundary is the value at the corresponding quantile
position in the ascending-sorted data.
Audited: 2026-06-12 11:26 EDT
Implementation
List<num> quantileBoundaries(List<num> values, int bins) {
assert(bins >= 1, 'quantileBoundaries requires bins >= 1');
// One bin (or fewer) and empty input both have no internal cut points.
if (bins < 2 || values.isEmpty) return <num>[];
final List<double> sorted = values.map((num v) => v.toDouble()).toList()..sort();
final List<num> cuts = <num>[];
// The k-th cut sits at the k/bins fraction of the sorted data.
for (int k = 1; k < bins; k++) {
final int index = (sorted.length * k / bins).floor().clamp(0, sorted.length - 1);
cuts.add(sorted[index]);
}
return cuts;
}