If you have a stream of numbers and you don’t want to store them or run in two pass to get the average, you might do:
but this can be unwieldy if you need to know the average at any time, as you need to store the sum and the count.
The following update rule allows you to easily accumulate the average in one pass, storing only the average itself:
\[\begin{align*} \mu_{n+1} &= \mu_n + \frac{x_{n+1} - \mu_n}{n+1} \\ \end{align*}\]