Skip to content

[Python] Make weighted ApproximateQuantiles independent of the weight scale - #41560

Open
anishmehta24 wants to merge 2 commits into
apache:masterfrom
anishmehta24:fix/weighted-quantiles-weight-scale
Open

anishmehta24 wants to merge 2 commits into
apache:masterfrom
anishmehta24:fix/weighted-quantiles-weight-scale

Conversation

@anishmehta24

@anishmehta24 anishmehta24 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #41559

ApproximateQuantilesCombineFn.extract_output used offset = (total_weight - 1) / (num_quantiles - 1) for weighted input too. The - 1 is 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 reaches k * total_weight / (num_quantiles - 1). The target is pulled back by 1e-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 ApproximateQuantiles docstring 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 of apache_beam/transforms/stats_test.py passes (86 tests), including the existing weighted, per-key and batched weighted expectations, which didn't change.

@github-actions github-actions Bot added python core size/s S: 10-49 lines changed labels Nov 22, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 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.py thoroughly 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.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.
Backup reviewer: jrmccluskey (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

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 assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@JunaidAhamed-7777 JunaidAhamed-7777 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@anishmehta24

Copy link
Copy Markdown
Contributor Author

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 [0, 5, 11] now (da377e8).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Weighted ApproximateQuantiles depends on the scale of the weights

2 participants