You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Mark R. Diggory" <md...@latte.harvard.edu> on 2003/05/14 19:23:54 UTC

[math] error in variance calculation...

I noticed the following code and was curious (as I do it differently in 
my packages):

     public double getVariance() {
         double xbar = getMean();
         // FIXME: throw something meaningful if n = 0
         return (sumsq - xbar*xbar*n)/(n-1);
     }

This is equivalent to:

     public double getVariance() {
	return (sumsq - (sum*sum / n)) / (n - 1);
     }

If ones storing the sum already, its just as easy to use it to calculate 
the variance rather than grabbing the mean. Is either way 
computationally better?

-M.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [math] error in variance calculation...

Posted by Phil Steitz <ph...@steitz.com>.
Mark R. Diggory wrote:
> My apologies, the subject heading is incorrect, there is no error, but 
> WOW, didn't that grab your attention!

OK, maybe that makes us even today ;-)

The second uses arithmetic operations and also avoids the function call, 
so it is in theory better.  Optimization might eliminate all but the 
stack operation advantage, but that alone probably justifies the change.

+1 to change this -- actually to add "investigate numerical optimization 
of all computations in current code" to TO DO list.

Phil

> 
> -M.
> 
> Mark R. Diggory wrote:
> 
>> I noticed the following code and was curious (as I do it differently 
>> in my packages):
>>
>>     public double getVariance() {
>>         double xbar = getMean();
>>         // FIXME: throw something meaningful if n = 0
>>         return (sumsq - xbar*xbar*n)/(n-1);
>>     }
>>
>> This is equivalent to:
>>
>>     public double getVariance() {
>>     return (sumsq - (sum*sum / n)) / (n - 1);
>>     }
>>
>> If ones storing the sum already, its just as easy to use it to 
>> calculate the variance rather than grabbing the mean. Is either way 
>> computationally better?
>>
>> -M.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [math] error in variance calculation...

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
My apologies, the subject heading is incorrect, there is no error, but 
WOW, didn't that grab your attention!

-M.

Mark R. Diggory wrote:
> I noticed the following code and was curious (as I do it differently in 
> my packages):
> 
>     public double getVariance() {
>         double xbar = getMean();
>         // FIXME: throw something meaningful if n = 0
>         return (sumsq - xbar*xbar*n)/(n-1);
>     }
> 
> This is equivalent to:
> 
>     public double getVariance() {
>     return (sumsq - (sum*sum / n)) / (n - 1);
>     }
> 
> If ones storing the sum already, its just as easy to use it to calculate 
> the variance rather than grabbing the mean. Is either way 
> computationally better?
> 
> -M.
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org