diff options
author | CloseChoice <tobias.pitters@gmx.de> | 2020-05-21 12:29:57 +0200 |
---|---|---|
committer | Tobias Pitters <tobias.pitters@gmail.com> | 2020-05-27 19:35:59 +0200 |
commit | 54de868a1a37b182a94f2d521fe2c695bf19ddea (patch) | |
tree | 4e44a60107cbded801cee243d8d5237b92eeee46 /numpy/lib | |
parent | 3b0516aded2f0ebc09511db63978c1f1a2445a43 (diff) | |
download | numpy-54de868a1a37b182a94f2d521fe2c695bf19ddea.tar.gz |
add hypothesis test, fix bug of non-monotonic ordering of quantile function
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 4 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 15 |
2 files changed, 17 insertions, 2 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 48b0a0830..3fb45b09a 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3875,7 +3875,8 @@ def _quantile_is_valid(q): def _lerp(a, b, t, out=None): """ Linearly interpolate from a to b by a factor of t """ - return add(a*(1 - t), b*t, out=out) + offset = subtract(b, a) * t + return add(a, offset, out=out) def _quantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, @@ -3948,6 +3949,7 @@ def _quantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False, else: # weight the points above and below the indices + #import pdb; pdb.set_trace() indices_below = not_scalar(floor(indices)).astype(intp) indices_above = not_scalar(indices_below + 1) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index c415653e4..ea562b466 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -5,6 +5,10 @@ import decimal from fractions import Fraction import math import pytest +import hypothesis +from hypothesis.extra.numpy import arrays +import hypothesis.strategies as st + import numpy as np from numpy import ma @@ -3104,7 +3108,7 @@ class TestQuantile: np.quantile(np.arange(100.), p, interpolation="midpoint") assert_array_equal(p, p0) - def test_quantile_monotic(self): + def test_quantile_monotonic(self): # GH 14685 # test that the return value of quantile is monotonic if p0 is ordered p0 = np.arange(0, 1, 0.01) @@ -3112,6 +3116,15 @@ class TestQuantile: equals_sorted = np.sort(quantile) == quantile assert equals_sorted.all() + @hypothesis.given(arr=arrays(dtype=np.float, shape=st.integers(min_value=3, max_value=1000), + elements=st.floats(allow_infinity=False, allow_nan=False))) + def test_quantile_monotonic_hypo(self, arr): + p0 = np.arange(0, 1, 0.01) + quantile = np.quantile(arr, p0) + equals_sorted = np.sort(quantile) == quantile + assert equals_sorted.all() + + class TestMedian: def test_basic(self): |