Search in sources :

Example 21 with DoubleSummaryStatistics

use of java.util.DoubleSummaryStatistics in project j2objc by google.

the class DoubleSummaryStatisticsTest method test_combine.

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

Example 22 with DoubleSummaryStatistics

use of java.util.DoubleSummaryStatistics in project intellij-community by JetBrains.

the class Main method generateNested.

public static DoubleSummaryStatistics generateNested() {
    Random r = new Random();
    DoubleSummaryStatistics stat = new DoubleSummaryStatistics();
    for (int x = 0; x < 10; x++) {
        double v = x;
        for (long count = (long) v; count > 0; count--) {
            double v1 = r.nextDouble() * v;
            stat.accept(v1);
        }
    }
    return stat;
}
Also used : SplittableRandom(java.util.SplittableRandom) Random(java.util.Random) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics)

Example 23 with DoubleSummaryStatistics

use of java.util.DoubleSummaryStatistics in project lucene-solr by apache.

the class TestDocValuesStatsCollector method testDocsWithMultipleDoubleValues.

public void testDocsWithMultipleDoubleValues() 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;
        for (int i = 0; i < numDocs; i++) {
            Document doc = new Document();
            if (random().nextBoolean()) {
                // not all documents have a value
                int numValues = TestUtil.nextInt(random(), 1, 5);
                docValues[i] = new double[numValues];
                for (int j = 0; j < numValues; j++) {
                    doc.add(new SortedNumericDocValuesField(field, Double.doubleToRawLongBits(nextVal)));
                    docValues[i][j] = nextVal;
                    ++nextVal;
                }
                doc.add(new StringField("id", "doc" + i, Store.NO));
            }
            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] = null;
                }
            }
        }
        try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {
            IndexSearcher searcher = new IndexSearcher(reader);
            SortedDoubleDocValuesStats stats = new SortedDoubleDocValuesStats(field);
            searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));
            assertEquals(nonNull(docValues).count(), stats.count());
            int numDocsWithoutField = (int) isNull(docValues).count();
            assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());
            if (stats.count() > 0) {
                DoubleSummaryStatistics sumStats = filterAndFlatValues(docValues, (v) -> v != null).summaryStatistics();
                assertEquals(sumStats.getMax(), stats.max().longValue(), 0.00001);
                assertEquals(sumStats.getMin(), stats.min().longValue(), 0.00001);
                assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);
                assertEquals(sumStats.getSum(), stats.sum().doubleValue(), 0.00001);
                assertEquals(sumStats.getCount(), stats.valuesCount());
                double variance = computeVariance(filterAndFlatValues(docValues, (v) -> v != null), stats.mean, stats.count());
                assertEquals(variance, stats.variance(), 0.00001);
                assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) DoubleDocValuesStats(org.apache.lucene.search.DocValuesStats.DoubleDocValuesStats) StringField(org.apache.lucene.document.StringField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) TestUtil(org.apache.lucene.util.TestUtil) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) SortedSetDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedSetDocValuesStats) Document(org.apache.lucene.document.Document) SortedDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedDocValuesStats) Directory(org.apache.lucene.store.Directory) Store(org.apache.lucene.document.Field.Store) LongSummaryStatistics(java.util.LongSummaryStatistics) SortedDoubleDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedDoubleDocValuesStats) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) LongStream(java.util.stream.LongStream) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) Predicate(java.util.function.Predicate) BytesRef(org.apache.lucene.util.BytesRef) DirectoryReader(org.apache.lucene.index.DirectoryReader) IOException(java.io.IOException) LongDocValuesStats(org.apache.lucene.search.DocValuesStats.LongDocValuesStats) DoubleStream(java.util.stream.DoubleStream) Objects(java.util.Objects) IndexWriter(org.apache.lucene.index.IndexWriter) Stream(java.util.stream.Stream) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) SortedLongDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedLongDocValuesStats) IndexReader(org.apache.lucene.index.IndexReader) SortedDoubleDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedDoubleDocValuesStats) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) Directory(org.apache.lucene.store.Directory)

Example 24 with DoubleSummaryStatistics

use of java.util.DoubleSummaryStatistics in project narchy by automenta.

the class QuestionTest method questionDrivesInference.

