Skip to content

Commit

Permalink
Add Stream support to Stats and StatsAccumulator.
Browse files Browse the repository at this point in the history
This adds:
 - A static method Stats.toStats() that returns a Collector<Number, StatsAccumulator, Stats>.
 - Stats.of overloads and StatsAccumulator.addAll overloads that take DoubleStream, IntStream, and LongStream.
 - A StatsAccumulator.addAll overload that takes StatsAccumulator.

(In passing, this also improves the tests in a couple of ways. It removes the use of static initializer blocks in StatsTesting, which is contrary to best practice. And it fixes some tolerances on the assertions: the min() and max() ones should use exact equality, and the ones with INTEGER_MANY_VALUES and LONG_MANY_VALUES should scale up the tolerance to match the order of magnitude of the values. This latter change fixes the ErrorProne warnings that, in some cases, the tolerance was actually less than the difference between the expected value and the next nearest double. It just happened that everything was close enough before.)

RELNOTES=Add Stream support to Stats and StatsAccumulator.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=274998178
  • Loading branch information
peteg authored and kluever committed Oct 16, 2019
1 parent bf4627d commit e2f4eba
Show file tree
Hide file tree
Showing 10 changed files with 624 additions and 362 deletions.

Large diffs are not rendered by default.

114 changes: 50 additions & 64 deletions android/guava-tests/test/com/google/common/math/StatsTest.java
Expand Up @@ -145,19 +145,19 @@ public void testMean() {
.isWithin(ALLOWED_ERROR * Double.MAX_VALUE)
.of(LARGE_VALUES_MEAN);
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.mean())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN)
.of(INTEGER_MANY_VALUES_MEAN);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.mean())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN)
.of(INTEGER_MANY_VALUES_MEAN);
assertThat(LARGE_INTEGER_VALUES_STATS.mean())
.isWithin(ALLOWED_ERROR * Integer.MAX_VALUE)
.of(LARGE_INTEGER_VALUES_MEAN);
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.mean())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN)
.of(LONG_MANY_VALUES_MEAN);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.mean())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN)
.of(LONG_MANY_VALUES_MEAN);
assertThat(LARGE_LONG_VALUES_STATS.mean())
.isWithin(ALLOWED_ERROR * Long.MAX_VALUE)
Expand All @@ -182,16 +182,16 @@ public void testSum() {
.isWithin(ALLOWED_ERROR)
.of(MANY_VALUES_MEAN * MANY_VALUES_COUNT);
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.sum())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN)
.of(INTEGER_MANY_VALUES_MEAN * INTEGER_MANY_VALUES_COUNT);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.sum())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN)
.of(INTEGER_MANY_VALUES_MEAN * INTEGER_MANY_VALUES_COUNT);
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.sum())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN)
.of(LONG_MANY_VALUES_MEAN * LONG_MANY_VALUES_COUNT);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.sum())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN)
.of(LONG_MANY_VALUES_MEAN * LONG_MANY_VALUES_COUNT);
}

Expand Down Expand Up @@ -236,19 +236,19 @@ public void testPopulationVariance() {
.isWithin(ALLOWED_ERROR)
.of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.populationVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.populationVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT);
assertThat(LARGE_INTEGER_VALUES_STATS.populationVariance())
.isWithin(ALLOWED_ERROR * Integer.MAX_VALUE * Integer.MAX_VALUE)
.of(LARGE_INTEGER_VALUES_POPULATION_VARIANCE);
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.populationVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.populationVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT);
assertThat(LARGE_LONG_VALUES_STATS.populationVariance())
.isWithin(ALLOWED_ERROR * Long.MAX_VALUE * Long.MAX_VALUE)
Expand Down Expand Up @@ -283,16 +283,16 @@ public void testPopulationStandardDeviation() {
.isWithin(ALLOWED_ERROR)
.of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT));
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.populationStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT));
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.populationStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT));
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.populationStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT));
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.populationStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT));
}

Expand Down Expand Up @@ -328,16 +328,16 @@ public void testSampleVariance() {
.isWithin(ALLOWED_ERROR)
.of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1));
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.sampleVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (INTEGER_MANY_VALUES_COUNT - 1));
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.sampleVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (INTEGER_MANY_VALUES_COUNT - 1));
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.sampleVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1));
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.sampleVariance())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)
.of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1));
}

