[Python-checkins] bpo-40855: Fix ignored mu and xbar parameters (GH-20835) (GH-20863)

Miss Islington (bot) webhook-mailer at python.org
Sat Jun 13 19:57:25 EDT 2020


https://github.com/python/cpython/commit/811e040b6e0241339545c2f055db8259b408802f
commit: 811e040b6e0241339545c2f055db8259b408802f
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020年06月13日T16:57:17-07:00
summary:
bpo-40855: Fix ignored mu and xbar parameters (GH-20835) (GH-20863)
files:
A Misc/NEWS.d/next/Library/2020-06-12-10-44-15.bpo-40855.jSot83.rst
M Lib/statistics.py
M Lib/test/test_statistics.py
diff --git a/Lib/statistics.py b/Lib/statistics.py
index 1e95c0b6639f1..c5c6e47fb3f3e 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -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.
diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py
index a9a427bc8d972..5b8ad874a9090 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -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.
@@ -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.
@@ -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):
 
diff --git a/Misc/NEWS.d/next/Library/2020-06-12-10-44-15.bpo-40855.jSot83.rst b/Misc/NEWS.d/next/Library/2020-06-12-10-44-15.bpo-40855.jSot83.rst
new file mode 100644
index 0000000000000..201d510327a47
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-12-10-44-15.bpo-40855.jSot83.rst
@@ -0,0 +1,2 @@
+The standard deviation and variance functions in the statistics module were
+ignoring their mu and xbar arguments.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /