MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_specific_cases

Method test_specific_cases

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 10

quantilesFunction · 0.85
lenFunction · 0.85
allFunction · 0.85
listClass · 0.85
sortedFunction · 0.85
shuffleMethod · 0.80
assertTrueMethod · 0.80
choicesMethod · 0.80
medianMethod · 0.80
assertEqualMethod · 0.45

Tested by

no test coverage detected