Incremental calculation of weighted mean and variance

Tony Finch

                     ⟨fanf2@cam.ac.uk⟩ ⟨dot@dotat.at⟩

University of Cambridge Computing Service

February 2009

Abstract In these notes I explain how to derive formulae for numerically stable calculation of the mean and standard deviation, which are also suitable for incremental on-line calculation. I then generalize these formulae to weighted means and standard deviations. I unpick the difficulties that arise when generalizing further to normalized weights. Finally I show that the exponentially weighted moving average is a special case of the incremental normalized weighted mean formula, and derive a formula for the exponentially weighted moving standard deviation.

1 Simple mean

Straightforward translation of equation 1 into code can suffer from loss of precision because of the difference in magnitude between a sample and the sum of all samples. Equation 4 calculates the mean in a way that is more numerically stable because it avoids accumulating large sums.

μ_{n} = \frac{1}{n} ∑_{i=1}^{n} x_{i}  (1)

= \frac{1}{n}(x_{n} + ∑_{i=1}^{n−1} x_{i})  (2)

= \frac{1}{n}(x_{n} + (n − 1)μ_{n−1})  (3)
= μ_{n−1} + \frac{1}{n}(x_{n} − μ_{n−1})  (4)

This formula also provides us with some useful identities.

x_{n} − μ_{n−1} = n(μ_{n} − μ_{n−1})  (5)
x_{n} − μ_{n} = n(μ_{n} − μ_{n−1}) − μ_{n} + μ_{n−1}
= (n − 1)(μ_{n} − μ_{n−1})  (6)

2 Simple variance

The definition of the standard deviation in equation 7 below requires us to already know the mean, which implies two passes over the data. This isn’t feasible for online algorithms that need to produce incremental results after each sample becomes available. Equation 12 solves this problem since it allows us to calculate the standard deviation from two running sums.

σ^{2} = \frac{1}{n} ∑_{i=1}^{n}(x_{i} − μ)^{2}  (7)

= \frac{1}{n} ∑_{i=1}^{n}(x_{i}^{2} − 2x_{i}μ + μ^{2})  (8)

= \frac{1}{n} ∑_{i=1}^{n} x_{i}^{2} − 2μ\frac{1}{n} ∑_{i=1}^{n} x_{i} + μ^{2} \frac{1}{n} ∑_{i=1}^{n} 1  (9)

= \frac{1}{n} ∑_{i=1}^{n} x_{i}^{2} − 2μμ + μ^{2} \frac{n}{n}  (10)

= \frac{1}{n} ∑_{i=1}^{n} x_{i}^{2} − μ^{2}  (11)

= (\frac{1}{n} ∑_{i=1}^{n} x_{i}^{2}) − (\frac{1}{n} ∑_{i=1}^{n} x_{i})^{2}  (12)

3 Incremental variance

Knuth notes [1] that equation 12 is prone to loss of precision because it takes the difference between two large sums of similar size, and suggests equation 24 as an alternative that avoids this problem. However he does not say how it is derived. In the following, equation 20 is derived from the previous step using equation 5.

Let S_{n} = nσ_{n}^{2}  (13)
= ∑_{i=1}^{n}(x_{i} − μ_{n})^{2}  (14)
= ∑_{i=1}^{n} x_{i}^{2} − nμ_{n}^{2}  (15)
S_{n} − S_{n−1} = ∑_{i=1}^{n} x_{i}^{2} − nμ_{n}^{2} − ∑_{i=1}^{n−1} x_{i}^{2} + (n − 1)μ_{n−1}^{2}  (16)
= x_{n}^{2} − nμ_{n}^{2} + (n − 1)μ_{n−1}^{2}  (17)
= x_{n}^{2} − μ_{n−1}^{2} + n(μ_{n−1}^{2} − μ_{n}^{2})  (18)
= x_{n}^{2} − μ_{n−1}^{2} + n(μ_{n−1} − μ_{n})(μ_{n−1} + μ_{n})  (19)
= x_{n}^{2} − μ_{n−1}^{2} + (μ_{n−1} − x_{n})(μ_{n−1} + μ_{n})  (20)
= x_{n}^{2} − μ_{n−1}^{2} + μ_{n−1}^{2} − x_{n}μ_{n} − x_{n}μ_{n−1} + μ_{n}μ_{n−1}  (21)
= x_{n}^{2} − x_{n}μ_{n} − x_{n}μ_{n−1} + μ_{n}μ_{n−1}  (22)
= (x_{n} − μ_{n−1})(x_{n} − μ_{n})  (23)
S_{n} = S_{n−1} + (x_{n} − μ_{n−1})(x_{n} − μ_{n})  (24)
σ_{n} = \sqrt{S_{n}/n}  (25)

