jax icon indicating copy to clipboard operation
jax copied to clipboard

jnp.histogram assigns values lying exactly on an interior bin edge to the wrong bin

Open the-Shallow opened this issue 2 months ago • 1 comments

Description

Description

jnp.histogram computes bin edges with jnp.linspace, which, unlike np.linspace, does not reproduce exact grid points. For range=(-1, 1), bins=10 the edge that should be 0.0 is 2.78e-17 (x64) or 7.45e-09 (float32), so data exactly on that boundary is counted in the bin below the one NumPy uses.

Steps to reproduce

import numpy as np, jax.numpy as jnp
print(jnp.histogram(jnp.zeros(3), bins=10, range=(-1.0, 1.0))[0])  # the 3 is in bin 4
print(np.histogram(np.zeros(3), bins=10, range=(-1.0, 1.0))[0])    # the 3 is in bin 5

Expected

The same counts as np.histogram (values on an edge go to the bin that starts at that edge).

System info (python version, jaxlib version, accelerator, etc.)

Environment

JAX / jaxlib 0.11.2 (the latest release, CPU) and 0.10.1, Python 3.12, Ubuntu 24.04, x86_64

the-Shallow avatar Sep 25 '26 03:09 the-Shallow

I think differences like this are unavoidable: JAX is working in float32, while NumPy is working in float64, and the exact values of bin boundaries are going to differ between these two implementations.

I'm going to close as working-as-intended. Thanks!

jakevdp avatar Sep 25 '26 13:09 jakevdp