Fix median calculation in MedianAbsoluteDeviationAggregatorTests (#38979)

Fixes an error in median calculation in
MedianAbsoluteDeviationAggregatorTests for odd number of sample points,
which causes some rare test failures.

Fixes #38937
This commit is contained in:
Igor Motov 2019-02-19 16:02:11 -05:00
parent 90b1b36f50
commit 3d93011e32
1 changed files with 9 additions and 3 deletions

View File

@ -260,9 +260,15 @@ public class MedianAbsoluteDeviationAggregatorTests extends AggregatorTestCase {
private static double calculateMedian(double[] sample) {
final double[] sorted = Arrays.copyOf(sample, sample.length);
Arrays.sort(sorted);
final int halfway = (int) Math.ceil(sorted.length / 2d);
final double median = (sorted[halfway - 1] + sorted[halfway]) / 2d;
final int halfway = (int) Math.ceil(sorted.length / 2d);
final double median;
if (sorted.length % 2 == 0) {
// even
median = (sorted[halfway - 1] + sorted[halfway]) / 2d;
} else {
// odd
median = (sorted[halfway - 1]);
}
return median;
}