I have an iterator of numbers, for example a file object:
f = open("datafile.dat")
now I want to compute:
mean = get_mean(f)
sigma = get_sigma(f, mean)
What is the best implementation? Suppose that the file is big and I would like to avoid to read it twice.
If you want to iterate once, you can write your sum function:
and use the result in your
sigmafunction.Edit: now you can calculate the variance like this: (s2 – (s*s) / N) / N
By taking account of @Adam Bowen’s comment,
keep in mind that if we use mathematical tricks and transform the original formulas
we may degrade the results.