Back to Influxdb

exponentialMovingAverage() function

content/flux/v0/stdlib/universe/exponentialmovingaverage.md

latest4.9 KB
Original Source
<!------------------------------------------------------------------------------ IMPORTANT: This page was generated from comments in the Flux source code. Any edits made directly to this page will be overwritten the next time the documentation is generated. To make updates to this documentation, update the function comments above the function definition in the Flux source code: https://github.com/influxdata/flux/blob/master/stdlib/universe/universe.flux#L574-L579 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

exponentialMovingAverage() calculates the exponential moving average of n number of values in the _value column giving more weight to more recent data.

Exponential moving average rules

  • The first value of an exponential moving average over n values is the algebraic mean of n values.
  • Subsequent values are calculated as y(t) = x(t) * k + y(t-1) * (1 - k), where:
    • y(t) is the exponential moving average at time t.
    • x(t) is the value at time t.
    • k = 2 / (1 + n).
  • The average over a period populated by only null values is null.
  • Exponential moving averages skip null values.
Function type signature
js
(<-tables: stream[{A with _value: B}], n: int) => stream[{A with _value: B}] where B: Numeric

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

n

({{< req >}}) Number of values to average.

tables

Input data. Default is piped-forward data (<-).

Examples

Calculate a three point exponential moving average

js
import "sampledata"

sampledata.int()
    |> exponentialMovingAverage(n: 3)

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Z10t1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Z17t1
2021-01-01T00:00:40Z15t1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Z19t2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Z13t2
2021-01-01T00:00:50Z1t2

Output data

_time_value*tag
2021-01-01T00:00:20Z5t1
2021-01-01T00:00:30Z11t1
2021-01-01T00:00:40Z13t1
2021-01-01T00:00:50Z8.5t1
_time_value*tag
2021-01-01T00:00:20Z6.666666666666667t2
2021-01-01T00:00:30Z12.833333333333334t2
2021-01-01T00:00:40Z12.916666666666668t2
2021-01-01T00:00:50Z6.958333333333334t2

{{% /expand %}} {{< /expand-wrapper >}}

Calculate a three point exponential moving average with null values

js
import "sampledata"

sampledata.int(includeNull: true)
    |> exponentialMovingAverage(n: 3)

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Zt1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Zt1
2021-01-01T00:00:40Zt1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Zt2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Zt2
2021-01-01T00:00:50Z1t2

Output data

_time_value*tag
2021-01-01T00:00:20Z2.5t1
2021-01-01T00:00:30Z2.5t1
2021-01-01T00:00:40Z2.5t1
2021-01-01T00:00:50Z3.25t1
_time_value*tag
2021-01-01T00:00:20Z0.5t2
2021-01-01T00:00:30Z9.75t2
2021-01-01T00:00:40Z9.75t2
2021-01-01T00:00:50Z5.375t2

{{% /expand %}} {{< /expand-wrapper >}}