claudevdm commented on code in PR #41560:
URL: https://github.com/apache/beam/pull/41560#discussion_r4786646170


##########
sdks/python/apache_beam/transforms/stats_test.py:
##########
@@ -321,6 +321,36 @@ def weight(x):
           equal_to([[3., 0.6720000000000002, 0., -0.6720000000000002, -3.]]),
           label="checkWeightedReversedQuantilesGlobally")
 
+  def test_weighted_quantiles_do_not_depend_on_weight_scale(self):
+    with TestPipeline() as p:
+      for scale in (0.001, 0.3, 0.5, 1, 1000):
+        uniform = (
+            p | 'Create uniform %s' % scale >> Create([(i, scale)
+                                                       for i in range(101)])
+            | 'Uniform %s' % scale >> beam.ApproximateQuantiles.Globally(
+                5, weighted=True))
+        assert_that(
+            uniform,
+            equal_to([[0, 25, 50, 75, 100]]),
+            label='checkUniform%s' % scale)
+        # The example from the ApproximateQuantiles docstring.
+        skewed = (
+            p | 'Create skewed %s' % scale >> Create(
+                [(i, scale if i < 10 else scale * 1e-5) for i in range(101)])
+            | 'Skewed %s' % scale >> beam.ApproximateQuantiles.Globally(
+                5, weighted=True))
+        assert_that(
+            skewed,
+            equal_to([[0, 2, 5, 7, 100]]),
+            label='checkSkewed%s' % scale)
+        # The cumulative weight of element 5 is exactly half of the total.
+        tie = (
+            p | 'Create tie %s' % scale >> Create([(i, scale)
+                                                   for i in range(12)])
+            | 'Tie %s' % scale >> beam.ApproximateQuantiles.Globally(
+                3, weighted=True))
+        assert_that(tie, equal_to([[0, 5, 11]]), label='checkTie%s' % scale)
+

Review Comment:
   
   ```suggestion
   
     def test_weighted_quantiles_with_decimal_and_float32_weights(self):
       import decimal
   
       import numpy as np
       with TestPipeline() as p:
         # The cumulative weight of element 5 is exactly half of the total.
         for name, weight in (('decimal', decimal.Decimal('2.5')),
                              ('float32', np.float32(1))):
           tie = (
               p | 'Create %s' % name >> Create([(i, weight) for i in 
range(12)])
               | 'Tie %s' % name >> beam.ApproximateQuantiles.Globally(
                   3, weighted=True))
           assert_that(tie, equal_to([[0, 5, 11]]), label='check_%s' % name)
   
   ```



##########
sdks/python/apache_beam/transforms/stats.py:
##########
@@ -984,7 +984,17 @@ def extract_output(self, accumulator):
         total_weight += len(buffer_elem.elements) * buffer_elem.weights[0]
 
     step = total_weight / (self._num_quantiles - 1)
-    offset = (total_weight - 1) / (self._num_quantiles - 1)
+    if self._spec.weighted:
+      # Weights don't count elements, so the -1 below would make the result
+      # depend on the scale of the weights. The k-th quantile is the first
+      # element whose cumulative weight reaches k * step. _interpolate moves
+      # past elements whose cumulative weight is <= its target, so the target
+      # is pulled back by a tiny fraction of the total weight. This picks the
+      # same element for an exact tie at any scale, even when float rounding
+      # puts the cumulative weight just below or above k * step.
+      offset = step - total_weight * 1e-9

Review Comment:
   What should happen with `Decimal` or `np.float32` weights? With 12 equal 
weights and `num_quantiles=3`:
   
   ```python
   quantiles(decimal.Decimal('2.5'))  # master [0, 5, 11], here TypeError 
(Decimal * float)
   quantiles(np.float32(1))           # master [0, 5, 11], here [0, 6, 11]
   ```
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to