3
$\begingroup$

Consider I have two sequences of numbers: 1 and 2. I don't have these sequences exactly but I know their means and numbers of elements in them: mean1, mean2, count1, count2.

I need to calculate mean of both sequences together knowing only means and counts of those sequences separately. The obvious formula is: mean12 = (mean1 * count1 + mean2 * count2)/(count1 + count2).

What I want to know: is there a way to calculate mean12 without multiplying mean1 * count1 and mean2 * count2. The reason for this: for my task mean and count could be huge numbers and multiplication could cause overflow, so I want to know if there is another solution, which avoids multiplication of those huge numbers.

$\endgroup$
1
  • 2
    $\begingroup$ If you're computing means in a computer program, you are probably working with floating-point numbers, which are much harder to overflow than your regular 32-bit signed integers. $\endgroup$
    – Magma
    Sep 24 at 19:16
8
$\begingroup$

How about $$ \text{mean}1 \times \frac{\text{count}1}{\text{count}1+\text{count}2} + \text{mean}2 \times \frac{\text{count}2}{\text{count}1+\text{count}2}$$

This way you're only ever multiplying by a number between $0$ and $1$.

$\endgroup$
2
  • 2
    $\begingroup$ If this is supposed to be implemented in a computer program, make sure you aren't performing integer division, otherwise $\frac{count1}{count1+count2}$ will likely evaluate to zero. $\endgroup$
    – Magma
    Sep 24 at 19:15
  • $\begingroup$ I feel a bit stupid since it's so obvious and easy, thx :) $\endgroup$
    – Kivan
    Sep 25 at 1:57
3
$\begingroup$

This can be done using only divisions:

$$\dfrac{\dfrac{mean_1}{count_2}+\dfrac{mean_2}{count_1}}{\dfrac{1}{count_2}+\dfrac{1}{count_1}}$$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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