Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|cite|improve this question
up vote 13 down vote accepted

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

share|cite|improve this answer
2  
And $ns$ is the old sum, as you can derive from the first equation. – Ross Millikan Feb 16 '11 at 13:21
    
I'm tempted to downvote as ns is not explained in the answer – Celeritas Aug 22 '14 at 20:32
    
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. – gwg Dec 10 '14 at 23:05
    
@Celeritas $ns = n \cdot s$, $n$ and $s$ are defined quantities in the post. – Axoren Jan 13 '15 at 15:13

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

share|cite|improve this answer
6  
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
2  
@501 - not implemented thank you! – ThelmaJay Jan 13 '15 at 14:34

To put it on a programmatic way, and since the question is add / substract:

Substract a value:

average = ((average * nbValues) - value) / (nbValues - 1);

Add a value:

average = average + ((value - average) / nbValues)
share|cite|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.