Mathworld [2] has an alternative derivation of a similar formula, which in our notation is as follows.

S_{n} = ∑_{i=1}^{n}(x_{i} − μ_{n})^{2}  (26)

= ∑_{i=1}^{n}((x_{i} − μ_{n−1}) − (μ_{n} − μ_{n−1}))^{2}  (27)

= ∑_{i=1}^{n}(x_{i} − μ_{n−1})^{2} + ∑_{i=1}^{n}(μ_{n} − μ_{n−1})^{2} − 2 ∑_{i=1}^{n}(x_{i} − μ_{n−1})(μ_{n} − μ_{n−1})  (28)

Simplify the first summation:

∑_{i=1}^{n}(x_{i} − μ_{n−1})^{2} = (x_{n} − μ_{n−1})^{2} + ∑_{i=1}^{n−1}(x_{i} − μ_{n−1})^{2}  (29)
= (x_{n} − μ_{n−1})^{2} + S_{n−1}  (30)
= S_{n−1} + n^{2}(μ_{n} − μ_{n−1})^{2}  (31)

Simplify the second summation:

∑_{i=1}^{n}(μ_{n} − μ_{n−1})^{2} = n(μ_{n} − μ_{n−1})^{2}  (32)

Simplify the third summation:

∑_{i=1}^{n}(x_{i} − μ_{n−1})(μ_{n} − μ_{n−1}) = (μ_{n} − μ_{n−1}) ∑_{i=1}^{n}(x_{i} − μ_{n−1})  (33)

= (μ_{n} − μ_{n−1}) (x_{n} − μ_{n−1} + ∑_{i=1}^{n−1}(x_{i} − μ_{n−1}))  (34)

= (μ_{n} − μ_{n−1}) (x_{n} − μ_{n−1} − (n − 1)μ_{n−1} + ∑_{i=1}^{n−1} x_{i})  (35)
= (μ_{n} − μ_{n−1})(x_{n} − μ_{n−1} − (n − 1)μ_{n−1} + (n − 1)μ_{n−1}) (36)
= (μ_{n} − μ_{n−1})(x_{n} − μ_{n−1})  (37)
= n(μ_{n} − μ_{n−1})^{2}  (38)

Back to the complete formula:

S_{n} = S_{n−1} + n^{2}(μ_{n} − μ_{n−1})^{2} + n(μ_{n} − μ_{n−1})^{2} − 2n(μ_{n} − μ_{n−1})^{2}  (39)
= S_{n−1} + n^{2}(μ_{n} − μ_{n−1})^{2} − n(μ_{n} − μ_{n−1})^{2}  (40)
= S_{n−1} + n(n − 1)(μ_{n} − μ_{n−1})^{2}  (41)

We can use equations 6 and 5 to show this is equivalent to equation 24.

S_{n} = S_{n−1} + n(n − 1)(μ_{n} − μ_{n−1})^{2}  (42)
= S_{n−1} + n(μ_{n} − μ_{n−1})(x_{n} − μ_{n})  (43)
= S_{n−1} + (x_{n} − μ_{n−1})(x_{n} − μ_{n})  (44)

4 Weighted mean

The weighted mean is defined as follows.

μ = \frac{∑_{i=1}^{n} w_{i}x_{i}}{∑_{i=1}^{n} w_{i}}  (45)

