Repository navigation
Description
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.
Activity
eendebakpt commentedon Dec 2, 2026
@ThryveBaseline The issue reproduces on main. Would you like to make a PR to address this? (make sure to follow the AI policy)
skirpichev commentedon Dec 2, 2026
Is this worth fixing?
ThryveBaseline commentedon Dec 3, 2026
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 incorrelation()means callers can rely on the documented range without adding their own bounds check.efanntyo commentedon Dec 3, 2026
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 those same results,
max(-1.0, min(1.0, r))returns1.0, while reversing the nesting tomin(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.pythongh-163030: Add NEWS entry
pythongh-163030: Address review feedback
pythongh-163030: Preserve non-finite correlations
rhettinger commentedon Dec 4, 2026
It would be reasonable to add
[-1.0, 1.0]clamping.