Skip to content

Commit b0a2c0f

Browse files
authored
bpo-36018: Test idempotence. Test two methods against one-another. (GH-13021)
1 parent ee0309f commit b0a2c0f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Lib/test/test_statistics.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,20 @@ def test_specific_cases(self):
21582158
result = quantiles(map(datatype, data), n=n)
21592159
self.assertTrue(all(type(x) == datatype) for x in result)
21602160
self.assertEqual(result, list(map(datatype, expected)))
2161+
# Quantiles should be idempotent
2162+
if len(expected) >= 2:
2163+
self.assertEqual(quantiles(expected, n=n), expected)
2164+
# Cross-check against other methods
2165+
if len(data) >= n:
2166+
# After end caps are added, method='inclusive' should
2167+
# give the same result as method='exclusive' whenever
2168+
# there are more data points than desired cut points.
2169+
padded_data = [min(data) - 1000] + data + [max(data) + 1000]
2170+
self.assertEqual(
2171+
quantiles(data, n=n),
2172+
quantiles(padded_data, n=n, method='inclusive'),
2173+
(n, data),
2174+
)
21612175
# Invariant under tranlation and scaling
21622176
def f(x):
21632177
return 3.5 * x - 1234.675
@@ -2219,6 +2233,15 @@ def f(x):
22192233
actual = quantiles(statistics.NormalDist(), n=n, method="inclusive")
22202234
self.assertTrue(all(math.isclose(e, a, abs_tol=0.0001)
22212235
for e, a in zip(expected, actual)))
2236+
# Whenever n is smaller than the number of data points, running
2237+
# method='inclusive' should give the same result as method='exclusive'
2238+
# after the two included extreme points are removed.
2239+
data = [random.randrange(10_000) for i in range(501)]
2240+
actual = quantiles(data, n=32, method='inclusive')
2241+
data.remove(min(data))
2242+
data.remove(max(data))
2243+
expected = quantiles(data, n=32)
2244+
self.assertEqual(expected, actual)
22222245

22232246
def test_equal_inputs(self):
22242247
quantiles = statistics.quantiles

0 commit comments

Comments
 (0)