Java Code Examples for java.util.DoubleSummaryStatistics

The following examples show how to use java.util.DoubleSummaryStatistics. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).stream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                     DoubleSummaryStatistics::accept,
                                                                     DoubleSummaryStatistics::combine));
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                             DoubleSummaryStatistics::accept,
                                                                             DoubleSummaryStatistics::combine));

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 2
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).stream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                     DoubleSummaryStatistics::accept,
                                                                     DoubleSummaryStatistics::combine));
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                             DoubleSummaryStatistics::accept,
                                                                             DoubleSummaryStatistics::combine));

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 3
Source Project: spectator   Source File: RegistryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void gauges() {
  Registry r = newRegistry(true, 10000);
  r.gauge(r.createId("foo", "a", "1")).set(1.0);
  r.gauge(r.createId("foo", "a", "2")).set(2.0);
  r.gauge(r.createId("bar")).set(7.0);

  Assertions.assertEquals(3, r.gauges().count());

  final DoubleSummaryStatistics valueSummary = r.gauges()
      .filter(Functions.nameEquals("foo"))
      .collect(Collectors.summarizingDouble(Gauge::value));
  Assertions.assertEquals(2, valueSummary.getCount());
  Assertions.assertEquals(3.0, valueSummary.getSum(), 1e-12);
  Assertions.assertEquals(1.5, valueSummary.getAverage(), 1e-12);
}
 
Example 4
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).stream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                     DoubleSummaryStatistics::accept,
                                                                     DoubleSummaryStatistics::combine));
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                             DoubleSummaryStatistics::accept,
                                                                             DoubleSummaryStatistics::combine));

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 5
Source Project: Java-11-Cookbook-Second-Edition   Source File: NumericStreams.java    License: MIT License 6 votes vote down vote up
private static void summary() {

        IntSummaryStatistics iss = IntStream.empty().summaryStatistics();
        System.out.println(iss);  //IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}
        iss = IntStream.range(1, 3).summaryStatistics();
        System.out.println(iss);  //IntSummaryStatistics{count=2, sum=3, min=1, average=1.500000, max=2}

        LongSummaryStatistics lss = LongStream.empty().summaryStatistics();
        System.out.println(lss);  //LongSummaryStatistics{count=0, sum=0, min=9223372036854775807, average=0.000000, max=-9223372036854775808}
        lss = LongStream.range(1, 3).summaryStatistics();
        System.out.println(lss);  //LongSummaryStatistics{count=2, sum=3, min=1, average=1.500000, max=2}

        DoubleSummaryStatistics dss = DoubleStream.empty().summaryStatistics();
        System.out.println(dss);  //DoubleSummaryStatistics{count=0, sum=0.000000, min=Infinity, average=0.000000, max=-Infinity}
        dss = DoubleStream.of(1, 2).summaryStatistics();
        System.out.println(dss);  //DoubleSummaryStatistics{count=2, sum=3.000000, min=1.000000, average=1.500000, max=2.000000}

        System.out.println(Integer.MAX_VALUE);  //prints:  2147483647
        System.out.println(Integer.MIN_VALUE);  //prints: -2147483648
        System.out.println(Long.MAX_VALUE);  //prints:  9223372036854775807
        System.out.println(Long.MIN_VALUE);  //prints: -9223372036854775808
        System.out.println(Double.MAX_VALUE);  //prints: 1.7976931348623157E308
        System.out.println(Double.MIN_VALUE);  //prints: 4.9E-324
    }
 
Example 6
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).stream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                     DoubleSummaryStatistics::accept,
                                                                     DoubleSummaryStatistics::combine));
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
                                                                             DoubleSummaryStatistics::accept,
                                                                             DoubleSummaryStatistics::combine));

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 7
Source Project: SLP-Core   Source File: CLI.java    License: MIT License 6 votes vote down vote up
private static void test() {
	File inDir = getTestFile();
	File counterFile = getCounterFile();
	if (inDir == null || !inDir.exists()) {
		exit("Test path does not exist: " + getArg(TEST));
	} else if (counterFile == null || !counterFile.exists()) {
		exit("Counter file to use not found: " + getArg(COUNTER));
	}
	
	Stream<Pair<File, List<List<Double>>>> fileProbs = modelRunner.modelDirectory(inDir);
	int[] fileCount = { 0 };
	DoubleSummaryStatistics stats = modelRunner.getStats(
			fileProbs.peek(f -> Writer.writeEntropies(lexerRunner, f))
			.peek(f -> fileCount[0]++));
	System.out.printf("Testing complete, modeled %d files with %d tokens yielding average entropy:\t%.4f\n",
			fileCount[0], stats.getCount(), stats.getAverage());
}
 
