Skip to content

statistics.correlation misclassifies tiny nonconstant inputs as constant #158752

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The correlation() docstring states: "Return the Pearson's correlation coefficient for two inputs." It also demonstrates "correlation(x, x)" returning "1.0".

Expected: Return approximately 1.0 without raising an exception.

Actual: Raises StatisticsError: at least one of the inputs is constant.

import math
import statistics
from fractions import Fraction

a = 1e-200
x = [-a, a]
if not (1e-200 <= a <= 1e-170 and all(map(math.isfinite, x))
        and len(x) >= 2 and len(set(x)) > 1):
    print("REFUTATION REJECTED: invalid input")
else:
    q = list(map(Fraction, x))
    mean = sum(q) / len(q)
    variance = sum((t - mean) ** 2 for t in q)
    expected = float(variance / variance)  # Exact Pearson self-correlation.
    try:
        result = statistics.correlation(x, x)
        actual = ("result", result)
        broken = not math.isclose(result, expected, rel_tol=1e-12)
    except Exception as e:
        actual = ("exception", type(e).__name__, str(e))
        broken = True
    if broken:
        print("REFUTATION CONFIRMED:", (x, x), "actual =", actual,
              "expected =", expected)
    else:
        print("REFUTATION REJECTED: actual matches exact Pearson self-correlation")

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

REFUTATION CONFIRMED: ([-1e-200, 1e-200], [-1e-200, 1e-200]) actual = ('exception', 'StatisticsError', 'at least one of the inputs is constant') expected = 1.0

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-045701-c3

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