diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2023-08-08 16:12:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-08 16:12:52 (GMT) |
commit | d4ac094cf9d15ec5705ec0fe8771df9e6ba915b9 (patch) | |
tree | d63a31f86319c15f1422a3b3d6bc1c3f5ddb5c5e /Lib/statistics.py | |
parent | f9e3ff1ea4b2c8b787360409d55f2037652b7456 (diff) | |
download | cpython-d4ac094cf9d15ec5705ec0fe8771df9e6ba915b9.zip cpython-d4ac094cf9d15ec5705ec0fe8771df9e6ba915b9.tar.gz cpython-d4ac094cf9d15ec5705ec0fe8771df9e6ba915b9.tar.bz2 |
Minor accuracy improvement for statistics.correlation() (GH-107781)
Diffstat (limited to 'Lib/statistics.py')
-rw-r--r-- | Lib/statistics.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Lib/statistics.py b/Lib/statistics.py index 6bd214b..066669d 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1004,6 +1004,14 @@ def _mean_stdev(data): # Handle Nans and Infs gracefully return float(xbar), float(xbar) / float(ss) +def _sqrtprod(x: float, y: float) -> float: + "Return sqrt(x * y) computed with high accuracy." + # Square root differential correction: + # https://www.wolframalpha.com/input/?i=Maclaurin+series+sqrt%28h**2+%2B+x%29+at+x%3D0 + h = sqrt(x * y) + x = sumprod((x, h), (y, -h)) + return h + x / (2.0 * h) + # === Statistics for relations between two inputs === @@ -1083,7 +1091,7 @@ def correlation(x, y, /, *, method='linear'): sxx = sumprod(x, x) syy = sumprod(y, y) try: - return sxy / sqrt(sxx * syy) + return sxy / _sqrtprod(sxx, syy) except ZeroDivisionError: raise StatisticsError('at least one of the inputs is constant') |