I am having trouble with calculating a mean of vector with sufficient accuracy. My current solution which works but it quite slow and has unpredictable performance:
mean_sum = mean = math.fsum(values) / n values = values - mean mean = math.fsum(values) / n while np.abs(mean) > np.finfo(np.float32).eps: values = values - mean mean_sum += mean mean = math.fsum(values) / n return mean_sum, values
Is there a better way?
EDIT: All the values are non-negative. The accuracy required is defined by
np.finfo(np.float32).eps
as the mean of the resulting series. I have edited the code a bit to make it more clear.
n
inside your loop, rather than just the mean? Look at the "pseudocode" link in my answer and not how only the sums are accumulated with the loop. If you really do need to access the current mean ass you sum your vector, do that in addition to a variable for accumulating. $\endgroup$