use of com.google.common.math.StatsTesting.ManyValues in project guava by google.
the class StatsAccumulatorTest method testPopulationVariance.
public void testPopulationVariance() {
try {
emptyAccumulator.populationVariance();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
try {
emptyAccumulatorByAddAllEmptyIterable.populationVariance();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
try {
emptyAccumulatorByAddAllEmptyStats.populationVariance();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertThat(oneValueAccumulator.populationVariance()).isWithin(0.0).of(0.0);
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isWithin(0.0).of(0.0);
assertThat(twoValuesAccumulator.populationVariance()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2);
assertThat(twoValuesAccumulatorByAddAllStats.populationVariance()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2);
assertThat(manyValuesAccumulatorByAddAllIterable.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(manyValuesAccumulatorByAddAllIterator.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(manyValuesAccumulatorByAddAllVarargs.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(manyValuesAccumulatorByRepeatedAdd.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(manyValuesAccumulatorByAddAndAddAll.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(manyValuesAccumulatorByAddAllStats.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
// finite and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
StatsAccumulator accumulator = new StatsAccumulator();
StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator();
accumulator.addAll(values.asIterable().iterator());
for (double value : values.asIterable()) {
accumulatorByAddAllStats.addAll(Stats.of(value));
}
double populationVariance = accumulator.populationVariance();
double populationVarianceByAddAllStats = accumulatorByAddAllStats.populationVariance();
if (values.hasAnyNonFinite()) {
assertThat(populationVariance).named("population variance of " + values).isNaN();
assertThat(populationVarianceByAddAllStats).named("population variance by addAll(Stats) of " + values).isNaN();
} else {
assertThat(populationVariance).named("population variance of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(populationVarianceByAddAllStats).named("population variance by addAll(Stats) of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
}
}
assertThat(integerManyValuesAccumulatorByAddAllIterable.populationVariance()).isWithin(ALLOWED_ERROR).of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT);
assertThat(longManyValuesAccumulatorByAddAllIterator.populationVariance()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT);
assertThat(longManyValuesAccumulatorByAddAllVarargs.populationVariance()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT);
}
use of com.google.common.math.StatsTesting.ManyValues in project guava by google.
the class StatsTest method testMeanOf.
public void testMeanOf() {
try {
Stats.meanOf();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
try {
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);
// and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
double mean = Stats.meanOf(values.asArray());
if (values.hasAnyNaN()) {
assertThat(mean).named("mean of " + values).isNaN();
} else if (values.hasAnyPositiveInfinity() && values.hasAnyNegativeInfinity()) {
assertThat(mean).named("mean of " + values).isNaN();
} else if (values.hasAnyPositiveInfinity()) {
assertThat(mean).named("mean of " + values).isPositiveInfinity();
} else if (values.hasAnyNegativeInfinity()) {
assertThat(mean).named("mean of " + values).isNegativeInfinity();
} else {
assertThat(mean).named("mean of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
}
}
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))).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MEAN);
}
use of com.google.common.math.StatsTesting.ManyValues in project guava by google.
the class StatsTest method testPopulationVariance.
public void testPopulationVariance() {
try {
EMPTY_STATS_VARARGS.populationVariance();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
try {
EMPTY_STATS_ITERABLE.populationVariance();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertThat(ONE_VALUE_STATS.populationVariance()).isWithin(0.0).of(0.0);
assertThat(Stats.of(POSITIVE_INFINITY).populationVariance()).isNaN();
assertThat(Stats.of(NEGATIVE_INFINITY).populationVariance()).isNaN();
assertThat(Stats.of(NaN).populationVariance()).isNaN();
assertThat(TWO_VALUES_STATS.populationVariance()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2);
assertThat(MANY_VALUES_STATS_VARARGS.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
// finite and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
double populationVariance = Stats.of(values.asIterable()).populationVariance();
if (values.hasAnyNonFinite()) {
assertThat(populationVariance).named("population variance of " + values).isNaN();
} else {
assertThat(populationVariance).named("population variance of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
}
}
assertThat(MANY_VALUES_STATS_ITERATOR.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(MANY_VALUES_STATS_SNAPSHOT.populationVariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(INTEGER_MANY_VALUES_STATS_VARARGS.populationVariance()).isWithin(ALLOWED_ERROR).of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT);
assertThat(INTEGER_MANY_VALUES_STATS_ITERABLE.populationVariance()).isWithin(ALLOWED_ERROR).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).of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT);
assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.populationVariance()).isWithin(ALLOWED_ERROR).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).of(LARGE_LONG_VALUES_POPULATION_VARIANCE);
}
use of com.google.common.math.StatsTesting.ManyValues in project guava by google.
the class StatsTest method testMin.
public void testMin() {
try {
EMPTY_STATS_VARARGS.min();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
try {
EMPTY_STATS_ITERABLE.min();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertThat(ONE_VALUE_STATS.min()).isWithin(ALLOWED_ERROR).of(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);
// combinations of finite and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
StatsAccumulator accumulator = new StatsAccumulator();
accumulator.addAll(values.asIterable());
double min = accumulator.snapshot().min();
if (values.hasAnyNaN()) {
assertThat(min).named("min of " + values).isNaN();
} else if (values.hasAnyNegativeInfinity()) {
assertThat(min).named("min of " + values).isNegativeInfinity();
} else {
assertThat(min).named("min of " + values).isWithin(ALLOWED_ERROR).of(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);
}
use of com.google.common.math.StatsTesting.ManyValues in project guava by google.
the class StatsTest method testMax.
public void testMax() {
try {
EMPTY_STATS_VARARGS.max();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
try {
EMPTY_STATS_ITERABLE.max();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertThat(ONE_VALUE_STATS.max()).isWithin(ALLOWED_ERROR).of(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);
// finite and non-finite values:
for (ManyValues values : ALL_MANY_VALUES) {
double max = Stats.of(values.asIterable().iterator()).max();
if (values.hasAnyNaN()) {
assertThat(max).named("max of " + values).isNaN();
} else if (values.hasAnyPositiveInfinity()) {
assertThat(max).named("max of " + values).isPositiveInfinity();
} else {
assertThat(max).named("max of " + values).isWithin(ALLOWED_ERROR).of(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);
}
Aggregations