Repository navigation
Match NumPy in percentile() for the midpoint, nearest and plotting-position methods - #1286
Merged
Merged
Conversation
…sition methods MIDPOINT averaged x[ceil(i)] and x[ceil(i)+1] instead of x[floor(i)] and x[ceil(i)], so it was wrong for every input (q=25 of [0,10,20,30,40] gave 15 instead of 10). HAZEN, WEIBULL, MEDIAN_UNBIASED and NORMAL_UNBIASED did not clamp the virtual index to [0, n-1] and read one element past the sorted buffer at high q, and NEAREST rounded ties away from zero instead of to even. Clamp the index as NumPy does, use floor and ceil for MIDPOINT, and round half to even for NEAREST. The existing test expected the old MIDPOINT result; it now expects NumPy's, and new cases cover both tails, n=1 and the NEAREST tie. The docs said that q=0 is the mean; it is the minimum. Signed-off-by: 0z5a <[email protected]>
Contributor
[Medium impact] The PR appears safe to merge; the previous rounding-mode issue is fixed and no new blocking issues were found. SummaryThis PR corrects
The previous finding is fixed. No new actionable issues were found. Tests were not run during this review. Reviews (2) · Last reviewed commit: "fix(percentile): make nearest rounding m..." · Reviewed by Greptile |
Collaborator
|
/build |
cliffburdick
reviewed
Nov 3, 2026
Select the nearest index explicitly with ties to even, using CUDA standard math without changing the caller rounding mode. Signed-off-by: 0z5a <[email protected]>
Collaborator
|
/build |
cliffburdick
approved these changes
Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1285.
Problem
percentile_impl(include/matx/transforms/percentile.h):x[ceil(i)]andx[ceil(i)+1]instead ofx[floor(i)]andx[ceil(i)](orx[i]when the index is whole), so it was wrong for every input.[0, n-1], and read one element past the sorted buffer at high q. LINEAR with n=1 also readx[1](with weight 0).nearbyintfix also depended on the calling thread's rounding mode.Fix
Clamp the virtual index as NumPy does, use floor and ceil for MIDPOINT, and select the nearest index explicitly with ties to even for NEAREST using
cuda::std::floor, independent of the host rounding mode.percentile.rstsaid that q=0 is the mean; it is the minimum.Tests
PercentileMethodsAndBoundsintest/00_operators/ReductionTests.cuexpected 15.0 for MIDPOINT at q=25; it now expects NumPy's 10.0. New cases cover both tails for each method, n=4 and n=6 MIDPOINT/NEAREST, and n=1.On the previous PR head
4eccd75, a standalone B300 repro (sm_103, CUDA 13.3) usedcudaExecutorandSingleThreadedHostExecutor:main(26f6df6)numpy.percentilecompute-sanitizer --tool memcheckFor LOWER, HIGHER and MIDPOINT with
(n-1)*qa multiple of 100, a random-q comparison against NumPy can still differ: NumPy's floating-pointq/100moves the index there, while MatX's integer path is exact. LOWER and HIGHER already behaved this way.The latest rounding-mode test and change have not been built or run on CUDA. A standalone host-only harness using the index expressions matched an integer reference for 198,396 cases across four rounding modes. The full MatX test suite was not built.