Skip to content

fix(numpy): normalize density with the returned bin widths - #1196

Open
antonsoo wants to merge 1 commit into
scikit-hep:developfrom
antonsoo:fix/numpy-density-bin-widths
Open

antonsoo wants to merge 1 commit into
scikit-hep:developfrom
antonsoo:fix/numpy-density-bin-widths

Conversation

@antonsoo

Copy link
Copy Markdown

The final bin is widened to include its right endpoint, but density uses that widened width while returning the original edges. For narrow bins at a large offset, the resulting density does not integrate to one:

import numpy as np
import boost_histogram as bh

edges = np.array([1e16, 1e16 + 2, 1e16 + 4])
density, returned_edges = bh.numpy.histogram(edges, bins=edges, density=True)
print(np.sum(density * np.diff(returned_edges)))
# Before: 0.6666666666666666; after: 1.0

Compute density bin volumes from the returned edges, using sparse meshgrids for multiple axes. The count/object return paths keep their existing behavior.

Sixteen of the 20 new regressions fail on current develop; all pass after, covering one to three dimensions, positive/negative offsets, weights and the 1D/2D entry points. On 2,048 previously recorded clock samples, the affected final-bin relative error reaches 50% before; all 12 configurations agree with independent count/bin-volume normalization to at most 2.2e-16 after. All count results and 12 object controls remain exact.

Local native suites: Python3.12, 1,238 passed/4 xfailed; Python3.14, 1,237 passed/1 skipped/4 xfailed. Full prek run -a, including strict mypy, passes.

@antonsoo

Copy link
Copy Markdown
Author

The PyPy3.11 job fails during dependency installation: NumPy has no usable wheel for that interpreter, and the workflow disables building NumPy from source. Configure, build and tests are skipped in that job. The reported CPython native and downstream Hist checks pass; the free-threaded macOS wheel job is still running. Job: https://github.com/scikit-hep/boost-histogram/actions/runs/38534673422/job/115799656501

@henryiii
henryiii force-pushed the fix/numpy-density-bin-widths branch from ecf251c to 7b41a0c Compare October 30, 2026 20:45

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant