def _shuffle_distance(xpoint: ReachPoint, ypoint: ReachPoint, k=5) -> float: """Computes shuffle distance of first k frequency buckets.""" if xpoint.max_frequency <= k or ypoint.max_frequency <= k: return 1.0 xfreq = np.array([xpoint.frequency(i + 1) for i in range(k)]) yfreq = np.array([ypoint.frequency(i + 1) for i in range(k)]) if sum(xfreq) == 0 or sum(yfreq) == 0: return 0.5 return 0.5 * np.sum(np.abs(xfreq / sum(xfreq) - yfreq / sum(yfreq)))
def test_frequency(self): point = ReachPoint([200, 300], [200, 125, 75]) self.assertEqual(point.frequency(1), 75) self.assertEqual(point.frequency(2), 50) self.assertRaises(ValueError, point.frequency, 3)