Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2022-01-05.15:39:37.204>created_at=<Date2022-01-04.17:04:05.171>labels= ['3.11', 'library', 'performance']
title='Convert statistics sum of squares to a single pass algorithm'updated_at=<Date2022-01-05.15:39:37.203>user='https://github.com/rhettinger'
The existing code makes two passes, one to compute the mean and another to compute the sum of squared differences from the mean. A consequence of making two passes is that iterator inputs must be converted to a list before processing. This throws away the memory saving advantages of iterators.
The ostensible reason for the two pass code is that the single pass variant is numerically unstable when implemented with floating point accumulators. However, this code uses fractions throughout, so the accumulation is exact.
Changing to a single pass saves memory, doubles the speed, and simplifies the upstream code in variance(), pvariance(), stdev(), and pstdev().
Activity
rhettinger commented on Jan 4, 2022
The existing code makes two passes, one to compute the mean and another to compute the sum of squared differences from the mean. A consequence of making two passes is that iterator inputs must be converted to a list before processing. This throws away the memory saving advantages of iterators.
The ostensible reason for the two pass code is that the single pass variant is numerically unstable when implemented with floating point accumulators. However, this code uses fractions throughout, so the accumulation is exact.
Changing to a single pass saves memory, doubles the speed, and simplifies the upstream code in variance(), pvariance(), stdev(), and pstdev().
rhettinger commented on Jan 5, 2022
New changeset 43aac29 by Raymond Hettinger in branch 'main':
bpo-46257: Convert statistics._ss() to a single pass algorithm (GH-30403)
43aac29