exponentialMovingAverage
exponentialMovingAverageβ
Calculates the exponential moving average of values for the determined time.
Syntax
exponentialMovingAverage(x)(value, timeunit)
Each value
corresponds to the determinate timeunit
. The half-life x
is the time lag at which the exponential weights decay by one-half. The function returns a weighted average: the older the time point, the less weight the corresponding value is considered to be.
Arguments
value
β Value. Integer, Float or Decimal.timeunit
β Timeunit. Integer, Float or Decimal. Timeunit is not timestamp (seconds), it's -- an index of the time interval. Can be calculated using intDiv.
Parameters
Returned values
- Returns an exponentially smoothed moving average of the values for the past
x
time at the latest point of time.
Type: Float64.
Examples
Input table:
βββtemperatureββ¬βtimestampβββ
β 95 β 1 β
β 95 β 2 β
β 95 β 3 β
β 96 β 4 β
β 96 β 5 β
β 96 β 6 β
β 96 β 7 β
β 97 β 8 β
β 97 β 9 β
β 97 β 10 β
β 97 β 11 β
β 98 β 12 β
β 98 β 13 β
β 98 β 14 β
β 98 β 15 β
β 99 β 16 β
β 99 β 17 β
β 99 β 18 β
β 100 β 19 β
β 100 β 20 β
ββββββββββββββββ΄βββββββββββββ
Query:
SELECT exponentialMovingAverage(5)(temperature, timestamp);
Result:
βββexponentialMovingAverage(5)(temperature, timestamp)βββ
β 92.25779635374204 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Query:
SELECT
value,
time,
round(exp_smooth, 3),
bar(exp_smooth, 0, 1, 50) AS bar
FROM
(
SELECT
(number = 0) OR (number >= 25) AS value,
number AS time,
exponentialMovingAverage(10)(value, time) OVER (Rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS exp_smooth
FROM numbers(50)
)
Result:
ββvalueββ¬βtimeββ¬βround(exp_smooth, 3)ββ¬βbarββββββββββββββββββββββββββββββββββ βββββββ
β 1 β 0 β 0.067 β ββββ β
β 0 β 1 β 0.062 β βββ β
β 0 β 2 β 0.058 β βββ β
β 0 β 3 β 0.054 β βββ β
β 0 β 4 β 0.051 β βββ β
β 0 β 5 β 0.047 β βββ β
β 0 β 6 β 0.044 β βββ β
β 0 β 7 β 0.041 β ββ β
β 0 β 8 β 0.038 β ββ β
β 0 β 9 β 0.036 β ββ β
β 0 β 10 β 0.033 β ββ β
β 0 β 11 β 0.031 β ββ β
β 0 β 12 β 0.029 β ββ β
β 0 β 13 β 0.027 β ββ β
β 0 β 14 β 0.025 β ββ β
β 0 β 15 β 0.024 β ββ β
β 0 β 16 β 0.022 β β β
β 0 β 17 β 0.021 β β β
β 0 β 18 β 0.019 β β β
β 0 β 19 β 0.018 β β β
β 0 β 20 β 0.017 β β β
β 0 β 21 β 0.016 β β β
β 0 β 22 β 0.015 β β β
β 0 β 23 β 0.014 β β β
β 0 β 24 β 0.013 β β β
β 1 β 25 β 0.079 β ββββ β
β 1 β 26 β 0.14 β βββββββ β
β 1 β 27 β 0.198 β ββββββββββ β
β 1 β 28 β 0.252 β βββββββββββββ β
β 1 β 29 β 0.302 β βββββββββββββββ β
β 1 β 30 β 0.349 β ββββββββββββββββββ β
β 1 β 31 β 0.392 β ββββββββββββββββββββ β
β 1 β 32 β 0.433 β ββββββββββββββββββββββ β
β 1 β 33 β 0.471 β ββββββββββββββββββββββββ β
β 1 β 34 β 0.506 β ββββββββββββββββββββββββββ β
β 1 β 35 β 0.539 β βββββββββββββββββββββββββββ β
β 1 β 36 β 0.57 β βββββββββββββββββββββββββββββ β
β 1 β 37 β 0.599 β ββββββββββββββββββββββββββββββ β
β 1 β 38 β 0.626 β ββββββββββββββββββββββββββββββββ β
β 1 β 39 β 0.651 β βββββββββββββββββββββββββββββββββ β
β 1 β 40 β 0.674 β ββββββββββββββββββββββββββββββββββ β
β 1 β 41 β 0.696 β βββββββββββββββββββββββββββββββββββ β
β 1 β 42 β 0.716 β ββββββββββββββββββββββββββββββββββββ β
β 1 β 43 β 0.735 β βββββββββββββββββββββββββββββββββββββ β
β 1 β 44 β 0.753 β ββββββββββββββββββββββββββββββββββββββ β
β 1 β 45 β 0.77 β βββββββββββββββββββββββββββββββββββββββ β
β 1 β 46 β 0.785 β ββββββββββββββββββββββββββββββββββββββββ β
β 1 β 47 β 0.8 β βββββββββββββββββββββββββββββββββ βββββββ β
β 1 β 48 β 0.813 β βββββββββββββββββββββββββββββββββββββββββ β
β 1 β 49 β 0.825 β ββββββββββββββββββββββββββββββββββββββββββ β
βββββββββ΄βββββββ΄βββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββ
CREATE TABLE data
ENGINE = Memory AS
SELECT
10 AS value,
toDateTime('2020-01-01') + (3600 * number) AS time
FROM numbers_mt(10);
-- Calculate timeunit using intDiv
SELECT
value,
time,
exponentialMovingAverage(1)(value, intDiv(toUInt32(time), 3600)) OVER (ORDER BY time ASC) AS res,
intDiv(toUInt32(time), 3600) AS timeunit
FROM data
ORDER BY time ASC;
ββvalueββ¬ββββββββββββββββtimeββ¬βββββββββresββ¬βtimeunitββ
β 10 β 2020-01-01 00:00:00 β 5 β 438288 β
β 10 β 2020-01-01 01:00:00 β 7.5 β 438289 β
β 10 β 2020-01-01 02:00:00 β 8.75 β 438290 β
β 10 β 2020-01-01 03:00:00 β 9.375 β 438291 β
β 10 β 2020-01-01 04:00:00 β 9.6875 β 438292 β
β 10 β 2020-01-01 05:00:00 β 9.84375 β 438293 β
β 10 β 2020-01-01 06:00:00 β 9.921875 β 438294 β
β 10 β 2020-01-01 07:00:00 β 9.9609375 β 438295 β
β 10 β 2020-01-01 08:00:00 β 9.98046875 β 438296 β
β 10 β 2020-01-01 09:00:00 β 9.990234375 β 438297 β
βββββββββ΄ββββββββββββββββββββββ΄ββββββββββββββ΄βββββββββββ
-- Calculate timeunit using toRelativeHourNum
SELECT
value,
time,
exponentialMovingAverage(1)(value, toRelativeHourNum(time)) OVER (ORDER BY time ASC) AS res,
toRelativeHourNum(time) AS timeunit
FROM data
ORDER BY time ASC;
ββvalueββ¬ββββββββββββββββtimeββ¬βββββββββresββ¬βtimeunitββ
β 10 β 2020-01-01 00:00:00 β 5 β 438288 β
β 10 β 2020-01-01 01:00:00 β 7.5 β 438289 β
β 10 β 2020-01-01 02:00:00 β 8.75 β 438290 β
β 10 β 2020-01-01 03:00:00 β 9.375 β 438291 β
β 10 β 2020-01-01 04:00:00 β 9.6875 β 438292 β
β 10 β 2020-01-01 05:00:00 β 9.84375 β 438293 β
β 10 β 2020-01-01 06:00:00 β 9.921875 β 438294 β
β 10 β 2020-01-01 07:00:00 β 9.9609375 β 438295 β
β 10 β 2020-01-01 08:00:00 β 9.98046875 β 438296 β
β 10 β 2020-01-01 09:00:00 β 9.990234375 β 438297 β
βββββββββ΄ββββββββββββββββββββββ΄ββββββββββββββ΄βββββββββββ