logistic regression cost function and gradient