Repository navigation
gh-162743: Fix correlation mean rounding for large timestamps - #162799
Sudhakar7J wants to merge 1 commit into
Conversation
Compensate the mean in statistics.correlation() so floating-point rounding in fsum/n does not corrupt centered data when inputs are large with small differences.
f7d110b to
5479f58
Compare
|
Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate. Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors. This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness. Please read our guidelines for using AI tools. |
|
Please explain with your own words why the correction is the correct change. |
|
I want to think about this one for a while. The extra generator expressions slow down the code but seem to serve only this one use case. |
Summary
statistics.correlation()computed means asfsum(x) / nand centered data with that rounded mean. For large timestamp-like inputs where the mean is not exactly representable as a float, this can shift the centered sums enough to produce a wrong correlation (the issue reports-0.2instead of+1.0).This adds a one-step mean compensation (
xbar += fsum(xi - xbar for xi in x) / n) before centering, which corrects the floating-point rounding error in the mean.Test Plan
test_correlation_large_timestamp_precisionregression test.statistics.py: reported case returns1.0, existing sanity cases still pass.AI assistance: this change was drafted with an AI coding agent; I reviewed the issue, fix, and test before opening the PR.