Search in sources :

Example 6 with ManyValues

use of com.google.common.math.StatsTesting.ManyValues in project guava by google.

the class PairedStatsTest method testPopulationCovariance.

public void testPopulationCovariance() {
    try {
        EMPTY_PAIRED_STATS.populationCovariance();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isWithin(0.0).of(0.0);
    assertThat(createSingleStats(Double.POSITIVE_INFINITY, 1.23).populationCovariance()).isNaN();
    assertThat(createSingleStats(Double.NEGATIVE_INFINITY, 1.23).populationCovariance()).isNaN();
    assertThat(createSingleStats(Double.NaN, 1.23).populationCovariance()).isNaN();
    assertThat(TWO_VALUES_PAIRED_STATS.populationCovariance()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / 2);
    // x-values:
    for (ManyValues values : ALL_MANY_VALUES) {
        PairedStats stats = createPairedStatsOf(values.asIterable(), OTHER_MANY_VALUES);
        double populationCovariance = stats.populationCovariance();
        if (values.hasAnyNonFinite()) {
            assertThat(populationCovariance).named("population covariance 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(HORIZONTAL_VALUES_PAIRED_STATS.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
    assertThat(VERTICAL_VALUES_PAIRED_STATS.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
    assertThat(CONSTANT_VALUES_PAIRED_STATS.populationCovariance()).isWithin(ALLOWED_ERROR).of(0.0);
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues)

Example 7 with ManyValues

use of com.google.common.math.StatsTesting.ManyValues in project guava by google.

the class StatsAccumulatorTest method testMean.

public void testMean() {
    try {
        emptyAccumulator.mean();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyIterable.mean();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyStats.mean();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertThat(oneValueAccumulator.mean()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
    assertThat(oneValueAccumulatorByAddAllEmptyStats.mean()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
    assertThat(twoValuesAccumulator.mean()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN);
    assertThat(twoValuesAccumulatorByAddAllStats.mean()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN);
    assertThat(manyValuesAccumulatorByAddAllIterable.mean()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
    assertThat(manyValuesAccumulatorByAddAllIterator.mean()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
    assertThat(manyValuesAccumulatorByAddAllVarargs.mean()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
    assertThat(manyValuesAccumulatorByRepeatedAdd.mean()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
    assertThat(manyValuesAccumulatorByAddAndAddAll.mean()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
    assertThat(manyValuesAccumulatorByAddAllStats.mean()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
    // finite and non-finite values:
    for (ManyValues values : ALL_MANY_VALUES) {
        StatsAccumulator accumulator = new StatsAccumulator();
        StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator();
        accumulator.addAll(values.asIterable());
        for (double value : values.asIterable()) {
            accumulatorByAddAllStats.addAll(Stats.of(value));
        }
        double mean = accumulator.mean();
        double meanByAddAllStats = accumulatorByAddAllStats.mean();
        if (values.hasAnyNaN()) {
            assertThat(mean).named("mean of " + values).isNaN();
            assertThat(meanByAddAllStats).named("mean by addAll(Stats) of " + values).isNaN();
        } else if (values.hasAnyPositiveInfinity() && values.hasAnyNegativeInfinity()) {
            assertThat(mean).named("mean of " + values).isNaN();
            assertThat(meanByAddAllStats).named("mean by addAll(Stats) of " + values).isNaN();
        } else if (values.hasAnyPositiveInfinity()) {
            assertThat(mean).named("mean of " + values).isPositiveInfinity();
            assertThat(meanByAddAllStats).named("mean by addAll(Stats) of " + values).isPositiveInfinity();
        } else if (values.hasAnyNegativeInfinity()) {
            assertThat(mean).named("mean of " + values).isNegativeInfinity();
            assertThat(meanByAddAllStats).named("mean by addAll(Stats) of " + values).isNegativeInfinity();
        } else {
            assertThat(mean).named("mean of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
            assertThat(meanByAddAllStats).named("mean by addAll(Stats) of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
        }
    }
    assertThat(integerManyValuesAccumulatorByAddAllIterable.mean()).isWithin(ALLOWED_ERROR).of(INTEGER_MANY_VALUES_MEAN);
    assertThat(longManyValuesAccumulatorByAddAllIterator.mean()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MEAN);
    assertThat(longManyValuesAccumulatorByAddAllVarargs.mean()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MEAN);
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues)

Example 8 with ManyValues

use of com.google.common.math.StatsTesting.ManyValues in project guava by google.

the class PairedStatsTest method testLeastSquaresFit.

public void testLeastSquaresFit() {
    try {
        EMPTY_PAIRED_STATS.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        ONE_VALUE_PAIRED_STATS.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        createSingleStats(Double.POSITIVE_INFINITY, 1.23).leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertDiagonalLinearTransformation(TWO_VALUES_PAIRED_STATS.leastSquaresFit(), TWO_VALUES_PAIRED_STATS.xStats().mean(), TWO_VALUES_PAIRED_STATS.yStats().mean(), TWO_VALUES_PAIRED_STATS.xStats().populationVariance(), TWO_VALUES_PAIRED_STATS.populationCovariance());
    // x-values:
    for (ManyValues values : ALL_MANY_VALUES) {
        PairedStats stats = createPairedStatsOf(values.asIterable(), OTHER_MANY_VALUES);
        LinearTransformation fit = stats.leastSquaresFit();
        if (values.hasAnyNonFinite()) {
            assertLinearTransformationNaN(fit);
        } else {
            assertDiagonalLinearTransformation(fit, stats.xStats().mean(), stats.yStats().mean(), stats.xStats().populationVariance(), stats.populationCovariance());
        }
    }
    assertHorizontalLinearTransformation(HORIZONTAL_VALUES_PAIRED_STATS.leastSquaresFit(), HORIZONTAL_VALUES_PAIRED_STATS.yStats().mean());
    assertVerticalLinearTransformation(VERTICAL_VALUES_PAIRED_STATS.leastSquaresFit(), VERTICAL_VALUES_PAIRED_STATS.xStats().mean());
    try {
        CONSTANT_VALUES_PAIRED_STATS.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues) StatsTesting.assertHorizontalLinearTransformation(com.google.common.math.StatsTesting.assertHorizontalLinearTransformation) StatsTesting.assertVerticalLinearTransformation(com.google.common.math.StatsTesting.assertVerticalLinearTransformation) StatsTesting.assertDiagonalLinearTransformation(com.google.common.math.StatsTesting.assertDiagonalLinearTransformation)

Example 9 with ManyValues

use of com.google.common.math.StatsTesting.ManyValues in project guava by google.

the class StatsAccumulatorTest method testMin.

public void testMin() {
    try {
        emptyAccumulator.min();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyIterable.min();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyStats.min();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertThat(oneValueAccumulator.min()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
    assertThat(oneValueAccumulatorByAddAllEmptyStats.min()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
    assertThat(twoValuesAccumulator.min()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MIN);
    assertThat(twoValuesAccumulatorByAddAllStats.min()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MIN);
    assertThat(manyValuesAccumulatorByAddAllIterable.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
    assertThat(manyValuesAccumulatorByAddAllIterator.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
    assertThat(manyValuesAccumulatorByAddAllVarargs.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
    assertThat(manyValuesAccumulatorByRepeatedAdd.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
    assertThat(manyValuesAccumulatorByAddAndAddAll.min()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
    assertThat(manyValuesAccumulatorByAddAllStats.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();
        StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator();
        for (double value : values.asIterable()) {
            accumulator.add(value);
            accumulatorByAddAllStats.addAll(Stats.of(value));
        }
        double min = accumulator.min();
        double minByAddAllStats = accumulatorByAddAllStats.min();
        if (values.hasAnyNaN()) {
            assertThat(min).named("min of " + values).isNaN();
            assertThat(minByAddAllStats).named("min by addAll(Stats) of " + values).isNaN();
        } else if (values.hasAnyNegativeInfinity()) {
            assertThat(min).named("min of " + values).isNegativeInfinity();
            assertThat(minByAddAllStats).named("min by addAll(Stats) of " + values).isNegativeInfinity();
        } else {
            assertThat(min).named("min of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
            assertThat(minByAddAllStats).named("min by addAll(Stats) of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MIN);
        }
    }
    assertThat(integerManyValuesAccumulatorByAddAllIterable.min()).isWithin(ALLOWED_ERROR).of(INTEGER_MANY_VALUES_MIN);
    assertThat(longManyValuesAccumulatorByAddAllIterator.min()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MIN);
    assertThat(longManyValuesAccumulatorByAddAllVarargs.min()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MIN);
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues)

Example 10 with ManyValues

use of com.google.common.math.StatsTesting.ManyValues in project guava by google.

the class StatsAccumulatorTest method testMax.

public void testMax() {
    try {
        emptyAccumulator.max();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyIterable.max();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyStats.max();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertThat(oneValueAccumulator.max()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
    assertThat(oneValueAccumulatorByAddAllEmptyStats.max()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
    assertThat(twoValuesAccumulator.max()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MAX);
    assertThat(twoValuesAccumulatorByAddAllStats.max()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MAX);
    assertThat(manyValuesAccumulatorByAddAllIterable.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
    assertThat(manyValuesAccumulatorByAddAllIterator.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
    assertThat(manyValuesAccumulatorByAddAllVarargs.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
    assertThat(manyValuesAccumulatorByRepeatedAdd.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
    assertThat(manyValuesAccumulatorByAddAndAddAll.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
    assertThat(manyValuesAccumulatorByAddAllStats.max()).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
    // finite and non-finite values:
    for (ManyValues values : ALL_MANY_VALUES) {
        StatsAccumulator accumulator = new StatsAccumulator();
        StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator();
        accumulator.addAll(values.asArray());
        for (double value : values.asIterable()) {
            accumulatorByAddAllStats.addAll(Stats.of(value));
        }
        double max = accumulator.max();
        double maxByAddAllStats = accumulatorByAddAllStats.max();
        if (values.hasAnyNaN()) {
            assertThat(max).named("max of " + values).isNaN();
            assertThat(maxByAddAllStats).named("max by addAll(Stats) of " + values).isNaN();
        } else if (values.hasAnyPositiveInfinity()) {
            assertThat(max).named("max of " + values).isPositiveInfinity();
            assertThat(maxByAddAllStats).named("max by addAll(Stats) of " + values).isPositiveInfinity();
        } else {
            assertThat(max).named("max of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
            assertThat(maxByAddAllStats).named("max by addAll(Stats) of " + values).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MAX);
        }
    }
    assertThat(integerManyValuesAccumulatorByAddAllIterable.max()).isWithin(ALLOWED_ERROR).of(INTEGER_MANY_VALUES_MAX);
    assertThat(longManyValuesAccumulatorByAddAllIterator.max()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MAX);
    assertThat(longManyValuesAccumulatorByAddAllVarargs.max()).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MAX);
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues)

Aggregations

ManyValues (com.google.common.math.StatsTesting.ManyValues)16 StatsTesting.createFilledPairedStatsAccumulator (com.google.common.math.StatsTesting.createFilledPairedStatsAccumulator)3 StatsTesting.createPartitionedFilledPairedStatsAccumulator (com.google.common.math.StatsTesting.createPartitionedFilledPairedStatsAccumulator)3 StatsTesting.assertDiagonalLinearTransformation (com.google.common.math.StatsTesting.assertDiagonalLinearTransformation)2 StatsTesting.assertHorizontalLinearTransformation (com.google.common.math.StatsTesting.assertHorizontalLinearTransformation)2 StatsTesting.assertVerticalLinearTransformation (com.google.common.math.StatsTesting.assertVerticalLinearTransformation)2 DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)1