/** * Returns the <a href="http://en.wikipedia.org/wiki/Arithmetic_mean">arithmetic mean</a> of the * values. The count must be non-zero. * * <p>The definition of the mean is the same as {@link Stats#mean}. * * @param values a series of values, which will be converted to {@code double} values (this may * cause loss of precision) * @throws IllegalArgumentException if the dataset is empty */ public static double meanOf(Iterable<? extends Number> values) { return meanOf(values.iterator()); }
/** * Returns the <a href="http://en.wikipedia.org/wiki/Arithmetic_mean">arithmetic mean</a> of the * values. The count must be non-zero. * * <p>The definition of the mean is the same as {@link Stats#mean}. * * @param values a series of values, which will be converted to {@code double} values (this may * cause loss of precision) * @throws IllegalArgumentException if the dataset is empty */ public static double meanOf(Iterable<? extends Number> values) { return meanOf(values.iterator()); }
/** * Returns the <a href="http://en.wikipedia.org/wiki/Arithmetic_mean">arithmetic mean</a> of the * values. The count must be non-zero. * * <p>The definition of the mean is the same as {@link Stats#mean}. * * @param values a series of values, which will be converted to {@code double} values (this may * cause loss of precision) * @throws IllegalArgumentException if the dataset is empty */ public static double meanOf(Iterable<? extends Number> values) { return meanOf(values.iterator()); }
public void testMeanOf() { try { Stats.meanOf(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { Stats.meanOf(ImmutableList.<Number>of()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(Stats.meanOf(ONE_VALUE)).isWithin(ALLOWED_ERROR).of(ONE_VALUE); assertThat(Stats.meanOf(POSITIVE_INFINITY)).isPositiveInfinity(); assertThat(Stats.meanOf(NEGATIVE_INFINITY)).isNegativeInfinity(); assertThat(Stats.meanOf(NaN)).isNaN(); assertThat(Stats.meanOf(TWO_VALUES)).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN); double mean = Stats.meanOf(values.asArray()); if (values.hasAnyNaN()) { assertThat(mean).named("mean of " + values).isNaN(); assertThat(Stats.meanOf(MANY_VALUES)).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN); assertThat(Stats.meanOf(MANY_VALUES.iterator())).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN); assertThat(Stats.meanOf(INTEGER_MANY_VALUES)) .isWithin(ALLOWED_ERROR) .of(INTEGER_MANY_VALUES_MEAN); assertThat(Stats.meanOf(Ints.toArray(INTEGER_MANY_VALUES))) .isWithin(ALLOWED_ERROR) .of(INTEGER_MANY_VALUES_MEAN); assertThat(Stats.meanOf(LONG_MANY_VALUES)).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MEAN); assertThat(Stats.meanOf(Longs.toArray(LONG_MANY_VALUES)))
public void testEquivalentStreams() { // For datasets of many double values created from an array, we test many combinations of finite // and non-finite values: for (ManyValues values : ALL_MANY_VALUES) { double[] array = values.asArray(); Stats stats = Stats.of(array); // instance methods on Stats vs on instance methods on DoubleStream assertThat(stats.count()).isEqualTo(stream(array).count()); assertEquivalent(stats.mean(), stream(array).average().getAsDouble()); assertEquivalent(stats.sum(), stream(array).sum()); assertEquivalent(stats.max(), stream(array).max().getAsDouble()); assertEquivalent(stats.min(), stream(array).min().getAsDouble()); // static method on Stats vs on instance method on DoubleStream assertEquivalent(Stats.meanOf(array), stream(array).average().getAsDouble()); // instance methods on Stats vs instance methods on DoubleSummaryStatistics DoubleSummaryStatistics streamStats = stream(array).summaryStatistics(); assertThat(stats.count()).isEqualTo(streamStats.getCount()); assertEquivalent(stats.mean(), streamStats.getAverage()); assertEquivalent(stats.sum(), streamStats.getSum()); assertEquivalent(stats.max(), streamStats.getMax()); assertEquivalent(stats.min(), streamStats.getMin()); } }
private static double[] meansOf(Collection<double[]> collectionOfDoubleArrays) { double[] result = new double[collectionOfDoubleArrays.size()]; int index = 0; for (double[] array : collectionOfDoubleArrays) { result[index++] = Stats.meanOf(array); } return result; }
/** * Returns the <a href="http://en.wikipedia.org/wiki/Arithmetic_mean">arithmetic mean</a> of the * values. The count must be non-zero. * * <p>The definition of the mean is the same as {@link Stats#mean}. * * @param values a series of values, which will be converted to {@code double} values (this may * cause loss of precision) * @throws IllegalArgumentException if the dataset is empty */ public static double meanOf(Iterable<? extends Number> values) { return meanOf(values.iterator()); }
/** * Returns the <a href="http://en.wikipedia.org/wiki/Arithmetic_mean">arithmetic mean</a> of the * values. The count must be non-zero. * * <p>The definition of the mean is the same as {@link Stats#mean}. * * @param values a series of values, which will be converted to {@code double} values (this may * cause loss of precision) * @throws IllegalArgumentException if the dataset is empty */ public static double meanOf(Iterable<? extends Number> values) { return meanOf(values.iterator()); }
int index = 0; for (double[] location : locations) { means[index++] = Stats.meanOf(location);
public void testMeanOf() { try { Stats.meanOf(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { Stats.meanOf(ImmutableList.<Number>of()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(Stats.meanOf(ONE_VALUE)).isWithin(ALLOWED_ERROR).of(ONE_VALUE); assertThat(Stats.meanOf(POSITIVE_INFINITY)).isPositiveInfinity(); assertThat(Stats.meanOf(NEGATIVE_INFINITY)).isNegativeInfinity(); assertThat(Stats.meanOf(NaN)).isNaN(); assertThat(Stats.meanOf(TWO_VALUES)).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN); double mean = Stats.meanOf(values.asArray()); if (values.hasAnyNaN()) { assertThat(mean).named("mean of " + values).isNaN(); assertThat(Stats.meanOf(MANY_VALUES)).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN); assertThat(Stats.meanOf(MANY_VALUES.iterator())).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN); assertThat(Stats.meanOf(INTEGER_MANY_VALUES)) .isWithin(ALLOWED_ERROR) .of(INTEGER_MANY_VALUES_MEAN); assertThat(Stats.meanOf(Ints.toArray(INTEGER_MANY_VALUES))) .isWithin(ALLOWED_ERROR) .of(INTEGER_MANY_VALUES_MEAN); assertThat(Stats.meanOf(LONG_MANY_VALUES)).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MEAN); assertThat(Stats.meanOf(Longs.toArray(LONG_MANY_VALUES)))
public void testEquivalentStreams() { // For datasets of many double values created from an array, we test many combinations of finite // and non-finite values: for (ManyValues values : ALL_MANY_VALUES) { double[] array = values.asArray(); Stats stats = Stats.of(array); // instance methods on Stats vs on instance methods on DoubleStream assertThat(stats.count()).isEqualTo(stream(array).count()); assertEquivalent(stats.mean(), stream(array).average().getAsDouble()); assertEquivalent(stats.sum(), stream(array).sum()); assertEquivalent(stats.max(), stream(array).max().getAsDouble()); assertEquivalent(stats.min(), stream(array).min().getAsDouble()); // static method on Stats vs on instance method on DoubleStream assertEquivalent(Stats.meanOf(array), stream(array).average().getAsDouble()); // instance methods on Stats vs instance methods on DoubleSummaryStatistics DoubleSummaryStatistics streamStats = stream(array).summaryStatistics(); assertThat(stats.count()).isEqualTo(streamStats.getCount()); assertEquivalent(stats.mean(), streamStats.getAverage()); assertEquivalent(stats.sum(), streamStats.getSum()); assertEquivalent(stats.max(), streamStats.getMax()); assertEquivalent(stats.min(), streamStats.getMin()); } }