median-absolute-deviation-aggregation.asciidoc 5.5 KB

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