Skip to content

Commit 55c1d21

Browse files
bpo-40855: Fix ignored mu and xbar parameters (GH-20835) (#GH-20862)
1 parent bda4cc8 commit 55c1d21

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Lib/statistics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,10 @@ def _ss(data, c=None):
682682
calculated from ``c`` as given. Use the second case with care, as it can
683683
lead to garbage results.
684684
"""
685-
if c is None:
686-
c = mean(data)
685+
if c is not None:
686+
T, total, count = _sum((x-c)**2 for x in data)
687+
return (T, total)
688+
c = mean(data)
687689
T, total, count = _sum((x-c)**2 for x in data)
688690
# The following sum should mathematically equal zero, but due to rounding
689691
# error may not.

Lib/test/test_statistics.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,10 @@ def test_decimals(self):
20892089
self.assertEqual(result, exact)
20902090
self.assertIsInstance(result, Decimal)
20912091

2092+
def test_center_not_at_mean(self):
2093+
data = (1.0, 2.0)
2094+
self.assertEqual(self.func(data), 0.5)
2095+
self.assertEqual(self.func(data, xbar=2.0), 1.0)
20922096

20932097
class TestPStdev(VarianceStdevMixin, NumericTestCase):
20942098
# Tests for population standard deviation.
@@ -2101,6 +2105,11 @@ def test_compare_to_variance(self):
21012105
expected = math.sqrt(statistics.pvariance(data))
21022106
self.assertEqual(self.func(data), expected)
21032107

2108+
def test_center_not_at_mean(self):
2109+
# See issue: 40855
2110+
data = (3, 6, 7, 10)
2111+
self.assertEqual(self.func(data), 2.5)
2112+
self.assertEqual(self.func(data, mu=0.5), 6.5)
21042113

21052114
class TestStdev(VarianceStdevMixin, NumericTestCase):
21062115
# Tests for sample standard deviation.
@@ -2118,6 +2127,9 @@ def test_compare_to_variance(self):
21182127
expected = math.sqrt(statistics.variance(data))
21192128
self.assertEqual(self.func(data), expected)
21202129

2130+
def test_center_not_at_mean(self):
2131+
data = (1.0, 2.0)
2132+
self.assertEqual(self.func(data, xbar=2.0), 1.0)
21212133

21222134
class TestGeometricMean(unittest.TestCase):
21232135

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The standard deviation and variance functions in the statistics module were
2+
ignoring their mu and xbar arguments.

0 commit comments

Comments
 (0)