Skip to content

Moving average shifted #20667

Closed
Closed
@l8liu

Description

@l8liu

I 'm using Elasticsearch 'Moving Average' in the pipeline aggregation to compute the moving average of time series data, but I found the moving average is shifted one day . For example, when I set window = 3, i.e., 3 days moving average, I expect the moving average equals the average of the current day and previous 2 days, but the results are the average of previous 3 days, and not including the current day. The first bucket doesn't include any moving average value because of this.

Also, could you please consider add a 'Moving Standard deviation' in the future? It will be helpful for the statistical process control.

Here are my codes and results.

GET /tweets-classified*/_search
{
  "size": 0,
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "(syndromes.GI: 2) AND (place.country_code: CA)"
        }
      },
      "filter": {
        "range": {
          "@timestamp": {
            "gte": "2016-01-01"

          }
        }
      }
    }
  },
   "aggs": {
      "daily_count": {
         "date_histogram": {
            "field": "@timestamp",
            "interval": "day"
         },
         "aggs": {
            "the_sum": {
               "sum": {
                  "field": "syndromes.GI"
             }
          },
            "mvavg": {
                "moving_avg": {
                  "buckets_path": "the_sum",
                  "window": 3,
                  "model": "simple"
                }
            }
        }
    }
  }
}
{
  "took": 6470,
  "timed_out": false,
  "_shards": {
    "total": 96,
    "successful": 96,
    "failed": 0
  },
  "hits": {
    "total": 1940,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "daily_count": {
      "buckets": [
        {
          "key_as_string": "2016-01-01T00:00:00.000Z",
          "key": 1451606400000,
          "doc_count": 10,
          "the_sum": {
            "value": 20
          }
        },
        {
          "key_as_string": "2016-01-02T00:00:00.000Z",
          "key": 1451692800000,
          "doc_count": 9,
          "the_sum": {
            "value": 18
          },
          "mvavg": {
            "value": 20
          }
        },
        {
          "key_as_string": "2016-01-03T00:00:00.000Z",
          "key": 1451779200000,
          "doc_count": 5,
          "the_sum": {
            "value": 10
          },
          "mvavg": {
            "value": 19
          }
        },
        {
          "key_as_string": "2016-01-04T00:00:00.000Z",
          "key": 1451865600000,
          "doc_count": 9,
          "the_sum": {
            "value": 18
          },
          "mvavg": {
            "value": 16
          }
        },
        {
          "key_as_string": "2016-01-05T00:00:00.000Z",
          "key": 1451952000000,
          "doc_count": 12,
          "the_sum": {
            "value": 24
          },
          "mvavg": {
            "value": 15.333333333333334
          }
        },
        {
          "key_as_string": "2016-01-06T00:00:00.000Z",
          "key": 1452038400000,
          "doc_count": 10,
          "the_sum": {
            "value": 20
          },
          "mvavg": {
            "value": 17.333333333333332
          }
        },

.......

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions