HOW TO: calculate an average without remembering a sum
This is some basic 8th grade shit that I've since forgotten, so this may be a no-brainer to you mathletics. Today at work I needed to calculate an average market price from yesterday's transactions, but since I had to hold all of these numbers in a data structure, I really didn't want to hold onto the running sum.
Let's break it down:
Avg = (n1 + n2) / number of items
If we add a new number to be averaged, we get:
Avg = (n1 + n2 + n3) / number of items
The downside of this is that you have to keep track of all the numbers to be averaged. To simplify, instead of remembering all the n's, you can just keep a sum:
Avg = (old sum + n3) / number of items
... but you're still remembering four numbers:
old sum
n3
number of items
average
In my case, it's a real pain to hang on to extra numbers, so how can we compute the average using only the current avg, number of items and n3?
Like this:
Avg = ((number of items * current avg) + n3) / (number of items + 1)
Or similarly in a different format:
Avg = (current avg + ( (n3 - current avg) / (number of items + 1) )
The thing to note is that since you're continually re-computing the average rather than one time off of a sum, your average may begin to deviate, especially if you're using decimals.
So yeah, probably simple to some, but 8th grade was quite some time ago.
Still can't figure out if there's a specific name for computing averages without sums. Please feel free to enlighten!