def _fpos_bias(df, fpos, tneg):
    """Choose a bias for a decision function, such that
    at most `frac` fraction of negative examples satisfy

    clf.decision_function(X) >= bias

    Parameters
    ----------
    df: array-like
        Decision function evalued for negative examples
    fpos : float
        Maximum false positive fraction
    tneg : int
        Total number of negative examples (may be larger than df,
        if previous negatives have been removed in previous versions
        of the cascade)

    Returns
    -------
    float
    """
    df.sort()

    tiny = 1e-7
    fpos = fpos * tneg / df.size

    if fpos >= 1:
        return df[0] - tiny

    ind = max(min(int((1 - fpos) * df.size), df.size - 1), 0)

    result = df[ind] + tiny
    assert (df >= result).mean() <= fpos

    return result