Example 8
Source Project: SLP-Core   Source File: CLI.java    License: MIT License 6 votes vote down vote up
private static void predict() {
	File inDir = getTestFile();
	File counterFile = getCounterFile();
	if (inDir == null || !inDir.exists()) {
		exit("Test path does not exist: " + getArg(TEST));
	} else if (counterFile == null || !counterFile.exists()) {
		exit("Counter file to use not found: " + getArg(COUNTER));
	}
	
	Stream<Pair<File, List<List<Double>>>> fileMRRs = modelRunner.predictDirectory(inDir);
	int[] fileCount = { 0 };
	DoubleSummaryStatistics stats = modelRunner.getStats(
			fileMRRs.peek(f -> Writer.writeEntropies(lexerRunner, f))
			.peek(f -> fileCount[0]++));
	System.out.printf("Testing complete, modeled %d files with %d tokens yielding average MRR:\t%.4f\n",
			fileCount[0], stats.getCount(), stats.getAverage());
}
 
Example 9
Source Project: j2objc   Source File: DoubleSummaryStatisticsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_getSum() {
    DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
    assertEquals(148.4, dss1.getSum());

    dss1.accept(Double.NaN);
    assertEquals(Double.NaN, dss1.getSum());
}
 
Example 10
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 11
public void testDoubleCollectNull() {
    checkNPE(() -> DoubleStream.of(1).collect(null,
                                            DoubleSummaryStatistics::accept,
                                            DoubleSummaryStatistics::combine));
    checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new,
                                            null,
                                            DoubleSummaryStatistics::combine));
    checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new,
                                              DoubleSummaryStatistics::accept,
                                              null));
}
 
Example 12
Source Project: j2objc   Source File: DoubleSummaryStatisticsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_getAverage() {
    DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
    assertEquals(21.2, dss1.getAverage(), 1E-6);

    dss1.accept(Double.NaN);
    assertEquals(Double.NaN, dss1.getAverage());
}
 
Example 13
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 14
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 15
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 16
Source Project: JImageHash   Source File: CategoricalMatcher.java    License: MIT License 5 votes vote down vote up
public void printClusterInfo(int minImagesInCluster) {
	for (Entry<Integer, DoubleSummaryStatistics> entry : clusterQuality.entrySet()) {
		if (entry.getValue().getCount() >= minImagesInCluster) {
			System.out
					.println("Category: " + entry.getKey() + " Average Distance: " + entry.getValue().getAverage());
		}
	}
}
 
Example 17
Source Project: tutorials   Source File: Java8CollectorsUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenSummarizing_shouldSummarize() throws Exception {
    final DoubleSummaryStatistics result = givenList.stream().collect(summarizingDouble(String::length));

    assertThat(result.getAverage()).isEqualTo(2);
    assertThat(result.getCount()).isEqualTo(4);
    assertThat(result.getMax()).isEqualTo(3);
    assertThat(result.getMin()).isEqualTo(1);
    assertThat(result.getSum()).isEqualTo(8);
}
 
Example 18
Source Project: jenetics   Source File: DoubleSummary.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return a new value object of the statistical summary, currently
 * represented by the {@code statistics} object.
 *
 * @param statistics the creating (mutable) statistics class
 * @return the statistical moments
 */
public static DoubleSummary of(final DoubleSummaryStatistics statistics) {
	return new DoubleSummary(
		statistics.getCount(),
		statistics.getMin(),
		statistics.getMax(),
		statistics.getSum(),
		statistics.getAverage()
	);
}
 
Example 19
Source Project: j2objc   Source File: DoubleSummaryStatisticsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_empty() {
    DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
    assertEquals(0, dss.getCount());
    assertEquals(0.0d, dss.getSum());
    assertEquals(0.0d, dss.getAverage());
    assertEquals(Double.POSITIVE_INFINITY, dss.getMin());
    assertEquals(Double.NEGATIVE_INFINITY, dss.getMax());
}
 
Example 20
Source Project: j2objc   Source File: DoubleSummaryStatisticsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_combine() {
    DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData2();
    DoubleSummaryStatistics dssCombined = getDoubleSummaryStatisticsData1();
    dssCombined.combine(dss1);
    assertEquals(12, dssCombined.getCount());
    assertEquals(164.4d, dssCombined.getSum());
    assertEquals(100.0d, dssCombined.getMax());
    assertEquals(-53.4d, dssCombined.getMin());
    assertEquals(13.7, dssCombined.getAverage(), 1E-6);
}
 
Example 21
Source Project: jenetics   Source File: DoubleSummaryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<DoubleSummary> factory() {
	return () -> {
		final Random random = RandomRegistry.random();

		final DoubleSummaryStatistics statistics = new DoubleSummaryStatistics();
		IntStream.range(0, 100)
			.mapToDouble(i -> random.nextDouble())
			.forEach(statistics);

		return DoubleSummary.of(statistics);
	};
}
 
Example 22
public void testDoubleCollectNull() {
    checkNPE(() -> DoubleStream.of(1).collect(null,
                                            DoubleSummaryStatistics::accept,
                                            DoubleSummaryStatistics::combine));
    checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new,
                                            null,
                                            DoubleSummaryStatistics::combine));
    checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new,
                                              DoubleSummaryStatistics::accept,
                                              null));
}
 
