OnlineStatisticsProvider implements StatisticsProvider, KryoSerializable { @Override public void addValue(double value) { long n1 = n; min = min == null?value:Math.min(min, value); max = max == null?value:Math.max(max, value); sum += value; sumOfLogs += Math.log(value); sumOfSquares += value*value; digest.add(value); n++; double delta, delta_n, delta_n2, term1; delta = value - M1; delta_n = delta / n; delta_n2 = delta_n * delta_n; term1 = delta * delta_n * n1; M1 += delta_n; M4 += term1 * delta_n2 * (n*n - 3*n + 3) + 6 * delta_n2 * M2 - 4 * delta_n * M3; M3 += term1 * delta_n * (n - 2) - 3 * delta_n * M2; M2 += term1; checkFlowError(sumOfSquares, sum, sumOfSquares, M1, M2, M3, M4); } OnlineStatisticsProvider(); @Override void addValue(double value); @Override long getCount(); @Override double getMin(); @Override double getMax(); @Override double getMean(); @Override double getSum(); @Override double getVariance(); @Override double getStandardDeviation(); @Override double getGeometricMean(); @Override double getPopulationVariance(); @Override double getQuadraticMean(); @Override double getSumLogs(); @Override double getSumSquares(); @Override double getKurtosis(); @Override double getSkewness(); @Override double getPercentile(double p); @Override StatisticsProvider merge(StatisticsProvider provider); @Override boolean equals(Object o); @Override int hashCode(); @Override void write(Kryo kryo, Output output); @Override void read(Kryo kryo, Input input); static final int COMPRESSION; }
@Test public void testOverflow() { OnlineStatisticsProvider statsProvider = new OnlineStatisticsProvider(); assertThrows(IllegalStateException.class, () -> statsProvider.addValue(Double.MAX_VALUE + 1)); }
@Test public void testUnderflow() { OnlineStatisticsProvider statsProvider = new OnlineStatisticsProvider(); double d = 3e-305; assertThrows(IllegalStateException.class, () -> statsProvider.addValue(d)); }