Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,9 +1776,10 @@ def _sqrtprod(x: float, y: float) -> float:
return _sqrtprod(scale * x, scale * y) / scale
return h

if not h:
if h < 2.0 ** -485:
# x * y is subnormal or underflowed, or the correction term
# below would be. Rescale non-zero inputs.
if x and y:
# Non-zero inputs underflowed, so scale up, and recompute.
# Scale: 1 / sqrt(sys.float_info.min * sys.float_info.epsilon)
scale = 2.0 ** 537
return _sqrtprod(scale * x, scale * y) / scale
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2790,6 +2790,29 @@ def test_sqrtprod_helper_function_fundamentals(self):
self.assertEqual(actual, expected)
self.assertEqual(sign(actual), sign(expected))

def test_sqrtprod_helper_function_subnormal_products(self):
# gh-161259: accuracy was lost when x * y is subnormal.
# The reference scales the inputs by an exact power of two so that
# the product is a normal float.
scale = 2.0**600
for x, y in [
(2e-200, 2e-124),
(1e-160, 3e-150),
(3e-170, 7e-140),
(1e-160, 1e-135),
]:
expected = math.sqrt((scale * x) * (scale * y)) / scale
actual = statistics._sqrtprod(x, y)
with self.subTest(x=x, y=y, expected=expected, actual=actual):
self.assertAlmostEqual(actual / expected, 1.0, delta=2**-50)

def test_correlation_tiny_inputs(self):
# gh-161259: correlation() is scale-invariant, even when the
# intermediate sums of squares are tiny.
x = [1e-100, 2e-100, 3e-100]
y = [1e-62, 2e-62, 3e-62]
self.assertAlmostEqual(statistics.correlation(x, y), 1.0)

@requires_IEEE_754
@skip_if_double_rounding
@support.cpython_only # Allow for a weaker sumprod() implementation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`statistics.correlation` returning inaccurate results for very
small inputs, where the product of the sums of squares is subnormal rather
than zero.
Loading