It is equivalent to the simple mean when all the weights are equal, since

μ = \frac{∑_{i=1}^{n} wx_{i}}{∑_{i=1}^{n} w} = \frac{w ∑_{i=1}^{n} x_{i}}{nw} = \frac{1}{n} ∑_{i=1}^{n} x_{i}  (46)

If the samples are all different, then weights can be thought of as sample frequencies, or they can be used to calculate probabilities where p_{i} = w_{i}/ ∑ w_{i}. The following derivation of the incremental formula (equation 53) follows the same pattern as the derivation of equation 4. For brevity we also define W_{n} as the sum of the weights.

W_{n} = ∑_{i=1}^{n} w_{i}  (47)

μ_{n} = \frac{1}{W_{n}} ∑_{i=1}^{n} w_{i}x_{i}  (48)

= \frac{1}{W_{n}} (w_{n}x_{n} + ∑_{i=1}^{n−1} w_{i}x_{i})  (49)

= \frac{1}{W_{n}} (w_{n}x_{n} + W_{n−1}μ_{n−1})  (50)
= \frac{1}{W_{n}} (w_{n}x_{n} + (W_{n} − w_{n})μ_{n−1})  (51)
= \frac{1}{W_{n}} (W_{n}μ_{n−1} + w_{n}x_{n} − w_{n}μ_{n−1})  (52)
= μ_{n−1} + \frac{w_{n}}{W_{n}} (x_{n} − μ_{n−1})  (53)

Useful identities derived from this formula are:

W_{n}(μ_{n−1} − μ_{n}) = w_{n}(μ_{n−1} − x_{n})  (54)
\frac{W_{n}}{w_{n}} (μ_{n} − μ_{n−1}) = x_{n} − μ_{n−1}  (55)
x_{n} − μ_{n} = \frac{W_{n}}{w_{n}} (μ_{n} − μ_{n−1}) − μ_{n} + μ_{n−1}
= \frac{W_{n} − w_{n}}{w_{n}} (μ_{n} − μ_{n−1})  (56)
= \frac{W_{n} − w_{n}}{W_{n}} (x_{n} − μ_{n−1})  (57)

5 Weighted variance

Similarly, we derive a numerically stable formula for calculating the weighted variance (equation 68) using the same pattern as the derivation of the unweighed wersion (equation 24).

σ^{2} = \frac{1}{W_{n}} ∑_{i=1}^{n} w_{i}(x_{i} − μ)^{2} = \frac{1}{W_{n}} ∑_{i=1}^{n} w_{i}x_{i}^{2} − μ^{2}  (58)
Let S_{n} = W_{n}σ_{n}^{2}  (59)
= ∑_{i=1}^{n} w_{i}x_{i}^{2} − W_{n}μ_{n}^{2}  (60)

S_{n} − S_{n−1} = ∑_{i=1}^{n} w_{i}x_{i}^{2} − W_{n}μ_{n}^{2} − ∑_{i=1}^{n−1} w_{i}x_{i}^{2} + W_{n−1}μ_{n−1}^{2}  (61)
= w_{n}x_{n}^{2} − W_{n}μ_{n}^{2} + W_{n−1}μ_{n−1}^{2}  (62)
= w_{n}x_{n}^{2} − W_{n}μ_{n}^{2} + (W_{n} − w_{n})μ_{n−1}^{2}  (63)
= w_{n} (x_{n}^{2} − μ_{n−1}^{2}) + W_{n}(μ_{n−1}^{2} − μ_{n}^{2})  (64)
= w_{n} (x_{n}^{2} − μ_{n−1}^{2}) + W_{n}(μ_{n−1} − μ_{n})(μ_{n−1} + μ_{n})  (65)
= w_{n} (x_{n}^{2} − μ_{n−1}^{2} + (μ_{n−1} − x_{n})(μ_{n−1} + μ_{n}))  (66)
= w_{n}(x_{n} − μ_{n−1})(x_{n} − μ_{n})  (67)
S_{n} = S_{n−1} + w_{n}(x_{n} − μ_{n−1})(x_{n} − μ_{n})  (68)
σ_{n} = \sqrt{S_{n}/W_{n}}  (69)

