def heuristic_distances(judgments, repeats):
    """
    Returns a numeric value for each distance (i, j) in judgments: d = (a + 1)/ (a + b + 2)
    a is number of times a distance is greater than another
    :param repeats: number of times each pairwise comparison is repeated
    :param judgments: (dict) key: pairs of distances (i,j) (m,n), value: counts
    :return: dict of distances (pair (i, j) -> distance value)
    """

    def increment(d, key, val):
        if key not in d:
            d[key] = val
        else:
            d[key] += val

    # take a pass through judgments and keep track of 'a' (win) and 'b' (loss) for each distance
    win = {}  # for readability, call 'a' WIN and call 'b' LOSS
    loss = {}
    distance = {}
    for comparison, count in judgments.items():
        increment(win, comparison[0], count)
        increment(loss, comparison[0], repeats - count)
        increment(loss, comparison[-1], count)
        increment(win, comparison[-1], repeats - count)

    # map win, loss values to distances
    for pair in win:
        distance[pair] = (win[pair] + 1) / float(win[pair] + loss[pair] + 2)

    return distance