Skip to main content
Version: Java (Groovy)

RollingAvg

RollingAvg calculates a window-based rolling average in an updateBy table operation. The rolling average can be calculated using forward and/or backward windows.

SMA=1nxinSMA = \frac{\sum_{1}^{n}x_{i}}{n}

Where:

  • nn is the number window size in ticks
  • xix_{i} is the current value

For a tick-based (row-based) SMA, n is the window size, determined by fwdTicks and revTicks.

For a time-based SMA, n is the number of observations in the window, determined by fwdTime and revTime.

Syntax

RollingAvg(revTicks, fwdTicks, pairs)
RollingAvg(revTicks, pairs)
RollingAvg(timestampCol, revTime, fwdTime, pairs)
RollingAvg(timestampCol, revTime, pairs)
RollingAvg(timestampCol, revDuration, pairs)
RollingAvg(timestampCol, revDuration, fwdDuration, pairs)

Parameters

ParameterTypeDescription
revTickslong

The look-behind window size in ticks (rows). If positive, it defines the maximum number of rows before the current row that will be used. If negative, it defines the minimum number of rows after the current row that will be used.

fwdTickslong

The look-forward window size in ticks (rows). If positive, it defines the maximum number of rows after the current row that will be used. If negative, it defines the minimum number of rows before the current row that will be used.

pairsString...

The input/output column name pairs.

timestampColString

The name of the timestamp column.

revDurationDuration

The look-behind window size in Duration.

fwdDurationDuration

The look-forward window size in Duration.

revTimelong

The look-behind window size in nanoseconds.

fwdTimelong

The look-forward window size in nanoseconds.

Returns

An UpdateByOperation to be used in an updateBy table operation.

Examples

The following example performs an updateBy on the source table using four row-based RollingAvg operations. Each operation uses different revTicks and fwdTicks values to show how they affect the output. Note that the first operation only uses revTicks.

rng = new Random()

source = emptyTable(10).update("X = rng.nextInt(25)")

opRollAvg = RollingAvg(3, "RollingAverage = X")
opPrior = RollingAvg(3, -1, "WindowPrior = X")
opPosterior = RollingAvg(-1, 3, "WindowPosterior = X")
opMiddle = RollingAvg(1, 1, "WindowMiddle = X")

result = source.updateBy([opRollAvg, opPrior, opPosterior, opMiddle])