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?