I'm conducting a set of computer simulations (of membrane). I sample a property (area, $a$) in the following manner:
$ A = \sum _1^N a \\ S = \sum _1^N a^2 $
During my data analysis, I calculate the variance:
$\text{Var} = S/N - (A/N)^2$
Now, I know that the variance should be positive but I get negative results. For example: $ N= 37956, A = 7764029.43233427, S = 1492730484.16737$. In my code, the summation is done by
A = A + a
S = S + a*a
So, what is wrong here?
This algorithm is prone to disastrous loss of precision due to subtraction of two almost-equal large numbers, especially if the standard deviation is small relative to the mean. Alternative computations with better numerical qualities can be found here:
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance