fix: reject NaN and speed up box plot distribution stats#382
Merged
Conversation
xBoxPlot, xBoxPlotWithOutliers and xRobustDistributionStats now sort the input once into a Float64Array and reuse it: outliers are read from the sorted prefix/suffix and the non-outlier range is taken as a zero-copy subarray, removing an O(n) filter-copy pass and its allocation. NaN is now rejected (it corrupts ranks, quartiles and moments): a NaN in the input throws "input must not contain NaN values". Infinity stays a valid extreme. Only the public box plot API is re-exported from x/index.ts; the sorted-array helpers stay internal. xRobustDistributionStats benchmark (deterministic data, ~5% outliers): n=1e3 0.030 -> 0.021 ms (1.4x) n=1e5 6.46 -> 5.60 ms (1.15x) n=1e6 63.7 -> 55.2 ms (1.15x)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #382 +/- ##
==========================================
+ Coverage 97.13% 97.16% +0.03%
==========================================
Files 207 208 +1
Lines 4147 4160 +13
Branches 1036 1030 -6
==========================================
+ Hits 4028 4042 +14
+ Misses 115 114 -1
Partials 4 4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Extract the identical lowIndex/highIndex scan that was duplicated in boxPlotWithOutliersFromSorted and xRobustDistributionStats into a shared getWhiskerBounds helper, so the outlier range cannot drift between the two. Also type outliers as number[], cache min/max in boxPlotFromSorted, and add a low-side outlier test for xBoxPlotWithOutliers.
boxPlotWithOutliersAndBounds returns the box plot together with the non-outlier index range, so xRobustDistributionStats reuses those bounds for its zero-copy subarray instead of scanning the sorted array a second time via getWhiskerBounds.
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.
xBoxPlot,xBoxPlotWithOutliersandxRobustDistributionStatsnow sort the input once into aFloat64Arrayand reuse it — outliers are read from the sorted prefix/suffix and the non-outlier range is a zero-copysubarray, removing an O(n) filter-copy pass and its allocation.NaN is now rejected (it corrupts ranks, quartiles and moments): a NaN in the input throws
input must not contain NaN values. Infinity stays a valid extreme. Only the public box plot API is re-exported fromx/index.ts; the sorted-array helpers stay internal.xRobustDistributionStatsbenchmark (deterministic data, ~5% outliers):