Stats aggregator

This Apache Druid extension includes stat-related aggregators, including variance and standard deviations, etc. Make sure to include druid-stats in the extensions load list.

Variance aggregator

Algorithm of the aggregator is the same with that of apache hive. This is the description in GenericUDAFVariance in hive.

Evaluate the variance using the algorithm described by Chan, Golub, and LeVeque in “Algorithms for computing the sample variance: analysis and recommendations” The American Statistician, 37 (1983) pp. 242—247.

variance = variance1 + variance2 + n/(m(m+n)) \ pow(((m/n)*t1 - t2),2)

where:

  • variance is sum(x-avg^2) (this is actually n times the variance) and is updated at every step.
  • n is the count of elements in chunk1
  • m is the count of elements in chunk2
  • t1 is the sum of elements in chunk1
  • t2 is the sum of elements in chunk2

This algorithm was proven to be numerically stable by J.L. Barlow in “Error analysis of a pairwise summation algorithm to compute sample variance” Numer. Math, 58 (1991) pp. 583—590

As with all aggregators, the order of operations across segments is non-deterministic. This means that if this aggregator operates with an input type of “float” or “double”, the result of the aggregation may not be precisely the same across multiple runs of the query.

To produce consistent results, round the variance to a fixed number of decimal places so that the results are precisely the same across query runs.

Pre-aggregating variance at ingestion time

To use this feature, an “variance” aggregator must be included at indexing time. The ingestion aggregator can only apply to numeric values. If you use “variance” then any input rows missing the value will be considered to have a value of 0.

User can specify expected input type as one of “float”, “double”, “long”, “variance” for ingestion, which is by default “float”.

  1. {
  2. "type" : "variance",
  3. "name" : <output_name>,
  4. "fieldName" : <metric_name>,
  5. "inputType" : <input_type>,
  6. "estimator" : <string>
  7. }

To query for results, “variance” aggregator with “variance” input type or simply a “varianceFold” aggregator must be included in the query.

  1. {
  2. "type" : "varianceFold",
  3. "name" : <output_name>,
  4. "fieldName" : <metric_name>,
  5. "estimator" : <string>
  6. }
PropertyDescriptionDefault
estimatorSet “population” to get variance_pop rather than variance_sample, which is default.null

Standard deviation post-aggregator

To acquire standard deviation from variance, user can use “stddev” post aggregator.

  1. {
  2. "type": "stddev",
  3. "name": "<output_name>",
  4. "fieldName": "<aggregator_name>",
  5. "estimator": <string>
  6. }

Query examples:

Timeseries query

  1. {
  2. "queryType": "timeseries",
  3. "dataSource": "testing",
  4. "granularity": "day",
  5. "aggregations": [
  6. {
  7. "type": "variance",
  8. "name": "index_var",
  9. "fieldName": "index_var"
  10. }
  11. ],
  12. "intervals": [
  13. "2016-03-01T00:00:00.000/2013-03-20T00:00:00.000"
  14. ]
  15. }

TopN query

  1. {
  2. "queryType": "topN",
  3. "dataSource": "testing",
  4. "dimensions": ["alias"],
  5. "threshold": 5,
  6. "granularity": "all",
  7. "aggregations": [
  8. {
  9. "type": "variance",
  10. "name": "index_var",
  11. "fieldName": "index"
  12. }
  13. ],
  14. "postAggregations": [
  15. {
  16. "type": "stddev",
  17. "name": "index_stddev",
  18. "fieldName": "index_var"
  19. }
  20. ],
  21. "intervals": [
  22. "2016-03-06T00:00:00/2016-03-06T23:59:59"
  23. ]
  24. }

GroupBy query

  1. {
  2. "queryType": "groupBy",
  3. "dataSource": "testing",
  4. "dimensions": ["alias"],
  5. "granularity": "all",
  6. "aggregations": [
  7. {
  8. "type": "variance",
  9. "name": "index_var",
  10. "fieldName": "index"
  11. }
  12. ],
  13. "postAggregations": [
  14. {
  15. "type": "stddev",
  16. "name": "index_stddev",
  17. "fieldName": "index_var"
  18. }
  19. ],
  20. "intervals": [
  21. "2016-03-06T00:00:00/2016-03-06T23:59:59"
  22. ]
  23. }