use of java.util.DoubleSummaryStatistics in project opennms by OpenNMS.
the class TrendController method variableReplacements.
private Map<String, String> variableReplacements(final List<Double> values) {
final Map<String, String> replacements = new HashMap<>();
final DoubleSummaryStatistics doubleSummaryStatistics = values.stream().mapToDouble(Double::doubleValue).summaryStatistics();
replacements.put("${doubleMax}", String.format("%.2f", doubleSummaryStatistics.getMax()));
replacements.put("${intMax}", String.format("%d", (int) doubleSummaryStatistics.getMax()));
replacements.put("${doubleMin}", String.format("%.2f", doubleSummaryStatistics.getMin()));
replacements.put("${intMin}", String.format("%d", (int) doubleSummaryStatistics.getMin()));
replacements.put("${doubleAvg}", String.format("%.2f", doubleSummaryStatistics.getAverage()));
replacements.put("${intAvg}", String.format("%d", (int) doubleSummaryStatistics.getAverage()));
replacements.put("${doubleSum}", String.format("%.2f", doubleSummaryStatistics.getSum()));
replacements.put("${intSum}", String.format("%d", (int) doubleSummaryStatistics.getSum()));
for (int i = 0; i < values.size(); i++) {
double current = values.get(i);
replacements.put("${doubleValue[" + i + "]}", String.format("%.2f", current));
replacements.put("${intValue[" + i + "]}", String.format("%d", (int) current));
if (i > 0) {
double previous = values.get(i - 1);
double change = current - previous;
replacements.put("${doubleValueChange[" + i + "]}", String.format("%+.2f", change));
replacements.put("${intValueChange[" + i + "]}", String.format("%+d", (int) change));
} else {
replacements.put("${doubleValueChange[" + i + "]}", "NaN");
replacements.put("${intValueChange[" + i + "]}", "NaN");
}
}
if (values.size() > 0) {
replacements.put("${doubleLastValueChange}", replacements.get("${doubleValueChange[" + (values.size() - 1) + "]}"));
replacements.put("${intLastValueChange}", replacements.get("${intValueChange[" + (values.size() - 1) + "]}"));
replacements.put("${doubleLastValue}", replacements.get("${doubleValue[" + (values.size() - 1) + "]}"));
replacements.put("${intLastValue}", replacements.get("${intValue[" + (values.size() - 1) + "]}"));
} else {
replacements.put("${doubleLastValueChange}", "NaN");
replacements.put("${intLastValueChange}", "NaN");
replacements.put("${doubleLastValue}", "NaN");
replacements.put("${intLastValue}", "NaN");
}
return replacements;
}
use of java.util.DoubleSummaryStatistics in project lucene-solr by apache.
the class TestDocValuesStatsCollector method testDocsWithDoubleValues.
public void testDocsWithDoubleValues() throws IOException {
try (Directory dir = newDirectory();
IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {
String field = "numeric";
int numDocs = TestUtil.nextInt(random(), 1, 100);
double[] docValues = new double[numDocs];
double nextVal = 1.0;
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
if (random().nextBoolean()) {
// not all documents have a value
doc.add(new DoubleDocValuesField(field, nextVal));
doc.add(new StringField("id", "doc" + i, Store.NO));
docValues[i] = nextVal;
++nextVal;
}
indexWriter.addDocument(doc);
}
// 20% of cases delete some docs
if (random().nextDouble() < 0.2) {
for (int i = 0; i < numDocs; i++) {
if (random().nextBoolean()) {
indexWriter.deleteDocuments(new Term("id", "doc" + i));
docValues[i] = 0;
}
}
}
try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {
IndexSearcher searcher = new IndexSearcher(reader);
DoubleDocValuesStats stats = new DoubleDocValuesStats(field);
searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));
int expCount = (int) Arrays.stream(docValues).filter(v -> v > 0).count();
assertEquals(expCount, stats.count());
int numDocsWithoutField = (int) getZeroValues(docValues).count();
assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());
if (stats.count() > 0) {
DoubleSummaryStatistics sumStats = getPositiveValues(docValues).summaryStatistics();
assertEquals(sumStats.getMax(), stats.max().doubleValue(), 0.00001);
assertEquals(sumStats.getMin(), stats.min().doubleValue(), 0.00001);
assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);
assertEquals(sumStats.getSum(), stats.sum(), 0.00001);
double variance = computeVariance(docValues, stats.mean, stats.count());
assertEquals(variance, stats.variance(), 0.00001);
assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);
}
}
}
}
use of java.util.DoubleSummaryStatistics in project graphhopper by graphhopper.
the class ShapeFileReaderTest method testTravelTimesBetweenRandomLocations.
@Test
public void testTravelTimesBetweenRandomLocations() {
int nTests = 200;
final Random random = new Random(123);
final GHPoint min = new GHPoint(35.882931, 14.403076);
final GHPoint max = new GHPoint(35.913523, 14.448566);
class RandPointGenerator {
double rand(double min, double max) {
return min + random.nextDouble() * (max - min);
}
GHPoint randPoint() {
return new GHPoint(rand(min.lat, max.lat), rand(min.lon, max.lon));
}
}
RandPointGenerator pointGenerator = new RandPointGenerator();
int nbFails = 0;
DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
for (int i = 0; i < nTests; i++) {
FromToPair pair = new FromToPair(pointGenerator.randPoint(), pointGenerator.randPoint());
// paths from random points can fail to don't assert on failure
PathWrapper shpPath = pair.getPath(hopperShp, false);
PathWrapper pbfPath = pair.getPath(hopperPbf, false);
// the road network)
if (shpPath == null || pbfPath == null) {
nbFails++;
continue;
}
double shpSecs = getSecondsTravel(shpPath);
double pbfSecs = getSecondsTravel(pbfPath);
double frac = shpSecs / pbfSecs;
double percentageDeviation = Math.abs(1.0 - frac) * 100;
stats.accept(percentageDeviation);
}
assertTrue("Number of fails should be small for the chosen box", nbFails < nTests / 3);
// Test mean fraction. There will be some deviation as not all tags are
// considered etc,
// but we expect it to be small for a large number of tests
double mean = stats.getAverage();
assertTrue("Should have a mean deviation in travel times of less than 1%", mean < 1.0);
}
use of java.util.DoubleSummaryStatistics in project jdk8u_jdk by JetBrains.
the class SummaryStatisticsTest method testDoubleStatistics.
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);
}
}
use of java.util.DoubleSummaryStatistics in project tutorials by eugenp.
the class EmployeeTest method whenApplySummarizing_thenGetBasicStats.
@Test
public void whenApplySummarizing_thenGetBasicStats() {
DoubleSummaryStatistics stats = empList.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
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);
}
Aggregations