def f1_at_n(is_match, potential_matches, n):
    """
    Takes a boolean list denoting if the n-th entry of the predictions is an actual match
    and the number of potential matches, i.e. how many matches are at most possible and
    an integer n and computed the f1 score if one were to only consider the n most
    relevant matches
    :param is_match:
    :param potential_matches:
    :param n:
    :return:
    """
    if potential_matches == 0:
        return 0
    correct_prediction = float(sum(is_match[:n]))
    precision = correct_prediction / n
    recall = correct_prediction / potential_matches
    try:
        if (recall + precision) != 0.0:
            f1 = 2 * (recall * precision) / (recall + precision)
        else:
            f1 = 0
    except ZeroDivisionError:
        f1 = 0
    return f1