Java Code Examples for com.tdunning.math.stats.AVLTreeDigest#add()

The following examples show how to use com.tdunning.math.stats.AVLTreeDigest#add() . 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
Source File: TestJsonFacets.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void XtestPercentiles() {
  AVLTreeDigest catA = new AVLTreeDigest(100);
  catA.add(4);
  catA.add(2);

  AVLTreeDigest catB = new AVLTreeDigest(100);
  catB.add(-9);
  catB.add(11);
  catB.add(-5);

  AVLTreeDigest all = new AVLTreeDigest(100);
  all.add(catA);
  all.add(catB);

  System.out.println(str(catA));
  System.out.println(str(catB));
  System.out.println(str(all));

  // 2.0 2.2 3.0 3.8 4.0
  // -9.0 -8.2 -5.0 7.800000000000001 11.0
  // -9.0 -7.3999999999999995 2.0 8.200000000000001 11.0
}
 
Example 2
Source File: PercentileAgg.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void collect(int doc, int slotNum, IntFunction<SlotContext> slotContext) throws IOException {
  if (!values.exists(doc)) return;
  double val = values.doubleVal(doc);

  AVLTreeDigest digest = digests[slotNum];
  if (digest == null) {
    digests[slotNum] = digest = new AVLTreeDigest(100);   // TODO: make compression configurable
  }

  digest.add(val);
}
 
Example 3
Source File: PercentileAgg.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void collectValues(int doc, int slot) throws IOException {
  AVLTreeDigest digest = digests[slot];
  if (digest == null) {
    digests[slot] = digest = new AVLTreeDigest(100);
  }
  for (int i = 0, count = values.docValueCount(); i < count; i++) {
    double val = getDouble(values.nextValue());
    digest.add(val);
  }
}
 
Example 4
Source File: PercentileAgg.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void collectValues(int doc, int slot) throws IOException {
  AVLTreeDigest digest = digests[slot];
  if (digest == null) {
    digests[slot] = digest = new AVLTreeDigest(100);
  }
  long ord;
  while ((ord = values.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
    BytesRef term = values.lookupOrd(ord);
    Object obj = sf.getType().toObject(sf, term);
    double val = obj instanceof Date ? ((Date)obj).getTime(): ((Number)obj).doubleValue();
    digest.add(val);
  }
}
 
Example 5
Source File: PercentileAgg.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void call(int ord) {
  AVLTreeDigest digest = digests[currentSlot];
  if (digest == null) {
    digests[currentSlot] = digest = new AVLTreeDigest(100);
  }
  try {
    BytesRef term = docToTerm.lookupOrd(ord);
    Object obj = sf.getType().toObject(sf, term);
    double val = obj instanceof Date ? ((Date) obj).getTime() : ((Number) obj).doubleValue();
    digest.add(val);
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example 6
Source File: TestJsonFacets.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/*** test code to ensure TDigest is working as we expect. */

  public void XtestTDigest() throws Exception {
    AVLTreeDigest t1 = new AVLTreeDigest(100);
    t1.add(10, 1);
    t1.add(90, 1);
    t1.add(50, 1);

    System.out.println(t1.quantile(0.1));
    System.out.println(t1.quantile(0.5));
    System.out.println(t1.quantile(0.9));

    assertEquals(t1.quantile(0.5), 50.0, 0.01);

    AVLTreeDigest t2 = new AVLTreeDigest(100);
    t2.add(130, 1);
    t2.add(170, 1);
    t2.add(90, 1);

    System.out.println(t2.quantile(0.1));
    System.out.println(t2.quantile(0.5));
    System.out.println(t2.quantile(0.9));

    AVLTreeDigest top = new AVLTreeDigest(100);

    t1.compress();
    ByteBuffer buf = ByteBuffer.allocate(t1.byteSize()); // upper bound
    t1.asSmallBytes(buf);
    byte[] arr1 = Arrays.copyOf(buf.array(), buf.position());

    ByteBuffer rbuf = ByteBuffer.wrap(arr1);
    top.add(AVLTreeDigest.fromBytes(rbuf));

    System.out.println(top.quantile(0.1));
    System.out.println(top.quantile(0.5));
    System.out.println(top.quantile(0.9));

    t2.compress();
    ByteBuffer buf2 = ByteBuffer.allocate(t2.byteSize()); // upper bound
    t2.asSmallBytes(buf2);
    byte[] arr2 = Arrays.copyOf(buf2.array(), buf2.position());

    ByteBuffer rbuf2 = ByteBuffer.wrap(arr2);
    top.add(AVLTreeDigest.fromBytes(rbuf2));

    System.out.println(top.quantile(0.1));
    System.out.println(top.quantile(0.5));
    System.out.println(top.quantile(0.9));
  }