# Regular average calculated accumulatively

is it possible to calculate the regular average of a sequence of numbers when i dont know everything of the sequence, but just everytime i get a new number i know the total count of numbers and the average for the numbers - 1.

for example: 2 3 10 the average is of course: 5

but in the last step to calculate i only have access to the previous average of 2 and 3: 2.5 the next number: 10 and the count of numbers: 3

if this is possible, how?

-

## 1 Answer

Yes, and you can derive it from the expression for the average. Let the average of the first $n$ numbers be $\mu_n$. The formula for it is

$$\mu_n = \frac{1}{n} \sum_{i=1}^n x_i$$

Then you can derive

$$n \mu_n = \sum_{i=1}^nx_i = x_n + \sum_{i=1}^{n-1} x_i = x_n + (n-1)\mu_{n-1}$$

and hence, dividing by $n$,

$$\mu_n = \frac{(n-1) \mu_{n-1} + x_n}{n}$$

i.e. to calculate the new average after then $n$th number, you multiply the old average by $n-1$, add the new number, and divide the total by $n$.

In your example, you have the old average of 2.5 and the third number is 10. So you multiply 2.5 by 2 (to get 5), add 10 (to get 15) and divide by 3 (to get 5, which is the correct average).

Note that this is functionally equivalent to keeping a running sum of all the numbers you've seen so far, and dividing by $n$ to get the average whenever you want it (although, from an implementation point of view, it may be better to compute the average as you go using the formula I gave above. For example, if the running sum ever gets larger than $10^{308}$ish then it may be too large to represent as a standard floating point number, even though the average can be represented).

-

Thanks alot man. You saved me alot of time :) – bksi Dec 8 '12 at 12:51
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 8, "mathjax_display_tex": 3, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9256511330604553, "perplexity_flag": "head"}