[MATRIX-SIG] random.gauss isn't Gaussian
Warren Focke
Warren Focke <warren@xtepca.gsfc.nasa.gov>
Fri, 30 Jan 1998 15:06:43 -0500 (EST)
On Fri, 30 Jan 1998, Christian Tismer wrote:
>
> > In addition, while looking into this, I found that the
> > test_generator function in random.py calculates standard
> > deviations as sqrt( Sum[ x*x ]/n - mean*mean ), which is
> > somewhat different from what I think of as the usual
> > definition - sqrt(Sum[ ( x - mean )^2 / N ]).
> >
> > Is this all intentional? Or is it a bug?
>
> The last one is definately no bug, but the formulas are equivalent
> what can be seen by two lines of transformations. For computational
> exactnes, the Python version might have slight advantages over
> the usual" one, since there is only a single subtraction. Having
> many mixed operations could worsen the result, especially for
> very skewed distributions (where the sdev makes no sense at all, btw).
>
Sum[ ( x - mean )^2 / N ]
is more accurate than
Sum[ x*x ]/n - mean*mean,
especially when the mean is much larger than the standard dev or there are
many points, because the two terms in the second formula tend to be large
and their difference small. Sometimes the difference in the second
formula comes out negative!
The advantage of the second formula is that it can be used to
calculate the std dev and the mean in a single pass through the data.
Warren Focke
_______________
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________