o
    yi                     @   s   d dl mZ d dlZd dlmZ d dlmZ 	ddededed	eeef fd
dZdeded	efddZ	deded	efddZ
dS )    )TupleN)Tensor)_check_same_shape-`>predstargetepsilonreturnc                 C   sX   t | | t| | }|tjt|t|  |d }dt| }| }||fS )a  Updates and returns variables required to compute Symmetric Mean Absolute Percentage Error.

    Checks for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        epsilon: Avoids ``ZeroDivisionError``.
    )min   )r   torchabsclampsumnumel)r   r   r   abs_diffabs_per_errorsum_abs_per_errornum_obs r   e/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/regression/symmetric_mape.py0_symmetric_mean_absolute_percentage_error_update   s   
"r   r   r   c                 C   s   | | S )a  Computes Symmetric Mean Absolute Percentage Error.

    Args:
        sum_abs_per_error: Sum of values of symmetric absolute percentage errors over all observations
            ``(symmetric absolute percentage error = 2 * |target - prediction| / (target + prediction))``
        num_obs: Number of predictions or observations

    Example:
        >>> target = torch.tensor([1, 10, 1e6])
        >>> preds = torch.tensor([0.9, 15, 1.2e6])
        >>> sum_abs_per_error, num_obs = _symmetric_mean_absolute_percentage_error_update(preds, target)
        >>> _symmetric_mean_absolute_percentage_error_compute(sum_abs_per_error, num_obs)
        tensor(0.2290)
    r   )r   r   r   r   r   1_symmetric_mean_absolute_percentage_error_compute1   s   r   c                 C   s   t | |\}}t||}|S )a  Computes symmetric mean absolute percentage error (SMAPE_):

    .. math:: \text{SMAPE} = \frac{2}{n}\sum_1^n\frac{|   y_i - \hat{y_i} |}{max(| y_i | + | \hat{y_i} |, \epsilon)}

    Where :math:`y` is a tensor of target values, and :math:`\hat{y}` is a tensor of predictions.

    Args:
        preds: estimated labels
        target: ground truth labels

    Return:
        Tensor with SMAPE.

    Example:
        >>> from torchmetrics.functional import symmetric_mean_absolute_percentage_error
        >>> target = torch.tensor([1, 10, 1e6])
        >>> preds = torch.tensor([0.9, 15, 1.2e6])
        >>> symmetric_mean_absolute_percentage_error(preds, target)
        tensor(0.2290)
    )r   r   )r   r   r   r   mean_aper   r   r   (symmetric_mean_absolute_percentage_errorD   s   r   )r   )typingr   r   r   torchmetrics.utilities.checksr   floatintr   r   r   r   r   r   r   <module>   s    