// @Test public void testQuestionHandler() throws Narsese.NarseseException {
// NAR nar = NARS.shell();
// 
// final int[] s = {0};
// new TaskMatch("add(%1, %2, #x)", nar) {
// 
// @Override public boolean test(@NotNull Task task) { return task.isQuestOrQuestion(); }
// 
// @Override
// protected void accept(Task task, Map<Term, Term> xy) {
// System.out.println(task + " " + xy);
// s[0] = xy.size();
// }
// };
// 
// nar.ask($.$("add(1, 2, #x)"));
// 
// assertEquals(3, s[0]);
// 
// }
// @Test public void testOperationHandler() throws Narsese.NarseseException {
// NAR nar = NARS.shell();
// 
// final int[] s = {0};
// StringBuilder match = new StringBuilder();
// new OperationTaskMatch( $.$("add(%1, %2, #x)"), nar) {
// 
// @Override public boolean test(@NotNull Task task) { return task.isQuestOrQuestion(); }
// 
// @Override
// protected void onMatch(Term[] args) {
// match.append(Arrays.toString(args)).append(' ');
// }
// };
// 
// nar.ask($.$("add(1, 2, #x)"));
// 
// assertTrue(match.toString().contains("[1, 2, #1026]"));
// 
// nar.ask($.$("add(1, #x)"));
// nar.ask($.$("(#x --> add)"));
// 
// assertFalse(match.toString().contains("[1, #1026]"));
// }
/**
 * tests whether the use of a question guides inference as measured by the speed to reach a specific conclusion
 */
@Test
public void questionDrivesInference() {
    final int[] dims = { 3, 2 };
    final int timelimit = 2400;
    TaskStatistics withTasks = new TaskStatistics();
    TaskStatistics withoutTasks = new TaskStatistics();
    DoubleSummaryStatistics withTime = new DoubleSummaryStatistics();
    DoubleSummaryStatistics withOutTime = new DoubleSummaryStatistics();
    IntFunction<NAR> narProvider = (seed) -> {
        NAR d = NARS.tmp(1);
        d.random().setSeed(seed);
        d.termVolumeMax.set(16);
        d.freqResolution.set(0.1f);
        return d;
    };
    BiFunction<Integer, Integer, TestNAR> testProvider = (seed, variation) -> {
        NAR n = narProvider.apply(seed);
        TestNAR t = new TestNAR(n);
        switch(variation) {
            case 0:
                new DeductiveMeshTest(t, dims, timelimit);
                break;
            case 1:
                new DeductiveMeshTest(t, dims, timelimit) {

                    @Override
                    public void ask(@NotNull TestNAR n, Term term) {
                    // disabled
                    }
                };
                break;
        }
        return t;
    };
    for (int i = 0; i < 10; i++) {
        int seed = i + 1;
        TestNAR withQuestion = testProvider.apply(seed, 0);
        withQuestion.test(true);
        withTime.accept(withQuestion.time());
        withTasks.add(withQuestion.nar);
        TestNAR withoutQuestion = testProvider.apply(seed, 1);
        withoutQuestion.test(true);
        withOutTime.accept(withoutQuestion.time());
        withoutTasks.add(withoutQuestion.nar);
    }
    withTasks.print();
    withoutTasks.print();
    assertNotEquals(withTime, withOutTime);
    System.out.println("with: " + withTime);
    System.out.println("withOut: " + withOutTime);
// assertTrue(withTime.getSum() < withOutTime.getSum());
// assertTrue(withTime.getSum() < 2 * withOutTime.getSum()); //less than half, considering that a search "diameter" becomes a "radius" by providing the answer end-point
}
Also used : nars(nars) BiFunction(java.util.function.BiFunction) DeductiveMeshTest(nars.test.DeductiveMeshTest) TestNAR(nars.test.TestNAR) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Disabled(org.junit.jupiter.api.Disabled) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) ETERNAL(nars.time.Tense.ETERNAL) Test(org.junit.jupiter.api.Test) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TaskStatistics(nars.task.util.TaskStatistics) NotNull(org.jetbrains.annotations.NotNull) Term(nars.term.Term) IntFunction(java.util.function.IntFunction) TestNAR(nars.test.TestNAR) DeductiveMeshTest(nars.test.DeductiveMeshTest) Term(nars.term.Term) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskStatistics(nars.task.util.TaskStatistics) TestNAR(nars.test.TestNAR) DeductiveMeshTest(nars.test.DeductiveMeshTest) Test(org.junit.jupiter.api.Test)

Example 25 with DoubleSummaryStatistics

use of java.util.DoubleSummaryStatistics in project MindsEye by SimiaCryptus.

the class TestUtil method compare.

/**
 * Compare plot canvas.
 *
 * @param title  the title
 * @param trials the trials
 * @return the plot canvas
 */
