def loss_function(params, estimates, fun, xval):
    """
    Loss function used by least squares.

    :param params: current values of parameters to fit
    :param estimates: estimates of function to fit
    :param fun: function to fit
    :param xval: x axis values to estimates (time points)
    :return: array of loss values for every estimation
    """
    return [(fun(xval[i], *params) - estimates[i]) for i in range(len(estimates))]