Expand Down Expand Up @@ -373,16 +373,16 @@ public void testSampleStandardDeviation() {
.isWithin(ALLOWED_ERROR)
.of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)));
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.sampleStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (INTEGER_MANY_VALUES_COUNT - 1)));
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.sampleStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (INTEGER_MANY_VALUES_COUNT - 1)));
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.sampleStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1)));
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.sampleStandardDeviation())
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS))
.of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1)));
}

Expand All @@ -397,13 +397,13 @@ public void testMax() {
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertThat(ONE_VALUE_STATS.max()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
assertThat(ONE_VALUE_STATS.max()).isEqualTo(ONE_VALUE);
assertThat(Stats.of(POSITIVE_INFINITY).max()).isPositiveInfinity();
assertThat(Stats.of(NEGATIVE_INFINITY).max()).isNegativeInfinity();
assertThat(Stats.of(NaN).max()).isNaN();
assertThat(TWO_VALUES_STATS.max()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MAX);
assertThat(MANY_VALUES_STATS_VARARGS.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
assertThat(MANY_VALUES_STATS_ITERABLE.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
assertThat(TWO_VALUES_STATS.max()).isEqualTo(TWO_VALUES_MAX);
assertThat(MANY_VALUES_STATS_VARARGS.max()).isEqualTo(MANY_VALUES_MAX);
assertThat(MANY_VALUES_STATS_ITERABLE.max()).isEqualTo(MANY_VALUES_MAX);
// For datasets of many double values created from an iterator, we test many combinations of
// finite and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
Expand All @@ -413,22 +413,14 @@ public void testMax() {
} else if (values.hasAnyPositiveInfinity()) {
assertWithMessage("max of " + values).that(max).isPositiveInfinity();
} else {
assertWithMessage("max of " + values).that(max).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
assertWithMessage("max of " + values).that(max).isEqualTo(MANY_VALUES_MAX);
}
}
assertThat(MANY_VALUES_STATS_SNAPSHOT.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.max())
.isWithin(ALLOWED_ERROR)
.of(INTEGER_MANY_VALUES_MAX);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.max())
.isWithin(ALLOWED_ERROR)
.of(INTEGER_MANY_VALUES_MAX);
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.max())
.isWithin(ALLOWED_ERROR)
.of(LONG_MANY_VALUES_MAX);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.max())
.isWithin(ALLOWED_ERROR)
.of(LONG_MANY_VALUES_MAX);
assertThat(MANY_VALUES_STATS_SNAPSHOT.max()).isEqualTo(MANY_VALUES_MAX);
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.max()).isEqualTo(INTEGER_MANY_VALUES_MAX);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.max()).isEqualTo(INTEGER_MANY_VALUES_MAX);
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.max()).isEqualTo(LONG_MANY_VALUES_MAX);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.max()).isEqualTo(LONG_MANY_VALUES_MAX);
}

