fix bug in gradient checking (had been differentiating wrt. p not phi)