functions: exponential moving average

This commit is contained in:
Alexander Zobnin
2017-07-03 23:01:18 +03:00
parent 273e6df7e3
commit f3ee574619
12 changed files with 89 additions and 4 deletions

View File

@@ -34,6 +34,9 @@ var scale = function scale(factor, datapoints) {
var simpleMovingAverage = function simpleMovingAverage(n, datapoints) {
return _timeseries2.default.simpleMovingAverage(datapoints, n);
};
var expMovingAverage = function expMovingAverage(a, datapoints) {
return _timeseries2.default.expMovingAverage(datapoints, a);
};
var SUM = _timeseries2.default.SUM;
var COUNT = _timeseries2.default.COUNT;
@@ -125,6 +128,7 @@ var metricFunctions = {
delta: delta,
rate: rate,
simpleMovingAverage: simpleMovingAverage,
expMovingAverage: expMovingAverage,
aggregateBy: aggregateByWrapper,
average: _lodash2.default.partial(aggregateWrapper, AVERAGE),
min: _lodash2.default.partial(aggregateWrapper, MIN),

View File

@@ -80,6 +80,13 @@ addFuncDef({
defaultParams: [10]
});
addFuncDef({
name: 'expMovingAverage',
category: 'Transform',
params: [{ name: 'smoothing', type: 'float', options: [0.001, 0.01, 0.05, 0.1, 0.2, 0.3, 0.5, 0.7, 0.9] }],
defaultParams: [0.2]
});
// Aggregate
addFuncDef({

View File

@@ -277,6 +277,22 @@ function simpleMovingAverage(datapoints, n) {
return sma;
}
function expMovingAverage(datapoints, a) {
var ema = [datapoints[0]];
var ema_prev = datapoints[0][POINT_VALUE];
var ema_cur = void 0;
for (var i = 1; i < datapoints.length; i++) {
if (datapoints[i][POINT_VALUE] !== null) {
ema_cur = a * datapoints[i][POINT_VALUE] + (1 - a) * ema_prev;
ema_prev = ema_cur;
ema.push([ema_cur, datapoints[i][POINT_TIMESTAMP]]);
} else {
ema.push([null, datapoints[i][POINT_TIMESTAMP]]);
}
}
return ema;
}
function COUNT(values) {
return values.length;
}
@@ -411,6 +427,7 @@ var exportedFunctions = {
delta: delta,
rate: rate,
simpleMovingAverage: simpleMovingAverage,
expMovingAverage: expMovingAverage,
SUM: SUM,
COUNT: COUNT,
AVERAGE: AVERAGE,