@Test public void testMath1129() { final double[] data = new double[] { -0.012086732064244697, -0.24975668704012527, 0.5706168483164684, -0.322111769955327, 0.24166759508327315, Double.NaN, 0.16698443218942854, -0.10427763937565114, -0.15595963093172435, -0.028075857595882995, -0.24137994506058857, 0.47543170476574426, -0.07495595384947631, 0.37445697625436497, -0.09944199541668033 }; final DescriptiveStatistics ds = new DescriptiveStatistics(data); final double t = ds.getPercentile(75); final double o = ds.getPercentile(25); final double iqr = t - o; // System.out.println(String.format("25th percentile %s 75th percentile %s", o, t)); Assert.assertTrue(iqr >= 0); }
@Test public void testSetterInjection() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(3); Assert.assertEquals(2, stats.getMean(), 1E-10); // Now lets try some new math stats.setMeanImpl(new deepMean()); Assert.assertEquals(42, stats.getMean(), 1E-10); }
@Test public void test20090720() { DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(100); for (int i = 0; i < 161; i++) { descriptiveStatistics.addValue(1.2); } descriptiveStatistics.clear(); descriptiveStatistics.addValue(1.2); Assert.assertEquals(1, descriptiveStatistics.getN()); }
@Test public void testQuadraticMean() { final double[] values = {1.2, 3.4, 5.6, 7.89}; final DescriptiveStatistics stats = new DescriptiveStatistics(values); final int len = values.length; double expected = 0; for (int i = 0; i < len; i++) { final double v = values[i]; expected += v * v / len; } expected = Math.sqrt(expected); Assert.assertEquals(expected, stats.getQuadraticMean(), Math.ulp(expected)); }
@Test public void testToString() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(2); stats.addValue(3); Locale d = Locale.getDefault(); Locale.setDefault(Locale.US); Assert.assertEquals( "DescriptiveStatistics:\n" + "n: 3\n" + "min: 1.0\n" + "max: 3.0\n" + "mean: 2.0\n" + "std dev: 1.0\n" + "median: 2.0\n" + "skewness: 0.0\n" + "kurtosis: NaN\n", stats.toString()); Locale.setDefault(d); }
@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); }
@Test public void testCopy() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(3); DescriptiveStatistics copy = new DescriptiveStatistics(stats); Assert.assertEquals(2, copy.getMean(), 1E-10); // Now lets try some new math stats.setMeanImpl(new deepMean()); copy = stats.copy(); Assert.assertEquals(42, copy.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())); }
@Test public void testPercentileSetter() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(2); stats.addValue(3); Assert.assertEquals(2, stats.getPercentile(50.0), 1E-10); // Inject wrapped Percentile impl stats.setPercentileImpl(new goodPercentile()); Assert.assertEquals(2, stats.getPercentile(50.0), 1E-10); // Try "new math" impl stats.setPercentileImpl(new subPercentile()); Assert.assertEquals(10.0, stats.getPercentile(10.0), 1E-10); // Try to set bad impl try { stats.setPercentileImpl(new badPercentile()); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } }
@Test public void testGetValues() { DescriptiveStatistics stats = createDescriptiveStatistics(); for (int i = 100; i > 0; --i) { stats.addValue(i); } int refSum = (100 * 101) / 2; Assert.assertEquals(refSum / 100.0, stats.getMean(), 1E-10); double[] v = stats.getValues(); for (int i = 0; i < v.length; ++i) { Assert.assertEquals(100.0 - i, v[i], 1.0e-10); } double[] s = stats.getSortedValues(); for (int i = 0; i < s.length; ++i) { Assert.assertEquals(i + 1.0, s[i], 1.0e-10); } Assert.assertEquals(12.0, stats.getElement(88), 1.0e-10); }
@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); }