@Test public void testSummaryConsistency() { final DescriptiveStatistics dstats = new DescriptiveStatistics(); final SummaryStatistics sstats = new SummaryStatistics(); final int windowSize = 5; dstats.setWindowSize(windowSize); final double tol = 1E-12; for (int i = 0; i < 20; i++) { dstats.addValue(i); sstats.clear(); double[] values = dstats.getValues(); for (int j = 0; j < values.length; j++) { sstats.addValue(values[j]); } TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol); TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol); TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol); TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol); TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol); TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol); TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol); TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol); TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol); TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol); TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol); TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol); TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol); TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol); TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol); TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol); TestUtils.assertEquals( new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol); } }
@Test public void testShuffledStatistics() { // the purpose of this test is only to check the get/set methods // we are aware shuffling statistics like this is really not // something sensible to do in production ... DescriptiveStatistics reference = createDescriptiveStatistics(); DescriptiveStatistics shuffled = createDescriptiveStatistics(); UnivariateStatistic tmp = shuffled.getGeometricMeanImpl(); shuffled.setGeometricMeanImpl(shuffled.getMeanImpl()); shuffled.setMeanImpl(shuffled.getKurtosisImpl()); shuffled.setKurtosisImpl(shuffled.getSkewnessImpl()); shuffled.setSkewnessImpl(shuffled.getVarianceImpl()); shuffled.setVarianceImpl(shuffled.getMaxImpl()); shuffled.setMaxImpl(shuffled.getMinImpl()); shuffled.setMinImpl(shuffled.getSumImpl()); shuffled.setSumImpl(shuffled.getSumsqImpl()); shuffled.setSumsqImpl(tmp); for (int i = 100; i > 0; --i) { reference.addValue(i); shuffled.addValue(i); } Assert.assertEquals(reference.getMean(), shuffled.getGeometricMean(), 1.0e-10); Assert.assertEquals(reference.getKurtosis(), shuffled.getMean(), 1.0e-10); Assert.assertEquals(reference.getSkewness(), shuffled.getKurtosis(), 1.0e-10); Assert.assertEquals(reference.getVariance(), shuffled.getSkewness(), 1.0e-10); Assert.assertEquals(reference.getMax(), shuffled.getVariance(), 1.0e-10); Assert.assertEquals(reference.getMin(), shuffled.getMax(), 1.0e-10); Assert.assertEquals(reference.getSum(), shuffled.getMin(), 1.0e-10); Assert.assertEquals(reference.getSumsq(), shuffled.getSum(), 1.0e-10); Assert.assertEquals(reference.getGeometricMean(), shuffled.getSumsq(), 1.0e-10); }