Skip to content

bin_edges

Mojo function 🡭

bin_edges

fn def bin_edges(data: List[Float64], bins: Int = Int(10)) -> List[Float64]

bins + 1 equally spaced boundaries covering data’s own range.

A constant sample – every value identical, which includes a one-element sample – gets the range [v - 0.5, v + 0.5] rather than raising, which is what this module did before. numpy’s rule, and the right one: a spike at a single value is a real distribution with a real answer, and “no span to divide into bins” told a caller their data was unplottable when what they wanted to see was that it was constant. The half-unit is arbitrary in magnitude but not in kind – it is the only choice that does not depend on data the sample does not have.

Args:

  • data (List[Float64]): The observations. Only their min and max are read.
  • bins (Int): How many intervals to divide the range into; must be positive.

Returns:

List[Float64]: bins + 1 strictly ascending boundaries.

Raises:

Error: data is empty, bins is not positive, or any value is NaN/infinite.

fn def bin_edges(data: List[Float64], rule: BinRule) -> List[Float64]

Boundaries covering data’s own range, with the bin count chosen by rule instead of named – the overload of bin_edges() that takes a BinRule where the other takes an Int.

bin_edges(data, BinRule.AUTO) is numpy.histogram_bin_edges(data, bins="auto"): same count, same range, and the same answer on the samples where the rules degenerate. A constant sample takes the range [v - 0.5, v + 0.5] in one bin, exactly as the Int overload does, since every rule’s width is 0 there and there is nothing to divide.

The interior boundaries are uniform_bin_edges()’s, not numpy’s: numpy steps a precomputed increment and this re-spans the range for each boundary, which differ in the last bit. See uniform_bin_edges() for why.

Args:

  • data (List[Float64]): The observations. The size-only rules read just the count and the range; SCOTT also reads the standard deviation and FREEDMAN_DIACONIS/AUTO the interquartile range.
  • rule (BinRule): Which rule picks the count. See BinRule.

Returns:

List[Float64]: n + 1 strictly ascending boundaries, n chosen by rule.

Raises:

Error: data is empty, any value is NaN/infinite, or the rule asks for an unusably large number of bins (see BinRule).