Combining Means

Earlier I develop a handy-dandy little algorithm for combining two uncertain values in the form x1 ± u1 and x2 ± u2. The code presumes that the four values are ready to be popped off the stack:

float u2 = boundedInverseTransform(pop());
float x2 = boundedInverseTransform(pop());
float u1 = boundedInverseTransform(pop()); 
float x1 = boundedInverseTransform(pop());
push(boundedTransform((float)Math.sqrt(u1*u1 + u2*u2))); // uncertainty value
push(boundedTransform((x1+(u1*x2)/u2)/(1 + (u1/u2))));  // new mean value

This is not giving me good results: after some testing, I realized that it’s off. So let’s re-derive it right now:

In this diagram, x1 is the mean of one value, with uncertainty u1, and x2 and u2 play the same roles for the second distribution. Our goal is to find x3, the point that is equidistant from the two mean values, as measured in standard deviations (the u values). From this diagram, it really is easy to see the answer. The topmost horizontal line, whose length is x2 - x1. To do this, we just have to stretch the sum of u1 and u2 to equal the size of x2 - x1. Thus, we have:

x3 - x1 = u* [(x2 - x1) / (u1  +  u2)]

with a final result:

x3 = u1 * [(x2 - x1) / (u1  +  u2)] + x1

which produces this line of code:

push(boundedTransform((x1+(u1*((x2-x1)/(u1+u2))));

But this didn’t work, either. After much screwing around, I realized that the problem lay not in this code but instead in one of the parameters fed into it: one of the uncertainty values was negative. That’s a serious no-no, and completely screwed up the calculation. 

One other thing: the original form of the code was also correct; it merely represents a different expression of the same formula.