checkState(count() > 1); if (isNaN(sumOfProductsOfDeltas)) { return NaN; ensurePositive(xSumOfSquaresOfDeltas * ySumOfSquaresOfDeltas); return ensureInUnitRange(sumOfProductsOfDeltas / Math.sqrt(productOfSumsOfSquaresOfDeltas));
public void testCountOverflow_doesNotThrow() { PairedStatsAccumulator accumulator = new PairedStatsAccumulator(); accumulator.add(ONE_VALUE, OTHER_ONE_VALUE); for (int power = 1; power < Long.SIZE - 1; power++) { accumulator.addAll(accumulator.snapshot()); } // Should overflow without throwing. accumulator.addAll(accumulator.snapshot()); assertThat(accumulator.count()).isLessThan(0L); }
/** * Creates a {@link PairedStatsAccumulator} filled with the given lists of {@code x} and {@code y} * values, which must be of the same size. */ static PairedStatsAccumulator createFilledPairedStatsAccumulator( List<Double> xValues, List<Double> yValues) { checkArgument(xValues.size() == yValues.size()); PairedStatsAccumulator accumulator = new PairedStatsAccumulator(); for (int index = 0; index < xValues.size(); index++) { accumulator.add(xValues.get(index), yValues.get(index)); } return accumulator; }
/** * Returns the sample covariance of the values. The count must be greater than one. * * <p>This is not guaranteed to return zero when the dataset consists of the same pair of values * multiple times, due to numerical errors. * * <h3>Non-finite values</h3> * * <p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link * Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link Double#NaN}. * * @throws IllegalStateException if the dataset is empty or contains a single pair of values */ public final double sampleCovariance() { checkState(count() > 1); return sumOfProductsOfDeltas / (count() - 1); }
/** * Creates a {@link PairedStatsAccumulator} filled with the given lists of {@code x} and {@code y} * values, which must be of the same size, added in groups of {@code partitionSize} using {@link * PairedStatsAccumulator#addAll(PairedStats)}. */ static PairedStatsAccumulator createPartitionedFilledPairedStatsAccumulator( List<Double> xValues, List<Double> yValues, int partitionSize) { checkArgument(xValues.size() == yValues.size()); checkArgument(partitionSize > 0); PairedStatsAccumulator accumulator = new PairedStatsAccumulator(); List<List<Double>> xPartitions = Lists.partition(xValues, partitionSize); List<List<Double>> yPartitions = Lists.partition(yValues, partitionSize); for (int index = 0; index < xPartitions.size(); index++) { accumulator.addAll(createPairedStatsOf(xPartitions.get(index), yPartitions.get(index))); } return accumulator; }
/** * Returns the population covariance of the values. The count must be non-zero. * * <p>This is guaranteed to return zero if the dataset contains a single pair of finite values. It * is not guaranteed to return zero when the dataset consists of the same pair of values multiple * times, due to numerical errors. * * <h3>Non-finite values</h3> * * <p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link * Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link Double#NaN}. * * @throws IllegalStateException if the dataset is empty */ public double populationCovariance() { checkState(count() != 0); return sumOfProductsOfDeltas / count(); }
/** * Creates a {@link PairedStatsAccumulator} filled with the given lists of {@code x} and {@code y} * values, which must be of the same size. */ static PairedStatsAccumulator createFilledPairedStatsAccumulator( List<Double> xValues, List<Double> yValues) { checkArgument(xValues.size() == yValues.size()); PairedStatsAccumulator accumulator = new PairedStatsAccumulator(); for (int index = 0; index < xValues.size(); index++) { accumulator.add(xValues.get(index), yValues.get(index)); } return accumulator; }
/** * Creates a {@link PairedStatsAccumulator} filled with the given lists of {@code x} and {@code y} * values, which must be of the same size, added in groups of {@code partitionSize} using * {@link PairedStatsAccumulator#addAll(PairedStats)}. */ static PairedStatsAccumulator createPartitionedFilledPairedStatsAccumulator( List<Double> xValues, List<Double> yValues, int partitionSize) { checkArgument(xValues.size() == yValues.size()); checkArgument(partitionSize > 0); PairedStatsAccumulator accumulator = new PairedStatsAccumulator(); List<List<Double>> xPartitions = Lists.partition(xValues, partitionSize); List<List<Double>> yPartitions = Lists.partition(yValues, partitionSize); for (int index = 0; index < xPartitions.size(); index++) { accumulator.addAll(createPairedStatsOf(xPartitions.get(index), yPartitions.get(index))); } return accumulator; }
checkState(count() > 1); if (isNaN(sumOfProductsOfDeltas)) { return NaN; ensurePositive(xSumOfSquaresOfDeltas * ySumOfSquaresOfDeltas); return ensureInUnitRange(sumOfProductsOfDeltas / Math.sqrt(productOfSumsOfSquaresOfDeltas));
/** * Returns the sample covariance of the values. The count must be greater than one. * * <p>This is not guaranteed to return zero when the dataset consists of the same pair of values * multiple times, due to numerical errors. * * <h3>Non-finite values</h3> * * <p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link * Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link Double#NaN}. * * @throws IllegalStateException if the dataset is empty or contains a single pair of values */ public final double sampleCovariance() { checkState(count() > 1); return sumOfProductsOfDeltas / (count() - 1); }
super.setUp(); emptyAccumulator = new PairedStatsAccumulator(); emptyAccumulatorByAddAllEmptyPairedStats = new PairedStatsAccumulator(); emptyAccumulatorByAddAllEmptyPairedStats.addAll(emptyAccumulator.snapshot()); oneValueAccumulator = new PairedStatsAccumulator(); oneValueAccumulator.add(ONE_VALUE, OTHER_ONE_VALUE); oneValueAccumulatorByAddAllEmptyPairedStats = new PairedStatsAccumulator(); oneValueAccumulatorByAddAllEmptyPairedStats.add(ONE_VALUE, OTHER_ONE_VALUE); oneValueAccumulatorByAddAllEmptyPairedStats.addAll(emptyAccumulator.snapshot());
checkState(count() > 1); if (isNaN(sumOfProductsOfDeltas)) { return NaN; ensurePositive(xSumOfSquaresOfDeltas * ySumOfSquaresOfDeltas); return ensureInUnitRange(sumOfProductsOfDeltas / Math.sqrt(productOfSumsOfSquaresOfDeltas));
/** * Returns the population covariance of the values. The count must be non-zero. * * <p>This is guaranteed to return zero if the dataset contains a single pair of finite values. It * is not guaranteed to return zero when the dataset consists of the same pair of values multiple * times, due to numerical errors. * * <h3>Non-finite values</h3> * * <p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link * Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link Double#NaN}. * * @throws IllegalStateException if the dataset is empty */ public double populationCovariance() { checkState(count() != 0); return sumOfProductsOfDeltas / count(); }
public void testCountOverflow_doesNotThrow() { PairedStatsAccumulator accumulator = new PairedStatsAccumulator(); accumulator.add(ONE_VALUE, OTHER_ONE_VALUE); for (int power = 1; power < Long.SIZE - 1; power++) { accumulator.addAll(accumulator.snapshot()); } // Should overflow without throwing. accumulator.addAll(accumulator.snapshot()); assertThat(accumulator.count()).isLessThan(0L); }
checkState(count() > 1); if (isNaN(sumOfProductsOfDeltas)) { return NaN; ensurePositive(xSumOfSquaresOfDeltas * ySumOfSquaresOfDeltas); return ensureInUnitRange(sumOfProductsOfDeltas / Math.sqrt(productOfSumsOfSquaresOfDeltas));
/** * Returns the sample covariance of the values. The count must be greater than one. * * <p>This is not guaranteed to return zero when the dataset consists of the same pair of values * multiple times, due to numerical errors. * * <h3>Non-finite values</h3> * * <p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link * Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link Double#NaN}. * * @throws IllegalStateException if the dataset is empty or contains a single pair of values */ public final double sampleCovariance() { checkState(count() > 1); return sumOfProductsOfDeltas / (count() - 1); }
super.setUp(); emptyAccumulator = new PairedStatsAccumulator(); emptyAccumulatorByAddAllEmptyPairedStats = new PairedStatsAccumulator(); emptyAccumulatorByAddAllEmptyPairedStats.addAll(emptyAccumulator.snapshot()); oneValueAccumulator = new PairedStatsAccumulator(); oneValueAccumulator.add(ONE_VALUE, OTHER_ONE_VALUE); oneValueAccumulatorByAddAllEmptyPairedStats = new PairedStatsAccumulator(); oneValueAccumulatorByAddAllEmptyPairedStats.add(ONE_VALUE, OTHER_ONE_VALUE); oneValueAccumulatorByAddAllEmptyPairedStats.addAll(emptyAccumulator.snapshot());
checkState(count() > 1); if (isNaN(sumOfProductsOfDeltas)) { return NaN; ensurePositive(xSumOfSquaresOfDeltas * ySumOfSquaresOfDeltas); return ensureInUnitRange(sumOfProductsOfDeltas / Math.sqrt(productOfSumsOfSquaresOfDeltas));
/** * Returns the population covariance of the values. The count must be non-zero. * * <p>This is guaranteed to return zero if the dataset contains a single pair of finite values. It * is not guaranteed to return zero when the dataset consists of the same pair of values multiple * times, due to numerical errors. * * <h3>Non-finite values</h3> * * <p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link * Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link Double#NaN}. * * @throws IllegalStateException if the dataset is empty */ public double populationCovariance() { checkState(count() != 0); return sumOfProductsOfDeltas / count(); }
public void testCount() { assertThat(emptyAccumulator.count()).isEqualTo(0); assertThat(emptyAccumulatorByAddAllEmptyPairedStats.count()).isEqualTo(0); assertThat(oneValueAccumulator.count()).isEqualTo(1); assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.count()).isEqualTo(1); assertThat(twoValuesAccumulator.count()).isEqualTo(2); assertThat(twoValuesAccumulatorByAddAllPartitionedPairedStats.count()).isEqualTo(2); assertThat(manyValuesAccumulator.count()).isEqualTo(MANY_VALUES_COUNT); assertThat(manyValuesAccumulatorByAddAllPartitionedPairedStats.count()) .isEqualTo(MANY_VALUES_COUNT); }