Search in sources :

Example 1 with DoubleSummaryStatistics

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;
}
Also used : HashMap(java.util.HashMap) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics)

Example 2 with DoubleSummaryStatistics

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);
            }
        }
    }
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) DoubleDocValuesStats(org.apache.lucene.search.DocValuesStats.DoubleDocValuesStats) SortedDoubleDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedDoubleDocValuesStats) IndexWriter(org.apache.lucene.index.IndexWriter) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) StringField(org.apache.lucene.document.StringField) Directory(org.apache.lucene.store.Directory)

Example 3 with DoubleSummaryStatistics

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);
}
Also used : Random(java.util.Random) PathWrapper(com.graphhopper.PathWrapper) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) Test(org.junit.Test)

Example 4 with DoubleSummaryStatistics

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);
    }
}
Also used : List(java.util.List) LambdaTestHelpers.countTo(java.util.stream.LambdaTestHelpers.countTo) OpTestCase(java.util.stream.OpTestCase) IntSummaryStatistics(java.util.IntSummaryStatistics) Test(org.testng.annotations.Test) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Collectors(java.util.stream.Collectors) LongSummaryStatistics(java.util.LongSummaryStatistics) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics)

Example 5 with DoubleSummaryStatistics

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);
}
Also used : DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Test(org.junit.Test)

Aggregations

DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)46 Arrays (java.util.Arrays)9 List (java.util.List)9 Nullable (javax.annotation.Nullable)9 Test (org.junit.Test)9 Layer (com.simiacryptus.mindseye.lang.Layer)8 Tensor (com.simiacryptus.mindseye.lang.Tensor)8 LongSummaryStatistics (java.util.LongSummaryStatistics)8 IntStream (java.util.stream.IntStream)8 Nonnull (javax.annotation.Nonnull)8 TrainingMonitor (com.simiacryptus.mindseye.opt.TrainingMonitor)7 Map (java.util.Map)6 LoggingWrapperLayer (com.simiacryptus.mindseye.layers.java.LoggingWrapperLayer)5 MonitoringWrapperLayer (com.simiacryptus.mindseye.layers.java.MonitoringWrapperLayer)5 StochasticComponent (com.simiacryptus.mindseye.layers.java.StochasticComponent)5 DAGNetwork (com.simiacryptus.mindseye.network.DAGNetwork)5 DAGNode (com.simiacryptus.mindseye.network.DAGNode)5 Step (com.simiacryptus.mindseye.opt.Step)5 MonitoredObject (com.simiacryptus.util.MonitoredObject)5 DoubleStatistics (com.simiacryptus.util.data.DoubleStatistics)5