Skip to content
histogram_bins

histogram_bins

Mojo function 🡭

histogram_bins

fn def histogram_bins(data: List[Float64], edges: List[Float64], weights: List[Float64] = List(), stat: HistStat = HistStat.COUNT, cumulative: Bool = False) -> HistogramBins

Bin data into the intervals edges names, and return the numeric edges alongside one value per bin.

Bin i collects every observation in [edges[i], edges[i + 1]), except the last bin, which is closed on the right so the sample maximum lands inside it. Observations outside [edges[0], edges[-1]] are dropped – an explicit range is a statement about what to show, and folding an outlier into an end bin would draw a bar taller than the interval under it justifies.

cumulative accumulates mass, never the drawn value: bin i becomes the running total of the observation weight at or below edges[i + 1], re-expressed in stat’s units. That distinction only shows up for the two width-normalized stats, where the naive running sum of the bin values would have units of density times bins and would depend on how many bins the caller asked for. Accumulating mass instead makes DENSITY cumulative the empirical CDF (last bin exactly 1.0) and FREQUENCY cumulative the running count, on both equal- and unequal-width edges.

Args:

  • data (List[Float64]): The observations to bin.
  • edges (List[Float64]): The bin boundaries, strictly ascending, at least 2 long – from bin_edges(), uniform_bin_edges(), shared_bin_edges(), or written out by hand for unequal widths.
  • weights (List[Float64]): One nonnegative weight per observation, or empty (the default) for one apiece. A weighted bin holds the total weight that landed in it, so every stat generalizes without a second definition.
  • stat (HistStat): What a bin’s value is – a count, or one of four normalizations. See HistStat.
  • cumulative (Bool): Accumulate each bin into the ones before it.

Returns:

HistogramBins: The bins: a copy of edges, and one value per bin.

Raises:

Error: data is empty; edges is shorter than 2 or not strictly ascending; weights is neither empty nor the same length as data; any weight is negative or non-finite; any value is NaN/infinite; or a normalized stat was asked for and no observation landed in any bin.