Repository navigation
Rounding errors with statistics.variance #64698
Copy link
Copy link
Closed
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
| BPO | 20499 |
|---|---|
| Nosy | @rhettinger, @stevendaprano, @wm75, @iritkatriel |
| PRs | |
| Files |
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 = 'https://github.com/rhettinger'
closed_at = <Date 2021-09-09.03:06:34.176>
created_at = <Date 2014-02-03.12:46:19.312>
labels = ['type-feature', 'library', '3.9', '3.10', '3.11']
title = 'Rounding errors with statistics.variance'
updated_at = <Date 2021-09-09.12:02:18.658>
user = 'https://bugs.python.org/oscarbenjamin'bugs.python.org fields:
activity = <Date 2021-09-09.12:02:18.658>
actor = 'steven.daprano'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2021-09-09.03:06:34.176>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2014-02-03.12:46:19.312>
creator = 'oscarbenjamin'
dependencies = []
files = ['33955', '33958', '33959', '33960']
hgrepos = []
issue_num = 20499
keywords = ['patch']
message_count = 15.0
messages = ['210121', '210452', '210456', '210459', '210479', '210481', '210491', '210495', '210700', '210702', '399940', '401432', '401433', '401464', '401465']
nosy_count = 5.0
nosy_names = ['rhettinger', 'steven.daprano', 'oscarbenjamin', 'wolma', 'iritkatriel']
pr_nums = ['28230', '28248']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue20499'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Activity
oscarbenjamin commentedon Feb 3, 2014
The mean/variance functions in the statistics module don't quite round correctly.
The reasons for this are that although exact rational arithmetic is used internally in the _sum function it is not used throughout the module. In particular the _sum function should be changed to return an exact result and exact arithmetic should be used right up to the point before returning to the user at which point a rounding/coercion should be used to give the user their answer in the appropriate type correctly rounded once.
Using exact arithmetic everywhere makes it possible to replace all of the variance* functions with single-pass algorithms based on the computational formula for variance which should be more efficient as well.
For example the following implements pvariance so that it returns a perfectly rounded result for float input and output in a single pass:
Comparing the above with the statistics module:
The true answer is:
The logic in the _sum function for computing exact integer ratios and coercing back to the output type could be moved into utility functions so that it does not need to be duplicated.
Some examples of rounding issues:
Variance with ints or floats returns a float but the float is not quite the nearest possible float:
Another example with Decimal:
The mean function may also not be correctly rounded:
wm75 commentedon Feb 7, 2014
I have written a patch for this issue (I'm uploading the complete new code for everyone to try it - importing it into Python3.3 works fine; a diff with additional tests against Oscar's example will follow soon).
Just as Oscar suggested, this new version performs all calculations using exact rational arithmetics and rounds/coerces only before returning the final result to the user. Its precision is, thus, only limited by that of the input data sequence.
It passes Oscar's examples 1-3 as you can easily test yourself. It also gives the correct answer in the fourth example - mean([D('1.2'), D('1.3'), D('1.55')]) -, although on my system the original statistics module gets this one right already.
The implementation I chose for this is a bit different from Oscar's suggestion. Essentially, it introduces a dedicated module-private class _ExactRatio to represent numbers as exact ratios and that gets passed between different functions in the module. This class borrows many of its algorithms from fractions.Fraction, but has some specialized methods for central tasks in the statistics module making it much more efficient in this context than fractions.Fraction. This class is currently really minimal, but can easily be extended if necessary.
In my implementation this new class is used throughout the module whenever calculations with or conversions to exact ratios have to be performed, which allowed me to preserve almost all of the original code and to factor out the changes to the class.
As for performance, the gain imagined by Oscar is not always realized even though the variance functions are now using single passes over the data. Specifically, in the case of floats the overhead of having to convert everything to exact ratios first eats up all the savings.
In the case of fractional input, there is a dramatic performance boost though. I compiled a small table comparing (kind of) average performance of the two versions with various input data types. Take this with a grain of salt because the differences can vary quite a bit depending on the exact data:
data type performance gain(+)/loss(-) over original module / %
--------- ----------------------------------------------------
float - 10 %
short Decimal + 10 %
long Decimal - 25 %
Fraction + 80 % (!!)
MyFloat + 25
With Decimal input the costs of conversion to exact ratios depends on the digits in the Decimals, so with short Decimals the savings from the single-pass algorithm are larger than the conversion costs, but this reverses for very long Decimals.
MyFloat is a minimal class inheriting from float and overriding just its arithmetic methods to return MyFloat instances again.
The performance gain with Fraction input comes from two changes, the single-pass algorithm and an optimization in _sum (with Fraction, more than with any other type, the dictionary built by _sum can grow quite large and the optimization is in the conversion of the dictionary elements to exact ratios). This is why the extent of this gain can sometimes be significantly higher than the 80% listed in the table.
Try this, for example:
I invite everybody to test my implementation, which is very unlikely to be free of bugs at this stage.
skrah commentedon Feb 7, 2014
We can add a fast Decimal.as_integer_ratio() in C.
That said, why is the sum of Decimals not done in decimal arithmetic
with a very high context precision? It would be exact and with usual exponents in the range [-384, 383] it should be very fast.
wm75 commentedon Feb 7, 2014
That would be a terrific thing to have !! Currently, Decimals perform really poorly with the statistics functions and this is entirely due to this bottleneck.
With regard to calculating the sum of Decimals in decimal arithmetic, the problem is how you'd detect that the input is all Decimals (or contains enough Decimals to justify a switch in the algorithm).
The current statistics module, accepts input data consisting of mixed types, e.g., you could have Decimal mixed with ints or floats. Much of the complexity of the module stems from this fact. You can also read about some of the complications under this issue: http://bugs.python.org/issue20481 .
wm75 commentedon Feb 7, 2014
Ok, I finally managed to get the test suite right for this, so here is the patch diff for everything.
I made a few final changes to the module itself (mostly to please the test suite), so I'll edited the complete code as well.
oscarbenjamin commentedon Feb 7, 2014
A fast Decimal.as_integer_ratio() would be useful in any case.
If you're going to use decimals though then you can trap inexact and
keep increasing the precision until it becomes exact. The problem is
with rationals that cannot be expressed in a finite number of decimal
digits - these need to be handled separately. I've attached
decimalsum.py that shows how to compute an exact sum of any mix of
int, float and Decimal, but not Fraction.
When I looked at this before, having special cases for everything from
int to float to Decimal to Fraction makes the code really complicated.
The common cases are int and float. For these cases sum() and fsum()
are much faster. However you need to also have code that checks
everything in the iterable.
One option is to do something like:
The main problem here is that fsum rounds every time it returns
meaning that this sum is order-dependent if there are a mix of floats
and other types (See bpo-19086 where I asked for way to change that).
Also having separate code blocks to manage all the different types
internally in e.g. the less trivial variance calculations is tedious.
wm75 commentedon Feb 7, 2014
In principle, your approach using itertools.groupby, the existing _sum, your decimalsum, and my _ExactRatio class could be combined.
You could call decimalsum for Decimal and subclasses, the current _sum for everything else.
_sum would return an _ExactRatio instance now anyway, just modify decimalsum accordingly and you are done.
The problem I see is that it would cause a slow down in many cases where no Decimals or just a few are involved (think of mixes of ints and floats as a realistic scenario, and consider also that you would have to do an isinstance check to catch subclasses of Decimal).
skrah commentedon Feb 7, 2014
Oscar Benjamin <report@bugs.python.org> wrote:
For sums that is not necessary. Create a context with MAX_EMAX, MIN_EMIN and
MAX_PREC and mpd_add() -- the underlying libmpdec function -- will only use
as many digits as neccessary.
Of course, calculating 1/3 in MAX_PREC would be catastrophic.
Yes, I was thinking of a "don't do that" approach. Do people have mixed
Fractions and Decimals in their data?
wm75 commentedon Feb 8, 2014
I worked out a slightly speedier version of decimal_to_ratio today (Stefan: that's when I duplicated your bug report):
makes the variance functions in my re-implementation about 20-30% faster for Decimal. It's not a big breakthrough, but still.
wm75 commentedon Feb 8, 2014
oops,
should read
of course. What I pasted comes from a micro-optimization I tried, but which doesn't give any benefit.
iritkatriel commentedon Aug 20, 2021
Reproduced on 3.11:
rhettinger commentedon Sep 9, 2021
New changeset 4a5cccb by Raymond Hettinger in branch 'main':
bpo-20499: Rounding error in statistics.pvariance (GH-28230)
4a5cccb
rhettinger commentedon Sep 9, 2021
New changeset 3c30805 by Raymond Hettinger in branch '3.10':
[3.10] bpo-20499: Rounding error in statistics.pvariance (GH-28230) (GH-28248)
3c30805
stevendaprano commentedon Sep 9, 2021
This is not a complaint Raymond, but you're too quick for me!
Your changes look good to me, thank you.
stevendaprano commentedon Sep 9, 2021
Regarding the call to sorted that you removed.
I just realised that this was buggy! In my testing, I found that there was a consistent speed-up by summing fractions in order of increasing denominators (small denominators first):
Summing larger denominators before smaller denominators is even slower:
But then I screwed up by sorting the *tuples* (num, den) which would have the effect of summing small *numerators* first, not denominators.
Thanks for catching that they way I had written it, the sort would have had at best no real performance benefit.
jneb commentedon Feb 19, 2023
Why don't you use Welford's algorithm?
This algorithm had good numeral stability, can be computed in one pass, uses no more memory than the "sum of squares" method and is only a little bit more work. (see Wikipedia ).