MCPcopy Index your code
hub / github.com/python/cpython / test_specific_cases

Method test_specific_cases

Lib/test/test_statistics.py:2534–2586  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2532class TestQuantiles(unittest.TestCase):
2533
2534 def test_specific_cases(self):
2535 # Match results computed by hand and cross-checked
2536 # against the PERCENTILE.EXC function in MS Excel.
2537 quantiles = statistics.quantiles
2538 data = [120, 200, 250, 320, 350]
2539 random.shuffle(data)
2540 for n, expected in [
2541 (1, []),
2542 (2, [250.0]),
2543 (3, [200.0, 320.0]),
2544 (4, [160.0, 250.0, 335.0]),
2545 (5, [136.0, 220.0, 292.0, 344.0]),
2546 (6, [120.0, 200.0, 250.0, 320.0, 350.0]),
2547 (8, [100.0, 160.0, 212.5, 250.0, 302.5, 335.0, 357.5]),
2548 (10, [88.0, 136.0, 184.0, 220.0, 250.0, 292.0, 326.0, 344.0, 362.0]),
2549 (12, [80.0, 120.0, 160.0, 200.0, 225.0, 250.0, 285.0, 320.0, 335.0,
2550 350.0, 365.0]),
2551 (15, [72.0, 104.0, 136.0, 168.0, 200.0, 220.0, 240.0, 264.0, 292.0,
2552 320.0, 332.0, 344.0, 356.0, 368.0]),
2553 ]:
2554 self.assertEqual(expected, quantiles(data, n=n))
2555 self.assertEqual(len(quantiles(data, n=n)), n - 1)
2556 # Preserve datatype when possible
2557 for datatype in (float, Decimal, Fraction):
2558 result = quantiles(map(datatype, data), n=n)
2559 self.assertTrue(all(type(x) == datatype) for x in result)
2560 self.assertEqual(result, list(map(datatype, expected)))
2561 # Quantiles should be idempotent
2562 if len(expected) >= 2:
2563 self.assertEqual(quantiles(expected, n=n), expected)
2564 # Cross-check against method='inclusive' which should give
2565 # the same result after adding in minimum and maximum values
2566 # extrapolated from the two lowest and two highest points.
2567 sdata = sorted(data)
2568 lo = 2 * sdata[0] - sdata[1]
2569 hi = 2 * sdata[-1] - sdata[-2]
2570 padded_data = data + [lo, hi]
2571 self.assertEqual(
2572 quantiles(data, n=n),
2573 quantiles(padded_data, n=n, method='inclusive'),
2574 (n, data),
2575 )
2576 # Invariant under translation and scaling
2577 def f(x):
2578 return 3.5 * x - 1234.675
2579 exp = list(map(f, expected))
2580 act = quantiles(map(f, data), n=n)
2581 self.assertTrue(all(math.isclose(e, a) for e, a in zip(exp, act)))
2582 # Q2 agrees with median()
2583 for k in range(2, 60):
2584 data = random.choices(range(100), k=k)
2585 q1, q2, q3 = quantiles(data)
2586 self.assertEqual(q2, statistics.median(data))
2587
2588 def test_specific_cases_inclusive(self):
2589 # Match results computed by hand and cross-checked

Callers

nothing calls this directly

Calls 7

quantilesFunction · 0.85
listClass · 0.85
shuffleMethod · 0.80
assertTrueMethod · 0.80
choicesMethod · 0.80
medianMethod · 0.80
assertEqualMethod · 0.45

Tested by

no test coverage detected