private static void assertEquivalent(double actual, double expected) { if (expected == POSITIVE_INFINITY) { assertThat(actual).isPositiveInfinity(); } else if (expected == NEGATIVE_INFINITY) { assertThat(actual).isNegativeInfinity(); } else if (Double.isNaN(expected)) { assertThat(actual).isNaN(); } else { assertThat(actual).isWithin(ALLOWED_ERROR).of(expected); } } }
public void testMultipleQuantile() { ImmutableSet<Integer> indexes = ImmutableSet.of(50, 90, 99); Map<Integer, Double> referenceQuantiles = REFERENCE_ALGORITHM.multipleQuantiles(indexes, 100, dataset.clone()); assertThat(referenceQuantiles.keySet()).isEqualTo(indexes); for (QuantilesAlgorithm algorithm : NON_REFERENCE_ALGORITHMS) { Map<Integer, Double> quantiles = algorithm.multipleQuantiles(indexes, 100, dataset.clone()); assertWithMessage("Wrong keys from " + algorithm).that(quantiles.keySet()).isEqualTo(indexes); for (int i : indexes) { assertWithMessage("Mismatch between %s and %s at %s", algorithm, REFERENCE_ALGORITHM, i) .that(quantiles.get(i)) .isWithin(ALLOWED_ERROR) .of(referenceQuantiles.get(i)); } } } }
public void testMappingWithSlope_maximalSlope() { double x1 = 1.2; double y1 = 3.4; double slope = Double.MAX_VALUE; LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope); assertThat(transformation.isVertical()).isFalse(); assertThat(transformation.isHorizontal()).isFalse(); assertThat(transformation.slope()).isWithin(ALLOWED_ERROR).of(slope); // Note that we cannot test the actual mapping of points, as the results will be unreliable due // to loss of precision with this value of the slope. }
@AndroidIncompatible // slow public void testPercentiles_index_computeInPlace() { // Assert that the computation gives the correct result for all possible percentiles. for (int index = 0; index <= 100; index++) { double[] dataset = Doubles.toArray(PSEUDORANDOM_DATASET); assertThat(percentiles().index(index).computeInPlace(dataset)) .named("quantile at index " + index) .isWithin(ALLOWED_ERROR) .of(expectedLargeDatasetPercentile(index)); } // Assert that the dataset contains the same elements after the in-place computation (although // they may be reordered). We only do this for one index rather than for all indexes, as it is // quite expensives (quadratic in the size of PSEUDORANDOM_DATASET). double[] dataset = Doubles.toArray(PSEUDORANDOM_DATASET); @SuppressWarnings("unused") double actual = percentiles().index(33).computeInPlace(dataset); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(PSEUDORANDOM_DATASET); }
public void testSingleQuantile_median() { double referenceValue = REFERENCE_ALGORITHM.singleQuantile(1, 2, dataset.clone()); for (QuantilesAlgorithm algorithm : NON_REFERENCE_ALGORITHMS) { assertWithMessage("Mismatch between %s and %s", algorithm, REFERENCE_ALGORITHM) .that(algorithm.singleQuantile(1, 2, dataset.clone())) .isWithin(ALLOWED_ERROR) .of(referenceValue); } }
public void testMappingWithSlope_minimalSlope() { double x1 = 1.2; double y1 = 3.4; double slope = Double.MIN_VALUE; LinearTransformation transformation = LinearTransformation.mapping(x1, y1).withSlope(slope); assertThat(transformation.isVertical()).isFalse(); assertThat(transformation.isHorizontal()).isFalse(); assertThat(transformation.slope()).isWithin(ALLOWED_ERROR).of(slope); // Note that we cannot test the actual mapping of points, as the results will be unreliable due // to loss of precision with this value of the slope. }
public void testScale_index_compute_longVarargs() { long[] dataset = Longs.toArray(SIXTEEN_SQUARES_LONGS); assertThat(Quantiles.scale(10).index(1).compute(dataset)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_LONGS); }
public void testQuartiles_index_compute_doubleCollection() { assertThat(quartiles().index(1).compute(SIXTEEN_SQUARES_DOUBLES)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_QUARTILE_1); }
public void testScale_index_computeInPlace_explicitVarargs() { assertThat(Quantiles.scale(10).index(5).computeInPlace(78.9, 12.3, 45.6)) .isWithin(ALLOWED_ERROR) .of(45.6); }
public void testScale_index_compute_doubleVarargs() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat(Quantiles.scale(10).index(1).compute(dataset)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); assertThat(dataset) .usingExactEquality() .containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES) .inOrder(); }
public void testScale_index_compute_longCollection() { assertThat(Quantiles.scale(10).index(1).compute(SIXTEEN_SQUARES_LONGS)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); }
public void testQuartiles_index_computeInPlace() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat(quartiles().index(1).computeInPlace(dataset)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_QUARTILE_1); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES); }
public void testMedian_computeInPlace() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat(median().computeInPlace(dataset)).isWithin(ALLOWED_ERROR).of(SIXTEEN_SQUARES_MEDIAN); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES); }
public void testPercentiles_index_compute_doubleCollection() { for (int index = 0; index <= 100; index++) { assertThat(percentiles().index(index).compute(PSEUDORANDOM_DATASET)) .named("quantile at index " + index) .isWithin(ALLOWED_ERROR) .of(expectedLargeDatasetPercentile(index)); } }
public void testMedian_compute_doubleCollection() { assertThat(median().compute(SIXTEEN_SQUARES_DOUBLES)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_MEDIAN); }
public void testScale_index_computeInPlace() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat(Quantiles.scale(10).index(1).computeInPlace(dataset)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES); }
public void testScale_index_compute_intVarargs() { int[] dataset = Ints.toArray(SIXTEEN_SQUARES_INTEGERS); assertThat(Quantiles.scale(10).index(1).compute(dataset)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); assertThat(dataset).asList().isEqualTo(SIXTEEN_SQUARES_INTEGERS); }
public void testSingleQuantile_percentile99() { double referenceValue = REFERENCE_ALGORITHM.singleQuantile(99, 100, dataset.clone()); for (QuantilesAlgorithm algorithm : NON_REFERENCE_ALGORITHMS) { assertWithMessage("Mismatch between %s and %s", algorithm, REFERENCE_ALGORITHM) .that(algorithm.singleQuantile(99, 100, dataset.clone())) .isWithin(ALLOWED_ERROR) .of(referenceValue); } }
public void testScale_index_compute_integerCollection() { assertThat(Quantiles.scale(10).index(1).compute(SIXTEEN_SQUARES_INTEGERS)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); }
public void testScale_index_compute_doubleCollection() { assertThat(Quantiles.scale(10).index(1).compute(SIXTEEN_SQUARES_DOUBLES)) .isWithin(ALLOWED_ERROR) .of(SIXTEEN_SQUARES_DECILE_1); }