feat: add P2QuantileEstimator, one quantile of a stream in constant memory - #7603
Merged
DenizAltunkapan merged 1 commit intoSep 12, 2026
Merged
Conversation
…emory The P-square algorithm of Jain and Chlamtac keeps five markers instead of the samples, so a quantile of an unbounded stream costs O(1) time per sample and O(1) memory. Each marker is nudged towards its desired position with a piecewise parabolic prediction, falling back to a linear one whenever the parabola would break the ordering of the heights. The first five samples are kept verbatim, so the estimate is exact until the sixth arrives, and the minimum and maximum stay exact for the whole stream. Signed-off-by: alxkm <19151554+alxkm@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7603 +/- ##
============================================
+ Coverage 81.03% 81.11% +0.07%
- Complexity 7766 7816 +50
============================================
Files 826 827 +1
Lines 24645 24742 +97
Branches 4814 4833 +19
============================================
+ Hits 19972 20070 +98
Misses 3907 3907
+ Partials 766 765 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DenizAltunkapan
approved these changes
Sep 12, 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.
Adds the P-square algorithm of Jain and Chlamtac, which estimates one quantile of a stream from five numbers and never stores the samples.
An exact quantile needs the whole stream in memory, which is not an option for an unbounded one. P-square keeps five markers instead: the minimum, the quantiles
p/2,pand(1 + p)/2, and the maximum. Each marker has a height and a position, that is how many samples are known to sit at or below it. A new sample shifts the positions of the markers it falls below, and every marker is then nudged back towards the position it should occupy, using a piecewise parabolic prediction fitted through its two neighbours. The parabolic step is rejected and replaced by a linear one whenever it would break the ordering of the heights, which is what keeps the estimator stable on sorted or violently jumping input.The first five samples are stored verbatim, so the answer is exact until the sixth arrives, and
min()andmax()stay exact for the whole stream. Accuracy is typically a fraction of a percent in rank on stationary data and improves as the stream grows, but there is no bound for adversarial input, and the Javadoc says so rather than implying a guarantee.addandquantilerun in O(1) and the estimator holds five markers regardless of how many samples pass through it. That is the point of it: a p99 latency counter per endpoint costs a fixed 80 bytes or so, where an exact one grows without limit.P2QuantileEstimatorTestcovers 27 cases. Among them: the estimate is exact for the first five samples, a ramp lands exactly on its textbook quantile which pins the marker convention, uniform streams are estimated within a percent of the exact quantile for several probabilities, the tail of an exponential stream is tracked, already sorted input and wildly jumping input both stay sane, estimates of different quantiles of the same stream keep their order, and the extremes are exact.Checklist
clang-format -i --style=file path/to/your/file.java