Calculating a new average without having the full set of no.s

1.5k Views Asked by At

Say we have the below data:

  • 100
  • 120
  • 80
  • 110

The average of these is 84.5

Is there any way, I can store some data from this original data and calculate new average when a new no. is added? For example, now a new no. 100 is added.

Now if I know the count of no.s till now was 4 and the old average was 84.5 is there a way to get new average without knowing what those 4 no.s was? Is there a mathematical solution to store some other single data and to avoid knowing all no.s for future calculations?

I am exploring this to solve a problem where there will be 1000s of digits and it is hard to be stored. In the use case, absolute accuracy is not a requirement.

2

There are 2 best solutions below

1
On BEST ANSWER

You indeed don't need the numbers, only their sum (and count).

In the given example after 3 numbers say you have only the average 100. And the forth number is 110 then the new average will be:

$$ \frac{100*3 + 110}{4} = 102.5 $$

1
On

Yes. It's called a cumulative average. In your example case, since you know the current average 84.5 represents the average of 4 numbers, the updated average will be (84.5 * 4 + 100) / 5

The logic here is since an average is the sum divided by the count, you just multiply the previous average (84.5) by the previous count (4) to get the previous sum. Then you add your new number (100). Then you divide the new sum by the new count (5) to get the new average.