movavg-aggregation.asciidoc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. [[search-aggregations-pipeline-movavg-aggregation]]
  2. === Moving Average Aggregation
  3. experimental[]
  4. Given an ordered series of data, the Moving Average aggregation will slide a window across the data and emit the average
  5. value of that window. For example, given the data `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, we can calculate a simple moving
  6. average with windows size of `5` as follows:
  7. - (1 + 2 + 3 + 4 + 5) / 5 = 3
  8. - (2 + 3 + 4 + 5 + 6) / 5 = 4
  9. - (3 + 4 + 5 + 6 + 7) / 5 = 5
  10. - etc
  11. Moving averages are a simple method to smooth sequential data. Moving averages are typically applied to time-based data,
  12. such as stock prices or server metrics. The smoothing can be used to eliminate high frequency fluctuations or random noise,
  13. which allows the lower frequency trends to be more easily visualized, such as seasonality.
  14. ==== Syntax
  15. A `moving_avg` aggregation looks like this in isolation:
  16. [source,js]
  17. --------------------------------------------------
  18. {
  19. "moving_avg": {
  20. "buckets_path": "the_sum",
  21. "model": "holt",
  22. "window": 5,
  23. "gap_policy": "insert_zeros",
  24. "settings": {
  25. "alpha": 0.8
  26. }
  27. }
  28. }
  29. --------------------------------------------------
  30. // NOTCONSOLE
  31. .`moving_avg` Parameters
  32. |===
  33. |Parameter Name |Description |Required |Default Value
  34. |`buckets_path` |Path to the metric of interest (see <<buckets-path-syntax, `buckets_path` Syntax>> for more details |Required |
  35. |`model` |The moving average weighting model that we wish to use |Optional |`simple`
  36. |`gap_policy` |Determines what should happen when a gap in the data is encountered. |Optional |`insert_zeros`
  37. |`window` |The size of window to "slide" across the histogram. |Optional |`5`
  38. |`minimize` |If the model should be algorithmically minimized. See <<movavg-minimizer, Minimization>> for more
  39. details |Optional |`false` for most models
  40. |`settings` |Model-specific settings, contents which differ depending on the model specified. |Optional |
  41. |===
  42. `moving_avg` aggregations must be embedded inside of a `histogram` or `date_histogram` aggregation. They can be
  43. embedded like any other metric aggregation:
  44. [source,js]
  45. --------------------------------------------------
  46. POST /_search
  47. {
  48. "size": 0,
  49. "aggs": {
  50. "my_date_histo":{ <1>
  51. "date_histogram":{
  52. "field":"date",
  53. "interval":"1M"
  54. },
  55. "aggs":{
  56. "the_sum":{
  57. "sum":{ "field": "price" } <2>
  58. },
  59. "the_movavg":{
  60. "moving_avg":{ "buckets_path": "the_sum" } <3>
  61. }
  62. }
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. // CONSOLE
  68. // TEST[setup:sales]
  69. <1> A `date_histogram` named "my_date_histo" is constructed on the "timestamp" field, with one-day intervals
  70. <2> A `sum` metric is used to calculate the sum of a field. This could be any metric (sum, min, max, etc)
  71. <3> Finally, we specify a `moving_avg` aggregation which uses "the_sum" metric as its input.
  72. Moving averages are built by first specifying a `histogram` or `date_histogram` over a field. You can then optionally
  73. add normal metrics, such as a `sum`, inside of that histogram. Finally, the `moving_avg` is embedded inside the histogram.
  74. The `buckets_path` parameter is then used to "point" at one of the sibling metrics inside of the histogram (see
  75. <<buckets-path-syntax>> for a description of the syntax for `buckets_path`.
  76. An example response from the above aggregation may look like:
  77. [source,js]
  78. --------------------------------------------------
  79. {
  80. "took": 11,
  81. "timed_out": false,
  82. "_shards": ...,
  83. "hits": ...,
  84. "aggregations": {
  85. "my_date_histo": {
  86. "buckets": [
  87. {
  88. "key_as_string": "2015/01/01 00:00:00",
  89. "key": 1420070400000,
  90. "doc_count": 3,
  91. "the_sum": {
  92. "value": 550.0
  93. }
  94. },
  95. {
  96. "key_as_string": "2015/02/01 00:00:00",
  97. "key": 1422748800000,
  98. "doc_count": 2,
  99. "the_sum": {
  100. "value": 60.0
  101. },
  102. "the_movavg": {
  103. "value": 550.0
  104. }
  105. },
  106. {
  107. "key_as_string": "2015/03/01 00:00:00",
  108. "key": 1425168000000,
  109. "doc_count": 2,
  110. "the_sum": {
  111. "value": 375.0
  112. },
  113. "the_movavg": {
  114. "value": 305.0
  115. }
  116. }
  117. ]
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  123. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  124. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  125. ==== Models
  126. The `moving_avg` aggregation includes four different moving average "models". The main difference is how the values in the
  127. window are weighted. As data-points become "older" in the window, they may be weighted differently. This will
  128. affect the final average for that window.
  129. Models are specified using the `model` parameter. Some models may have optional configurations which are specified inside
  130. the `settings` parameter.
  131. ===== Simple
  132. The `simple` model calculates the sum of all values in the window, then divides by the size of the window. It is effectively
  133. a simple arithmetic mean of the window. The simple model does not perform any time-dependent weighting, which means
  134. the values from a `simple` moving average tend to "lag" behind the real data.
  135. [source,js]
  136. --------------------------------------------------
  137. POST /_search
  138. {
  139. "size": 0,
  140. "aggs": {
  141. "my_date_histo":{
  142. "date_histogram":{
  143. "field":"date",
  144. "interval":"1M"
  145. },
  146. "aggs":{
  147. "the_sum":{
  148. "sum":{ "field": "price" }
  149. },
  150. "the_movavg":{
  151. "moving_avg":{
  152. "buckets_path": "the_sum",
  153. "window" : 30,
  154. "model" : "simple"
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. --------------------------------------------------
  162. // CONSOLE
  163. // TEST[setup:sales]
  164. A `simple` model has no special settings to configure
  165. The window size can change the behavior of the moving average. For example, a small window (`"window": 10`) will closely
  166. track the data and only smooth out small scale fluctuations:
  167. [[movavg_10window]]
  168. .Moving average with window of size 10
  169. image::images/pipeline_movavg/movavg_10window.png[]
  170. In contrast, a `simple` moving average with larger window (`"window": 100`) will smooth out all higher-frequency fluctuations,
  171. leaving only low-frequency, long term trends. It also tends to "lag" behind the actual data by a substantial amount:
  172. [[movavg_100window]]
  173. .Moving average with window of size 100
  174. image::images/pipeline_movavg/movavg_100window.png[]
  175. ==== Linear
  176. The `linear` model assigns a linear weighting to points in the series, such that "older" datapoints (e.g. those at
  177. the beginning of the window) contribute a linearly less amount to the total average. The linear weighting helps reduce
  178. the "lag" behind the data's mean, since older points have less influence.
  179. [source,js]
  180. --------------------------------------------------
  181. POST /_search
  182. {
  183. "size": 0,
  184. "aggs": {
  185. "my_date_histo":{
  186. "date_histogram":{
  187. "field":"date",
  188. "interval":"1M"
  189. },
  190. "aggs":{
  191. "the_sum":{
  192. "sum":{ "field": "price" }
  193. },
  194. "the_movavg": {
  195. "moving_avg":{
  196. "buckets_path": "the_sum",
  197. "window" : 30,
  198. "model" : "linear"
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. --------------------------------------------------
  206. // CONSOLE
  207. // TEST[setup:sales]
  208. A `linear` model has no special settings to configure
  209. Like the `simple` model, window size can change the behavior of the moving average. For example, a small window (`"window": 10`)
  210. will closely track the data and only smooth out small scale fluctuations:
  211. [[linear_10window]]
  212. .Linear moving average with window of size 10
  213. image::images/pipeline_movavg/linear_10window.png[]
  214. In contrast, a `linear` moving average with larger window (`"window": 100`) will smooth out all higher-frequency fluctuations,
  215. leaving only low-frequency, long term trends. It also tends to "lag" behind the actual data by a substantial amount,
  216. although typically less than the `simple` model:
  217. [[linear_100window]]
  218. .Linear moving average with window of size 100
  219. image::images/pipeline_movavg/linear_100window.png[]
  220. ==== EWMA (Exponentially Weighted)
  221. The `ewma` model (aka "single-exponential") is similar to the `linear` model, except older data-points become exponentially less important,
  222. rather than linearly less important. The speed at which the importance decays can be controlled with an `alpha`
  223. setting. Small values make the weight decay slowly, which provides greater smoothing and takes into account a larger
  224. portion of the window. Larger valuers make the weight decay quickly, which reduces the impact of older values on the
  225. moving average. This tends to make the moving average track the data more closely but with less smoothing.
  226. The default value of `alpha` is `0.3`, and the setting accepts any float from 0-1 inclusive.
  227. The EWMA model can be <<movavg-minimizer, Minimized>>
  228. [source,js]
  229. --------------------------------------------------
  230. POST /_search
  231. {
  232. "size": 0,
  233. "aggs": {
  234. "my_date_histo":{
  235. "date_histogram":{
  236. "field":"date",
  237. "interval":"1M"
  238. },
  239. "aggs":{
  240. "the_sum":{
  241. "sum":{ "field": "price" }
  242. },
  243. "the_movavg": {
  244. "moving_avg":{
  245. "buckets_path": "the_sum",
  246. "window" : 30,
  247. "model" : "ewma",
  248. "settings" : {
  249. "alpha" : 0.5
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. --------------------------------------------------
  258. // CONSOLE
  259. // TEST[setup:sales]
  260. [[single_0.2alpha]]
  261. .EWMA with window of size 10, alpha = 0.2
  262. image::images/pipeline_movavg/single_0.2alpha.png[]
  263. [[single_0.7alpha]]
  264. .EWMA with window of size 10, alpha = 0.7
  265. image::images/pipeline_movavg/single_0.7alpha.png[]
  266. ==== Holt-Linear
  267. The `holt` model (aka "double exponential") incorporates a second exponential term which
  268. tracks the data's trend. Single exponential does not perform well when the data has an underlying linear trend. The
  269. double exponential model calculates two values internally: a "level" and a "trend".
  270. The level calculation is similar to `ewma`, and is an exponentially weighted view of the data. The difference is
  271. that the previously smoothed value is used instead of the raw value, which allows it to stay close to the original series.
  272. The trend calculation looks at the difference between the current and last value (e.g. the slope, or trend, of the
  273. smoothed data). The trend value is also exponentially weighted.
  274. Values are produced by multiplying the level and trend components.
  275. The default value of `alpha` is `0.3` and `beta` is `0.1`. The settings accept any float from 0-1 inclusive.
  276. The Holt-Linear model can be <<movavg-minimizer, Minimized>>
  277. [source,js]
  278. --------------------------------------------------
  279. POST /_search
  280. {
  281. "size": 0,
  282. "aggs": {
  283. "my_date_histo":{
  284. "date_histogram":{
  285. "field":"date",
  286. "interval":"1M"
  287. },
  288. "aggs":{
  289. "the_sum":{
  290. "sum":{ "field": "price" }
  291. },
  292. "the_movavg": {
  293. "moving_avg":{
  294. "buckets_path": "the_sum",
  295. "window" : 30,
  296. "model" : "holt",
  297. "settings" : {
  298. "alpha" : 0.5,
  299. "beta" : 0.5
  300. }
  301. }
  302. }
  303. }
  304. }
  305. }
  306. }
  307. --------------------------------------------------
  308. // CONSOLE
  309. // TEST[setup:sales]
  310. In practice, the `alpha` value behaves very similarly in `holt` as `ewma`: small values produce more smoothing
  311. and more lag, while larger values produce closer tracking and less lag. The value of `beta` is often difficult
  312. to see. Small values emphasize long-term trends (such as a constant linear trend in the whole series), while larger
  313. values emphasize short-term trends. This will become more apparently when you are predicting values.
  314. [[double_0.2beta]]
  315. .Holt-Linear moving average with window of size 100, alpha = 0.5, beta = 0.2
  316. image::images/pipeline_movavg/double_0.2beta.png[]
  317. [[double_0.7beta]]
  318. .Holt-Linear moving average with window of size 100, alpha = 0.5, beta = 0.7
  319. image::images/pipeline_movavg/double_0.7beta.png[]
  320. ==== Holt-Winters
  321. The `holt_winters` model (aka "triple exponential") incorporates a third exponential term which
  322. tracks the seasonal aspect of your data. This aggregation therefore smooths based on three components: "level", "trend"
  323. and "seasonality".
  324. The level and trend calculation is identical to `holt` The seasonal calculation looks at the difference between
  325. the current point, and the point one period earlier.
  326. Holt-Winters requires a little more handholding than the other moving averages. You need to specify the "periodicity"
  327. of your data: e.g. if your data has cyclic trends every 7 days, you would set `period: 7`. Similarly if there was
  328. a monthly trend, you would set it to `30`. There is currently no periodicity detection, although that is planned
  329. for future enhancements.
  330. There are two varieties of Holt-Winters: additive and multiplicative.
  331. ===== "Cold Start"
  332. Unfortunately, due to the nature of Holt-Winters, it requires two periods of data to "bootstrap" the algorithm. This
  333. means that your `window` must always be *at least* twice the size of your period. An exception will be thrown if it
  334. isn't. It also means that Holt-Winters will not emit a value for the first `2 * period` buckets; the current algorithm
  335. does not backcast.
  336. [[holt_winters_cold_start]]
  337. .Holt-Winters showing a "cold" start where no values are emitted
  338. image::images/pipeline_movavg/triple_untruncated.png[]
  339. Because the "cold start" obscures what the moving average looks like, the rest of the Holt-Winters images are truncated
  340. to not show the "cold start". Just be aware this will always be present at the beginning of your moving averages!
  341. ===== Additive Holt-Winters
  342. Additive seasonality is the default; it can also be specified by setting `"type": "add"`. This variety is preferred
  343. when the seasonal affect is additive to your data. E.g. you could simply subtract the seasonal effect to "de-seasonalize"
  344. your data into a flat trend.
  345. The default values of `alpha` and `gamma` are `0.3` while `beta` is `0.1`. The settings accept any float from 0-1 inclusive.
  346. The default value of `period` is `1`.
  347. The additive Holt-Winters model can be <<movavg-minimizer, Minimized>>
  348. [source,js]
  349. --------------------------------------------------
  350. POST /_search
  351. {
  352. "size": 0,
  353. "aggs": {
  354. "my_date_histo":{
  355. "date_histogram":{
  356. "field":"date",
  357. "interval":"1M"
  358. },
  359. "aggs":{
  360. "the_sum":{
  361. "sum":{ "field": "price" }
  362. },
  363. "the_movavg": {
  364. "moving_avg":{
  365. "buckets_path": "the_sum",
  366. "window" : 30,
  367. "model" : "holt_winters",
  368. "settings" : {
  369. "type" : "add",
  370. "alpha" : 0.5,
  371. "beta" : 0.5,
  372. "gamma" : 0.5,
  373. "period" : 7
  374. }
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. --------------------------------------------------
  382. // CONSOLE
  383. // TEST[setup:sales]
  384. [[holt_winters_add]]
  385. .Holt-Winters moving average with window of size 120, alpha = 0.5, beta = 0.7, gamma = 0.3, period = 30
  386. image::images/pipeline_movavg/triple.png[]
  387. ===== Multiplicative Holt-Winters
  388. Multiplicative is specified by setting `"type": "mult"`. This variety is preferred when the seasonal affect is
  389. multiplied against your data. E.g. if the seasonal affect is x5 the data, rather than simply adding to it.
  390. The default values of `alpha` and `gamma` are `0.3` while `beta` is `0.1`. The settings accept any float from 0-1 inclusive.
  391. The default value of `period` is `1`.
  392. The multiplicative Holt-Winters model can be <<movavg-minimizer, Minimized>>
  393. [WARNING]
  394. ======
  395. Multiplicative Holt-Winters works by dividing each data point by the seasonal value. This is problematic if any of
  396. your data is zero, or if there are gaps in the data (since this results in a divid-by-zero). To combat this, the
  397. `mult` Holt-Winters pads all values by a very small amount (1*10^-10^) so that all values are non-zero. This affects
  398. the result, but only minimally. If your data is non-zero, or you prefer to see `NaN` when zero's are encountered,
  399. you can disable this behavior with `pad: false`
  400. ======
  401. [source,js]
  402. --------------------------------------------------
  403. POST /_search
  404. {
  405. "size": 0,
  406. "aggs": {
  407. "my_date_histo":{
  408. "date_histogram":{
  409. "field":"date",
  410. "interval":"1M"
  411. },
  412. "aggs":{
  413. "the_sum":{
  414. "sum":{ "field": "price" }
  415. },
  416. "the_movavg": {
  417. "moving_avg":{
  418. "buckets_path": "the_sum",
  419. "window" : 30,
  420. "model" : "holt_winters",
  421. "settings" : {
  422. "type" : "mult",
  423. "alpha" : 0.5,
  424. "beta" : 0.5,
  425. "gamma" : 0.5,
  426. "period" : 7,
  427. "pad" : true
  428. }
  429. }
  430. }
  431. }
  432. }
  433. }
  434. }
  435. --------------------------------------------------
  436. // CONSOLE
  437. // TEST[setup:sales]
  438. ==== Prediction
  439. All the moving average model support a "prediction" mode, which will attempt to extrapolate into the future given the
  440. current smoothed, moving average. Depending on the model and parameter, these predictions may or may not be accurate.
  441. Predictions are enabled by adding a `predict` parameter to any moving average aggregation, specifying the number of
  442. predictions you would like appended to the end of the series. These predictions will be spaced out at the same interval
  443. as your buckets:
  444. [source,js]
  445. --------------------------------------------------
  446. POST /_search
  447. {
  448. "size": 0,
  449. "aggs": {
  450. "my_date_histo":{
  451. "date_histogram":{
  452. "field":"date",
  453. "interval":"1M"
  454. },
  455. "aggs":{
  456. "the_sum":{
  457. "sum":{ "field": "price" }
  458. },
  459. "the_movavg": {
  460. "moving_avg":{
  461. "buckets_path": "the_sum",
  462. "window" : 30,
  463. "model" : "simple",
  464. "predict" : 10
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. --------------------------------------------------
  472. // CONSOLE
  473. // TEST[setup:sales]
  474. The `simple`, `linear` and `ewma` models all produce "flat" predictions: they essentially converge on the mean
  475. of the last value in the series, producing a flat:
  476. [[simple_prediction]]
  477. .Simple moving average with window of size 10, predict = 50
  478. image::images/pipeline_movavg/simple_prediction.png[]
  479. In contrast, the `holt` model can extrapolate based on local or global constant trends. If we set a high `beta`
  480. value, we can extrapolate based on local constant trends (in this case the predictions head down, because the data at the end
  481. of the series was heading in a downward direction):
  482. [[double_prediction_local]]
  483. .Holt-Linear moving average with window of size 100, predict = 20, alpha = 0.5, beta = 0.8
  484. image::images/pipeline_movavg/double_prediction_local.png[]
  485. In contrast, if we choose a small `beta`, the predictions are based on the global constant trend. In this series, the
  486. global trend is slightly positive, so the prediction makes a sharp u-turn and begins a positive slope:
  487. [[double_prediction_global]]
  488. .Double Exponential moving average with window of size 100, predict = 20, alpha = 0.5, beta = 0.1
  489. image::images/pipeline_movavg/double_prediction_global.png[]
  490. The `holt_winters` model has the potential to deliver the best predictions, since it also incorporates seasonal
  491. fluctuations into the model:
  492. [[holt_winters_prediction_global]]
  493. .Holt-Winters moving average with window of size 120, predict = 25, alpha = 0.8, beta = 0.2, gamma = 0.7, period = 30
  494. image::images/pipeline_movavg/triple_prediction.png[]
  495. [[movavg-minimizer]]
  496. ==== Minimization
  497. Some of the models (EWMA, Holt-Linear, Holt-Winters) require one or more parameters to be configured. Parameter choice
  498. can be tricky and sometimes non-intuitive. Furthermore, small deviations in these parameters can sometimes have a drastic
  499. effect on the output moving average.
  500. For that reason, the three "tunable" models can be algorithmically *minimized*. Minimization is a process where parameters
  501. are tweaked until the predictions generated by the model closely match the output data. Minimization is not fullproof
  502. and can be susceptible to overfitting, but it often gives better results than hand-tuning.
  503. Minimization is disabled by default for `ewma` and `holt_linear`, while it is enabled by default for `holt_winters`.
  504. Minimization is most useful with Holt-Winters, since it helps improve the accuracy of the predictions. EWMA and
  505. Holt-Linear are not great predictors, and mostly used for smoothing data, so minimization is less useful on those
  506. models.
  507. Minimization is enabled/disabled via the `minimize` parameter:
  508. [source,js]
  509. --------------------------------------------------
  510. POST /_search
  511. {
  512. "size": 0,
  513. "aggs": {
  514. "my_date_histo":{
  515. "date_histogram":{
  516. "field":"date",
  517. "interval":"1M"
  518. },
  519. "aggs":{
  520. "the_sum":{
  521. "sum":{ "field": "price" }
  522. },
  523. "the_movavg": {
  524. "moving_avg":{
  525. "buckets_path": "the_sum",
  526. "model" : "holt_winters",
  527. "window" : 30,
  528. "minimize" : true, <1>
  529. "settings" : {
  530. "period" : 7
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. --------------------------------------------------
  539. // CONSOLE
  540. // TEST[setup:sales]
  541. <1> Minimization is enabled with the `minimize` parameter
  542. When enabled, minimization will find the optimal values for `alpha`, `beta` and `gamma`. The user should still provide
  543. appropriate values for `window`, `period` and `type`.
  544. [WARNING]
  545. ======
  546. Minimization works by running a stochastic process called *simulated annealing*. This process will usually generate
  547. a good solution, but is not guaranteed to find the global optimum. It also requires some amount of additional
  548. computational power, since the model needs to be re-run multiple times as the values are tweaked. The run-time of
  549. minimization is linear to the size of the window being processed: excessively large windows may cause latency.
  550. Finally, minimization fits the model to the last `n` values, where `n = window`. This generally produces
  551. better forecasts into the future, since the parameters are tuned around the end of the series. It can, however, generate
  552. poorer fitting moving averages at the beginning of the series.
  553. ======