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
as the mean of the resulting series. I have edited the code a bit to make it more clear.