Search in sources :

Example 1 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()) {
            assertWithMessage("population covariance of " + values).that(populationCovariance).isNaN();
        } else {
            assertWithMessage("population covariance of " + values).that(populationCovariance).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 2 with ManyValues

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

the class PairedStatsTest method testPearsonsCorrelationCoefficient.

public void testPearsonsCorrelationCoefficient() {
    try {
        EMPTY_PAIRED_STATS.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        ONE_VALUE_PAIRED_STATS.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        createSingleStats(Double.POSITIVE_INFINITY, 1.23).pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertThat(TWO_VALUES_PAIRED_STATS.pearsonsCorrelationCoefficient()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_PAIRED_STATS.populationCovariance() / (TWO_VALUES_PAIRED_STATS.xStats().populationStandardDeviation() * TWO_VALUES_PAIRED_STATS.yStats().populationStandardDeviation()));
    // y-values:
    for (ManyValues values : ALL_MANY_VALUES) {
        PairedStats stats = createPairedStatsOf(MANY_VALUES, values.asIterable());
        double pearsonsCorrelationCoefficient = stats.pearsonsCorrelationCoefficient();
        if (values.hasAnyNonFinite()) {
            assertWithMessage("Pearson's correlation coefficient of " + values).that(pearsonsCorrelationCoefficient).isNaN();
        } else {
            assertWithMessage("Pearson's correlation coefficient of " + values).that(pearsonsCorrelationCoefficient).isWithin(ALLOWED_ERROR).of(stats.populationCovariance() / (stats.xStats().populationStandardDeviation() * stats.yStats().populationStandardDeviation()));
        }
    }
    try {
        HORIZONTAL_VALUES_PAIRED_STATS.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        VERTICAL_VALUES_PAIRED_STATS.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        CONSTANT_VALUES_PAIRED_STATS.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues)

Example 3 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);
    assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.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()) {
            assertWithMessage("mean of " + values).that(mean).isNaN();
            assertWithMessage("mean by addAll(Stats) of " + values).that(meanByAddAllStats).isNaN();
        } else if (values.hasAnyPositiveInfinity() && values.hasAnyNegativeInfinity()) {
            assertWithMessage("mean of " + values).that(mean).isNaN();
            assertWithMessage("mean by addAll(Stats) of " + values).that(meanByAddAllStats).isNaN();
        } else if (values.hasAnyPositiveInfinity()) {
            assertWithMessage("mean of " + values).that(mean).isPositiveInfinity();
            assertWithMessage("mean by addAll(Stats) of " + values).that(meanByAddAllStats).isPositiveInfinity();
        } else if (values.hasAnyNegativeInfinity()) {
            assertWithMessage("mean of " + values).that(mean).isNegativeInfinity();
            assertWithMessage("mean by addAll(Stats) of " + values).that(meanByAddAllStats).isNegativeInfinity();
        } else {
            assertWithMessage("mean of " + values).that(mean).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
            assertWithMessage("mean by addAll(Stats) of " + values).that(meanByAddAllStats).isWithin(ALLOWED_ERROR).of(MANY_VALUES_MEAN);
        }
    }
    assertThat(integerManyValuesAccumulatorByAddAllIterable.mean()).isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN).of(INTEGER_MANY_VALUES_MEAN);
    assertThat(longManyValuesAccumulatorByAddAllIterator.mean()).isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN).of(LONG_MANY_VALUES_MEAN);
    assertThat(longManyValuesAccumulatorByAddAllVarargs.mean()).isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN).of(LONG_MANY_VALUES_MEAN);
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues)

Example 4 with ManyValues

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

the class PairedStatsAccumulatorTest method testLeastSquaresFit.

