Skip to content

statistics.linear_regression misclassifies small nonconstant inputs as constant due to underflow #158751

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The linear_regression docstring states: "Return the slope and intercept of simple linear regression parameters estimated using ordinary least squares."

Expected: Slope 1.0 and intercept 0.0.

Actual: Raises StatisticsError: x is constant.

import math
import statistics
from fractions import Fraction

x = y = [0.0, 1e-200]
data = {"x": x, "y": y, "proportional": False}
if not (len(x) == len(y) == 2 and x[0] != x[1]
        and all(math.isfinite(v) for v in x + y)):
    print("REFUTATION REJECTED:", "invalid regression input")
else:
    X, Y = list(map(Fraction, x)), list(map(Fraction, y))
    # Two distinct points determine the unique zero-residual OLS line.
    slope = (Y[1] - Y[0]) / (X[1] - X[0])
    expected = (float(slope), float(Y[0] - slope * X[0]))
    try:
        actual = tuple(statistics.linear_regression(x, y))
    except Exception as e:
        actual = {"exception": type(e).__name__, "message": str(e)}
    if actual != expected:
        print("REFUTATION CONFIRMED:", data, "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED:", "actual matches independent OLS expectation")

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

REFUTATION CONFIRMED: {'x': [0.0, 1e-200], 'y': [0.0, 1e-200], 'proportional': False} actual: {'exception': 'StatisticsError', 'message': 'x is constant'} expected: (1.0, 0.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-c2

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