/**
 * Implements exponential moving average. Ported from `moving-average`.
 *
 * @see https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
 * @see https://www.npmjs.com/package/moving-average
 */
export class MovingAverage {
    movingAverage;
    variance;
    deviation;
    forecast;
    timeSpan;
    previousTime;
    constructor(timeSpan) {
        this.timeSpan = timeSpan;
        this.movingAverage = 0;
        this.variance = 0;
        this.deviation = 0;
        this.forecast = 0;
    }
    alpha(t, pt) {
        return 1 - (Math.exp(-(t - pt) / this.timeSpan));
    }
    push(value, time = Date.now()) {
        if (this.previousTime != null) {
            // calculate moving average
            const a = this.alpha(time, this.previousTime);
            const diff = value - this.movingAverage;
            const incr = a * diff;
            this.movingAverage = a * value + (1 - a) * this.movingAverage;
            // calculate variance & deviation
            this.variance = (1 - a) * (this.variance + diff * incr);
            this.deviation = Math.sqrt(this.variance);
            // calculate forecast
            this.forecast = this.movingAverage + a * diff;
        }
        else {
            this.movingAverage = value;
        }
        this.previousTime = time;
    }
}
//# sourceMappingURL=moving-average.js.map