45
$\begingroup$

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?

$\endgroup$

4 Answers 4

83
$\begingroup$

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

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

$\endgroup$
6
  • 26
    $\begingroup$ 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! $\endgroup$
    – Ty H.
    Commented Nov 13, 2014 at 16:37
  • 3
    $\begingroup$ @501 - not implemented thank you! $\endgroup$
    – ThelmaJay
    Commented Jan 13, 2015 at 14:34
  • 1
    $\begingroup$ This answer was exactly what I needed. Elegant! $\endgroup$
    – visc
    Commented Nov 16, 2016 at 23:02
  • $\begingroup$ -1 Careful this formula works only for adding 1 new value! Please work out the correct equation using this answer and you'll see! math.stackexchange.com/a/1153800/503549 $\endgroup$
    – kouton
    Commented Nov 16, 2017 at 4:25
  • 2
    $\begingroup$ @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. $\endgroup$
    – Jamie
    Commented Apr 5, 2022 at 0:14
38
$\begingroup$

$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}$.

$\endgroup$
5
  • 3
    $\begingroup$ And $ns$ is the old sum, as you can derive from the first equation. $\endgroup$ Commented Feb 16, 2011 at 13:21
  • 2
    $\begingroup$ I'm tempted to downvote as ns is not explained in the answer $\endgroup$
    – Celeritas
    Commented Aug 22, 2014 at 20:32
  • $\begingroup$ At first I commented that @501's answer was clearer but realized I didn't fully understand why the question worked. This answer, while denser, actually explains the result. $\endgroup$
    – jds
    Commented Dec 10, 2014 at 23:05
  • 4
    $\begingroup$ @Celeritas $ns = n \cdot s$, $n$ and $s$ are defined quantities in the post. $\endgroup$
    – Axoren
    Commented Jan 13, 2015 at 15:13
  • $\begingroup$ This is the working answer. @501's answer doesn't work when increasing n by more than 1. $\endgroup$
    – kouton
    Commented Nov 16, 2017 at 4:26
26
$\begingroup$

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)
$\endgroup$
3
  • 10
    $\begingroup$ Oh thank god there's another programmer on here. Those math equations make my head hurt. $\endgroup$
    – Matt
    Commented Jan 5, 2019 at 6:38
  • $\begingroup$ @MattD: see also this answer on StackOverflow for replacing a number in a running average. $\endgroup$ Commented Jan 13, 2020 at 5:03
  • 3
    $\begingroup$ Shouldn't the first formula end with / nValues + 1) rather than / nValues)? $\endgroup$
    – Magnus
    Commented Jun 18, 2021 at 19:56
3
$\begingroup$

Another formula can be

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – dekuShrub
    Commented Mar 3, 2022 at 6:57
  • $\begingroup$ And also easy for multiple values: newAvg = (sum1 + sum2) / (size1 + size2). Or in the other format newAvg = (avg1 * size1 + avg2 * size2) / (size1 + size2). $\endgroup$
    – dekuShrub
    Commented Mar 3, 2022 at 7:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .