def setUp(self): # Generate random results, calculate intermediate values for those # results. self.random_results = [random.choice(THINGS) for _ in range(50)] self.past_counts = oracle.count_past_events( self.random_results, LARGE_WINDOW) self.weights = oracle.calculate_weights( THINGS, self.past_counts, LARGE_WINDOW) self.cdf = oracle.calculate_cdf(self.weights) self.random_thing = oracle.sample_cdf(self.cdf)
def test_calculate_weights(self): # No existing results, all weights should be equal to base chance. zero_counts = collections.defaultdict(int) self.assertEqual({ 'a': BASE_CHANCE, 'b': BASE_CHANCE, 'c': BASE_CHANCE, 'd': BASE_CHANCE, }, oracle.calculate_weights(THINGS, zero_counts, LARGE_WINDOW)) # Window full of single result, weights should be split between all # other choices. results = ('c', 'c', 'c', 'c') past_counts = oracle.count_past_events(results, SMALL_WINDOW) one_third = 1 / float(3) self.assertEqual({'a': one_third, 'b': one_third, 'd': one_third}, oracle.calculate_weights(THINGS, past_counts, SMALL_WINDOW)) # Weights should be normalized to (0, 1] range. self.assertNotIn(False, [1 >= w >= 0 for w in self.weights.values()]) # All keys of weights should be THINGS. self.assertNotIn(False, [thing in THINGS for thing in self.weights])