ML Foundations

Convert NumPy histogram density to bin mass

Multiply density by bin width to interpret unequal-width histogram bins.

Example results for Convert NumPy histogram density to bin mass. counts: 2, 2
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

Density height is not bin probability

With density=True, NumPy histogram returns density heights normalized so their area over the histogram range is one. The heights alone generally do not sum to one: each height must be multiplied by its bin width to obtain the corresponding bin mass. This is particularly visible when widths differ. [1]

For a finite unweighted fixture with positive-width bins that include every observation, each bin mass also equals that bin count divided by the total count. The height divides that mass by the width. These statements describe the chosen histogram, not a claim about the true distribution outside it. [1]

References: [1], [2]

Check mass with unequal widths

Run python examples/numpy_histogram_density_mass.py. Four values are placed into two bins with widths 0.5 and 1.5; each bin contains two observations. The density heights differ because the same mass is spread across different widths. Multiplying the heights by np.diff of the returned edges yields masses of 0.5 and 0.5. [1, 2]

The assertions check the counts, each bin mass and their sum, and confirm that the sum of heights is not one in this fixture. The printed heights are rounded for readability; the assertions use unrounded values and numerical tolerance.

NumPy includes the rightmost edge in its final bin. If values fall outside an explicitly supplied range, they are not counted; interpret normalization against the observations included in the histogram. [1]

Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.

import numpy as np

values = np.array([0.1, 0.4, 1.0, 1.5])
edges = np.array([0.0, 0.5, 2.0])
counts, _ = np.histogram(values, bins=edges)
density, returned_edges = np.histogram(values, bins=edges, density=True)
widths = np.diff(returned_edges)
mass = density * widths
assert counts.tolist() == [2, 2]
assert np.allclose(mass, [0.5, 0.5])
assert np.isclose(mass.sum(), 1.0)
assert not np.isclose(density.sum(), 1.0)
print("counts:", counts.tolist())
print("widths:", widths.tolist())
print("density:", np.round(density, 6).tolist())
print("bin mass:", mass.tolist())
print("density sum:", round(float(density.sum()), 6))
print("mass sum:", float(mass.sum()))

Recorded output

counts: [2, 2]
widths: [0.5, 1.5]
density: [1.0, 0.333333]
bin mass: [0.5, 0.5]
density sum: 1.333333
mass sum: 1.0

References: [1], [2]

State the comparison contract

Use consistent bin edges when comparing histograms, and retain the widths along with the density heights. Comparing heights from differently sized bins as if they were counts or probabilities can give a misleading impression.

The example uses finite unweighted observations and nonempty positive-width bins. It does not cover weighted histograms, empty input, density estimation quality or calibration. Validate those cases separately if the application requires them.

References: [1], [2]

Sources

  1. NumPy histogram
  2. NumPy diff