t-test-aggregation.asciidoc 6.1 KB

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