Example 23
Source Project: j2objc   Source File: DoubleSummaryStatisticsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_getMin() {
    DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
    assertEquals(-53.4d, dss1.getMin());

    dss1.accept(Double.NaN);
    assertEquals(Double.NaN, dss1.getMin());
}
 
Example 24
public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());

    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
 
Example 25
Source Project: SLP-Core   Source File: CLI.java    License: MIT License 5 votes vote down vote up
private static void trainPredict() {
	trainModel();
	File testDir = getTestFile();
	Stream<Pair<File, List<List<Double>>>> fileMRRs = modelRunner.predictDirectory(testDir);
	int[] fileCount = { 0 };
	DoubleSummaryStatistics stats = modelRunner.getStats(
			fileMRRs.peek(f -> Writer.writeEntropies(lexerRunner, f))
			.peek(f -> fileCount[0]++));
	System.out.printf("Testing complete, modeled %d files with %d tokens yielding average MRR:\t%.4f\n",
			fileCount[0], stats.getCount(), stats.getAverage());
}
 
Example 26
Source Project: SLP-Core   Source File: ModelRunner.java    License: MIT License 5 votes vote down vote up
private void logModelingProgress(List<Double> modeled) {
	DoubleSummaryStatistics stats = modeled.stream().skip(1)
			.mapToDouble(Double::doubleValue).summaryStatistics();
	long prevCount = this.modelStats[0];
	this.modelStats[0] += stats.getCount();
	this.ent += stats.getSum();
	if (this.modelStats[0] / this.MODEL_PRINT_INTERVAL > prevCount / this.MODEL_PRINT_INTERVAL
			&& this.modelStats[1] != 0) {
		System.out.printf("Modeling: %dK tokens processed in %ds, avg. entropy: %.4f\n",
				Math.round(this.modelStats[0]/1e3),
				(System.currentTimeMillis() + this.modelStats[1])/1000, this.ent/this.modelStats[0]);
	}
}
 
Example 27
Source Project: spectator   Source File: RegistryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void maxGauge() {
  Registry r = newRegistry(true, 10000);
  r.maxGauge("foo").set(1.0);
  r.maxGauge("foo").set(3.0);
  r.maxGauge("foo").set(2.0);

  final DoubleSummaryStatistics valueSummary = r.gauges()
      .filter(Functions.nameEquals("foo"))
      .collect(Collectors.summarizingDouble(Gauge::value));
  Assertions.assertEquals(1, valueSummary.getCount());
  Assertions.assertEquals(3.0, valueSummary.getSum(), 1e-12);
}
 
Example 28
Source Project: SLP-Core   Source File: ModelRunner.java    License: MIT License 5 votes vote down vote up
private void logCompletionProgress(List<Completion> completions) {
	DoubleSummaryStatistics stats = completions.stream().skip(1)
			.map(Completion::getRank)
			.mapToDouble(ModelRunner::toMRR)
			.summaryStatistics();
	long prevCount = this.modelStats[0];
	this.modelStats[0] += stats.getCount();
	this.mrr += stats.getSum();
	if (this.modelStats[0] / this.MODEL_PRINT_INTERVAL > prevCount / this.MODEL_PRINT_INTERVAL
			&& this.modelStats[1] != 0) {
		System.out.printf("Predicting: %dK tokens processed in %ds, avg. MRR: %.4f\n",
				Math.round(this.modelStats[0]/1e3),
				(System.currentTimeMillis() + this.modelStats[1])/1000, this.mrr/this.modelStats[0]);
	}
}
 
Example 29
Source Project: SLP-Core   Source File: ModelRunner.java    License: MIT License 5 votes vote down vote up
private DoubleSummaryStatistics getFileStats(Stream<List<List<Double>>> fileProbs) {
	if (this.lexerRunner.isPerLine()) {
		return fileProbs.flatMap(List::stream)
				.flatMap(l -> l.stream().skip(1))
				.mapToDouble(p -> p).summaryStatistics();
	}
	else {
		return fileProbs.flatMap(f -> f.stream()
					.flatMap(l -> l.stream())
					.skip(1))
				.mapToDouble(p -> p).summaryStatistics();
	}
}
 
Example 30
Source Project: tutorials   Source File: EmployeeTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenApplySummaryStatistics_thenGetBasicStats() {
    DoubleSummaryStatistics stats = empList.stream()
      .mapToDouble(Employee::getSalary)
      .summaryStatistics();

    assertEquals(stats.getCount(), 3);
    assertEquals(stats.getSum(), 600000.0, 0);
    assertEquals(stats.getMin(), 100000.0, 0);
    assertEquals(stats.getMax(), 300000.0, 0);
    assertEquals(stats.getAverage(), 200000.0, 0);
}