Skip to content

EWStats: following a drift

← Moments

EWStats keeps the mean and the variance of a stream but weights the recent readings more, so it follows a change instead of averaging it away.

A three-dimensional surface: payments seen on one axis, the forgetting factor alpha on another and the average payment in euros on the vertical axis. The true average jumps from 12 to 35 euros at payment 300; the surface rises after payment 300, faster for a larger alpha, and the front curve of RunningMoments stays far below, at 23.4 euros at payment 600.

The picture is a stream of card payments simulated 40 times, the average of the 40 shown. The average payment is 12 euros until payment 300 and 35 euros after it, and the surface is the mean that EWStats reports after each payment, for each forgetting factor alpha. At payment 350 RunningMoments (the front curve) says 15.2 euros and at payment 600 it says 23.4, still far from 35, because it averages the whole past with the new regime. EWStats with alpha = 0.052 says 33.2 at payment 350 and 34.6 at payment 600; with alpha = 0.005 it says 17.1 and 29.8.

1. Following a drift

Averaging over the whole history is the wrong answer when the world drifts. A robot picks up a payload, a sensor warms up, a network changes: the old samples describe a regime that no longer exists.

from dense_armor.core.signals.stats.moments import EWStats

e = EWStats(alpha=0.05)
for _ in range(500):
    e.learn_one({"x": 1.0})
for _ in range(500):
    e.learn_one({"x": 5.0})
print(round(e.mean, 3))
5.0

After a thousand samples, split evenly between the two regimes, the mean is 5.0: the exponentially weighted mean remembers the recent regime, not the historical average of 3.0. The dashed line in the figure of the first page is this estimator applied to a stream that steps from 0 to 3.

2. The forgetting factor

The running mean becomes

\[ \text{mean}_t = (1 - \alpha)\,\text{mean}_{t-1} + \alpha\, x_t \]

where \(\alpha\) is a number between 0 and 1 called the forgetting factor. \(\alpha = 1\) copies the last value; \(\alpha = 0.05\) corresponds roughly to averaging over the last twenty samples. The variance follows West (1979), so it also forgets:

\[ \begin{aligned} \delta &= x_t - \text{mean}_{t-1} \\\ \text{mean}_t &= \text{mean}_{t-1} + \alpha\,\delta \\\ \text{var}_t &= (1 - \alpha)\,\bigl(\text{var}_{t-1} + \alpha\,\delta^2\bigr) \end{aligned} \]

The first sample sets the mean and starts the variance at 0. A small \(\alpha\) is steady and slow to follow a change; a large \(\alpha\) follows quickly and is nervous.

A stream that steps from 1 to 5 at sample 300. The running mean of RunningMoments rises slowly and is still near 3 at the end. EWStats with alpha 0.5 follows the stream and its noise; with alpha 0.05 it reaches the new level in about 100 samples; with alpha 0.005 it is still climbing at the end.

The figure shows the three choices on a stream that steps from 1 to 5 at sample 300. With \(\alpha = 0.5\) the mean follows every wiggle of the noise; with \(\alpha = 0.05\) it reaches the new level in about 100 samples and is steady; with \(\alpha = 0.005\) it is still climbing at the end. The black line, RunningMoments, averages the whole past and stays near 3.

3. A hand case

Take the five samples \(1, 2, 3, 4, 5\) with \(\alpha = 0.5\). The mean goes \(1,\ 1.5,\ 2.25,\ 3.125,\ 4.0625\) and the variance goes \(0,\ 0.25,\ 0.6875,\ 1.109375,\ 1.43359375\). For the last sample, \(\delta = 5 - 3.125 = 1.875\) and

\[ \text{var}_5 = 0.5\,\bigl(1.109375 + 0.5 \cdot 1.875^2\bigr) = 1.43359375 \]

The code produces exactly these numbers:

from dense_armor.core.signals.stats.moments import EWStats

e = EWStats(alpha=0.5)
for v in [1.0, 2.0, 3.0, 4.0, 5.0]:
    e.learn_one({"x": v})
print(e.mean, e.var)
4.0625 1.43359375

The mean 4.0625 is far above the plain average 3.0, because the last readings weigh most.

Previous: merging and many channels.

Details

References

  • West, D. H. D. (1979). Updating mean and variance estimates: an improved method. Communications of the ACM 22(9), 532–535: the exponentially weighted variance.

The class

EWStats(alpha=0.1, feature=None, dtype=None) is a running mean and variance with an exponential forgetting factor. It subclasses dense_armor.roles.Transformer and passes dense_armor.checks.check_estimator.

Numeric results on this page

Quantity Value
EWStats(alpha=0.5) on [1..5] mean 4.0625, variance 1.43359375
EWStats(alpha=0.05) on 500 + 500 samples of 1 and 5 mean 5.0

The figures on this page

docs/assets/moments/moments_alpha.png is generated by docs/assets/moments/make_moments_pages.py: a stream of 300 samples around 1 and 300 around 5, with noise of standard deviation 0.3, followed by RunningMoments and by EWStats with \(\alpha\) of 0.5, 0.05 and 0.005.

The image docs/assets/moments/moments_stream.png, on the first page, is generated by docs/assets/moments/make_figure.py. The script builds a stream of 400 samples from a Gaussian whose mean shifts from 0 to 3 at sample 200, feeds it into RunningMoments and EWStats(alpha=0.05), and plots the running mean, the running standard deviation and the exponentially weighted mean against the raw trace.

The image docs/assets/moments/moments_3d.png is generated by docs/assets/moments/make_moments_3d.py. The script simulates a stream of 600 payments 40 times, with amounts drawn from a gamma distribution whose mean is 12 euros before payment 300 and 35 euros after it, feeds each stream into RunningMoments and into EWStats for 36 values of \(\alpha\) between 0.005 and 0.15, and draws the mean of the 40 means.