t-test-aggregation.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[search-aggregations-metrics-ttest-aggregation]]
  4. === TTest 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. ==== Script
  65. The `t_test` metric supports scripting. For example, if we need to adjust out load times for the before values, we could use
  66. a script to recalculate them on-the-fly:
  67. [source,console]
  68. --------------------------------------------------
  69. GET node_upgrade/_search
  70. {
  71. "size": 0,
  72. "aggs" : {
  73. "startup_time_ttest" : {
  74. "t_test" : {
  75. "a": {
  76. "script" : {
  77. "lang": "painless",
  78. "source": "doc['startup_time_before'].value - params.adjustment", <1>
  79. "params" : {
  80. "adjustment" : 10 <2>
  81. }
  82. }
  83. },
  84. "b": {
  85. "field": "startup_time_after" <3>
  86. },
  87. "type": "paired"
  88. }
  89. }
  90. }
  91. }
  92. --------------------------------------------------
  93. // TEST[setup:node_upgrade]
  94. <1> The `field` parameter is replaced with a `script` parameter, which uses the
  95. script to generate values which percentiles are calculated on
  96. <2> Scripting supports parameterized input just like any other script
  97. <3> We can mix scripts and fields