Weighted inverted-CDF quantiles select zero-weight observations at q=0
Description
I was comparing JAX's weighted quantile implementation against NumPy and found a silent correctness issue with method="inverted_cdf" at q=0.
When the smallest observation has zero weight, JAX incorrectly selects it instead of the smallest observation with positive weight. The issue reproduces across all four weighted quantile/percentile APIs.
Minimal reproduction
import numpy as np
import jax
import jax.numpy as jnp
x = np.array([1.0, 10.0], dtype=np.float32)
w = np.array([0.0, 1.0], dtype=np.float32)
for name in ("quantile", "percentile", "nanquantile", "nanpercentile"):
expected = getattr(np, name)(
x, 0.0, weights=w, method="inverted_cdf"
)
actual = getattr(jnp, name)(
jnp.asarray(x), 0.0,
weights=jnp.asarray(w), method="inverted_cdf"
)
print(f"{name}: NumPy={expected}, JAX={actual}")
Actual results
quantile: NumPy=10.0, JAX=1.0
percentile: NumPy=10.0, JAX=1.0
nanquantile: NumPy=10.0, JAX=1.0
nanpercentile: NumPy=10.0, JAX=1.0
All four discrepancies reproduced on JAX 0.12.0.1 (CPU).
Expected behavior: 10.0 in every case, because the observation 1.0 has zero weight and should not contribute to the weighted distribution.
Likely root cause
In jax/_src/numpy/reductions.py, the weighted inverted_cdf path computes cumulative weights and selects an index using:
is_less = cum_weights < target_w
idx = sum(is_less, axis=...)
At q=0, target_w=0. For this example, the cumulative weights are [0, 1], so no entries satisfy the comparison and idx=0, selecting the zero-weight observation.
Impact
This silently affects weighted minimum/0th-percentile calculations when zero weights are used to exclude observations, potentially affecting masked statistics and threshold selection.
A possible fix would ensure that the zero-quantile boundary selects the first positively weighted observation. Regression tests could cover leading zero weights, multiple zero-weight observations, the four affected APIs, and JIT execution.
System info (python version, jaxlib version, accelerator, etc.)
JAX: 0.12.0.1
jaxlib: 0.12.0.1
NumPy: 2.1.3
Accelerator: CPU