In the question "What's the numerically best way to calculate the average" it was suggested, that calculating a rolling mean, i.e.
mean = a[n]/n + (n-1)/n * mean
might be numerically more stable than calculating the sum and then dividing by the total number of elements. This was questioned by a commenter. I can not tell which one is true - can someone else? The advantage of the rolling mean is, that you keep the mean small (i.e. at roughly the same size of all vector entries). Intuitively this should keep the error small. But the commenter claims:
Part of the issue is that 1/n introduces errors in the least significant bits, so n/n != 1, at least when it is performed as a three step operation (divide-store-multiply). This is minimized if the division is only performed once, but you'd be doing it over GB of data.
So I have multiple questions:
- Is the rolling mean more precise than summing and then dividing?
- Does that depend on the question whether 1/n is calculated first and then multiplied?
- If so, do computers implement a one step division? (I thought so, but I am unsure now)
- If yes, is it more precise than Kahan summation and then dividing?
- If compareable - which one is faster? In both cases we have additional calculations.
- If more precise, could you use this for precise summation?