def progress_heuristic(losses):
    """
The progress heuristic: how to determine that it's time to stop CG?
There are many possible ways to address this question, and the
progerss heuristic is a pretty decent way; we look at the sequence of
losses as defined by the quadratic model. That is, losses[i] =
ip(g,x_i) -.5*ip(x_i,Ax_i).  If the progress heuristic notices that
losses stops getting better relative to the size of the reduction,
it'll return True, which means that CG should stop. Otherwise it
should return false. It is copied verbatim from the original HF paper.
"""
    eps = 0.0005

    i = len(losses)
    k = int(max(10, 0.1*i))

    if len(losses) < k+1:
        return False

    phi_x_i   = losses[-1]
    phi_x_imk = losses[-1-k]

    if i>k and phi_x_i<0 and (phi_x_i-phi_x_imk)/phi_x_i < k*eps:
        return True
    else:
        return False