Search in sources :

Example 1 with QUANTILE_FIELD

use of com.yahoo.bullet.aggregations.sketches.QuantileSketch.QUANTILE_FIELD in project bullet-core by yahoo.

the class DistributionTest method testQuantiles.

@Test
public void testQuantiles() {
    Distribution distribution = makeDistribution("field", Distribution.Type.QUANTILE, 3);
    IntStream.range(0, 2000).mapToDouble(i -> (i * 0.1)).mapToObj(d -> RecordBox.get().add("field", d).getRecord()).forEach(distribution::consume);
    Clip result = distribution.getResult();
    Map<String, Object> metadata = (Map<String, Object>) result.getMeta().asMap().get("meta");
    Assert.assertEquals(metadata.size(), 7);
    Assert.assertTrue((Boolean) metadata.get("isEst"));
    Assert.assertEquals((String) metadata.get("family"), Family.QUANTILES.getFamilyName());
    // Size should be at least 512 bytes since we inserted 2K uniques with sketch k set to 512
    Assert.assertTrue((Integer) metadata.get("size") >= 512);
    Assert.assertEquals(metadata.get("nre"), DoublesSketch.getNormalizedRankError(512));
    // 60 items
    Assert.assertEquals(metadata.get("n"), 2000L);
    Assert.assertEquals(metadata.get("min"), 0.0);
    Assert.assertEquals(metadata.get("max"), 199.9);
    List<BulletRecord> records = result.getRecords();
    Assert.assertEquals(records.size(), 3);
    BulletRecord expectedA = RecordBox.get().add(QUANTILE_FIELD, 0.0).add(VALUE_FIELD, 0.0).getRecord();
    BulletRecord expectedC = RecordBox.get().add(QUANTILE_FIELD, 1.0).add(VALUE_FIELD, 199.9).getRecord();
    Assert.assertEquals(records.get(0), expectedA);
    Assert.assertEquals(records.get(2), expectedC);
    BulletRecord actualB = records.get(1);
    Assert.assertEquals(actualB.get(QUANTILE_FIELD), 0.5);
    Double actualMedian = (Double) actualB.get(VALUE_FIELD);
    // We insert 0,0.1, ... 199.9. Our median is around 100.0. Our NRE < 1%, so we can be pretty certain the median
    // from the sketch is around this.
    assertApproxEquals(actualMedian, 100.0, 2.0);
    Assert.assertEquals(distribution.getRecords(), result.getRecords());
    Assert.assertEquals(distribution.getMetadata().asMap(), result.getMeta().asMap());
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BulletError(com.yahoo.bullet.common.BulletError) Concept(com.yahoo.bullet.result.Meta.Concept) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) RecordBox(com.yahoo.bullet.result.RecordBox) PROBABILITY_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.PROBABILITY_FIELD) Clip(com.yahoo.bullet.result.Clip) Family(com.yahoo.sketches.Family) SEPARATOR(com.yahoo.bullet.aggregations.sketches.QuantileSketch.SEPARATOR) HashSet(java.util.HashSet) VALUE_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.VALUE_FIELD) Pair(org.apache.commons.lang3.tuple.Pair) Assert(org.testng.Assert) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) AggregationUtils.makeAttributes(com.yahoo.bullet.parsing.AggregationUtils.makeAttributes) QUANTILE_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.QUANTILE_FIELD) BulletRecord(com.yahoo.bullet.record.BulletRecord) DoublesSketch(com.yahoo.sketches.quantiles.DoublesSketch) Aggregation(com.yahoo.bullet.parsing.Aggregation) COUNT_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.COUNT_FIELD) POSITIVE_INFINITY_END(com.yahoo.bullet.aggregations.sketches.QuantileSketch.POSITIVE_INFINITY_END) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) TestHelpers.assertApproxEquals(com.yahoo.bullet.TestHelpers.assertApproxEquals) START_INCLUSIVE(com.yahoo.bullet.aggregations.sketches.QuantileSketch.START_INCLUSIVE) TestHelpers.addMetadata(com.yahoo.bullet.TestHelpers.addMetadata) NEGATIVE_INFINITY_START(com.yahoo.bullet.aggregations.sketches.QuantileSketch.NEGATIVE_INFINITY_START) BulletConfig(com.yahoo.bullet.common.BulletConfig) Optional(java.util.Optional) RANGE_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.RANGE_FIELD) Collections(java.util.Collections) END_EXCLUSIVE(com.yahoo.bullet.aggregations.sketches.QuantileSketch.END_EXCLUSIVE) Clip(com.yahoo.bullet.result.Clip) BulletRecord(com.yahoo.bullet.record.BulletRecord) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 2 with QUANTILE_FIELD

use of com.yahoo.bullet.aggregations.sketches.QuantileSketch.QUANTILE_FIELD in project bullet-core by yahoo.

the class DistributionTest method testRounding.