public void testLeastSquaresFit() {
    try {
        emptyAccumulator.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyPairedStats.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        oneValueAccumulator.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        oneValueAccumulatorByAddAllEmptyPairedStats.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertDiagonalLinearTransformation(twoValuesAccumulator.leastSquaresFit(), twoValuesAccumulator.xStats().mean(), twoValuesAccumulator.yStats().mean(), twoValuesAccumulator.xStats().populationVariance(), twoValuesAccumulator.populationCovariance());
    assertDiagonalLinearTransformation(twoValuesAccumulatorByAddAllPartitionedPairedStats.leastSquaresFit(), twoValuesAccumulatorByAddAllPartitionedPairedStats.xStats().mean(), twoValuesAccumulatorByAddAllPartitionedPairedStats.yStats().mean(), twoValuesAccumulatorByAddAllPartitionedPairedStats.xStats().populationVariance(), twoValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance());
    assertDiagonalLinearTransformation(manyValuesAccumulator.leastSquaresFit(), manyValuesAccumulator.xStats().mean(), manyValuesAccumulator.yStats().mean(), manyValuesAccumulator.xStats().populationVariance(), manyValuesAccumulator.populationCovariance());
    assertDiagonalLinearTransformation(manyValuesAccumulatorByAddAllPartitionedPairedStats.leastSquaresFit(), manyValuesAccumulatorByAddAllPartitionedPairedStats.xStats().mean(), manyValuesAccumulatorByAddAllPartitionedPairedStats.yStats().mean(), manyValuesAccumulatorByAddAllPartitionedPairedStats.xStats().populationVariance(), manyValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance());
    // 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);
        LinearTransformation fit = accumulator.leastSquaresFit();
        LinearTransformation fitByAddAllPartitionedPairedStats = accumulatorByAddAllPartitionedPairedStats.leastSquaresFit();
        if (values.hasAnyNonFinite()) {
            assertLinearTransformationNaN(fit);
            assertLinearTransformationNaN(fitByAddAllPartitionedPairedStats);
        } else {
            assertDiagonalLinearTransformation(fit, accumulator.xStats().mean(), accumulator.yStats().mean(), accumulator.xStats().populationVariance(), accumulator.populationCovariance());
            assertDiagonalLinearTransformation(fitByAddAllPartitionedPairedStats, accumulatorByAddAllPartitionedPairedStats.xStats().mean(), accumulatorByAddAllPartitionedPairedStats.yStats().mean(), accumulatorByAddAllPartitionedPairedStats.xStats().populationVariance(), accumulatorByAddAllPartitionedPairedStats.populationCovariance());
        }
    }
    assertHorizontalLinearTransformation(horizontalValuesAccumulator.leastSquaresFit(), horizontalValuesAccumulator.yStats().mean());
    assertHorizontalLinearTransformation(horizontalValuesAccumulatorByAddAllPartitionedPairedStats.leastSquaresFit(), horizontalValuesAccumulatorByAddAllPartitionedPairedStats.yStats().mean());
    assertVerticalLinearTransformation(verticalValuesAccumulator.leastSquaresFit(), verticalValuesAccumulator.xStats().mean());
    assertVerticalLinearTransformation(verticalValuesAccumulatorByAddAllPartitionedPairedStats.leastSquaresFit(), verticalValuesAccumulatorByAddAllPartitionedPairedStats.xStats().mean());
    try {
        constantValuesAccumulator.leastSquaresFit();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        constantValuesAccumulatorByAddAllPartitionedPairedStats.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) StatsTesting.createFilledPairedStatsAccumulator(com.google.common.math.StatsTesting.createFilledPairedStatsAccumulator) StatsTesting.createPartitionedFilledPairedStatsAccumulator(com.google.common.math.StatsTesting.createPartitionedFilledPairedStatsAccumulator)

Example 5 with ManyValues

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

the class PairedStatsAccumulatorTest method testPearsonsCorrelationCoefficient.

public void testPearsonsCorrelationCoefficient() {
    try {
        emptyAccumulator.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        emptyAccumulatorByAddAllEmptyPairedStats.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        oneValueAccumulator.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        oneValueAccumulatorByAddAllEmptyPairedStats.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    assertThat(twoValuesAccumulator.pearsonsCorrelationCoefficient()).isWithin(ALLOWED_ERROR).of(twoValuesAccumulator.populationCovariance() / (twoValuesAccumulator.xStats().populationStandardDeviation() * twoValuesAccumulator.yStats().populationStandardDeviation()));
    assertThat(manyValuesAccumulator.pearsonsCorrelationCoefficient()).isWithin(ALLOWED_ERROR).of(manyValuesAccumulator.populationCovariance() / (manyValuesAccumulator.xStats().populationStandardDeviation() * manyValuesAccumulator.yStats().populationStandardDeviation()));
    assertThat(manyValuesAccumulatorByAddAllPartitionedPairedStats.pearsonsCorrelationCoefficient()).isWithin(ALLOWED_ERROR).of(manyValuesAccumulatorByAddAllPartitionedPairedStats.populationCovariance() / (manyValuesAccumulatorByAddAllPartitionedPairedStats.xStats().populationStandardDeviation() * manyValuesAccumulatorByAddAllPartitionedPairedStats.yStats().populationStandardDeviation()));
    // y-values:
    for (ManyValues values : ALL_MANY_VALUES) {
        PairedStatsAccumulator accumulator = createFilledPairedStatsAccumulator(MANY_VALUES, values.asIterable());
        PairedStatsAccumulator accumulatorByAddAllPartitionedPairedStats = createPartitionedFilledPairedStatsAccumulator(MANY_VALUES, values.asIterable(), 2);
        double pearsonsCorrelationCoefficient = accumulator.pearsonsCorrelationCoefficient();
        double pearsonsCorrelationCoefficientByAddAllPartitionedPairedStats = accumulatorByAddAllPartitionedPairedStats.pearsonsCorrelationCoefficient();
        if (values.hasAnyNonFinite()) {
            assertWithMessage("Pearson's correlation coefficient of " + values).that(pearsonsCorrelationCoefficient).isNaN();
            assertWithMessage("Pearson's correlation coefficient by addAll(PairedStats) of " + values).that(pearsonsCorrelationCoefficient).isNaN();
        } else {
            assertWithMessage("Pearson's correlation coefficient of " + values).that(pearsonsCorrelationCoefficient).isWithin(ALLOWED_ERROR).of(accumulator.populationCovariance() / (accumulator.xStats().populationStandardDeviation() * accumulator.yStats().populationStandardDeviation()));
            assertWithMessage("Pearson's correlation coefficient by addAll(PairedStats) of " + values).that(pearsonsCorrelationCoefficientByAddAllPartitionedPairedStats).isWithin(ALLOWED_ERROR).of(accumulatorByAddAllPartitionedPairedStats.populationCovariance() / (accumulatorByAddAllPartitionedPairedStats.xStats().populationStandardDeviation() * accumulatorByAddAllPartitionedPairedStats.yStats().populationStandardDeviation()));
        }
    }
    try {
        horizontalValuesAccumulator.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        horizontalValuesAccumulatorByAddAllPartitionedPairedStats.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        verticalValuesAccumulator.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        verticalValuesAccumulatorByAddAllPartitionedPairedStats.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        constantValuesAccumulator.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    try {
        constantValuesAccumulatorByAddAllPartitionedPairedStats.pearsonsCorrelationCoefficient();
        fail("Expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
}
Also used : ManyValues(com.google.common.math.StatsTesting.ManyValues) StatsTesting.createFilledPairedStatsAccumulator(com.google.common.math.StatsTesting.createFilledPairedStatsAccumulator) StatsTesting.createPartitionedFilledPairedStatsAccumulator(com.google.common.math.StatsTesting.createPartitionedFilledPairedStatsAccumulator)

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 Stats.toStats (com.google.common.math.Stats.toStats)1 DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)1