I know that calculating the new average $a_{new}$ from the old average $a_{old}$ can be done in the following way (for uniform weights):
Suppose the old average is based on $n$ elements. The old total is then $t_{old}=a_{old}\cdot n$. The new total is $t_{new}=t_{old}+x$. The new average is $a_{new}=\frac{t_{new}}{n+1}=\frac{a_{old}\cdot n + x}{n+1}$. (4 flops)
However, I found another way of calculating the new average $a_{new}$ from the old average $a_{old}$. I tried it numerical for some example, which is correct, but how can I prove it for all cases? My method is the following (also suitable for weights that are not uniform):
- Calculate the difference $d$ between the old average $a_{old}$ and the new element $x$ with $d=x-a_{old}$
- Multiply it with the weight $w$ of the new element $x$, so $q=d\cdot w$
- Add it to the old average $a_{old}$, so $a_{new}=a_{old}+q=a_{old}+(x-a_{old})\cdot w=x\cdot w + (1-w)\cdot a_{old}$.
The calculation $a_{new}=a_{old}+(x-a_{old})\cdot w$ has 3 flops
Which method is more efficient in terms of computing time? Or are they equally efficient?
\begin{align}a_{new}&=\frac{t_{new}}{n+1}\\&=\frac{a_{old}\cdot n + x}{n+1}\\ &= \frac{n}{n+1}a_{old}+\frac{1}{n+1}x\end{align}
The old formula requires one multiplication, two additions, and one division.
We have to set $w = \frac{1}{n+1}$ for the formula to be equal $a_{new}=a_{old}+q=a_{old}+(x-a_{old})\cdot w=x\cdot w + (1-w)\cdot a_{old}$
The new formula requires one subtraction, one division, one multiplication, and one addtion. The division is due to $\frac{1}{n+1}$.
Edit: consider data $x_1, \ldots, a_{n+1}$ with weight $w_1, \ldots, w_{n+1}$ \begin{align} a_{new} &= \frac{\sum_{i=1}^{n+1} w_ix_i}{\sum_{i=1}^{n+1} w_i} \\ &= \frac{\sum_{i=1}^{n} w_ix_i}{\sum_{i=1}^{n+1} w_i} + \frac{w_{n+1}x_{n+1}}{\sum_{i=1}^{n+1}w_i}\\ &= \frac{\sum_{i=1}^{n} w_ix_i}{\sum_{i=1}^{n} w_i} \left( \frac{\sum_{i=1}^{n} w_i}{\sum_{i=1}^{n+1} w_i}\right) + \frac{w_{n+1}x_{n+1}}{\sum_{i=1}^{n+1}w_i}\\ &= a_{old}\left( \frac{\sum_{i=1}^{n} w_i}{\sum_{i=1}^{n+1} w_i}\right) + \frac{w_{n+1}(x_{n+1}-a_{old}+a_{old)}}{\sum_{i=1}^{n+1}w_i} \\ &= a_{old} + \frac{w_{n+1}(x_{n+1}-a_{old})}{\sum_{i=1}^{n+1}w_i} \end{align}