8000 bpo-40855: Fix ignored mu and xbar parameters (GH-20835) (GH-20863) · python/cpython@811e040 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 811e040

Browse files
bpo-40855: Fix ignored mu and xbar parameters (GH-20835) (GH-20863)
1 parent f8c05bb commit 811e040

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
@@ -2029,6 +2029,10 @@ def test_decimals(self):
20292029
self.assertEqual(result, exact)
20302030
self.assertIsInstance(result, Decimal)
20312031

2032+
def test_center_not_at_mean(self):
2033+
data = (1.0, 2.0)
2034+
self.assertEqual(self.func(data), 0.5)
2035+
self.assertEqual(self.func(data, xbar=2.0), 1.0)
20322036

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

2048+
def test_center_not_at_mean(self):
2049+
# See issue: 40855
2050+
data = (3, 6, 7, 10)
2051+
self.assertEqual(self.func(data), 2.5)
2052+
self.assertEqual(self.func(data, mu=0.5), 6.5)
20442053

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

2070+
def test_center_not_at_mean(self):
2071+
data = (1.0, 2.0)
2072+
self.assertEqual(self.func(data, xbar=2.0), 1.0)
20612073

20622074
class TestGeometricMean(unittest.TestCase):
20632075

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)
0