Math for least squares algorithm.