diff --git a/src/corner/core.py b/src/corner/core.py index 0b160d2..a518425 100644 --- a/src/corner/core.py +++ b/src/corner/core.py @@ -526,7 +526,7 @@ def quantile(x, q, weights=None): raise ValueError("Dimension mismatch: len(weights) != len(x)") idx = np.argsort(x) sw = weights[idx] - cdf = np.cumsum(sw)[:-1] + cdf = np.cumsum(sw, dtype=np.result_type(sw.dtype, np.float64))[:-1] cdf /= cdf[-1] cdf = np.append(0, cdf) return np.interp(q, cdf, x[idx]).tolist() diff --git a/tests/test_quantile.py b/tests/test_quantile.py index e854204..d761122 100644 --- a/tests/test_quantile.py +++ b/tests/test_quantile.py @@ -53,3 +53,22 @@ def test_weighted_quantile(seed=42): q = [0.0, 1.0] a = corner.quantile(x, q, weights=np.random.rand(len(x))) assert np.allclose(a, (np.min(x), np.max(x))) + + +@pytest.mark.parametrize("dtype", [np.int8, np.int32, np.uint64]) +def test_integer_weights(dtype): + x = np.array([7.0, 2.0, 9.0, 1.0, 5.0]) + weights = np.array([90, 40, 30, 100, 60], dtype=dtype) + q = [0.0, 0.16, 0.5, 0.84, 1.0] + actual = corner.quantile(x, q, weights=weights) + expected = corner.quantile(x, q, weights=weights.astype(float)) + np.testing.assert_array_equal(actual, expected) + + +def test_python_integer_weights(): + x = np.arange(10.0) + q = [0.16, 0.5, 0.84] + np.testing.assert_allclose( + corner.quantile(x, q, weights=[1] * 10), + np.percentile(x, 100 * np.array(q)), + )