t-test-aggregation.asciidoc 5.5 KB

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