summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2021-11-09 16:16:48 -0600
committerGitHub <noreply@github.com>2021-11-09 16:16:48 -0600
commita0d86e298b18f6c1bc13d2ed032ac7fd9d67e592 (patch)
tree1d4ceae313113e10ce957c3a2a0c95009b3f31e9 /numpy
parente1239e7152c87fa97266f62e107d383905e6006c (diff)
parent53e3df3c99a26791cc07e2ea1570e87643fdf7e0 (diff)
downloadnumpy-a0d86e298b18f6c1bc13d2ed032ac7fd9d67e592.tar.gz
Merge pull request #20331 from bzah/fix/quantile-default-lerp-method
MAINT: Update quantile default lerp method
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/function_base.py12
-rw-r--r--numpy/lib/tests/test_function_base.py8
2 files changed, 15 insertions, 5 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 86125168a..3c9983edf 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -67,7 +67,7 @@ __all__ = [
# fix_gamma : Callable
# A function used for discret methods to force the index to a specific value.
_QuantileInterpolation = dict(
- # --- HYNDMAN and FAN methods
+ # --- HYNDMAN AND FAN METHODS
# Discrete methods
inverted_cdf=dict(
get_virtual_index=lambda n, quantiles: _inverted_cdf(n, quantiles),
@@ -102,10 +102,12 @@ _QuantileInterpolation = dict(
_compute_virtual_index(n, quantiles, 0, 0),
fix_gamma=lambda gamma, _: gamma,
),
- # Default value
+ # Default method.
+ # To avoid some rounding issues, `(n-1) * quantiles` is preferred to
+ # `_compute_virtual_index(n, quantiles, 1, 1)`.
+ # They are mathematically equivalent.
linear=dict(
- get_virtual_index=lambda n, quantiles:
- _compute_virtual_index(n, quantiles, 1, 1),
+ get_virtual_index=lambda n, quantiles: (n - 1) * quantiles,
fix_gamma=lambda gamma, _: gamma,
),
median_unbiased=dict(
@@ -118,7 +120,7 @@ _QuantileInterpolation = dict(
_compute_virtual_index(n, quantiles, 3 / 8.0, 3 / 8.0),
fix_gamma=lambda gamma, _: gamma,
),
- # --- OTHER METHODS fixme add deprecated ?
+ # --- OTHER METHODS
lower=dict(
get_virtual_index=lambda n, quantiles: np.floor(
(n - 1) * quantiles).astype(np.intp),
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index d5fa012f1..1c274afae 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -3356,6 +3356,14 @@ class TestPercentile:
class TestQuantile:
# most of this is already tested by TestPercentile
+ def test_max_ulp(self):
+ x = [0.0, 0.2, 0.4]
+ a = np.quantile(x, 0.45)
+ # The default linear method would result in 0 + 0.2 * (0.45/2) = 0.18.
+ # 0.18 is not exactly representable and the formula leads to a 1 ULP
+ # different result. Ensure it is this exact within 1 ULP, see gh-20331.
+ np.testing.assert_array_max_ulp(a, 0.18, maxulp=1)
+
def test_basic(self):
x = np.arange(8) * 0.5
assert_equal(np.quantile(x, 0), 0.)