Repository navigation
[Python] Make weighted ApproximateQuantiles independent of the weight scale - #41560
anishmehta24 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a scale-variance bug in ApproximateQuantilesCombineFn.extract_output when weighted=True, where subtracting 1 from total_weight when computing offset caused the computed quantiles to depend on the arbitrary magnitude of the weights. Setting offset = step in the weighted branch makes the quantile calculation invariant to weight scaling and aligns the behavior with the existing docstring examples.
🔍 General Feedback
- The fix is clean, well-commented, and preserves the exact behavior for unweighted inputs.
- The added unit test in
stats_test.pythoroughly covers multiple weight scales across both uniform and skewed distributions.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @claudevdm for label python. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
JunaidAhamed-7777
left a comment
There was a problem hiding this comment.
The 101-element test avoids an exact cumulative-weight boundary. In a focused run of the combiner on this patch, the same 12 values with equal weights still gave [0, 6, 11] for 1.0 and [0, 5, 11] for 0.3, with three quantiles. Could we add this case and define how to handle rounding when the cumulative weight is right on the target? This was an isolated combiner probe, not a full Beam pipeline run.
Review text generated with ChatGPT; results are from a focused source probe.
|
Added the 12-element case to the test. A tie now goes to the first element whose cumulative weight reaches the target, which matches the old formula for integer weights, and the target is pulled back by 1e-9 of the total weight so float rounding can't flip it. Every scale gives |
Fixes #41559
ApproximateQuantilesCombineFn.extract_outputusedoffset = (total_weight - 1) / (num_quantiles - 1)for weighted input too. The- 1is one element when every weight is 1, but with real weights it is one unit of weight, so the quantiles moved with the scale of the weights (uniform weights of 0.001 on 0..100 gave[0, 0, 0, 0, 100]). For weighted input the k-th quantile is now the first element whose cumulative weight reachesk * total_weight / (num_quantiles - 1). The target is pulled back by1e-9 * total_weight, so an exact tie picks the same element at any scale even when float rounding lands the cumulative weight just above or below it. With integer weights this is the element the old formula picks in exact arithmetic, and unweighted input is unchanged.This also makes the weighted example in the
ApproximateQuantilesdocstring give its documented output,[0, 2, 5, 7, 100](master gives[0, 2, 4, 7, 100]).Added
test_weighted_quantiles_do_not_depend_on_weight_scale: at weights of 0.001, 0.3, 0.5, 1 and 1000, uniform weights on 0..100 give[0, 25, 50, 75, 100], the docstring example gives[0, 2, 5, 7, 100], and 12 equal weights with 3 quantiles (an exact tie at the median) give[0, 5, 11]. It fails on master. All ofapache_beam/transforms/stats_test.pypasses (86 tests), including the existing weighted, per-key and batched weighted expectations, which didn't change.