Skip to main content
Version: Java (Groovy)

Ems

Ems creates an EMS (exponential moving sum) for an updateBy table operation. The formula for an EMS is:

a=e1τa = e^{\frac{-1}{\tau}}

ϵn=aϵn1+x\epsilon_{n} = a * \epsilon_{n-1} + x

Where:

  • τ\tau is the window size, an input parameter to the method.
  • ϵ\epsilon is the EMS.
  • xx is the current value.
  • nn denotes the step. The current step is nn, and the previous step is n1n - 1.

Syntax

Ems(tickDecay, pairs...)
Ems(control, tickDecay, pairs...)
Ems(control, timestampColumn, timeDecay, pairs...)
Ems(control, timestampColumn, durationDecay, pairs...)
Ems(timestampColumn, timeDecay, pairs...)
Ems(timestampColumn, durationDecay, pairs...)

Parameters

ParameterTypeDescription
tickDecaylong

The decay rate in ticks (rows).

pairsString...

The input/output column name pairs.

controlOperationControl

Defines how special cases should behave. If not given, default OperationControl settings are used.

timestampColumnString

The column in the source table to use for timestamps.

timeDecaylong

The decay rate in nanoseconds.

durationDecayDuration

The decay rate in a Duration object.

Returns

An UpdateByOperation to be used in an updateBy table operation.

Examples

One column, no groups

The following example calculates the tick-based and time-based EMS of the X column, renaming the resulting column to EmsX. The tick decay rate is set to 5 rows, and the time decay rate is set to 5 seconds. No grouping columns are specified, so the EMS is calculated for all rows.

baseTime = parseInstant("2023-01-01T00:00:00 ET")

source = emptyTable(10).update("Timestamp = baseTime + i * SECOND", "Letter = (i % 2 == 0) ? `A` : `B`", "X = i")

result = source.updateBy([Ems(5, "EmsTickX = X"), Ems("Timestamp", 5 * SECOND, "EmsTimeX = X")])

One EMS column, one grouping column

The following example builds on the previous by specifying Letter as the key column. Thus, the EMA is calculated on a per-letter basis.

baseTime = parseInstant("2023-01-01T00:00:00 ET")

source = emptyTable(10).update("Timestamp = baseTime + i * SECOND", "Letter = (i % 2 == 0) ? `A` : `B`", "X = i")

result = source.updateBy([Ems(5, "EmsTickX = X"), Ems("Timestamp", 5 * SECOND, "EmsTimeX = X")], "Letter")

Multiple EMS columns, multiple grouping columns

The following example builds on the previous by calculating the EMS of multiple columns with each UpdateByOperation. Also, the groups are defined by unique combinations of letter and boolean in the Letter and Truth columns, respectively.

baseTime = parseInstant("2023-01-01T00:00:00 ET")

source = emptyTable(20).update("Timestamp = baseTime + i * SECOND", "Letter = (i % 2 == 0) ? `A` : `B`", "Truth = randomBool()", "X = i", "Y = randomInt(5, 10)")

result = source.updateBy([Ems(2, "EmsTickX = X", "EmsTickY = Y"), Ems("Timestamp", 3 * SECOND, "EmsTimeX = X", "EmsTimeY = Y")], "Letter", "Truth")

Multiple UpdateByOperations, multiple grouping columns

The following example builds on the previous by calculating the tick- and time-based EMS of the X and Y columns using different EMS UpdateByOperations. This allows each EMS to have its own decay rate. The decay rates are reflected in the renamed resultant columns.

baseTime = parseInstant("2023-01-01T00:00:00 ET")

source = emptyTable(20).update("Timestamp = baseTime + i * SECOND", "Letter = (i % 2 == 0) ? `A` : `B`", "Truth = randomBool()", "X = i", "Y = randomInt(5, 10)")

emsTickX = Ems(1, "EmsTickX_1row = X")
emsTickY = Ems(5, "EmsTickY_5rows = Y")
emsTimeX = Ems("Timestamp", 2 * SECOND, "EmsTimeX_2sec = X")
emsTimeY = Ems("Timestamp", 4 * SECOND, "EmsTimeY_4sec = Y")

result = source.updateBy([emsTickX, emsTickY, emsTimeX, emsTimeY], "Letter", "Truth")