Skip to content

Commit

Permalink
variance is imprecise when size is small (#418)
Browse files Browse the repository at this point in the history
In my opinion the current variance (and by consequence
the standard deviation) computation are imprecise, when
curSize is not high (when curSize < 10 for example):

```java
    variance = (sumSquares / curSize) - (mean * mean);
```

The computation I suggest in this pull request does not hurt
and is precise:

```java
    if (curSize == 1) {
      variance = 0d;
    } else {
      variance = (sumSquares - ((double) total * total / curSize)) / (curSize - 1);
    }
```

For reference:
http://web.archive.org/web/20050512031826/http://helios.bto.ed.ac.uk/bto/statistics/tress3.html
  • Loading branch information
evernat authored and brharrington committed Jun 4, 2017
1 parent b5f703d commit 243b8f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Expand Up @@ -122,7 +122,11 @@ public void computeStats() {
sumSquares += values[i] * values[i];
}
mean = (double) total / curSize;
variance = (sumSquares / curSize) - (mean * mean);
if (curSize == 1) {
variance = 0d;
} else {
variance = (sumSquares - ((double) total * total / curSize)) / (curSize - 1);
}
stddev = Math.sqrt(variance);

computePercentiles(curSize);
Expand Down
Expand Up @@ -72,13 +72,13 @@ public void testTotalNoWrap() {
@Test
public void testVarianceNoWrap() {
StatsBuffer buffer = getNoWrap();
assertEquals(buffer.getVariance(), 20916.66667, 1e-4);
assertEquals(buffer.getVariance(), 20958.5, 1e-4);
}

@Test
public void testStdDevNoWrap() {
StatsBuffer buffer = getNoWrap();
assertEquals(buffer.getStdDev(), 144.62595, 1e-4);
assertEquals(buffer.getStdDev(), 144.77051, 1e-4);
}

@Test
Expand Down Expand Up @@ -173,7 +173,7 @@ public void testMeanWrap() {
assertEquals(buffer.getMean(), (double) EXPECTED_TOTAL_WRAP / SIZE);
}

static final double EXPECTED_VARIANCE_WRAP = 83333.25;
static final double EXPECTED_VARIANCE_WRAP = 83416.66667;

@Test
public void testVarianceWrap() {
Expand Down

0 comments on commit 243b8f3

Please sign in to comment.