movfn-aggregation.asciidoc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. [[search-aggregations-pipeline-movfn-aggregation]]
  2. === Moving Function Aggregation
  3. Given an ordered series of data, the Moving Function aggregation will slide a window across the data and allow the user to specify a custom
  4. script that is executed on each window of data. For convenience, a number of common functions are predefined such as min/max, moving averages,
  5. etc.
  6. This is conceptually very similar to the <<search-aggregations-pipeline-movavg-aggregation, Moving Average>> pipeline aggregation, except
  7. it provides more functionality.
  8. ==== Syntax
  9. A `moving_fn` aggregation looks like this in isolation:
  10. [source,js]
  11. --------------------------------------------------
  12. {
  13. "moving_fn": {
  14. "buckets_path": "the_sum",
  15. "window": 10,
  16. "script": "MovingFunctions.min(values)"
  17. }
  18. }
  19. --------------------------------------------------
  20. // NOTCONSOLE
  21. .`moving_avg` Parameters
  22. |===
  23. |Parameter Name |Description |Required |Default Value
  24. |`buckets_path` |Path to the metric of interest (see <<buckets-path-syntax, `buckets_path` Syntax>> for more details |Required |
  25. |`window` |The size of window to "slide" across the histogram. |Required |
  26. |`script` |The script that should be executed on each window of data |Required |
  27. |===
  28. `moving_fn` aggregations must be embedded inside of a `histogram` or `date_histogram` aggregation. They can be
  29. embedded like any other metric aggregation:
  30. [source,js]
  31. --------------------------------------------------
  32. POST /_search
  33. {
  34. "size": 0,
  35. "aggs": {
  36. "my_date_histo":{ <1>
  37. "date_histogram":{
  38. "field":"date",
  39. "interval":"1M"
  40. },
  41. "aggs":{
  42. "the_sum":{
  43. "sum":{ "field": "price" } <2>
  44. },
  45. "the_movfn": {
  46. "moving_fn": {
  47. "buckets_path": "the_sum", <3>
  48. "window": 10,
  49. "script": "MovingFunctions.unweightedAvg(values)"
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. --------------------------------------------------
  57. // CONSOLE
  58. // TEST[setup:sales]
  59. <1> A `date_histogram` named "my_date_histo" is constructed on the "timestamp" field, with one-day intervals
  60. <2> A `sum` metric is used to calculate the sum of a field. This could be any numeric metric (sum, min, max, etc)
  61. <3> Finally, we specify a `moving_fn` aggregation which uses "the_sum" metric as its input.
  62. Moving averages are built by first specifying a `histogram` or `date_histogram` over a field. You can then optionally
  63. add numeric metrics, such as a `sum`, inside of that histogram. Finally, the `moving_fn` is embedded inside the histogram.
  64. The `buckets_path` parameter is then used to "point" at one of the sibling metrics inside of the histogram (see
  65. <<buckets-path-syntax>> for a description of the syntax for `buckets_path`.
  66. An example response from the above aggregation may look like:
  67. [source,js]
  68. --------------------------------------------------
  69. {
  70. "took": 11,
  71. "timed_out": false,
  72. "_shards": ...,
  73. "hits": ...,
  74. "aggregations": {
  75. "my_date_histo": {
  76. "buckets": [
  77. {
  78. "key_as_string": "2015/01/01 00:00:00",
  79. "key": 1420070400000,
  80. "doc_count": 3,
  81. "the_sum": {
  82. "value": 550.0
  83. },
  84. "the_movfn": {
  85. "value": null
  86. }
  87. },
  88. {
  89. "key_as_string": "2015/02/01 00:00:00",
  90. "key": 1422748800000,
  91. "doc_count": 2,
  92. "the_sum": {
  93. "value": 60.0
  94. },
  95. "the_movfn": {
  96. "value": 550.0
  97. }
  98. },
  99. {
  100. "key_as_string": "2015/03/01 00:00:00",
  101. "key": 1425168000000,
  102. "doc_count": 2,
  103. "the_sum": {
  104. "value": 375.0
  105. },
  106. "the_movfn": {
  107. "value": 305.0
  108. }
  109. }
  110. ]
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  116. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  117. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  118. ==== Custom user scripting
  119. The Moving Function aggregation allows the user to specify any arbitrary script to define custom logic. The script is invoked each time a
  120. new window of data is collected. These values are provided to the script in the `values` variable. The script should then perform some
  121. kind of calculation and emit a single `double` as the result. Emitting `null` is not permitted, although `NaN` and +/- `Inf` are allowed.
  122. For example, this script will simply return the first value from the window, or `NaN` if no values are available:
  123. [source,js]
  124. --------------------------------------------------
  125. POST /_search
  126. {
  127. "size": 0,
  128. "aggs": {
  129. "my_date_histo":{
  130. "date_histogram":{
  131. "field":"date",
  132. "interval":"1M"
  133. },
  134. "aggs":{
  135. "the_sum":{
  136. "sum":{ "field": "price" }
  137. },
  138. "the_movavg": {
  139. "moving_fn": {
  140. "buckets_path": "the_sum",
  141. "window": 10,
  142. "script": "return values.length > 0 ? values[0] : Double.NaN"
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. --------------------------------------------------
  150. // CONSOLE
  151. // TEST[setup:sales]
  152. ==== Pre-built Functions
  153. For convenience, a number of functions have been prebuilt and are available inside the `moving_fn` script context:
  154. - `max()`
  155. - `min()`
  156. - `sum()`
  157. - `stdDev()`
  158. - `unweightedAvg()`
  159. - `linearWeightedAvg()`
  160. - `ewma()`
  161. - `holt()`
  162. - `holtWinters()`
  163. The functions are available from the `MovingFunctions` namespace. E.g. `MovingFunctions.max()`
  164. ===== max Function
  165. This function accepts a collection of doubles and returns the maximum value in that window. `null` and `NaN` values are ignored; the maximum
  166. is only calculated over the real values. If the window is empty, or all values are `null`/`NaN`, `NaN` is returned as the result.
  167. .`max(double[] values)` Parameters
  168. |===
  169. |Parameter Name |Description
  170. |`values` |The window of values to find the maximum
  171. |===
  172. [source,js]
  173. --------------------------------------------------
  174. POST /_search
  175. {
  176. "size": 0,
  177. "aggs": {
  178. "my_date_histo":{
  179. "date_histogram":{
  180. "field":"date",
  181. "interval":"1M"
  182. },
  183. "aggs":{
  184. "the_sum":{
  185. "sum":{ "field": "price" }
  186. },
  187. "the_moving_max": {
  188. "moving_fn": {
  189. "buckets_path": "the_sum",
  190. "window": 10,
  191. "script": "MovingFunctions.max(values)"
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. --------------------------------------------------
  199. // CONSOLE
  200. // TEST[setup:sales]
  201. ===== min Function
  202. This function accepts a collection of doubles and returns the minimum value in that window. `null` and `NaN` values are ignored; the minimum
  203. is only calculated over the real values. If the window is empty, or all values are `null`/`NaN`, `NaN` is returned as the result.
  204. .`min(double[] values)` Parameters
  205. |===
  206. |Parameter Name |Description
  207. |`values` |The window of values to find the minimum
  208. |===
  209. [source,js]
  210. --------------------------------------------------
  211. POST /_search
  212. {
  213. "size": 0,
  214. "aggs": {
  215. "my_date_histo":{
  216. "date_histogram":{
  217. "field":"date",
  218. "interval":"1M"
  219. },
  220. "aggs":{
  221. "the_sum":{
  222. "sum":{ "field": "price" }
  223. },
  224. "the_moving_min": {
  225. "moving_fn": {
  226. "buckets_path": "the_sum",
  227. "window": 10,
  228. "script": "MovingFunctions.min(values)"
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. --------------------------------------------------
  236. // CONSOLE
  237. // TEST[setup:sales]
  238. ===== sum Function
  239. This function accepts a collection of doubles and returns the sum of the values in that window. `null` and `NaN` values are ignored;
  240. the sum is only calculated over the real values. If the window is empty, or all values are `null`/`NaN`, `0.0` is returned as the result.
  241. .`sum(double[] values)` Parameters
  242. |===
  243. |Parameter Name |Description
  244. |`values` |The window of values to find the sum of
  245. |===
  246. [source,js]
  247. --------------------------------------------------
  248. POST /_search
  249. {
  250. "size": 0,
  251. "aggs": {
  252. "my_date_histo":{
  253. "date_histogram":{
  254. "field":"date",
  255. "interval":"1M"
  256. },
  257. "aggs":{
  258. "the_sum":{
  259. "sum":{ "field": "price" }
  260. },
  261. "the_moving_sum": {
  262. "moving_fn": {
  263. "buckets_path": "the_sum",
  264. "window": 10,
  265. "script": "MovingFunctions.sum(values)"
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. --------------------------------------------------
  273. // CONSOLE
  274. // TEST[setup:sales]
  275. ===== stdDev Function
  276. This function accepts a collection of doubles and average, then returns the standard deviation of the values in that window.
  277. `null` and `NaN` values are ignored; the sum is only calculated over the real values. If the window is empty, or all values are
  278. `null`/`NaN`, `0.0` is returned as the result.
  279. .`stdDev(double[] values)` Parameters
  280. |===
  281. |Parameter Name |Description
  282. |`values` |The window of values to find the standard deviation of
  283. |`avg` |The average of the window
  284. |===
  285. [source,js]
  286. --------------------------------------------------
  287. POST /_search
  288. {
  289. "size": 0,
  290. "aggs": {
  291. "my_date_histo":{
  292. "date_histogram":{
  293. "field":"date",
  294. "interval":"1M"
  295. },
  296. "aggs":{
  297. "the_sum":{
  298. "sum":{ "field": "price" }
  299. },
  300. "the_moving_sum": {
  301. "moving_fn": {
  302. "buckets_path": "the_sum",
  303. "window": 10,
  304. "script": "MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. --------------------------------------------------
  312. // CONSOLE
  313. // TEST[setup:sales]
  314. The `avg` parameter must be provided to the standard deviation function because different styles of averages can be computed on the window
  315. (simple, linearly weighted, etc). The various moving averages that are detailed below can be used to calculate the average for the
  316. standard deviation function.
  317. ===== unweightedAvg Function
  318. The `unweightedAvg` function calculates the sum of all values in the window, then divides by the size of the window. It is effectively
  319. a simple arithmetic mean of the window. The simple moving average does not perform any time-dependent weighting, which means
  320. the values from a `simple` moving average tend to "lag" behind the real data.
  321. `null` and `NaN` values are ignored; the average is only calculated over the real values. If the window is empty, or all values are
  322. `null`/`NaN`, `NaN` is returned as the result. This means that the count used in the average calculation is count of non-`null`,non-`NaN`
  323. values.
  324. .`unweightedAvg(double[] values)` Parameters
  325. |===
  326. |Parameter Name |Description
  327. |`values` |The window of values to find the sum of
  328. |===
  329. [source,js]
  330. --------------------------------------------------
  331. POST /_search
  332. {
  333. "size": 0,
  334. "aggs": {
  335. "my_date_histo":{
  336. "date_histogram":{
  337. "field":"date",
  338. "interval":"1M"
  339. },
  340. "aggs":{
  341. "the_sum":{
  342. "sum":{ "field": "price" }
  343. },
  344. "the_movavg": {
  345. "moving_fn": {
  346. "buckets_path": "the_sum",
  347. "window": 10,
  348. "script": "MovingFunctions.unweightedAvg(values)"
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355. --------------------------------------------------
  356. // CONSOLE
  357. // TEST[setup:sales]
  358. ==== linearWeightedAvg Function
  359. The `linearWeightedAvg` function assigns a linear weighting to points in the series, such that "older" datapoints (e.g. those at
  360. the beginning of the window) contribute a linearly less amount to the total average. The linear weighting helps reduce
  361. the "lag" behind the data's mean, since older points have less influence.
  362. If the window is empty, or all values are `null`/`NaN`, `NaN` is returned as the result.
  363. .`linearWeightedAvg(double[] values)` Parameters
  364. |===
  365. |Parameter Name |Description
  366. |`values` |The window of values to find the sum of
  367. |===
  368. [source,js]
  369. --------------------------------------------------
  370. POST /_search
  371. {
  372. "size": 0,
  373. "aggs": {
  374. "my_date_histo":{
  375. "date_histogram":{
  376. "field":"date",
  377. "interval":"1M"
  378. },
  379. "aggs":{
  380. "the_sum":{
  381. "sum":{ "field": "price" }
  382. },
  383. "the_movavg": {
  384. "moving_fn": {
  385. "buckets_path": "the_sum",
  386. "window": 10,
  387. "script": "MovingFunctions.linearWeightedAvg(values)"
  388. }
  389. }
  390. }
  391. }
  392. }
  393. }
  394. --------------------------------------------------
  395. // CONSOLE
  396. // TEST[setup:sales]
  397. ==== ewma Function
  398. The `ewma` function (aka "single-exponential") is similar to the `linearMovAvg` function,
  399. except older data-points become exponentially less important,
  400. rather than linearly less important. The speed at which the importance decays can be controlled with an `alpha`
  401. setting. Small values make the weight decay slowly, which provides greater smoothing and takes into account a larger
  402. portion of the window. Larger valuers make the weight decay quickly, which reduces the impact of older values on the
  403. moving average. This tends to make the moving average track the data more closely but with less smoothing.
  404. `null` and `NaN` values are ignored; the average is only calculated over the real values. If the window is empty, or all values are
  405. `null`/`NaN`, `NaN` is returned as the result. This means that the count used in the average calculation is count of non-`null`,non-`NaN`
  406. values.
  407. .`ewma(double[] values, double alpha)` Parameters
  408. |===
  409. |Parameter Name |Description
  410. |`values` |The window of values to find the sum of
  411. |`alpha` |Exponential decay
  412. |===
  413. [source,js]
  414. --------------------------------------------------
  415. POST /_search
  416. {
  417. "size": 0,
  418. "aggs": {
  419. "my_date_histo":{
  420. "date_histogram":{
  421. "field":"date",
  422. "interval":"1M"
  423. },
  424. "aggs":{
  425. "the_sum":{
  426. "sum":{ "field": "price" }
  427. },
  428. "the_movavg": {
  429. "moving_fn": {
  430. "buckets_path": "the_sum",
  431. "window": 10,
  432. "script": "MovingFunctions.ewma(values, 0.3)"
  433. }
  434. }
  435. }
  436. }
  437. }
  438. }
  439. --------------------------------------------------
  440. // CONSOLE
  441. // TEST[setup:sales]
  442. ==== holt Function
  443. The `holt` function (aka "double exponential") incorporates a second exponential term which
  444. tracks the data's trend. Single exponential does not perform well when the data has an underlying linear trend. The
  445. double exponential model calculates two values internally: a "level" and a "trend".
  446. The level calculation is similar to `ewma`, and is an exponentially weighted view of the data. The difference is
  447. that the previously smoothed value is used instead of the raw value, which allows it to stay close to the original series.
  448. The trend calculation looks at the difference between the current and last value (e.g. the slope, or trend, of the
  449. smoothed data). The trend value is also exponentially weighted.
  450. Values are produced by multiplying the level and trend components.
  451. `null` and `NaN` values are ignored; the average is only calculated over the real values. If the window is empty, or all values are
  452. `null`/`NaN`, `NaN` is returned as the result. This means that the count used in the average calculation is count of non-`null`,non-`NaN`
  453. values.
  454. .`holt(double[] values, double alpha)` Parameters
  455. |===
  456. |Parameter Name |Description
  457. |`values` |The window of values to find the sum of
  458. |`alpha` |Level decay value
  459. |`beta` |Trend decay value
  460. |===
  461. [source,js]
  462. --------------------------------------------------
  463. POST /_search
  464. {
  465. "size": 0,
  466. "aggs": {
  467. "my_date_histo":{
  468. "date_histogram":{
  469. "field":"date",
  470. "interval":"1M"
  471. },
  472. "aggs":{
  473. "the_sum":{
  474. "sum":{ "field": "price" }
  475. },
  476. "the_movavg": {
  477. "moving_fn": {
  478. "buckets_path": "the_sum",
  479. "window": 10,
  480. "script": "MovingFunctions.holt(values, 0.3, 0.1)"
  481. }
  482. }
  483. }
  484. }
  485. }
  486. }
  487. --------------------------------------------------
  488. // CONSOLE
  489. // TEST[setup:sales]
  490. In practice, the `alpha` value behaves very similarly in `holtMovAvg` as `ewmaMovAvg`: small values produce more smoothing
  491. and more lag, while larger values produce closer tracking and less lag. The value of `beta` is often difficult
  492. to see. Small values emphasize long-term trends (such as a constant linear trend in the whole series), while larger
  493. values emphasize short-term trends.
  494. ==== holtWinters Function
  495. The `holtWinters` function (aka "triple exponential") incorporates a third exponential term which
  496. tracks the seasonal aspect of your data. This aggregation therefore smooths based on three components: "level", "trend"
  497. and "seasonality".
  498. The level and trend calculation is identical to `holt` The seasonal calculation looks at the difference between
  499. the current point, and the point one period earlier.
  500. Holt-Winters requires a little more handholding than the other moving averages. You need to specify the "periodicity"
  501. of your data: e.g. if your data has cyclic trends every 7 days, you would set `period = 7`. Similarly if there was
  502. a monthly trend, you would set it to `30`. There is currently no periodicity detection, although that is planned
  503. for future enhancements.
  504. `null` and `NaN` values are ignored; the average is only calculated over the real values. If the window is empty, or all values are
  505. `null`/`NaN`, `NaN` is returned as the result. This means that the count used in the average calculation is count of non-`null`,non-`NaN`
  506. values.
  507. .`holtWinters(double[] values, double alpha)` Parameters
  508. |===
  509. |Parameter Name |Description
  510. |`values` |The window of values to find the sum of
  511. |`alpha` |Level decay value
  512. |`beta` |Trend decay value
  513. |`gamma` |Seasonality decay value
  514. |`period` |The periodicity of the data
  515. |`multiplicative` |True if you wish to use multiplicative holt-winters, false to use additive
  516. |===
  517. [source,js]
  518. --------------------------------------------------
  519. POST /_search
  520. {
  521. "size": 0,
  522. "aggs": {
  523. "my_date_histo":{
  524. "date_histogram":{
  525. "field":"date",
  526. "interval":"1M"
  527. },
  528. "aggs":{
  529. "the_sum":{
  530. "sum":{ "field": "price" }
  531. },
  532. "the_movavg": {
  533. "moving_fn": {
  534. "buckets_path": "the_sum",
  535. "window": 10,
  536. "script": "if (values.length > 5*2) {MovingFunctions.holtWinters(values, 0.3, 0.1, 0.1, 5, false)}"
  537. }
  538. }
  539. }
  540. }
  541. }
  542. }
  543. --------------------------------------------------
  544. // CONSOLE
  545. // TEST[setup:sales]
  546. [WARNING]
  547. ======
  548. Multiplicative Holt-Winters works by dividing each data point by the seasonal value. This is problematic if any of
  549. your data is zero, or if there are gaps in the data (since this results in a divid-by-zero). To combat this, the
  550. `mult` Holt-Winters pads all values by a very small amount (1*10^-10^) so that all values are non-zero. This affects
  551. the result, but only minimally. If your data is non-zero, or you prefer to see `NaN` when zero's are encountered,
  552. you can disable this behavior with `pad: false`
  553. ======
  554. ===== "Cold Start"
  555. Unfortunately, due to the nature of Holt-Winters, it requires two periods of data to "bootstrap" the algorithm. This
  556. means that your `window` must always be *at least* twice the size of your period. An exception will be thrown if it
  557. isn't. It also means that Holt-Winters will not emit a value for the first `2 * period` buckets; the current algorithm
  558. does not backcast.
  559. You'll notice in the above example we have an `if ()` statement checking the size of values. This is checking to make sure
  560. we have two periods worth of data (`5 * 2`, where 5 is the period specified in the `holtWintersMovAvg` function) before calling
  561. the holt-winters function.