← 输入框|github.com|刷新
Skip to content

gh-162743: Fix correlation mean rounding for large timestamps - #162799

Open
Sudhakar7J wants to merge 1 commit into
python:mainfrom
Sudhakar7J:fix/gh-162743-correlation-mean
Open

Sudhakar7J wants to merge 1 commit into
python:mainfrom
Sudhakar7J:fix/gh-162743-correlation-mean

Conversation

@Sudhakar7J

Copy link
Copy Markdown

Summary

statistics.correlation() computed means as fsum(x) / n and 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.2 instead 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

  • Added test_correlation_large_timestamp_precision regression test.
  • Verified locally on Windows/Python 3.15.2 with the patched statistics.py: reported case returns 1.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.

@python-cla-bot

python-cla-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

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.
@Sudhakar7J
Sudhakar7J force-pushed the fix/gh-162743-correlation-mean branch from f7d110b to 5479f58 Compare November 28, 2026 09:41
@picnixz
picnixz requested a review from rhettinger November 28, 2026 12:27
@picnixz picnixz added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 28, 2026
@bedevere-app

bedevere-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

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.

@picnixz

picnixz commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Please explain with your own words why the correction is the correct change.

@rhettinger rhettinger self-assigned this Nov 29, 2026
@rhettinger

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants