Skip to content

Commit

Permalink
Correct statistics with an infinite value
Browse files Browse the repository at this point in the history
  • Loading branch information
aherbert committed Sep 20, 2023
1 parent 622ebc7 commit fd485f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public double getStandardDeviation() {
if (stdDev > 0) {
stdDev = Math.sqrt(stdDev / (size - 1));
} else {
stdDev = 0.0;
stdDev = Double.isFinite(stdDev) ? 0.0 : Double.NaN;
}
return stdDev;
}
Expand All @@ -428,7 +428,7 @@ public double getVariance() {
if (variance > 0) {
variance = variance / (size - 1);
} else {
variance = 0.0;
variance = Double.isFinite(variance) ? 0.0 : Double.NaN;
}
return variance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,13 @@ void testConfidenceInterval() {
Assertions.assertTrue(intervals[1] > intervals[0]);
Assertions.assertTrue(intervals[2] > intervals[1]);
}

@Test
void testStdDevVarianceWithInfiniteValues() {
final Statistics stats = new Statistics();
stats.add(Double.POSITIVE_INFINITY);
Assertions.assertEquals(Double.NaN, stats.getStandardError());
Assertions.assertEquals(Double.NaN, stats.getStandardDeviation());
Assertions.assertEquals(Double.NaN, stats.getVariance());
}
}

0 comments on commit fd485f5

Please sign in to comment.