median-absolute-deviation-aggregation.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. [[search-aggregations-metrics-median-absolute-deviation-aggregation]]
  2. === Median Absolute Deviation Aggregation
  3. This `single-value` aggregation approximates the https://en.wikipedia.org/wiki/Median_absolute_deviation[median absolute deviation]
  4. of its search results.
  5. Median absolute deviation is a measure of variability. It is a robust
  6. statistic, meaning that it is useful for describing data that may have
  7. outliers, or may not be normally distributed. For such data it can be more
  8. descriptive than standard deviation.
  9. It is calculated as the median of each data point's deviation from the median
  10. of the entire sample. That is, for a random variable X, the median absolute
  11. deviation is median(|median(X) - X~i~|).
  12. ==== Example
  13. Assume our data represents product reviews on a one to five star scale.
  14. Such reviews are usually summarized as a mean, which is easily understandable
  15. but doesn't describe the reviews' variability. Estimating the median absolute
  16. deviation can provide insight into how much reviews vary from one another.
  17. In this example we have a product which has an average rating of
  18. 3 stars. Let's look at its ratings' median absolute deviation to determine
  19. how much they vary
  20. [source,console]
  21. ---------------------------------------------------------
  22. GET reviews/_search
  23. {
  24. "size": 0,
  25. "aggs": {
  26. "review_average": {
  27. "avg": {
  28. "field": "rating"
  29. }
  30. },
  31. "review_variability": {
  32. "median_absolute_deviation": {
  33. "field": "rating" <1>
  34. }
  35. }
  36. }
  37. }
  38. ---------------------------------------------------------
  39. // TEST[setup:reviews]
  40. <1> `rating` must be a numeric field
  41. The resulting median absolute deviation of `2` tells us that there is a fair
  42. amount of variability in the ratings. Reviewers must have diverse opinions about
  43. this product.
  44. [source,js]
  45. ---------------------------------------------------------
  46. {
  47. ...
  48. "aggregations": {
  49. "review_average": {
  50. "value": 3.0
  51. },
  52. "review_variability": {
  53. "value": 2.0
  54. }
  55. }
  56. }
  57. ---------------------------------------------------------
  58. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  59. ==== Approximation
  60. The naive implementation of calculating median absolute deviation stores the
  61. entire sample in memory, so this aggregation instead calculates an
  62. approximation. It uses the https://github.com/tdunning/t-digest[TDigest data structure]
  63. to approximate the sample median and the median of deviations from the sample
  64. median. For more about the approximation characteristics of TDigests, see
  65. <<search-aggregations-metrics-percentile-aggregation-approximation>>.
  66. The tradeoff between resource usage and accuracy of a TDigest's quantile
  67. approximation, and therefore the accuracy of this aggregation's approximation
  68. of median absolute deviation, is controlled by the `compression` parameter. A
  69. higher `compression` setting provides a more accurate approximation at the
  70. cost of higher memory usage. For more about the characteristics of the TDigest
  71. `compression` parameter see
  72. <<search-aggregations-metrics-percentile-aggregation-compression>>.
  73. [source,console]
  74. ---------------------------------------------------------
  75. GET reviews/_search
  76. {
  77. "size": 0,
  78. "aggs": {
  79. "review_variability": {
  80. "median_absolute_deviation": {
  81. "field": "rating",
  82. "compression": 100
  83. }
  84. }
  85. }
  86. }
  87. ---------------------------------------------------------
  88. // TEST[setup:reviews]
  89. The default `compression` value for this aggregation is `1000`. At this
  90. compression level this aggregation is usually within 5% of the exact result,
  91. but observed performance will depend on the sample data.
  92. ==== Script
  93. This metric aggregation supports scripting. In our example above, product
  94. reviews are on a scale of one to five. If we wanted to modify them to a scale
  95. of one to ten, we can using scripting.
  96. To provide an inline script:
  97. [source,console]
  98. ---------------------------------------------------------
  99. GET reviews/_search
  100. {
  101. "size": 0,
  102. "aggs": {
  103. "review_variability": {
  104. "median_absolute_deviation": {
  105. "script": {
  106. "lang": "painless",
  107. "source": "doc['rating'].value * params.scaleFactor",
  108. "params": {
  109. "scaleFactor": 2
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. ---------------------------------------------------------
  117. // TEST[setup:reviews]
  118. To provide a stored script:
  119. [source,console]
  120. ---------------------------------------------------------
  121. GET reviews/_search
  122. {
  123. "size": 0,
  124. "aggs": {
  125. "review_variability": {
  126. "median_absolute_deviation": {
  127. "script": {
  128. "id": "my_script",
  129. "params": {
  130. "field": "rating"
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. ---------------------------------------------------------
  138. // TEST[setup:reviews,stored_example_script]
  139. ==== Missing value
  140. The `missing` parameter defines how documents that are missing a value should be
  141. treated. By default they will be ignored but it is also possible to treat them
  142. as if they had a value.
  143. Let's be optimistic and assume some reviewers loved the product so much that
  144. they forgot to give it a rating. We'll assign them five stars
  145. [source,console]
  146. ---------------------------------------------------------
  147. GET reviews/_search
  148. {
  149. "size": 0,
  150. "aggs": {
  151. "review_variability": {
  152. "median_absolute_deviation": {
  153. "field": "rating",
  154. "missing": 5
  155. }
  156. }
  157. }
  158. }
  159. ---------------------------------------------------------
  160. // TEST[setup:reviews]