Gradient and Hessian for logistic regression