bucket-count-ks-test-aggregation.asciidoc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. [role="xpack"]
  2. [[search-aggregations-bucket-count-ks-test-aggregation]]
  3. === Bucket count K-S test correlation aggregation
  4. ++++
  5. <titleabbrev>Bucket count K-S test</titleabbrev>
  6. ++++
  7. experimental::[]
  8. A sibling pipeline aggregation which executes a two sample Kolmogorov–Smirnov test
  9. (referred to as a "K-S test" from now on) against a provided distribution, and the
  10. distribution implied by the documents counts in the configured sibling aggregation.
  11. Specifically, for some metric, assuming that the percentile intervals of the metric are
  12. known beforehand or have been computed by an aggregation, then one would use range
  13. aggregation for the sibling to compute the p-value of the distribution difference between
  14. the metric and the restriction of that metric to a subset of the documents. A natural use
  15. case is if the sibling aggregation range aggregation nested in a terms aggregation, in
  16. which case one compares the overall distribution of metric to its restriction to each term.
  17. [[bucket-count-ks-test-agg-syntax]]
  18. ==== Parameters
  19. `buckets_path`::
  20. (Required, string)
  21. Path to the buckets that contain one set of values to correlate. Must be a `_count` path
  22. For syntax, see <<buckets-path-syntax>>.
  23. `alternative`::
  24. (Optional, list)
  25. A list of string values indicating which K-S test alternative to calculate.
  26. The valid values are: "greater", "less", "two_sided". This parameter is key for
  27. determining the K-S statistic used when calculating the K-S test. Default value is
  28. all possible alternative hypotheses.
  29. `fractions`::
  30. (Optional, list)
  31. A list of doubles indicating the distribution of the samples with which to compare to the
  32. `buckets_path` results. In typical usage this is the overall proportion of documents in
  33. each bucket, which is compared with the actual document proportions in each bucket
  34. from the sibling aggregation counts. The default is to assume that overall documents
  35. are uniformly distributed on these buckets, which they would be if one used equal
  36. percentiles of a metric to define the bucket end points.
  37. `sampling_method`::
  38. (Optional, string)
  39. Indicates the sampling methodology when calculating the K-S test. Note, this is sampling
  40. of the returned values. This determines the cumulative distribution function (CDF) points
  41. used comparing the two samples. Default is `upper_tail`, which emphasizes the upper
  42. end of the CDF points. Valid options are: `upper_tail`, `uniform`, and `lower_tail`.
  43. ==== Syntax
  44. A `bucket_count_ks_test` aggregation looks like this in isolation:
  45. [source,js]
  46. --------------------------------------------------
  47. {
  48. "bucket_count_ks_test": {
  49. "buckets_path": "range_values>_count", <1>
  50. "alternative": ["less", "greater", "two_sided"], <2>
  51. "sampling_method": "upper_tail" <3>
  52. }
  53. }
  54. --------------------------------------------------
  55. // NOTCONSOLE
  56. <1> The buckets containing the values to test against.
  57. <2> The alternatives to calculate.
  58. <3> The sampling method for the K-S statistic.
  59. [[bucket-count-ks-test-agg-example]]
  60. ==== Example
  61. The following snippet runs the `bucket_count_ks_test` on the individual terms in the field `version` against a uniform distribution.
  62. The uniform distribution reflects the `latency` percentile buckets. Not shown is the pre-calculation of the `latency` indicator values,
  63. which was done utilizing the
  64. <<search-aggregations-metrics-percentile-aggregation,percentiles>> aggregation.
  65. This example is only using the deciles of `latency`.
  66. [source,console]
  67. -------------------------------------------------
  68. POST correlate_latency/_search?size=0&filter_path=aggregations
  69. {
  70. "aggs": {
  71. "buckets": {
  72. "terms": { <1>
  73. "field": "version",
  74. "size": 2
  75. },
  76. "aggs": {
  77. "latency_ranges": {
  78. "range": { <2>
  79. "field": "latency",
  80. "ranges": [
  81. { "to": 0 },
  82. { "from": 0, "to": 105 },
  83. { "from": 105, "to": 225 },
  84. { "from": 225, "to": 445 },
  85. { "from": 445, "to": 665 },
  86. { "from": 665, "to": 885 },
  87. { "from": 885, "to": 1115 },
  88. { "from": 1115, "to": 1335 },
  89. { "from": 1335, "to": 1555 },
  90. { "from": 1555, "to": 1775 },
  91. { "from": 1775 }
  92. ]
  93. }
  94. },
  95. "ks_test": { <3>
  96. "bucket_count_ks_test": {
  97. "buckets_path": "latency_ranges>_count",
  98. "alternative": ["less", "greater", "two_sided"]
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. -------------------------------------------------
  106. // TEST[setup:correlate_latency]
  107. <1> The term buckets containing a range aggregation and the bucket correlation aggregation. Both are utilized to calculate
  108. the correlation of the term values with the latency.
  109. <2> The range aggregation on the latency field. The ranges were created referencing the percentiles of the latency field.
  110. <3> The bucket count K-S test aggregation that tests if the bucket counts comes from the same distribution as `fractions`;
  111. where `fractions` is a uniform distribution.
  112. And the following may be the response:
  113. [source,console-result]
  114. ----
  115. {
  116. "aggregations" : {
  117. "buckets" : {
  118. "doc_count_error_upper_bound" : 0,
  119. "sum_other_doc_count" : 0,
  120. "buckets" : [
  121. {
  122. "key" : "1.0",
  123. "doc_count" : 100,
  124. "latency_ranges" : {
  125. "buckets" : [
  126. {
  127. "key" : "*-0.0",
  128. "to" : 0.0,
  129. "doc_count" : 0
  130. },
  131. {
  132. "key" : "0.0-105.0",
  133. "from" : 0.0,
  134. "to" : 105.0,
  135. "doc_count" : 1
  136. },
  137. {
  138. "key" : "105.0-225.0",
  139. "from" : 105.0,
  140. "to" : 225.0,
  141. "doc_count" : 9
  142. },
  143. {
  144. "key" : "225.0-445.0",
  145. "from" : 225.0,
  146. "to" : 445.0,
  147. "doc_count" : 0
  148. },
  149. {
  150. "key" : "445.0-665.0",
  151. "from" : 445.0,
  152. "to" : 665.0,
  153. "doc_count" : 0
  154. },
  155. {
  156. "key" : "665.0-885.0",
  157. "from" : 665.0,
  158. "to" : 885.0,
  159. "doc_count" : 0
  160. },
  161. {
  162. "key" : "885.0-1115.0",
  163. "from" : 885.0,
  164. "to" : 1115.0,
  165. "doc_count" : 10
  166. },
  167. {
  168. "key" : "1115.0-1335.0",
  169. "from" : 1115.0,
  170. "to" : 1335.0,
  171. "doc_count" : 20
  172. },
  173. {
  174. "key" : "1335.0-1555.0",
  175. "from" : 1335.0,
  176. "to" : 1555.0,
  177. "doc_count" : 20
  178. },
  179. {
  180. "key" : "1555.0-1775.0",
  181. "from" : 1555.0,
  182. "to" : 1775.0,
  183. "doc_count" : 20
  184. },
  185. {
  186. "key" : "1775.0-*",
  187. "from" : 1775.0,
  188. "doc_count" : 20
  189. }
  190. ]
  191. },
  192. "ks_test" : {
  193. "less" : 2.248673241788478E-4,
  194. "greater" : 1.0,
  195. "two_sided" : 5.791639181800257E-4
  196. }
  197. },
  198. {
  199. "key" : "2.0",
  200. "doc_count" : 100,
  201. "latency_ranges" : {
  202. "buckets" : [
  203. {
  204. "key" : "*-0.0",
  205. "to" : 0.0,
  206. "doc_count" : 0
  207. },
  208. {
  209. "key" : "0.0-105.0",
  210. "from" : 0.0,
  211. "to" : 105.0,
  212. "doc_count" : 19
  213. },
  214. {
  215. "key" : "105.0-225.0",
  216. "from" : 105.0,
  217. "to" : 225.0,
  218. "doc_count" : 11
  219. },
  220. {
  221. "key" : "225.0-445.0",
  222. "from" : 225.0,
  223. "to" : 445.0,
  224. "doc_count" : 20
  225. },
  226. {
  227. "key" : "445.0-665.0",
  228. "from" : 445.0,
  229. "to" : 665.0,
  230. "doc_count" : 20
  231. },
  232. {
  233. "key" : "665.0-885.0",
  234. "from" : 665.0,
  235. "to" : 885.0,
  236. "doc_count" : 20
  237. },
  238. {
  239. "key" : "885.0-1115.0",
  240. "from" : 885.0,
  241. "to" : 1115.0,
  242. "doc_count" : 10
  243. },
  244. {
  245. "key" : "1115.0-1335.0",
  246. "from" : 1115.0,
  247. "to" : 1335.0,
  248. "doc_count" : 0
  249. },
  250. {
  251. "key" : "1335.0-1555.0",
  252. "from" : 1335.0,
  253. "to" : 1555.0,
  254. "doc_count" : 0
  255. },
  256. {
  257. "key" : "1555.0-1775.0",
  258. "from" : 1555.0,
  259. "to" : 1775.0,
  260. "doc_count" : 0
  261. },
  262. {
  263. "key" : "1775.0-*",
  264. "from" : 1775.0,
  265. "doc_count" : 0
  266. }
  267. ]
  268. },
  269. "ks_test" : {
  270. "less" : 0.9642895789647244,
  271. "greater" : 4.58718174664754E-9,
  272. "two_sided" : 5.916656831139733E-9
  273. }
  274. }
  275. ]
  276. }
  277. }
  278. }
  279. ----