Esempio n. 1
0
def print_cumulative_prob(n=1, m=1, digits=4):
    """Given a number of top results n and a number of nodes m print odds.

    Inputs
      :n: total number (e.g., total number of highest scoring results)
      :m: number of non-negative integers to sum to n (e.g., # of workers)
      :digits: number of digits after decimal to print

    Output
      print the probability of a result being omitted from returned set

    Exceptions
      raises ValueError if inputs are not non-negative integers

    """

    for param in (n, m, digits):
        if not is_nonneg_int(param):
            raise ValueError

    print_template = ('Probability of %(count_str)s or more of top %(n)d' +
        ' from one of %(m)d sets is %(p_str)s.')

    formats = {'count': '%%%dd' % len(str(n)), 'p': '%%0.%se' % str(digits)}
    
    for stats in compute_all_probabilities(n, m):
        stats['count_str'] = formats['count'] % stats['count']
        stats['p_str'] = formats['p'] % stats['p']
        print print_template % stats
Esempio n. 2
0
def compute_probabilities(n, m, t=()):
    """Compute probability that a result is missed.

    Inputs
      :n: total number (e.g., total number of highest scoring results)
      :m: number of non-negative integers to sum to n (e.g., number of
          workers, each returning an integer number of results)
      :t: optional threshold to short-circuit computation
          * integer t is the maximum number of results to return per worker

    Output
      :stats: dict containing fields:
          * count is the the number of results returned per worker
          * n is the total number of highest scoring results
          * m is the number of workers
          * p is the cumulative probability that a result is missed

    """

    if not is_nonneg_int(t):
        t = ()
    numerator = m ** n
    denominator = float(numerator)
    stats = {'n': n, 'm': m, 'count': 0, 'p': 0}
    mset = Multiset(n)
    for (cnt, ways) in mset.num_ways(n, m):
        stats['count'] = cnt
        stats['p'] = numerator / denominator
        if cnt < t:
            yield stats.copy()
        elif cnt == t:
            yield stats.copy()
            raise StopIteration
        else:
            raise StopIteration
        numerator -= ways
Esempio n. 3
0
 def test_is_nonneg_int_on_several_inputs(self):
     """Test is_nonneg_int on several inputs."""
     pairs = ((None, False), (-1, False), (0, True), (1, True), (5.0, True))
     for (value, expected) in pairs:
         self.assertEqual(is_nonneg_int(value), expected)
Esempio n. 4
0
 def test_is_nonneg_int_on_several_inputs(self):
     """Test is_nonneg_int on several inputs."""
     pairs = ((None, False), (-1, False),
         (0, True), (1, True), (5.0, True))
     for (value, expected) in pairs:
         self.assertEqual(is_nonneg_int(value), expected)