6 Variable weights

In the previous three sections, I have assumed that weights are constant once assigned. However a common requirement is to normalize the weights, such that

W = ∑_{i=1}^{n} w_{i} = 1  (70)

If we are repeatedly adding new data to our working set, then we can’t have both constant weights and normalized weights. To allow us to keep weights normalized, we need to allow the weight of each sample to change as the set of samples changes. To indicate this we will give weights two indices, the first identifying the set of samples using the sample count (as we have been doing for μ_{n} etc.) and the second being the index of the sample in that set. We will not make any assumptions about the sum of the weights, that is we will not require them to be normalized. For example,

W_{n} = ∑_{i=1}^{n} w_{n,i}, μ_{n} = \frac{1}{W_{n}} ∑_{i=1}^{n} w_{n,i}x_{i}  (71)

Having done this we need to re-examine some of the logical steps in the previous sections to ensure they are still valid. In equations 49–51, we used the fact that in the fixed-weight setting,

∑_{i=1}^{n−1} w_{i}x_{i} = W_{n−1}μ_{n−1} = (W_{n} − w_{n})μ_{n−1}  (72)

In the new setting, this equality is fairly obviously no longer true. (For example, if we are keeping weights normalized then W_{n} = W_{n−1} = 1.) Fortunately there is a different middle step which justifies equation 72 when weights vary, so the results of section 4 remain valid.

∑_{i=1}^{n−1} w_{n,i}x_{i} = (W_{n} − w_{n,n})μ_{n−1}  (73)

∑_{i=1}^{n−1} w_{n,i}x_{i} = ∑_{i=1}^{n−1} w_{n,i} \frac{∑_{i=1}^{n−1} w_{n−1,i}x_{i}}{∑_{i=1}^{n−1} w_{n−1,i}}  (74)

\frac{∑_{i=1}^{n−1} w_{n,i}x_{i}}{∑_{i=1}^{n−1} w_{n,i}} = \frac{∑_{i=1}^{n−1} w_{n−1,i}x_{i}}{∑_{i=1}^{n−1} w_{n−1,i}}  (75)

∑_{i=1}^{n−1} \frac{w_{n,i}}{∑_{i=1}^{n−1} w_{n,i}} x_{i} = ∑_{i=1}^{n−1} \frac{w_{n−1,i}}{∑_{i=1}^{n−1} w_{n−1,i}} x_{i}  (76)
\frac{w_{n,j}}{∑_{i=1}^{n−1} w_{n,i}} = \frac{w_{n−1,j}}{∑_{i=1}^{n−1} w_{n−1,i}} where 1 ≤ j ≤ n − 1  (77)

This says that for the weighted mean formulae to remain valid the new and old weights should be consistent. Equation 75 says that we get the same result when we calculate the mean of the previous working set whether we use the old weights or the new weights. Equation 77 says that when we normalize the weights across the previous set (up to n − 1) we get the same set of weights whether we start from the old weights or the new ones. This requirement isn’t enough by itself to make the weighted variance formulae work, so we will examine it again below.

7 The expectation function

At this point it is worth defining some better notation to reduce the number of summations we need to write. The expectation function is a generalized version of the mean, whose argument is some arbitrary function of each sample.

E_{n}(f(x)) = \frac{1}{W_{n}} ∑_{i=1}^{n} w_{n,i}f(x_{i})  (78)
E_{n}(k) = k  (79)
E_{n}(af(x)) = aE_{n}(f(x))  (80)
E_{n}(f(x) + g(x)) = E_{n}(f(x)) + E_{n}(g(x))  (81)
μ_{n} = E_{n}(x)  (82)
σ_{n}^{2} = E_{n}((x − μ_{n})^{2})  (83)
= E_{n}(x^{2} + μ_{n}^{2} − 2μ_{n}x)  (84)
= E_{n}(x^{2}) + μ_{n}^{2} − 2μ_{n}E_{n}(x)  (85)