public void testMin() {
Expand All @@ -442,14 +434,14 @@ public void testMin() {
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertThat(ONE_VALUE_STATS.min()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
assertThat(ONE_VALUE_STATS.min()).isEqualTo(ONE_VALUE);
assertThat(Stats.of(POSITIVE_INFINITY).min()).isPositiveInfinity();
assertThat(Stats.of(NEGATIVE_INFINITY).min()).isNegativeInfinity();
assertThat(Stats.of(NaN).min()).isNaN();
assertThat(TWO_VALUES_STATS.min()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MIN);
assertThat(MANY_VALUES_STATS_VARARGS.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
assertThat(MANY_VALUES_STATS_ITERABLE.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
assertThat(MANY_VALUES_STATS_ITERATOR.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
assertThat(TWO_VALUES_STATS.min()).isEqualTo(TWO_VALUES_MIN);
assertThat(MANY_VALUES_STATS_VARARGS.min()).isEqualTo(MANY_VALUES_MIN);
assertThat(MANY_VALUES_STATS_ITERABLE.min()).isEqualTo(MANY_VALUES_MIN);
assertThat(MANY_VALUES_STATS_ITERATOR.min()).isEqualTo(MANY_VALUES_MIN);
// For datasets of many double values created from an accumulator snapshot, we test many
// combinations of finite and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
Expand All @@ -461,21 +453,13 @@ public void testMin() {
} else if (values.hasAnyNegativeInfinity()) {
assertWithMessage("min of " + values).that(min).isNegativeInfinity();
} else {
assertWithMessage("min of " + values).that(min).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
assertWithMessage("min of " + values).that(min).isEqualTo(MANY_VALUES_MIN);
}
}
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.min())
.isWithin(ALLOWED_ERROR)
.of(INTEGER_MANY_VALUES_MIN);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.min())
.isWithin(ALLOWED_ERROR)
.of(INTEGER_MANY_VALUES_MIN);
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.min())
.isWithin(ALLOWED_ERROR)
.of(LONG_MANY_VALUES_MIN);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.min())
.isWithin(ALLOWED_ERROR)
.of(LONG_MANY_VALUES_MIN);
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.min()).isEqualTo(INTEGER_MANY_VALUES_MIN);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.min()).isEqualTo(INTEGER_MANY_VALUES_MIN);
assertThat(LONG_MANY_VALUES_STATS_ITERATOR.min()).isEqualTo(LONG_MANY_VALUES_MIN);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.min()).isEqualTo(LONG_MANY_VALUES_MIN);
}

public void testEqualsAndHashCode() {
Expand Down Expand Up @@ -558,14 +542,16 @@ public void testMeanOf() {
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)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN)
.of(INTEGER_MANY_VALUES_MEAN);
assertThat(Stats.meanOf(Ints.toArray(INTEGER_MANY_VALUES)))
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN)
.of(INTEGER_MANY_VALUES_MEAN);
assertThat(Stats.meanOf(LONG_MANY_VALUES)).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MEAN);
assertThat(Stats.meanOf(LONG_MANY_VALUES))
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN)
.of(LONG_MANY_VALUES_MEAN);
assertThat(Stats.meanOf(Longs.toArray(LONG_MANY_VALUES)))
.isWithin(ALLOWED_ERROR)
.isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN)
.of(LONG_MANY_VALUES_MEAN);
}

