def testDiscrepancyAnalytic(self): """Computes discrepancy for sample sets with known statistics.""" interval_multiplier = 100000 samples = [] d = statistics.Discrepancy(samples, interval_multiplier) self.assertEquals(d, 1.0) samples = [0.5] d = statistics.Discrepancy(samples, interval_multiplier) self.assertEquals(round(d), 1.0) samples = [0.0, 1.0] d = statistics.Discrepancy(samples, interval_multiplier) self.assertAlmostEquals(round(d, 2), 1.0) samples = [0.5, 0.5, 0.5] d = statistics.Discrepancy(samples, interval_multiplier) self.assertAlmostEquals(d, 1.0) samples = [1.0/8.0, 3.0/8.0, 5.0/8.0, 7.0/8.0] d = statistics.Discrepancy(samples, interval_multiplier) self.assertAlmostEquals(round(d, 2), 0.25) samples = [0.0, 1.0/3.0, 2.0/3.0, 1.0] d = statistics.Discrepancy(samples, interval_multiplier) self.assertAlmostEquals(round(d, 2), 0.5) samples = statistics.NormalizeSamples(samples)[0] d = statistics.Discrepancy(samples, interval_multiplier) self.assertAlmostEquals(round(d, 2), 0.25) time_stamps_a = [0, 1, 2, 3, 5, 6] time_stamps_b = [0, 1, 2, 3, 5, 7] time_stamps_c = [0, 2, 3, 4] time_stamps_d = [0, 2, 3, 4, 5] d_abs_a = statistics.FrameDiscrepancy(time_stamps_a, True, interval_multiplier) d_abs_b = statistics.FrameDiscrepancy(time_stamps_b, True, interval_multiplier) d_abs_c = statistics.FrameDiscrepancy(time_stamps_c, True, interval_multiplier) d_abs_d = statistics.FrameDiscrepancy(time_stamps_d, True, interval_multiplier) d_rel_a = statistics.FrameDiscrepancy(time_stamps_a, False, interval_multiplier) d_rel_b = statistics.FrameDiscrepancy(time_stamps_b, False, interval_multiplier) d_rel_c = statistics.FrameDiscrepancy(time_stamps_c, False, interval_multiplier) d_rel_d = statistics.FrameDiscrepancy(time_stamps_d, False, interval_multiplier) self.assertTrue(d_abs_a < d_abs_b) self.assertTrue(d_rel_a < d_rel_b) self.assertTrue(d_rel_d < d_rel_c) self.assertEquals(round(d_abs_d, 2), round(d_abs_c, 2))
def testNormalizeSamples(self): samples = [] normalized_samples, scale = statistics.NormalizeSamples(samples) self.assertEquals(normalized_samples, samples) self.assertEquals(scale, 1.0) samples = [0.0, 0.0] normalized_samples, scale = statistics.NormalizeSamples(samples) self.assertEquals(normalized_samples, samples) self.assertEquals(scale, 1.0) samples = [0.0, 1.0/3.0, 2.0/3.0, 1.0] normalized_samples, scale = statistics.NormalizeSamples(samples) self.assertEquals(normalized_samples, [1.0/8.0, 3.0/8.0, 5.0/8.0, 7.0/8.0]) self.assertEquals(scale, 0.75) samples = [1.0/8.0, 3.0/8.0, 5.0/8.0, 7.0/8.0] normalized_samples, scale = statistics.NormalizeSamples(samples) self.assertEquals(normalized_samples, samples) self.assertEquals(scale, 1.0)
def testDiscrepancyRandom(self): """Tests NormalizeSamples and Discrepancy with random samples. Generates 10 sets of 10 random samples, computes the discrepancy, relaxes the samples using Llloyd's algorithm in 1D, and computes the discrepancy of the relaxed samples. Discrepancy of the relaxed samples must be less than or equal to the discrepancy of the original samples. """ random.seed(1234567) for _ in xrange(0, 10): samples = [] num_samples = 10 clock = 0.0 samples.append(clock) for _ in xrange(1, num_samples): clock += random.random() samples.append(clock) samples = statistics.NormalizeSamples(samples)[0] d = statistics.Discrepancy(samples) relaxed_samples = Relax(samples) d_relaxed = statistics.Discrepancy(relaxed_samples) self.assertTrue(d_relaxed <= d)