t-test-aggregation.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. [role="xpack"]
  2. [[search-aggregations-metrics-ttest-aggregation]]
  3. === T-test aggregation
  4. ++++
  5. <titleabbrev>T-test</titleabbrev>
  6. ++++
  7. A `t_test` metrics aggregation that performs a statistical hypothesis test in which the test statistic follows a Student's t-distribution
  8. under the null hypothesis on numeric values extracted from the aggregated documents. In practice, this
  9. will tell you if the difference between two population means are statistically significant and did not occur by chance alone.
  10. ==== Syntax
  11. A `t_test` aggregation looks like this in isolation:
  12. [source,js]
  13. --------------------------------------------------
  14. {
  15. "t_test": {
  16. "a": "value_before",
  17. "b": "value_after",
  18. "type": "paired"
  19. }
  20. }
  21. --------------------------------------------------
  22. // NOTCONSOLE
  23. Assuming that we have a record of node start up times before and after upgrade, let's look at a t-test to see if upgrade affected
  24. the node start up time in a meaningful way.
  25. [source,console]
  26. --------------------------------------------------
  27. GET node_upgrade/_search
  28. {
  29. "size": 0,
  30. "aggs": {
  31. "startup_time_ttest": {
  32. "t_test": {
  33. "a": { "field": "startup_time_before" }, <1>
  34. "b": { "field": "startup_time_after" }, <2>
  35. "type": "paired" <3>
  36. }
  37. }
  38. }
  39. }
  40. --------------------------------------------------
  41. // TEST[setup:node_upgrade]
  42. <1> The field `startup_time_before` must be a numeric field.
  43. <2> The field `startup_time_after` must be a numeric field.
  44. <3> Since we have data from the same nodes, we are using paired t-test.
  45. The response will return the p-value or probability value for the test. It is the probability of obtaining results at least as extreme as
  46. the result processed by the aggregation, assuming that the null hypothesis is correct (which means there is no difference between
  47. population means). Smaller p-value means the null hypothesis is more likely to be incorrect and population means are indeed different.
  48. [source,console-result]
  49. --------------------------------------------------
  50. {
  51. ...
  52. "aggregations": {
  53. "startup_time_ttest": {
  54. "value": 0.1914368843365979 <1>
  55. }
  56. }
  57. }
  58. --------------------------------------------------
  59. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  60. <1> The p-value.
  61. ==== T-Test Types
  62. The `t_test` aggregation supports unpaired and paired two-sample t-tests. The type of the test can be specified using the `type` parameter:
  63. `"type": "paired"`:: performs paired t-test
  64. `"type": "homoscedastic"`:: performs two-sample equal variance test
  65. `"type": "heteroscedastic"`:: performs two-sample unequal variance test (this is default)
  66. ==== Filters
  67. It is also possible to run unpaired t-test on different sets of records using filters. For example, if we want to test the difference
  68. of startup times before upgrade between two different groups of nodes, we use the same field `startup_time_before` by separate groups of
  69. nodes using terms filters on the group name field:
  70. [source,console]
  71. --------------------------------------------------
  72. GET node_upgrade/_search
  73. {
  74. "size": 0,
  75. "aggs": {
  76. "startup_time_ttest": {
  77. "t_test": {
  78. "a": {
  79. "field": "startup_time_before", <1>
  80. "filter": {
  81. "term": {
  82. "group": "A" <2>
  83. }
  84. }
  85. },
  86. "b": {
  87. "field": "startup_time_before", <3>
  88. "filter": {
  89. "term": {
  90. "group": "B" <4>
  91. }
  92. }
  93. },
  94. "type": "heteroscedastic" <5>
  95. }
  96. }
  97. }
  98. }
  99. --------------------------------------------------
  100. // TEST[setup:node_upgrade]
  101. <1> The field `startup_time_before` must be a numeric field.
  102. <2> Any query that separates two groups can be used here.
  103. <3> We are using the same field
  104. <4> but we are using different filters.
  105. <5> Since we have data from different nodes, we cannot use paired t-test.
  106. [source,console-result]
  107. --------------------------------------------------
  108. {
  109. ...
  110. "aggregations": {
  111. "startup_time_ttest": {
  112. "value": 0.2981858007281437 <1>
  113. }
  114. }
  115. }
  116. --------------------------------------------------
  117. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  118. <1> The p-value.
  119. Populations don't have to be in the same index. If data sets are located in different
  120. indices, the term filter on the <<mapping-index-field,`_index`>> field can be used to select populations.
  121. ==== Script
  122. If you need to run the `t_test` on values that aren't represented cleanly
  123. by a field you should, run the aggregation on a <<runtime,runtime field>>.
  124. For example, if you want to adjust out load times for the before values:
  125. [source,console]
  126. ----
  127. GET node_upgrade/_search
  128. {
  129. "size": 0,
  130. "runtime_mappings": {
  131. "startup_time_before.adjusted": {
  132. "type": "long",
  133. "script": {
  134. "source": "emit(doc['startup_time_before'].value - params.adjustment)",
  135. "params": {
  136. "adjustment": 10
  137. }
  138. }
  139. }
  140. },
  141. "aggs": {
  142. "startup_time_ttest": {
  143. "t_test": {
  144. "a": {
  145. "field": "startup_time_before.adjusted"
  146. },
  147. "b": {
  148. "field": "startup_time_after"
  149. },
  150. "type": "paired"
  151. }
  152. }
  153. }
  154. }
  155. ----
  156. // TEST[setup:node_upgrade]
  157. // TEST[s/_search/_search?filter_path=aggregations/]
  158. ////
  159. [source,console-result]
  160. ----
  161. {
  162. "aggregations": {
  163. "startup_time_ttest": {
  164. "value": 0.9397399375119482
  165. }
  166. }
  167. }
  168. ----
  169. ////