Expand Down
38 changes: 20 additions & 18 deletions android/guava-tests/test/com/google/common/math/StatsTesting.java
Expand Up @@ -222,28 +222,29 @@ private static ImmutableList<ManyValues> createAll() {
static final Stats MANY_VALUES_STATS_VARARGS = Stats.of(1.1, -44.44, 33.33, 555.555, -2.2);
static final Stats MANY_VALUES_STATS_ITERABLE = Stats.of(MANY_VALUES);
static final Stats MANY_VALUES_STATS_ITERATOR = Stats.of(MANY_VALUES.iterator());
static final Stats MANY_VALUES_STATS_SNAPSHOT;
static final Stats MANY_VALUES_STATS_SNAPSHOT = buildManyValuesStatsSnapshot();
static final Stats LARGE_VALUES_STATS = Stats.of(LARGE_VALUES);
static final Stats OTHER_MANY_VALUES_STATS = Stats.of(OTHER_MANY_VALUES);
static final Stats INTEGER_MANY_VALUES_STATS_VARARGS =
Stats.of(Ints.toArray(INTEGER_MANY_VALUES));
static final Stats INTEGER_MANY_VALUES_STATS_ITERABLE = Stats.of(INTEGER_MANY_VALUES);
static final Stats LARGE_INTEGER_VALUES_STATS = Stats.of(LARGE_INTEGER_VALUES);
static final Stats LONG_MANY_VALUES_STATS_ITERATOR = Stats.of(LONG_MANY_VALUES.iterator());
static final Stats LONG_MANY_VALUES_STATS_SNAPSHOT;
static final Stats LONG_MANY_VALUES_STATS_SNAPSHOT = buildLongManyValuesStatsSnapshot();
static final Stats LARGE_LONG_VALUES_STATS = Stats.of(LARGE_LONG_VALUES);

static {
private static Stats buildManyValuesStatsSnapshot() {
StatsAccumulator accumulator = new StatsAccumulator();
accumulator.addAll(MANY_VALUES);
MANY_VALUES_STATS_SNAPSHOT = accumulator.snapshot();
Stats stats = accumulator.snapshot();
accumulator.add(999.999); // should do nothing to the snapshot
return stats;
}

static {
private static Stats buildLongManyValuesStatsSnapshot() {
StatsAccumulator accumulator = new StatsAccumulator();
accumulator.addAll(LONG_MANY_VALUES);
LONG_MANY_VALUES_STATS_SNAPSHOT = accumulator.snapshot();
return accumulator.snapshot();
}

static final ImmutableList<Stats> ALL_STATS =
Expand Down Expand Up @@ -275,42 +276,43 @@ private static ImmutableList<ManyValues> createAll() {
createPairedStatsOf(ImmutableList.of(ONE_VALUE), ImmutableList.of(OTHER_ONE_VALUE));
static final PairedStats TWO_VALUES_PAIRED_STATS =
createPairedStatsOf(TWO_VALUES, OTHER_TWO_VALUES);
static final PairedStats MANY_VALUES_PAIRED_STATS;
static final PairedStats MANY_VALUES_PAIRED_STATS = buildManyValuesPairedStats();
static final PairedStats DUPLICATE_MANY_VALUES_PAIRED_STATS =
createPairedStatsOf(MANY_VALUES, OTHER_MANY_VALUES);
static final PairedStats HORIZONTAL_VALUES_PAIRED_STATS;
static final PairedStats VERTICAL_VALUES_PAIRED_STATS;
static final PairedStats CONSTANT_VALUES_PAIRED_STATS;
static final PairedStats HORIZONTAL_VALUES_PAIRED_STATS = buildHorizontalValuesPairedStats();
static final PairedStats VERTICAL_VALUES_PAIRED_STATS = buildVerticalValuesPairedStats();
static final PairedStats CONSTANT_VALUES_PAIRED_STATS = buildConstantValuesPairedStats();

static {
private static PairedStats buildManyValuesPairedStats() {
PairedStatsAccumulator accumulator =
createFilledPairedStatsAccumulator(MANY_VALUES, OTHER_MANY_VALUES);
MANY_VALUES_PAIRED_STATS = accumulator.snapshot();
PairedStats stats = accumulator.snapshot();
accumulator.add(99.99, 9999.9999); // should do nothing to the snapshot
return stats;
}

static {
private static PairedStats buildHorizontalValuesPairedStats() {
PairedStatsAccumulator accumulator = new PairedStatsAccumulator();
for (double x : MANY_VALUES) {
accumulator.add(x, OTHER_ONE_VALUE);
}
HORIZONTAL_VALUES_PAIRED_STATS = accumulator.snapshot();
return accumulator.snapshot();
}

static {
private static PairedStats buildVerticalValuesPairedStats() {
PairedStatsAccumulator accumulator = new PairedStatsAccumulator();
for (double y : OTHER_MANY_VALUES) {
accumulator.add(ONE_VALUE, y);
}
VERTICAL_VALUES_PAIRED_STATS = accumulator.snapshot();
return accumulator.snapshot();
}

static {
private static PairedStats buildConstantValuesPairedStats() {
PairedStatsAccumulator accumulator = new PairedStatsAccumulator();
for (int i = 0; i < MANY_VALUES_COUNT; ++i) {
accumulator.add(ONE_VALUE, OTHER_ONE_VALUE);
}
CONSTANT_VALUES_PAIRED_STATS = accumulator.snapshot();
return accumulator.snapshot();
}

static final ImmutableList<PairedStats> ALL_PAIRED_STATS =
Expand Down
3 changes: 2 additions & 1 deletion android/guava/src/com/google/common/math/Stats.java
Expand Up @@ -103,7 +103,8 @@ public static Stats of(Iterable<? extends Number> values) {
}

/**
* Returns statistics over a dataset containing the given values.
* Returns statistics over a dataset containing the given values. The iterator will be completely
* consumed by this method.
*
* @param values a series of values, which will be converted to {@code double} values (this may
* cause loss of precision)
Expand Down

0 comments on commit e2f4eba

Please sign in to comment.