Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ public double metric(String name, long owningBucketOrd) {
private double variance(long owningBucketOrd) {
double sum = sums.get(owningBucketOrd);
long count = counts.get(owningBucketOrd);
return (sumOfSqrs.get(owningBucketOrd) - ((sum * sum) / count)) / count;
double variance = (sumOfSqrs.get(owningBucketOrd) - ((sum * sum) / count)) / count;
return variance < 0 ? 0 : variance;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public double getSumOfSquares() {

@Override
public double getVariance() {
return (sumOfSqrs - ((sum * sum) / count)) / count;
double variance = (sumOfSqrs - ((sum * sum) / count)) / count;
return variance < 0 ? 0 : variance;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ public void testRandomDoubles() throws IOException {
);
}

/**
* Testcase for https://github.com/elastic/elasticsearch/issues/37303
*/
public void testVarianceNonNegative() throws IOException {
MappedFieldType ft =
new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
ft.setName("field");
final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator();
testCase(ft,
iw -> {
int numDocs = 3;
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
double value = 49.95d;
long valueAsLong = NumericUtils.doubleToSortableLong(value);
doc.add(new SortedNumericDocValuesField("field", valueAsLong));
expected.add(value);
iw.addDocument(doc);
}
},
stats -> {
//since the value(49.95) is a constant, variance should be 0
assertEquals(0.0d, stats.getVariance(), TOLERANCE);
assertEquals(0.0d, stats.getStdDeviation(), TOLERANCE);
}
);
}

public void testRandomLongs() throws IOException {
MappedFieldType ft =
new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
Expand Down Expand Up @@ -236,7 +264,8 @@ void add(double value) {
}

double variance() {
return (sumOfSqrs - ((sum * sum) / count)) / count;
double variance = (sumOfSqrs - ((sum * sum) / count)) / count;
return variance < 0 ? 0 : variance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ private static double variance(int... vals) {
sum += val;
sumOfSqrs += val * val;
}
return (sumOfSqrs - ((sum * sum) / vals.length)) / vals.length;
double variance = (sumOfSqrs - ((sum * sum) / vals.length)) / vals.length;
return variance < 0 ? 0 : variance;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void testGappyIndexWithSigma() {
double sumOfSqrs = 1.0 + 1.0 + 1.0 + 4.0 + 0.0 + 1.0;
double avg = sum / count;
double var = (sumOfSqrs - ((sum * sum) / count)) / count;
var = var < 0 ? 0 : var;
double stdDev = Math.sqrt(var);
assertThat(extendedStatsBucketValue, notNullValue());
assertThat(extendedStatsBucketValue.getName(), equalTo("extended_stats_bucket"));
Expand Down