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);
}
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;
}
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);
}
}
}
}
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
}
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;
}
}
Aggregations