Fix swapped interpolation weights and right tail sign in tdigest Quantile - #171
Open
jaideeppyne wants to merge 1 commit into
Open
Fix swapped interpolation weights and right tail sign in tdigest Quantile#171jaideeppyne wants to merge 1 commit into
jaideeppyne wants to merge 1 commit into
Conversation
…reference subtracts The reference MergingDigest pairs mean[i] with the distance to the right edge of the bracket and mean[i+1] with the distance to the left edge. We passed them the other way round, so within a bracket a rising rank pulls the estimate toward the lower centroid and Quantile is not monotonic. The right tail branch used max + (...) where the reference uses max - (...), so it can return a value above MaxValue. Reachable through DecodeDouble, which accepts a tail centroid of weight above 1.
jaideeppyne
force-pushed
the
fix-tdigest-quantile-interpolation
branch
from
August 31, 2026 13:18
54f7a82 to
c87f67c
Compare
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.
Two spots in
tdigest.Double.Quantilediffer from the reference implementation (MergingDigest). The first affects ordinary use, the second needs a decoded sketch.The interpolation weights are passed in the wrong order
Reference:
We compute the same two quantities as
w1/w2and then callweightedAverage(d.centroids[i].mean, w1, d.centroids[i+1].mean, w2). The weight onmean[i]should be the distance to the far end of the bracket, not the near one. As the rank rises inside a bracket the estimate moves toward the lower centroid instead of the upper one.That makes
Quantilenon-monotonic from a plainUpdatesequence. 2000 of 2000 random digests return someQuantile(r2) < Quantile(r1)withr2 > r1, k in {10, 20, 50, 100, 200}, n in [100, 50000], gaussian / uniform / lognormal. It also shows on the checked-in fixtures:tdigest_ref_k100_n10000_double.skand everyserialization_test_data/cpp_generated_files/tdigest_double_*file give thousands of descents over 5000 evenly spaced ranks, and 0 after.It costs accuracy too. Rank error against the exact quantiles of the input, 29970 queries at k=100, n=20000 gaussian:
The right tail branch adds where the reference subtracts
Reference is
max - (...), we haved.max + (...), so the branch returns quantiles aboveMaxValue(). On min 0, centroids (10,100), (20,100), (30,100), max 40, rank 0.90 returns 45.92 where the reference gives 34.08.This one is not reachable from
UpdateorMerge.mergenever lets the first or last centroid absorb a neighbour, so both tails stay singletons andlastWeight > 1is false. I looked for a counterexample over 3000 digests built from random updates, merges and encode/decode round trips and never saw a first or last centroid of weight above 1. It is reachable fromDecodeDouble, which accepts one, so the test builds it that way.Rank's left tail already has the/ centroidsWeightnormalizer, so there is nothing to change there. That is the one difference from apache/datasketches-java#755, which fixes the same two plus a missing normalizer ingetRank.Two new tests, each verified failing before its own fix with the tests kept in place.
go test ./...andgo test -race ./tdigest/are green before and after, so nothing existing covered this.I used Claude Code on this. The oracles were tdunning's
MergingDigestand monotonicity, which needs no second implementation. Differential testing against the C++ core would not have found it, since C++ has the same two.