Skip to content

statistics.correlation() can round outside [-1, 1] #163030

Todo

Description

@ThryveBaseline

Bug report

For the finite, nearly collinear inputs below, statistics.correlation() returns one ulp outside [-1, 1] in both directions. Passing either result to math.acos() raises ValueError: math domain error.

The documentation describes the coefficient as taking values between -1 and +1. I expected the returned value to remain inside that range.

import math
import statistics

pairs = [
    (
        [-96.58825002910613, -33.41758764126037, 80.88757048749329, 24.053051499453687],
        [-243.97798201783226, -86.05132604821785, 199.7115692736663, 57.62527180356729],
    ),
    (
        [78.45139891383266, 34.043422148339516, 77.90250993685783],
        [-33.54753437409516, -11.343545991348583, -33.27308988560774],
    ),
]

for x, y in pairs:
    r = statistics.correlation(x, y)
    print(repr(r), -1 <= r <= 1)
    try:
        print(math.acos(r))
    except ValueError as error:
        print(type(error).__name__ + ": " + str(error))

Observed output:

1.0000000000000002 False
ValueError: math domain error
-1.0000000000000002 False
ValueError: math domain error

CPython versions tested on

Directly reproduced for this report on 3.12.10, Windows ARM64 (MSC v.1943 64 bit (ARM64)).

The originating collaborator also recorded reproduction on 3.13.18 and a standalone copy of main's statistics.py, at d9d8cb4f104318e9fbe64d9fb12aa5664320cd5c. That main-source test ran with unrelated development-only math imports stubbed; it was not a built main interpreter, and I have not independently repeated that configuration.

Source inspection and scope

At that revision, correlation returns sxy / _sqrtprod(sxx, syy) without a final range check.

This report concerns ordinary-magnitude inputs and a one-ulp range violation. It is separate from the tiny-input issues #161259 / #158752 and the large-timestamp mean-rounding issue #162743. A range correction may be appropriate, but should preserve intended non-finite-input behavior; I am not proposing an untested blanket min/max replacement.

Provenance

Discovered by Fable (Claude Code) during differential testing of documentation claims. Codex reviewed the report and upstream source, checked related issues/PRs, and independently executed the reproducer on the Windows version above. Research, drafting, and review were AI-assisted; no human technical-review claim is made.

Linked PRs

Activity

  1. added
    stdlib
    type-bug
    on Dec 2, 2026
  2. eendebakpt commented on Dec 2, 2026

    @eendebakpt
    Contributor

    @ThryveBaseline The issue reproduces on main. Would you like to make a PR to address this? (make sure to follow the AI policy)

  3. skirpichev commented on Dec 2, 2026

    @skirpichev
    Member

    Is this worth fixing?

  4. ThryveBaseline commented on Dec 3, 2026

    @ThryveBaseline
    Author

    I think so. If you’re using correlation to calculate an angle, perfectly correlated data is about the last place you’d expect it to fail. But acos(correlation(x, y)) can raise there today. Fixing it in correlation() means callers can rely on the documented range without adding their own bounds check.

  5. efanntyo commented on Dec 3, 2026

    @efanntyo

    A concrete non-finite regression control for the report's range-correction caveat: on CPython 3.12.16/Linux, each of these returns NaN, rather than a finite coefficient:

    for value in (math.nan, math.inf, -math.inf):
        r = statistics.correlation([1, 2, 3], [value, 2, 3])
        assert math.isnan(r)

    For those same results, max(-1.0, min(1.0, r)) returns 1.0, while reversing the nesting to min(1.0, max(-1.0, r)) returns -1.0. Both silently turn undefined correlation into perfect correlation. I executed all three controls locally.

    If a range correction is added, preserving math.isnan(r) in a regression test alongside the two finite overflows would distinguish a NaN-preserving comparison guard from these min/max forms. This is an installed-3.12 compatibility control, not a main-build or proposed-patch verification. AI-assisted investigation; no patch submitted.

  6. added 3 commits that reference this issue on Dec 3, 2026
  7. self-assigned this
    on Dec 4, 2026
  8. added
    performance
    and removed
    type-bug
    on Dec 4, 2026
  9. rhettinger commented on Dec 4, 2026

    @rhettinger
    Contributor

    It would be reasonable to add [-1.0, 1.0] clamping.

  10. removed their assignment
    on Dec 4, 2026
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

    performancestdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions