Use np.minimum() instead of the bespoke code, which gives a ~30x performance boost.