import torch


def mape(target, predictions:list, total = True):
    """
    Calculate root mean absolute error (mean absolute percentage error in %)

    Parameters
    ----------
    target : torch.Tensor
        true values of the target variable
    predictions : list
        - predictions[0] = predicted expected values of the target variable (torch.Tensor)
    total : bool, default = True
        Used in other loss functions to specify whether to return overall loss or loss over
        the horizon. This function only supports the former.

    Returns
    -------
    torch.Tensor
        A scalar with the mean absolute percentage error in % (lower the better)

    Raises
    ------
    NotImplementedError
        When 'total' is set to False, as MAPE does not support loss over the horizon
    """
    
    if not total:
        raise NotImplementedError("MAPE does not support loss over the horizon")

    return torch.mean(torch.abs((target - predictions[0]) / target)) * 100