56 lines
2.0 KiB
JavaScript
56 lines
2.0 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
/*
|
|
* compute an Exponential Weighted moving average
|
|
* - https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
|
|
* - heavily inspired from shaka-player
|
|
*/
|
|
|
|
var EWMA = function () {
|
|
|
|
// About half of the estimated value will be from the last |halfLife| samples by weight.
|
|
function EWMA(halfLife) {
|
|
_classCallCheck(this, EWMA);
|
|
|
|
// Larger values of alpha expire historical data more slowly.
|
|
this.alpha_ = halfLife ? Math.exp(Math.log(0.5) / halfLife) : 0;
|
|
this.estimate_ = 0;
|
|
this.totalWeight_ = 0;
|
|
}
|
|
|
|
_createClass(EWMA, [{
|
|
key: "sample",
|
|
value: function sample(weight, value) {
|
|
var adjAlpha = Math.pow(this.alpha_, weight);
|
|
this.estimate_ = value * (1 - adjAlpha) + adjAlpha * this.estimate_;
|
|
this.totalWeight_ += weight;
|
|
}
|
|
}, {
|
|
key: "getTotalWeight",
|
|
value: function getTotalWeight() {
|
|
return this.totalWeight_;
|
|
}
|
|
}, {
|
|
key: "getEstimate",
|
|
value: function getEstimate() {
|
|
if (this.alpha_) {
|
|
var zeroFactor = 1 - Math.pow(this.alpha_, this.totalWeight_);
|
|
return this.estimate_ / zeroFactor;
|
|
} else {
|
|
return this.estimate_;
|
|
}
|
|
}
|
|
}]);
|
|
|
|
return EWMA;
|
|
}();
|
|
|
|
exports.default = EWMA; |