Skip to content

statistics.pstdev overflows for large finite values when given the correct mean #158754

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The pvariance docstring, incorporated by pstdev's sentence "See pvariance for arguments and other details", states: "The optional argument mu, if given, should be the mean of the data. If it is missing or None, the mean is automatically calculated." pstdev promises: "Return the square root of the population variance."

Expected: pstdev([-1e160, 1e160], mu=0.0) returns approximately 1e160, agreeing with the call without mu.

Actual: Raises ValueError: inf or nan encountered in data.

import math
import statistics

a = 1e160
data, mu = [-a, a], 0.0
expected = math.hypot(*data) / math.sqrt(len(data))
if not (all(map(math.isfinite, data)) and math.fsum(data) / len(data) == mu):
    print("REFUTATION REJECTED: invalid input or incorrect mean")
else:
    try:
        actual = statistics.pstdev(data, mu=mu)
    except Exception as e:
        actual = f"{type(e).__name__}: {e}"
    if not isinstance(actual, float) or not math.isclose(actual, expected, rel_tol=1e-15):
        print("REFUTATION CONFIRMED:", {"data": data, "mu": mu},
              "actual=", actual, "expected=", expected)
    else:
        print("REFUTATION REJECTED: result agrees with documented expectation")

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

REFUTATION CONFIRMED: {'data': [-1e+160, 1e+160], 'mu': 0.0} actual= ValueError: inf or nan encountered in data expected= 1e+160

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-081656-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