1
0

serial-diff-aggregation.asciidoc 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. [[search-aggregations-pipeline-serialdiff-aggregation]]
  2. === Serial Differencing Aggregation
  3. Serial differencing is a technique where values in a time series are subtracted from itself at
  4. different time lags or periods. For example, the datapoint f(x) = f(x~t~) - f(x~t-n~), where n is the period being used.
  5. A period of 1 is equivalent to a derivative with no time normalization: it is simply the change from one point to the
  6. next. Single periods are useful for removing constant, linear trends.
  7. Single periods are also useful for transforming data into a stationary series. In this example, the Dow Jones is
  8. plotted over ~250 days. The raw data is not stationary, which would make it difficult to use with some techniques.
  9. By calculating the first-difference, we de-trend the data (e.g. remove a constant, linear trend). We can see that the
  10. data becomes a stationary series (e.g. the first difference is randomly distributed around zero, and doesn't seem to
  11. exhibit any pattern/behavior). The transformation reveals that the dataset is following a random-walk; the value is the
  12. previous value +/- a random amount. This insight allows selection of further tools for analysis.
  13. [[serialdiff_dow]]
  14. .Dow Jones plotted and made stationary with first-differencing
  15. image::images/pipeline_serialdiff/dow.png[]
  16. Larger periods can be used to remove seasonal / cyclic behavior. In this example, a population of lemmings was
  17. synthetically generated with a sine wave + constant linear trend + random noise. The sine wave has a period of 30 days.
  18. The first-difference removes the constant trend, leaving just a sine wave. The 30th-difference is then applied to the
  19. first-difference to remove the cyclic behavior, leaving a stationary series which is amenable to other analysis.
  20. [[serialdiff_lemmings]]
  21. .Lemmings data plotted made stationary with 1st and 30th difference
  22. image::images/pipeline_serialdiff/lemmings.png[]
  23. ==== Syntax
  24. A `serial_diff` aggregation looks like this in isolation:
  25. [source,js]
  26. --------------------------------------------------
  27. {
  28. "serial_diff": {
  29. "buckets_path": "the_sum",
  30. "lag": "7"
  31. }
  32. }
  33. --------------------------------------------------
  34. // NOTCONSOLE
  35. .`serial_diff` Parameters
  36. |===
  37. |Parameter Name |Description |Required |Default Value
  38. |`buckets_path` |Path to the metric of interest (see <<buckets-path-syntax, `buckets_path` Syntax>> for more details |Required |
  39. |`lag` |The historical bucket to subtract from the current value. E.g. a lag of 7 will subtract the current value from
  40. the value 7 buckets ago. Must be a positive, non-zero integer |Optional |`1`
  41. |`gap_policy` |Determines what should happen when a gap in the data is encountered. |Optional |`insert_zero`
  42. |`format` |Format to apply to the output value of this aggregation |Optional | `null`
  43. |===
  44. `serial_diff` aggregations must be embedded inside of a `histogram` or `date_histogram` aggregation:
  45. [source,js]
  46. --------------------------------------------------
  47. POST /_search
  48. {
  49. "size": 0,
  50. "aggs": {
  51. "my_date_histo": { <1>
  52. "date_histogram": {
  53. "field": "timestamp",
  54. "interval": "day"
  55. },
  56. "aggs": {
  57. "the_sum": {
  58. "sum": {
  59. "field": "lemmings" <2>
  60. }
  61. },
  62. "thirtieth_difference": {
  63. "serial_diff": { <3>
  64. "buckets_path": "the_sum",
  65. "lag" : 30
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. --------------------------------------------------
  73. // CONSOLE
  74. <1> A `date_histogram` named "my_date_histo" is constructed on the "timestamp" field, with one-day intervals
  75. <2> A `sum` metric is used to calculate the sum of a field. This could be any metric (sum, min, max, etc)
  76. <3> Finally, we specify a `serial_diff` aggregation which uses "the_sum" metric as its input.
  77. Serial differences are built by first specifying a `histogram` or `date_histogram` over a field. You can then optionally
  78. add normal metrics, such as a `sum`, inside of that histogram. Finally, the `serial_diff` is embedded inside the histogram.
  79. The `buckets_path` parameter is then used to "point" at one of the sibling metrics inside of the histogram (see
  80. <<buckets-path-syntax>> for a description of the syntax for `buckets_path`.