Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix np.quantile([Fraction(2,1)], 0.5) #24711

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BUG: Fix np.quantile([Fraction(2,1)], 0.5)
  • Loading branch information
eendebakpt committed Nov 8, 2023
commit 5b9730c0a7bca6b1647c836b9cc709b778a72369
3 changes: 2 additions & 1 deletion numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4585,7 +4585,8 @@ def _lerp(a, b, t, out=None):
diff_b_a = subtract(b, a)
# asanyarray is a stop-gap until gh-13105
lerp_interpolation = asanyarray(add(a, diff_b_a * t, out=out))
subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5)
subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5,
casting='unsafe')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may sound weird, but we should probably also add dtype=type(lerp_interpolation.dtype).

If we have unsafe casting, this means that at least to some degree we ignore the precision of the weights of the quantiles for the output precision (promotion). Can you give me a hint in how things are currently? Does the same happen if we use np.longdouble for the quantile level (q)?

(Sorry, but I am a bit surprised how many subtleties there still seem to be along promotion in quantiles :(, and hoping we can clarify things a bit for me and everyone else.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seberg I tested with

import numpy as np
print(np,np.__version__)

x = np.linspace(.1, .7, 5, dtype=np.longdouble)
for q in [0, 1, 0., 1., .5]:
    r = np.quantile(x, q)
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')
    r = np.quantile(x, np.longdouble(q))
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')
    
    
print('----')    
x = np.array([0, 1])
for q in [0, 1, 0., 1., .5]:
    r = np.quantile(x, q)
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')
    r = np.quantile(x, np.longdouble(q))
    print(f'{q} ({type(q)}) -> {r} ({type(r)})')    

Output on 1.26:

<module 'numpy' from '/home/eendebakpt/py310/lib/python3.10/site-packages/numpy/__init__.py'> 1.26.0
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
----
0 (<class 'int'>) -> 0 (<class 'numpy.int64'>)
0 (<class 'int'>) -> 0.0 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 1 (<class 'numpy.int64'>)
1 (<class 'int'>) -> 1.0 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.float64'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.float64'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.float64'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.longdouble'>)

Output with this PR:

<module 'numpy' from '/mnt/data/numpy/numpy/__init__.py'> 2.0.0.dev0+git20231107.a555e7d
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
0 (<class 'int'>) -> 0.1 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 0.7 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.1 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 0.7 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.4 (<class 'numpy.longdouble'>)
----
0 (<class 'int'>) -> 0 (<class 'numpy.int64'>)
0 (<class 'int'>) -> 0.0 (<class 'numpy.longdouble'>)
1 (<class 'int'>) -> 1 (<class 'numpy.int64'>)
1 (<class 'int'>) -> 1.0 (<class 'numpy.longdouble'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.float64'>)
0.0 (<class 'float'>) -> 0.0 (<class 'numpy.longdouble'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.float64'>)
1.0 (<class 'float'>) -> 1.0 (<class 'numpy.longdouble'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.float64'>)
0.5 (<class 'float'>) -> 0.5 (<class 'numpy.longdouble'>)

If you want I can add tests for this case as well.

if lerp_interpolation.ndim == 0 and out is None:
lerp_interpolation = lerp_interpolation[()] # unpack 0d arrays
return lerp_interpolation
Expand Down
4 changes: 4 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3604,6 +3604,10 @@ def test_fraction(self):
assert_equal(q, Fraction(7, 2))
assert_equal(type(q), Fraction)

q = np.quantile(x, .5)
assert_equal(q, 1.75)
assert_equal(type(q), np.float64)

q = np.quantile(x, Fraction(1, 2))
assert_equal(q, Fraction(7, 4))
assert_equal(type(q), Fraction)
Expand Down