use of com.google.common.math.StatsTesting.ManyValues in project guava by google.
the class PairedStatsAccumulatorTest method testPopulationCovariance.
public void testPopulationCovariance() {
try {
emptyAccumulator.populationCovariance();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
try {
emptyAccumulatorByAddAllEmptyPairedStats.populationCovariance();
fail("Expected IllegalStateException");
} catch (IllegalStateException expected) {
}
assertThat(oneValueAccumulator.populationCovariance()).isWithin(0.0).of(0.0);
assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance()).isWithin(0.0).of(0.0);
assertThat(twoValuesAccumulator.populationCovariance()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / 2);
assertThat(twoValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / 2);
assertThat(manyValuesAccumulator.populationCovariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(manyValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / MANY_VALUES_COUNT);
// x-values:
for (ManyValues values : ALL_MANY_VALUES) {
PairedStatsAccumulator accumulator = createFilledPairedStatsAccumulator(values.asIterable(), OTHER_MANY_VALUES);
PairedStatsAccumulator accumulatorByAddAllPartitionedPairedStats = createPartitionedFilledPairedStatsAccumulator(values.asIterable(), OTHER_MANY_VALUES, 2);
double populationCovariance = accumulator.populationCovariance();
double populationCovarianceByAddAllPartitionedPairedStats = accumulatorByAddAllPartitionedPairedStats.populationCovariance();
if (values.hasAnyNonFinite()) {
assertThat(populationCovariance).named("population covariance of " + values).isNaN();
assertThat(populationCovarianceByAddAllPartitionedPairedStats).named("population covariance by addAll(PairedStats) of " + values).isNaN();
} else {
assertThat(populationCovariance).named("population covariance of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / MANY_VALUES_COUNT);
assertThat(populationCovarianceByAddAllPartitionedPairedStats).named("population covariance by addAll(PairedStats) of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / MANY_VALUES_COUNT);
}
}
assertThat(horizontalValuesAccumulator.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
assertThat(horizontalValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
assertThat(verticalValuesAccumulator.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
assertThat(verticalValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
assertThat(constantValuesAccumulator.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
assertThat(constantValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
}
Aggregations