public static PlotCanvas compare(final String title, @Nonnull final ProblemRun... trials) {
    try {
        final DoubleSummaryStatistics xStatistics = Arrays.stream(trials).flatMapToDouble(x -> x.history.stream().mapToDouble(step -> step.iteration)).filter(Double::isFinite).summaryStatistics();
        final DoubleSummaryStatistics yStatistics = Arrays.stream(trials).flatMapToDouble(x -> x.history.stream().filter(y -> y.fitness > 0).mapToDouble(step -> Math.log10(step.fitness))).filter(Double::isFinite).summaryStatistics();
        if (xStatistics.getCount() == 0) {
            log.info("No Data");
            return null;
        }
        @Nonnull final double[] lowerBound = { xStatistics.getCount() == 0 ? 0 : xStatistics.getMin(), yStatistics.getCount() < 2 ? 0 : yStatistics.getMin() };
        @Nonnull final double[] upperBound = { xStatistics.getCount() == 0 ? 1 : xStatistics.getMax(), yStatistics.getCount() < 2 ? 1 : yStatistics.getMax() };
        @Nonnull final PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound);
        canvas.setTitle(title);
        canvas.setAxisLabels("Iteration", "log10(Fitness)");
        canvas.setSize(600, 400);
        final List<ProblemRun> filtered = Arrays.stream(trials).filter(x -> !x.history.isEmpty()).collect(Collectors.toList());
        if (filtered.isEmpty()) {
            log.info("No Data");
            return null;
        }
        DoubleSummaryStatistics valueStatistics = filtered.stream().flatMap(x -> x.history.stream()).mapToDouble(x -> x.fitness).filter(x -> x > 0).summaryStatistics();
        log.info(String.format("Plotting range=%s, %s; valueStats=%s", Arrays.toString(lowerBound), Arrays.toString(upperBound), valueStatistics));
        for (@Nonnull final ProblemRun trial : filtered) {
            final double[][] pts = trial.history.stream().map(step -> new double[] { step.iteration, Math.log10(Math.max(step.fitness, valueStatistics.getMin())) }).filter(x -> Arrays.stream(x).allMatch(Double::isFinite)).toArray(i -> new double[i][]);
            if (pts.length > 1) {
                log.info(String.format("Plotting %s points for %s", pts.length, trial.name));
                canvas.add(trial.plot(pts));
            } else {
                log.info(String.format("Only %s points for %s", pts.length, trial.name));
            }
        }
        return canvas;
    } catch (@Nonnull final Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}
Also used : Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) DoubleStatistics(com.simiacryptus.util.data.DoubleStatistics) IntUnaryOperator(java.util.function.IntUnaryOperator) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) TrainingMonitor(com.simiacryptus.mindseye.opt.TrainingMonitor) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) Layer(com.simiacryptus.mindseye.lang.Layer) URI(java.net.URI) Graph(guru.nidi.graphviz.model.Graph) LongToIntFunction(java.util.function.LongToIntFunction) StochasticComponent(com.simiacryptus.mindseye.layers.java.StochasticComponent) BufferedImage(java.awt.image.BufferedImage) UUID(java.util.UUID) ComponentEvent(java.awt.event.ComponentEvent) WindowAdapter(java.awt.event.WindowAdapter) DAGNode(com.simiacryptus.mindseye.network.DAGNode) Collectors(java.util.stream.Collectors) WindowEvent(java.awt.event.WindowEvent) Executors(java.util.concurrent.Executors) List(java.util.List) Stream(java.util.stream.Stream) ScalarStatistics(com.simiacryptus.util.data.ScalarStatistics) LoggingWrapperLayer(com.simiacryptus.mindseye.layers.java.LoggingWrapperLayer) DAGNetwork(com.simiacryptus.mindseye.network.DAGNetwork) IntStream(java.util.stream.IntStream) MonitoringWrapperLayer(com.simiacryptus.mindseye.layers.java.MonitoringWrapperLayer) ActionListener(java.awt.event.ActionListener) ScatterPlot(smile.plot.ScatterPlot) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkSource(guru.nidi.graphviz.model.LinkSource) Tensor(com.simiacryptus.mindseye.lang.Tensor) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) JsonUtil(com.simiacryptus.util.io.JsonUtil) MutableNode(guru.nidi.graphviz.model.MutableNode) Charset(java.nio.charset.Charset) Factory(guru.nidi.graphviz.model.Factory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) WeakReference(java.lang.ref.WeakReference) LinkTarget(guru.nidi.graphviz.model.LinkTarget) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) LongSummaryStatistics(java.util.LongSummaryStatistics) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) PlotCanvas(smile.plot.PlotCanvas) RankDir(guru.nidi.graphviz.attribute.RankDir) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) ActionEvent(java.awt.event.ActionEvent) PercentileStatistics(com.simiacryptus.util.data.PercentileStatistics) File(java.io.File) java.awt(java.awt) ComponentAdapter(java.awt.event.ComponentAdapter) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MonitoredObject(com.simiacryptus.util.MonitoredObject) IntToLongFunction(java.util.function.IntToLongFunction) Link(guru.nidi.graphviz.model.Link) Step(com.simiacryptus.mindseye.opt.Step) Comparator(java.util.Comparator) javax.swing(javax.swing) Nonnull(javax.annotation.Nonnull) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) IOException(java.io.IOException) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)39 Arrays (java.util.Arrays)9 List (java.util.List)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 Nullable (javax.annotation.Nullable)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