Skip to content

statistics.covariance overflows when the final covariance is representable #158760

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: "Return the sample covariance of two inputs x and y." — statistics.covariance docstring.

Expected: A finite sample covariance approximately equal to 6.533333333333333e+307 for the reproduced input.

Actual: statistics.covariance(x, x) returns inf.

import statistics, math
from fractions import Fraction

x = [-7e153, -7e153, 7e153, 7e153]
y = x.copy()
if len(x) != len(y) or len(x) < 2 or not all(map(math.isfinite, x + y)):
    print("REFUTATION REJECTED: invalid input")
else:
    n = len(x)
    u, v = list(map(Fraction, x)), list(map(Fraction, y))
    expected = float((sum(a*b for a, b in zip(u, v)) - sum(u)*sum(v)/n)/(n-1))
    try:
        actual = statistics.covariance(x, y)
        broken = not math.isfinite(actual) or not math.isclose(actual, expected, rel_tol=1e-14)
    except Exception as e:
        actual, broken = repr(e), True
    if broken:
        print("REFUTATION CONFIRMED:", (x, y), "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED: sample covariance agrees with exact reference")

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library statistics:

REFUTATION CONFIRMED: ([-7e+153, -7e+153, 7e+153, 7e+153], [-7e+153, -7e+153, 7e+153, 7e+153]) actual: inf expected: 6.533333333333333e+307

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/statistics-20261003-185112-c4

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions