Skip to content

Stats

These features are in beta, and must be enabled by setting the $KXI_SP_BETA_FEATURES environment variable to "yes".

.qsp.stats.sma

(Beta Feature) Calculates the simple moving average over a stream

Beta Features

To enable beta features, set the environment variable KXI_SP_BETA_FEATURES to true.

.qsp.stats.sma[getter; n; setter]

Parameters:

name type description default
getter symbol or symbol[] A list of column names. Required
n long The number of records to include in the average Required
setter symbol or symbol[] The columns to write to. These can overwrite existing columns. The same as getter

For all common arguments, refer to configuring operators

This calculates, for each data point, the arithmetic mean of a moving window including that point and the n-1 prior data points.

This example replaces each value in y with the simple moving average of that value and the nine prior values.

.qsp.run
    .qsp.read.fromCallback[`publish]
    .qsp.stats.sma[`y; 10]
    .qsp.write.toConsole[];

publish ([] x: til 10; y: 0 1 4 2 5 3 6 7 9 8)

Parameter:

Name Type Description
op dict The operator

Returns:

Type Description
null

.qsp.stats.ema

(Beta Feature) Calculates the exponential moving average over a stream

Beta Features

To enable beta features, set the environment variable KXI_SP_BETA_FEATURES to true.

.qsp.stats.ema[getter; alpha; setter]

Parameters:

name type description default
getter symbol or symbol[] A list of column names. Required
alpha float The decay rate Required
setter symbol or symbol[] The columns to write to. These can overwrite existing column. The same as getter

For all common arguments, refer to configuring operators

This calculates the exponential moving average for each data point.

This example replaces the columns x and y with their exponential moving averages.

.qsp.run
    .qsp.read.fromCallback[`publish]
    .qsp.stats.ema[`x`y; .33]
    .qsp.write.toConsole[];

publish ([] x: til 10; y: 0 1 4 2 5 3 6 7 9 8)

.qsp.stats.twa

(Beta Feature) Calculates the time weighted average over a stream

Beta Features

To enable beta features, set the environment variable KXI_SP_BETA_FEATURES to true.

.qsp.stats.twa[getter; times; range; setter]

Parameters:

name type description default
getter symbol or symbol[] A list of column names. Required
times symbol The column name containing the time Required
range long, int or short The number of records to include in the average Required
setter symbol or symbol[] The columns to write to. These can overwrite existing columns. The same as getter

For all common arguments, refer to configuring operators

This calculates, for each data point, the arithmetic mean of a moving window including that point and the n-1 prior data points weighted by the time deltas found in times.

Data must be sorted

The incoming data must be sorted, because the average is calculated using the deltas between each timestamp. Out of order data would cause negative weight to be applied to the calculation.

This example replaces each value in y with the time weighted average of that value and the nine prior values using column t as a series of times.

.qsp.run
    .qsp.read.fromCallback[`publish]
    // The windowing is to ensure that records are sorted by timestamp
    .qsp.window.tumbling[00:01:00; `time; .qsp.use `sort`lateness!(1b; 00:00:10)]
    .qsp.stats.twa[`data; `time; 10]
    .qsp.write.toConsole[]

// Because the spikes at 5 and 57 seconds are very short,
// they don't significantly affect the average.
publish ([] time: 0p + 00:00:01 * 0 5 6 17 14 21 57 58 71;
            data: 10 20 10 9 11 8 21 10 9)

This example replaces each value in c and in d with the time weighted average of the values within a and b respectively and the five prior values using column t as a series of times.

.qsp.run
    .qsp.read.fromCallback[`publish]
    .qsp.window.tumbling[00:00:01; `time; .qsp.use `sort`lateness!(1b; 00:00:01)]
    .qsp.stats.twa[`a`b; `time; 5; `c`d]
    .qsp.write.toConsole[];

// Note that because the average is assigned to the record starting an interval,
// the results will appear to cross window boundaries, as the first record in a window
// will cause the last record of the previous window to be emitted.
publish ([] time: 0p + 00:00:00.1 * 0 8 13 17 19 21; a: 1 7 8 7 7 8; b: til 6);