/** * Computes the quantile value of the given dataset. * * @param dataset the dataset to do the calculation on, which must be non-empty, which will be * cast to doubles (with any associated lost of precision), and which will not be mutated by * this call (it is copied instead) * @return the quantile value */ public double compute(Collection<? extends Number> dataset) { return computeInPlace(Doubles.toArray(dataset)); }
/** * Computes the quantile values of the given dataset. * * @param dataset the dataset to do the calculation on, which must be non-empty, which will be * cast to doubles (with any associated lost of precision), and which will not be mutated by * this call (it is copied instead) * @return an unmodifiable map of results: the keys will be the specified quantile indexes, and * the values the corresponding quantile values */ public Map<Integer, Double> compute(Collection<? extends Number> dataset) { return computeInPlace(Doubles.toArray(dataset)); }
/** Returns an immutable array containing the given values, in order. */ public static ImmutableDoubleArray copyOf(Collection<Double> values) { return values.isEmpty() ? EMPTY : new ImmutableDoubleArray(Doubles.toArray(values)); }
double[] asArray() { return Doubles.toArray(values); }
public void testToArray_withNull() { List<Double> list = Arrays.asList((double) 0, (double) 1, null); try { Doubles.toArray(list); fail(); } catch (NullPointerException expected) { } }
public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Double> none = Arrays.<Double>asList(); assertTrue(Arrays.equals(EMPTY, Doubles.toArray(none))); List<Double> one = Arrays.asList((double) 1); assertTrue(Arrays.equals(ARRAY1, Doubles.toArray(one))); double[] array = {(double) 0, (double) 1, Math.PI}; List<Double> three = Arrays.asList((double) 0, (double) 1, Math.PI); assertTrue(Arrays.equals(array, Doubles.toArray(three))); assertTrue(Arrays.equals(array, Doubles.toArray(Doubles.asList(array)))); }
public void testToArray_withConversion() { double[] array = {(double) 0, (double) 1, (double) 2}; List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2); List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2); List<Integer> ints = Arrays.asList(0, 1, 2); List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2); List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2); List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2); assertTrue(Arrays.equals(array, Doubles.toArray(bytes))); assertTrue(Arrays.equals(array, Doubles.toArray(shorts))); assertTrue(Arrays.equals(array, Doubles.toArray(ints))); assertTrue(Arrays.equals(array, Doubles.toArray(floats))); assertTrue(Arrays.equals(array, Doubles.toArray(longs))); assertTrue(Arrays.equals(array, Doubles.toArray(doubles))); }
public void testAsList_subList_toArray_roundTrip() { double[] array = {(double) 0, (double) 1, (double) 2, (double) 3}; List<Double> list = Doubles.asList(array); assertTrue( Arrays.equals(new double[] {(double) 1, (double) 2}, Doubles.toArray(list.subList(1, 3)))); assertTrue(Arrays.equals(new double[] {}, Doubles.toArray(list.subList(2, 2)))); }
public void testAsList_toArray_roundTrip() { double[] array = {(double) 0, (double) 1, (double) 2}; List<Double> list = Doubles.asList(array); double[] newArray = Doubles.toArray(list); // Make sure it returned a copy list.set(0, (double) 4); assertTrue(Arrays.equals(new double[] {(double) 0, (double) 1, (double) 2}, newArray)); newArray[1] = (double) 5; assertEquals((double) 1, (double) list.get(1)); }
public void testToArray_threadSafe() { for (int delta : new int[] {+1, 0, -1}) { for (int i = 0; i < VALUES.length; i++) { List<Double> list = Doubles.asList(VALUES).subList(0, i); Collection<Double> misleadingSize = Helpers.misleadingSizeCollection(delta); misleadingSize.addAll(list); double[] arr = Doubles.toArray(misleadingSize); assertEquals(i, arr.length); for (int j = 0; j < i; j++) { assertEquals(VALUES[j], arr[j]); } } } }
@AndroidIncompatible // slow public void testPercentiles_indexes_varargsAll_computeInPlace() { double[] dataset = Doubles.toArray(PSEUDORANDOM_DATASET); List<Integer> indexes = new ArrayList<>(); ImmutableMap.Builder<Integer, Double> expectedBuilder = ImmutableMap.builder(); for (int index = 0; index <= 100; index++) { indexes.add(index); expectedBuilder.put(index, expectedLargeDatasetPercentile(index)); } Random random = new Random(770683168895677741L); Collections.shuffle(indexes, random); assertThat(percentiles().indexes(Ints.toArray(indexes)).computeInPlace(dataset)) .comparingValuesUsing(QUANTILE_CORRESPONDENCE) .containsExactlyEntriesIn(expectedBuilder.build()); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(PSEUDORANDOM_DATASET); }
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 testScale_indexes_collection_computeInPlace() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat( Quantiles.scale(10) .indexes(ImmutableList.of(0, 10, 5, 1, 8, 1)) .computeInPlace(dataset)) .comparingValuesUsing(QUANTILE_CORRESPONDENCE) .containsExactly( 0, SIXTEEN_SQUARES_MIN, 10, SIXTEEN_SQUARES_MAX, 5, SIXTEEN_SQUARES_MEDIAN, 1, SIXTEEN_SQUARES_DECILE_1, 8, SIXTEEN_SQUARES_DECILE_8); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES); }
public void testQuartiles_indexes_varargs_computeInPlace() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat(quartiles().indexes(1, 3).computeInPlace(dataset)) .comparingValuesUsing(QUANTILE_CORRESPONDENCE) .containsExactly( 1, SIXTEEN_SQUARES_QUARTILE_1, 3, SIXTEEN_SQUARES_QUARTILE_3); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES); }
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 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 testScale_indexes_varargs_computeInPlace() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat(Quantiles.scale(10).indexes(0, 10, 5, 1, 8, 1).computeInPlace(dataset)) .comparingValuesUsing(QUANTILE_CORRESPONDENCE) .containsExactly( 0, SIXTEEN_SQUARES_MIN, 10, SIXTEEN_SQUARES_MAX, 5, SIXTEEN_SQUARES_MEDIAN, 1, SIXTEEN_SQUARES_DECILE_1, 8, SIXTEEN_SQUARES_DECILE_8); assertThat(dataset).usingExactEquality().containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES); }
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_indexes_varargs_compute_doubleVarargs() { double[] dataset = Doubles.toArray(SIXTEEN_SQUARES_DOUBLES); assertThat(Quantiles.scale(10).indexes(0, 10, 5, 1, 8, 1).compute(dataset)) .comparingValuesUsing(QUANTILE_CORRESPONDENCE) .containsExactly( 0, SIXTEEN_SQUARES_MIN, 10, SIXTEEN_SQUARES_MAX, 5, SIXTEEN_SQUARES_MEDIAN, 1, SIXTEEN_SQUARES_DECILE_1, 8, SIXTEEN_SQUARES_DECILE_8); assertThat(dataset) .usingExactEquality() .containsExactlyElementsIn(SIXTEEN_SQUARES_DOUBLES) .inOrder(); }
@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); }