@Test
public void testRounding() {
    Distribution distribution = makeDistribution(Distribution.Type.QUANTILE, 20, 6, 0.0, 1.0, 0.1);
    IntStream.range(0, 10).mapToDouble(i -> (i * 0.1)).mapToObj(d -> RecordBox.get().add("field", d).getRecord()).forEach(distribution::consume);
    Clip result = distribution.getResult();
    Map<String, Object> metadata = (Map<String, Object>) result.getMeta().asMap().get("meta");
    Assert.assertEquals(metadata.size(), 7);
    Assert.assertFalse((Boolean) metadata.get("isEst"));
    List<BulletRecord> records = result.getRecords();
    Assert.assertEquals(records.size(), 11);
    Set<String> actualQuantilePoints = records.stream().map(r -> r.get(QUANTILE_FIELD).toString()).collect(Collectors.toSet());
    Set<String> expectedQuantilePoints = new HashSet<>(Arrays.asList("0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0"));
    Assert.assertEquals(actualQuantilePoints, expectedQuantilePoints);
    Assert.assertEquals(distribution.getRecords(), result.getRecords());
    Assert.assertEquals(distribution.getMetadata().asMap(), result.getMeta().asMap());
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BulletError(com.yahoo.bullet.common.BulletError) Concept(com.yahoo.bullet.result.Meta.Concept) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) RecordBox(com.yahoo.bullet.result.RecordBox) PROBABILITY_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.PROBABILITY_FIELD) Clip(com.yahoo.bullet.result.Clip) Family(com.yahoo.sketches.Family) SEPARATOR(com.yahoo.bullet.aggregations.sketches.QuantileSketch.SEPARATOR) HashSet(java.util.HashSet) VALUE_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.VALUE_FIELD) Pair(org.apache.commons.lang3.tuple.Pair) Assert(org.testng.Assert) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) AggregationUtils.makeAttributes(com.yahoo.bullet.parsing.AggregationUtils.makeAttributes) QUANTILE_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.QUANTILE_FIELD) BulletRecord(com.yahoo.bullet.record.BulletRecord) DoublesSketch(com.yahoo.sketches.quantiles.DoublesSketch) Aggregation(com.yahoo.bullet.parsing.Aggregation) COUNT_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.COUNT_FIELD) POSITIVE_INFINITY_END(com.yahoo.bullet.aggregations.sketches.QuantileSketch.POSITIVE_INFINITY_END) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) TestHelpers.assertApproxEquals(com.yahoo.bullet.TestHelpers.assertApproxEquals) START_INCLUSIVE(com.yahoo.bullet.aggregations.sketches.QuantileSketch.START_INCLUSIVE) TestHelpers.addMetadata(com.yahoo.bullet.TestHelpers.addMetadata) NEGATIVE_INFINITY_START(com.yahoo.bullet.aggregations.sketches.QuantileSketch.NEGATIVE_INFINITY_START) BulletConfig(com.yahoo.bullet.common.BulletConfig) Optional(java.util.Optional) RANGE_FIELD(com.yahoo.bullet.aggregations.sketches.QuantileSketch.RANGE_FIELD) Collections(java.util.Collections) END_EXCLUSIVE(com.yahoo.bullet.aggregations.sketches.QuantileSketch.END_EXCLUSIVE) Clip(com.yahoo.bullet.result.Clip) BulletRecord(com.yahoo.bullet.record.BulletRecord) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

TestHelpers.addMetadata (com.yahoo.bullet.TestHelpers.addMetadata)2 TestHelpers.assertApproxEquals (com.yahoo.bullet.TestHelpers.assertApproxEquals)2 COUNT_FIELD (com.yahoo.bullet.aggregations.sketches.QuantileSketch.COUNT_FIELD)2 END_EXCLUSIVE (com.yahoo.bullet.aggregations.sketches.QuantileSketch.END_EXCLUSIVE)2 NEGATIVE_INFINITY_START (com.yahoo.bullet.aggregations.sketches.QuantileSketch.NEGATIVE_INFINITY_START)2 POSITIVE_INFINITY_END (com.yahoo.bullet.aggregations.sketches.QuantileSketch.POSITIVE_INFINITY_END)2 PROBABILITY_FIELD (com.yahoo.bullet.aggregations.sketches.QuantileSketch.PROBABILITY_FIELD)2 QUANTILE_FIELD (com.yahoo.bullet.aggregations.sketches.QuantileSketch.QUANTILE_FIELD)2 RANGE_FIELD (com.yahoo.bullet.aggregations.sketches.QuantileSketch.RANGE_FIELD)2 SEPARATOR (com.yahoo.bullet.aggregations.sketches.QuantileSketch.SEPARATOR)2 START_INCLUSIVE (com.yahoo.bullet.aggregations.sketches.QuantileSketch.START_INCLUSIVE)2 VALUE_FIELD (com.yahoo.bullet.aggregations.sketches.QuantileSketch.VALUE_FIELD)2 BulletConfig (com.yahoo.bullet.common.BulletConfig)2 BulletError (com.yahoo.bullet.common.BulletError)2 Aggregation (com.yahoo.bullet.parsing.Aggregation)2 AggregationUtils.makeAttributes (com.yahoo.bullet.parsing.AggregationUtils.makeAttributes)2 BulletRecord (com.yahoo.bullet.record.BulletRecord)2 Clip (com.yahoo.bullet.result.Clip)2 Concept (com.yahoo.bullet.result.Meta.Concept)2 RecordBox (com.yahoo.bullet.result.RecordBox)2