= E_{n}(x^{2}) − μ_{n}^{2}  (86)
= E_{n}(x^{2}) − E_{n}(x)^{2}  (87)

The incremental formula is derived in the usual way. Equation 92 is particularly useful.

W_{n}E_{n}(f(x)) = ∑_{i=1}^{n} w_{n,i}f(x_{i})  (88)
= w_{n,n}f(x_{n}) + ∑_{i=1}^{n−1} w_{n,i}f(x_{i})  (89)
= w_{n,n}f(x_{n}) + (W_{n} − w_{n,n})\frac{∑_{i=1}^{n−1} w_{n,i}f(x_{i})}{∑_{i=1}^{n−1} w_{n,i}}  (90)
= w_{n,n}f(x_{n}) + (W_{n} − w_{n,n})\frac{∑_{i=1}^{n−1} w_{n−1,i}f(x_{i})}{∑_{i=1}^{n−1} w_{n−1,i}}  (91)
= w_{n,n}f(x_{n}) + (W_{n} − w_{n,n})E_{n−1}(f(x))  (92)
E_{n}(f(x)) = E_{n−1}(f(x)) + \frac{w_{n,n}}{W_{n}} (f(x_{n}) − E_{n−1}(f(x)))  (93)

8 Variable-weight variance

In equations 61–63 we made the following assumptions which are not true when weights can vary.

∑_{i=1}^{n} w_{n,i}x_{i}^{2} − W_{n}μ_{n}^{2} − ∑_{i=1}^{n−1} w_{n−1,i}x_{i}^{2} + W_{n−1}μ_{n−1}^{2}

≠ w_{n,n}x_{n}^{2} − W_{n}μ_{n}^{2} + W_{n−1}μ_{n−1}^{2}
≠ w_{n,n}x_{n}^{2} − W_{n}μ_{n}^{2} + (W_{n} − w_{n,n})μ_{n−1}^{2}

If we try to re-do the short derivation of the incremental standard deviation formula starting from S_{n} − S_{n−1} then we soon get stuck. Fortunately the longer derivation shows how to made it work.

S_{n} = W_{n}σ_{n}^{2}  (94)
= W_{n}E_{n} ((x − μ_{n})^{2})  (95)
= W_{n}E_{n} (([x − μ_{n−1}] − [μ_{n} − μ_{n−1}])^{2})  (96)
= W_{n}E_{n} ([x − μ_{n−1}]^{2} + [μ_{n} − μ_{n−1}]^{2} − 2[x − μ_{n−1}][μ_{n} − μ_{n−1}])  (97)
= W_{n}E_{n} ([x − μ_{n−1}]^{2}) + W_{n}E_{n} ([μ_{n} − μ_{n−1}]^{2}) − 2W_{n}E_{n} ([x − μ_{n−1}][μ_{n} − μ_{n−1}]) (98)

Simplify the first term:

W_{n}E_{n} ([x − μ_{n−1}]^{2}) = w_{n,n}[x_{n} − μ_{n−1}]^{2} + (W_{n} − w_{n,n})E_{n−1} ([x − μ_{n−1}]^{2})  (99)
= w_{n,n}[x_{n} − μ_{n−1}]^{2} + (W_{n} − w_{n,n})\frac{S_{n−1}}{W_{n−1}}  (100)
= \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1} + \frac{W_{n}^{2}}{w_{n,n}} [μ_{n} − μ_{n−1}]^{2}  (101)

Simplify the second term:

W_{n}E_{n} ([μ_{n} − μ_{n−1}]^{2}) = W_{n}[μ_{n} − μ_{n−1}]^{2}  (102)

Simplify the third term:

W_{n}E_{n}([x − μ_{n−1}][μ_{n} − μ_{n−1}])

= [μ_{n} − μ_{n−1}]W_{n}E_{n}[x − μ_{n−1}]  (103)

