import torch


def residuals(target, predictions:list, total = True):
    """
    Calculates the mean of the prediction error

    Parameters
    ----------
    target : torch.Tensor
        The true values of the target variable
    predictions : list (torch.Tensor)
        The predicted expected values of the target variable
    total : bool, default = True
        - When total is set to True, return the overall mean of the error
        - When total is set to False, return the mean of the error along the horizon

    Returns
    -------
    torch.Tensor
        The mean of the error, which depending on the value of 'total'
        is either a scalar (overall mean) or 1d-array over the horizon, in which case it is
        expected to increase as we move along the horizon. Generally, lower is better.

    Raises
    ------
    ValueError
        When the dimensions of the predictions and targets are not compatible
    """

    if predictions[0].shape != target.shape:
        raise ValueError('dimensions of predictions and targets need to be compatible')

    error = target - predictions[0]
    if total:
        return torch.mean(error)
    else:
        return torch.mean(error, dim=0)