You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.1 KiB

var exp = Math.exp;
var pow = Math.pow;
var E = Math.E;
function squared(n) {
return Math.pow(n, 2);
}
exports =
module.exports =
function MovingAverage(timespan) {
if (typeof timespan != 'number')
throw new Error('must provide a timespan to the moving average constructor');
if (timespan <= 0)
throw new Error('must provide a timespan > 0 to the moving average constructor');
var ma; // moving average
var v = 0; // variance
var nSamples = 0;
var previousTime;
var ret = {};
function alpha(t, pt) {
return 1 - (exp(- (t - pt) / timespan));
}
ret.push =
function push(time, value) {
nSamples++;
if (previousTime) {
// calculate moving average
var a = alpha(time, previousTime);
var previousMa = ma;
ma = a * value + (1 - a) * ma;
// calculate variance
v = v + (value - previousMa) * (value - ma);
} else {
ma = value;
}
previousTime = time;
};
// Exponential Moving Average
ret.movingAverage =
function movingAverage() {
return ma;
};
// Variance
ret.variance =
function variance() {
return v / nSamples;
};
return ret;
};