def _median(d):
    """Returns the median key from histogram (as a dict with values=counts) of frequencies"""
    assert len(d)
    count = 0
    total = sum(d.values())
    for key in sorted(d.keys()):
        count += d[key]
        if count >= 0.5 * total:
            return key