= [μ_{n} − μ_{n−1}] (w_{n,n}[x_{n} − μ_{n−1}] + (W_{n} − w_{n,n})E_{n−1}[x − μ_{n−1}])  (104)
= [μ_{n} − μ_{n−1}] (w_{n,n}[x_{n} − μ_{n−1}] + (W_{n} − w_{n,n})[E_{n−1}(x) − E_{n−1}(μ_{n−1})])  (105)
= [μ_{n} − μ_{n−1}] (w_{n,n}[x_{n} − μ_{n−1}] + (W_{n} − w_{n,n})[μ_{n−1} − μ_{n−1}])  (106)
= [μ_{n} − μ_{n−1}]w_{n,n}[x_{n} − μ_{n−1}]  (107)
= W_{n}[μ_{n} − μ_{n−1}]^{2}  (108)

Back to the complete formula:

S_{n} = \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1} + \frac{W_{n}^{2}}{w_{n,n}} [μ_{n} − μ_{n−1}]^{2} + W_{n}[μ_{n} − μ_{n−1}]^{2} − 2W_{n}[μ_{n} − μ_{n−1}]^{2} (109)
= \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1} + \frac{W_{n}^{2}}{w_{n,n}} [μ_{n} − μ_{n−1}]^{2} − \frac{W_{n}w_{n,n}}{w_{n,n}} [μ_{n} − μ_{n−1}]^{2}  (110)
= \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1} + (W_{n} − w_{n,n})\frac{W_{n}}{w_{n,n}} [μ_{n} − μ_{n−1}]^{2}  (111)
= \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1} + (W_{n} − w_{n,n})[μ_{n} − μ_{n−1}](x_{n} − μ_{n−1})  (112)
S_{n} = \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1} + w_{n,n}(x_{n} − μ_{n})(x_{n} − μ_{n−1})  (113)

This is the same as equation 68, except for the multiplier \frac{W_{n}−w_{n,n}}{W_{n−1}} which captures the change in weights between the old and new sets.

\frac{W_{n} − w_{n,n}}{W_{n−1}} = \frac{∑_{i=1}^{n−1} w_{n,i}}{∑_{i=1}^{n−1} w_{n−1,i}} = \frac{w_{n,j}}{w_{n−1,j}} where 1 ≤ j ≤ n − 1  (114)

Now that we know the rescaling trick which makes it work, we can write down the short version.

S_{n} − \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1}

= W_{n} (E_{n}(x^{2}) − μ_{n}^{2}) − (W_{n} − w_{n,n}) (E_{n−1}(x^{2}) − μ_{n−1}^{2})  (115)
= W_{n} (E_{n}(x^{2}) − μ_{n}^{2}) − W_{n}E_{n}(x^{2}) + w_{n,n}x_{n}^{2} + (W_{n} − w_{n,n})μ_{n−1}^{2}  (116)
= w_{n,n}x_{n}^{2} − W_{n}μ_{n}^{2} + (W_{n} − w_{n,n})μ_{n−1}^{2}  (117)
= w_{n,n} (x_{n}^{2} − μ_{n−1}^{2}) + W_{n}(μ_{n−1}^{2} − μ_{n}^{2})  (118)
= w_{n,n} (x_{n}^{2} − μ_{n−1}^{2}) + W_{n}(μ_{n−1} − μ_{n})(μ_{n−1} + μ_{n})  (119)
= w_{n,n} (x_{n}^{2} − μ_{n−1}^{2} + (μ_{n−1} − x_{n})(μ_{n−1} + μ_{n}))  (120)
= w_{n,n}(x_{n} − μ_{n})(x_{n} − μ_{n−1})  (121)

9 Exponentially-weighted mean and variance

Starting from equation 53, let’s set w_{n,n}/W_{n} to a constant 0 < α < 1 and let a = 1 − α. This produces the standard formula for the exponentially weighted moving average.

