lib/qme/map/cv_aggregator.rb in quality-measure-engine-2.5.1 vs lib/qme/map/cv_aggregator.rb in quality-measure-engine-2.5.2
- old
+ new
@@ -4,25 +4,30 @@
class CVAggregator
def self.median(frequencies)
set_size = frequencies.values.reduce(0, :+)
offset = set_size.even? ? 1 : 0
- median_positions = [(set_size / 2), (set_size / 2)+offset]
+ left_position, right_position = [(set_size / 2), (set_size / 2) + offset]
+ current_position = -1 + offset #compensate for integer math flooring
- current_position = 0
median_left = nil
median_right = nil
frequencies.keys.sort.each do |value|
- current_position += frequencies[value]
-
- median_left = value if median_left.nil? && current_position >= median_positions[0]
- if current_position >= median_positions[1]
- median_right = value
+ current_position += (frequencies[value])
+
+ if current_position >= left_position && median_left == nil
+ median_left = value
+ return median_left if set_size.odd?
+ end
+
+ if current_position >= right_position
+ median_right = value
break
end
end
+
median_left ||= 0
median_right ||= 0
(median_left + median_right)/2
end
@@ -35,6 +40,6 @@
end
end
end
-end
\ No newline at end of file
+end