@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 testWindowSize() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.setWindowSize(300); for (int i = 0; i < 100; ++i) { stats.addValue(i + 1); } int refSum = (100 * 101) / 2; Assert.assertEquals(refSum / 100.0, stats.getMean(), 1E-10); Assert.assertEquals(300, stats.getWindowSize()); try { stats.setWindowSize(-3); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected } Assert.assertEquals(300, stats.getWindowSize()); stats.setWindowSize(50); Assert.assertEquals(50, stats.getWindowSize()); int refSum2 = refSum - (50 * 51) / 2; Assert.assertEquals(refSum2 / 50.0, stats.getMean(), 1E-10); }
public void checkremoval( DescriptiveStatistics dstat, int wsize, double mean1, double mean2, double mean3) { dstat.setWindowSize(wsize); dstat.clear(); for (int i = 1; i <= 6; ++i) { dstat.addValue(i); } Assert.assertTrue(Precision.equalsIncludingNaN(mean1, dstat.getMean())); dstat.replaceMostRecentValue(0); Assert.assertTrue(Precision.equalsIncludingNaN(mean2, dstat.getMean())); dstat.removeMostRecentValue(); Assert.assertTrue(Precision.equalsIncludingNaN(mean3, dstat.getMean())); }