Skip to content

Commit 18735c3

Browse files
author
Priyanshu Maurya
committed
gh-162743: Fix correlation and covariance for large common offsets
Centering on the rounded mean discards the signal when every value shares a large offset, since the deviations are tiny next to the values being subtracted. Center on a shifted frame instead, which keeps the deviations exact, and reuse that for covariance(), which had the same defect.
1 parent 49a344e commit 18735c3

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

‎Lib/statistics.py‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,23 @@ def pstdev(data, mu=None):
656656

657657
## Statistics for relations between two inputs #############################
658658

659+
def _center(data, n):
660+
"""Return ``data`` minus its mean, keeping precision for large offsets.
661+
662+
Subtracting a large common offset first keeps the deviations exact
663+
instead of losing them when the mean itself is rounded.
664+
"""
665+
origin = next(iter(data))
666+
shifted = [value - origin for value in data]
667+
if all(isfinite(value) for value in shifted):
668+
mean = fsum(value / n for value in shifted)
669+
centered = [value - mean for value in shifted]
670+
if all(isfinite(value) for value in centered):
671+
return centered
672+
mean = fsum(data) / n
673+
return [value - mean for value in data]
674+
675+
659676
def covariance(x, y, /):
660677
"""Covariance
661678
@@ -679,9 +696,9 @@ def covariance(x, y, /):
679696
raise StatisticsError('covariance requires that both inputs have same number of data points')
680697
if n < 2:
681698
raise StatisticsError('covariance requires at least two data points')
682-
xbar = fsum(x) / n
683-
ybar = fsum(y) / n
684-
sxy = sumprod((xi - xbar for xi in x), (yi - ybar for yi in y))
699+
x = _center(x, n)
700+
y = _center(y, n)
701+
sxy = sumprod(x, y)
685702
return sxy / (n - 1)
686703

687704

@@ -725,10 +742,8 @@ def correlation(x, y, /, *, method='linear'):
725742
y = _rank(y, start=start)
726743

727744
else:
728-
xbar = fsum(x) / n
729-
ybar = fsum(y) / n
730-
x = [xi - xbar for xi in x]
731-
y = [yi - ybar for yi in y]
745+
x = _center(x, n)
746+
y = _center(y, n)
732747

733748
sxy = sumprod(x, y)
734749
sxx = sumprod(x, x)

‎Lib/test/test_statistics.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,14 @@ def test_different_scales(self):
27472747
self.assertAlmostEqual(statistics.correlation(x, y), 1)
27482748
self.assertAlmostEqual(statistics.covariance(x, y), 0.1)
27492749

2750+
def test_large_offset(self):
2751+
# gh-162743: a large common offset must not swallow the signal.
2752+
epoch = 1_700_000_000_000_000_000
2753+
event = [epoch + 256, epoch + 512, epoch + 768]
2754+
received = [t + 1024 for t in event]
2755+
self.assertAlmostEqual(statistics.correlation(event, received), 1.0)
2756+
self.assertAlmostEqual(statistics.covariance(event, received), 65536.0)
2757+
27502758
def test_sqrtprod_helper_function_fundamentals(self):
27512759
# Verify that results are close to sqrt(x * y)
27522760
for i in range(100):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :func:`statistics.correlation` and :func:`statistics.covariance`
2+
returning incorrect results when the data share a large common offset,
3+
as with timestamp-like values that differ by a small amount.

0 commit comments

Comments
 (0)