Example #1
0
 def test_count_past_events(self):
     # Empty counts for empty results.
     self.assertEqual({}, oracle.count_past_events((), LARGE_WINDOW))
     # Simple case with different window sizes.
     results = ('c', 'a', 'b', 'a', 'b')
     self.assertEqual({'a': 1, 'b': 2},
                      oracle.count_past_events(results, SMALL_WINDOW))
     self.assertEqual({'a': 2, 'b': 2, 'c': 1},
                      oracle.count_past_events(results, LARGE_WINDOW))
     # Total of counts should be equal to window size.
     self.assertEqual(LARGE_WINDOW, sum(self.past_counts.values()))
     # All keys of past_counts should be THINGS.
     self.assertNotIn(
         False, [
             thing in THINGS for thing in self.past_counts])
Example #2
0
 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)
Example #3
0
def show_stats():
    """Display statistics about historical selections."""
    _, results = _init_data()
    count = len(results)
    if count:
        stats = oracle.count_past_events(results, WINDOW_SIZE)
        print 'Your last %d choices included:' % count
        print '\n'.join(['  %sx %s' % (v, k) for (k, v) in stats.items()])
    else:
        print 'No activity recorded so far.'
Example #4
0
def _check_for_uniform_distribution(testcase, results, things):
    """Check if results contain an uniform distribution of things within certain
    TOLERANCE."""
    dist = oracle.count_past_events(results, len(results))
    num_results = len(results)
    expected_count = num_results / float(len(things))
    allowed_delta = TOLERANCE * num_results
    for (thing, count) in dist.items():
        testcase.assertAlmostEqual(
            expected_count,
            count,
            delta=allowed_delta,
            msg='In a run of %s iterations, thing "%s" appeared %d times, which is off the expected count of %d (±%d)' %
            (num_results,
             thing,
             count,
             expected_count,
             TOLERANCE))
Example #5
0
 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])