μ_{n} = μ_{n−1} + α(x_{n} − μ_{n−1})  (122)
= (1 − α)μ_{n−1} + αx_{n}  (123)
= aμ_{n−1} + (1 − a)x_{n}  (124)

In the following it’s more convenient to use a lower bound of 0 instead of 1, i.e. 0 ≤ i ≤ n. We are going to show that the weights are renormalized each time a datum is added. First, we expand out the inductive definition of the mean.

μ_{n} = aμ_{n−1} + (1 − a)x_{n}  (125)
= a^{2}μ_{n−2} + a(1 − a)x_{n−1} + (1 − a)x_{n}  (126)

= a^{3}μ_{n−3} + a^{2}(1 − a)x_{n−2} + a(1 − a)x_{n−1} + (1 − a)x_{n}  (127)
μ_{n} = a^{n}x_{0} + ∑_{i=1}^{n} a^{n−i}(1 − a)x_{i}  (128)

This allows us to write down the weights directly. Note that w_{n,n} is independent of n.

w_{n,0} = a^{n}  (129)
w_{n,i} = a^{n−i}(1 − a), for 1 ≤ i ≤ n  (130)
w_{n,n} = 1 − a = α  (131)

Since w_{n,n} = α = w_{n,n}/W_{n} we can see that ∀n. W_{n} = 1, that is, the weights are always normalized. We can get the same result by summing the geometric series.

∑_{i=1}^{n} a^{n−i} = ∑_{j=0}^{n−1} a^{j} = \frac{1 − a^{n}}{1 − a}  (132)

∑_{i=1}^{n} w_{n,i} = ∑_{i=1}^{n} a^{n−i}(1 − a) = 1 − a^{n}  (133)

W_{n} = w_{n,0} + ∑_{i=1}^{n} w_{n,i} = a^{n} + (1 − a^{n}) = 1  (134)

These weights satisfy the consistency requirement because

\frac{w_{n,j}}{∑_{i=1}^{n−1} w_{n,i}} = \frac{aw_{n−1,j}}{∑_{i=1}^{n−1} aw_{n−1,i}} = \frac{w_{n−1,j}}{∑_{i=1}^{n−1} w_{n−1,i}}  (135)

We can use the expectation function to write down the naïve formula for the variance.

E_{n}(f(x)) = E_{n−1}(f(x)) + \frac{w_{n,n}}{W_{n}} (f(x_{n}) − E_{n−1}(f(x)))  (136)
= E_{n−1}(f(x)) + α(f(x_{n}) − E_{n−1}(f(x)))  (137)
E_{n}(x^{2}) = E_{n−1}(x^{2}) + α(x_{n}^{2} − E_{n−1}(x^{2}))  (138)
σ_{n}^{2} = E_{n}(x^{2}) − μ^{2}  (139)

So using the formula from the previous section we can write the incremental version:

S_{n} = \frac{W_{n} − w_{n,n}}{W_{n−1}} S_{n−1} + w_{n,n}(x_{n} − μ_{n})(x_{n} − μ_{n−1})  (140)
S_{n} = \frac{1 − α}{1}S_{n−1} + α(x_{n} − μ_{n})(x_{n} − μ_{n−1})  (141)
σ_{n}^{2} = \frac{S_{n}}{W_{n}} = S_{n} = aS_{n−1} + (1 − a)(x_{n} − μ_{n})(x_{n} − μ_{n−1})  (142)
= (1 − α)(S_{n−1} + α(x_{n} − μ_{n−1})^{2})  (143)

This latter form is slightly more convenient for code:

  diff := x - mean
  incr := alpha * diff
  mean := mean + incr
  variance := (1 - alpha) * (variance + diff * incr)

References

[1] Donald E. Knuth. Seminumerical Algorithms, volume 2 of The Art of Computer Programming, chapter 4.2.2, page 232. Addison-Wesley, Boston, third edition, 1998.

[2] Eric W. Weisstein. Sample variance computation. From Mathworld, a Wolfram web resource,

   http://mathworld.wolfram.com/SampleVarianceComputation.html.