Class: Ewma
Defined in: Ewma.ts:12
Exponential Weighted Moving Average (EWMA)
Each sample has a weight, for example the duration of a download in seconds. A half-life of h weight units corresponds to an alpha of Math.exp(Math.log(0.5) / h).
Example
// A half-life of 2 seconds
const ewma = new Ewma(Math.exp(Math.log(0.5) / 2))
// 500 bps over 2 seconds
ewma.sample(2, 500)
// 1000 bps over 4 seconds
ewma.sample(4, 1000)
equal(roundToEven(ewma.getEstimate(), 2), 928.57)
equal(ewma.getTotalDuration(), 6)Constructors
Constructor
new Ewma(alpha): Ewma;Defined in: Ewma.ts:23
Parameters
alpha
number
The smoothing factor, between 0 and 1. A larger alpha expires old samples more slowly.
Returns
Ewma
Methods
getEstimate()
getEstimate(): number;Defined in: Ewma.ts:53
Returns the estimate of the value (in units, as value during sampling)
For example, if the value is throughput (in bits per seconds), the estimate will also be in bits per seconds.
Returns
number
getTotalDuration()
getTotalDuration(): number;Defined in: Ewma.ts:68
Returns the total duration of the resource (in units, as weight during sampling)
Returns
number
sample()
sample(weight, value): void;Defined in: Ewma.ts:35
Samples the value of the resource and updates the estimate.
The new value has the weight 1 - alpha ^ weight. The method ignores a sample that would make the estimate NaN.
Parameters
weight
number
value
number
Returns
void