10000 [3.8] bpo-40855: Fix ignored mu and xbar parameters (GH-20835) by miss-islington · Pull Request #20863 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

8000 [3.8] bpo-40855: Fix ignored mu and xbar parameters (GH-20835) #20863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,10 @@ def _ss(data, c=None):
calculated from ``c`` as given. Use the second case with care, as it can
lead to garbage results.
"""
if c is None:
c = mean(data)
if c is not None:
T, total, count = _sum((x-c)**2 for x in data)
return (T, total)
c = mean(data)
T, total, count = _sum((x-c)**2 for x in data)
# The following sum should mathematically equal zero, but due to rounding
# error may not.
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,10 @@ def test_decimals(self):
self.assertEqual(result, exact)
self.assertIsInstance(result, Decimal)

def test_center_not_at_mean(self):
data = (1.0, 2.0)
self.assertEqual(self.func(data), 0.5)
self.assertEqual(self.func(data, xbar=2.0), 1.0)

class TestPStdev(VarianceStdevMixin, NumericTestCase):
# Tests for population standard deviation.
Expand All @@ -2041,6 +2045,11 @@ def test_compare_to_variance(self):
expected = math.sqrt(statistics.pvariance(data))
self.assertEqual(self.func(data), expected)

def test_center_not_at_mean(self):
# See issue: 40855
data = (3, 6, 7, 10)
self.assertEqual(self.func(data), 2.5)
self.assertEqual(self.func(data, mu=0.5), 6.5)

class TestStdev(VarianceStdevMixin, NumericTestCase):
# Tests for sample standard deviation.
Expand All @@ -2058,6 +2067,9 @@ def test_compare_to_variance(self):
expected = math.sqrt(statistics.variance(data))
self.assertEqual(self.func(data), expected)

def test_center_not_at_mean(self):
data = (1.0, 2.0)
self.assertEqual(self.func(data, xbar=2.0), 1.0)

class TestGeometricMean(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The standard deviation and variance functions in the statistics module were
ignoring their mu and xbar arguments.
0