import torch


def sharpness(predictions:list, total = True):
    """
    Calculate the mean size of the intervals, called the sharpness (lower the better)

    Parameters
    ----------
    predictions : list
        - predictions[0] = y_pred_upper, predicted upper limit of the target variable (torch.Tensor)
        - predictions[1] = y_pred_lower, predicted lower limit of the target variable (torch.Tensor)
    total : bool, default = True
        - When total is set to True, return overall sharpness
        - When total is set to False, return sharpness along the horizon

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

    """

    assert len(predictions) == 2
    y_pred_upper = predictions[0]
    y_pred_lower = predictions[1]
    if total:
        return torch.mean(y_pred_upper - y_pred_lower)
    else:
        return torch.mean(y_pred_upper - y_pred_lower, dim=0)