45

Say I have 100 numbers that are averaged:

number of values = 100
total sum of values = 2000
mean = 2000 / 100 => 20

If I want to add a value and find out the new average:

total sum of values = 2000 + 100
mean = 2100 / 101 => 20.79

If I want to subtract a value and find out the new average:

total sum of values = 2100 - 100
mean = 2000 / 100 => 20

It seems to work, but is the above correct?

Is this the proper way to add/subtract values from a average without having to re-sum all the 100 numbers first?

Zabba
  • 553

4 Answers4

83

I know that's an old thread but I had the same problem. I want to add a value to an existing average without having to calculate the total sum again.

to add a value to an exisitng average we only must know for how many values the average was calculated for: $$ average_{new} = average_{old} + \frac{ value_{new} - average_{old}}{size_{new}} $$

  • 26
    I may be a smart guy, but the accepted answer above was incomprehensible to me. This answer works perfectly, and I can understand it. Thanks! – Ty H. Nov 13 '14 at 16:37
  • 3
    @501 - not implemented thank you! – ThelmaJay Jan 13 '15 at 14:34
  • 1
    This answer was exactly what I needed. Elegant! – visc Nov 16 '16 at 23:02
  • -1 Careful this formula works only for adding 1 new value! Please work out the correct equation using this answer and you'll see! https://math.stackexchange.com/a/1153800/503549 – kouton Nov 16 '17 at 04:25
  • Is it possible to calculate median in a similar way? – Przemysław Niedziela Mar 09 '20 at 22:14
  • 3
    @kouton why would you downvote for an answer that only works for adding one value when the requirement was "...to add A value..." (emphasis added). If I ask you to run a mile and you run a mile, I'm not going to penalize you for not running two miles. – Jamie Apr 05 '22 at 00:14
38

$s=\frac{a_1+...+a_n}{n}$.

If you want the average of $a_1,...,a_n$ and $a_{n+1}$, then $s'=\frac{a_1+...+a_n+a_{n+1}}{n+1}=\frac{ns+a_{n+1}}{n+1} = \frac{(n+1)s+a_{n+1}}{n+1} - \frac{s}{n+1} = s + \frac{a_{n+1}-s}{n+1}$

If you want the average of $a_1,...,a_{n-1}$ then $s''=\frac{a_1+...+a_{n-1}}{n-1}=\frac{ns-a_n}{n-1}= \frac{(n-1)s-a_n}{n-1} + \frac{s}{n-1}=s+\frac{s-a_n}{n-1}$.

Weltschmerz
  • 7,148
27

To put it programmatically, and since the question was about how to both add and subtract:

Add a value:

average = average + ((value - average) / (nValues + 1))

Subtract a value:

average = (average * nValues - value) / (nValues - 1)
9 Guy
  • 103
Damien
  • 511
4

Another formula can be

$$\text{NewAvg} = \frac{( \text{OldAvg} \cdot \text{OldSize} ) + \text{NewValue} } { \text{NewSize}}$$

656475
  • 5,473
  • 1
    I was looking for this answer. It's intuitive because newAvg = (sum + newValue) / newSize (by definition of average). And also by definition sum = avg * size. – dekuShrub Mar 03 '22 at 06:57
  • And also easy for multiple values: newAvg = (sum1 + sum2) / (size1 + size2). Or in the other format newAvg = (avg1 * size1 + avg2 * size2) / (size1 + size2). – dekuShrub Mar 03 '22 at 07:00