Skip to content

Commit

Permalink
bpo-40855: Fix ignored mu and xbar parameters (GH-20835) (GH-20863)
Browse files Browse the repository at this point in the history
  • Loading branch information
miss-islington committed Jun 13, 2020
1 parent f8c05bb commit 811e040
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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 comments on commit 811e040

Please sign in to comment.