Fix inconsistent fsum vs sum and fmean vs mean (GH-25898)

This commit is contained in:
Raymond Hettinger 2021-05-04 11:27:28 -07:00 committed by GitHub
parent 9ee8448243
commit 1add719516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 3 deletions

View File

@ -882,8 +882,8 @@ def covariance(x, y, /):
raise StatisticsError('covariance requires that both inputs have same number of data points')
if n < 2:
raise StatisticsError('covariance requires at least two data points')
xbar = mean(x)
ybar = mean(y)
xbar = fmean(x)
ybar = fmean(y)
total = fsum((xi - xbar) * (yi - ybar) for xi, yi in zip(x, y))
return total / (n - 1)
@ -956,7 +956,7 @@ def linear_regression(regressor, dependent_variable, /):
slope = covariance(regressor, dependent_variable) / variance(regressor)
except ZeroDivisionError:
raise StatisticsError('regressor is constant')
intercept = mean(dependent_variable) - slope * mean(regressor)
intercept = fmean(dependent_variable) - slope * fmean(regressor)
return LinearRegression(intercept=intercept, slope=slope)