o
    .wi7
                  	   @   sv   d dl Z d dl mZ d dlmZ dededeeef fddZ		dd
edededefddZdededefddZdS )    N)Tensor)_check_same_shapepredstargetreturnc                 C   s.   t | | | |   }|  }||fS )zUpdate and returns variables required to compute Weighted Absolute Percentage Error.

    Check for same shape of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor

    )r   abssumr   r   sum_abs_error	sum_scale r   e/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/regression/wmape.py/_weighted_mean_absolute_percentage_error_update   s   
r   -`>r
   r   epsilonc                 C   s   | t j||d S )zCompute Weighted Absolute Percentage Error.

    Args:
        sum_abs_error: scalar with sum of absolute errors
        sum_scale: scalar with sum of target values
        epsilon: small float to prevent division by zero

    )min)torchclamp)r
   r   r   r   r   r   0_weighted_mean_absolute_percentage_error_compute*   s   r   c                 C   s   t | |\}}t||S )a  Compute weighted mean absolute percentage error (`WMAPE`_).

    The output of WMAPE metric is a non-negative floating point, where the optimal value is 0. It is computes as:

    .. math::
        \text{WMAPE} = \frac{\sum_{t=1}^n | y_t - \hat{y}_t | }{\sum_{t=1}^n |y_t| }

    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 WMAPE.

    Example:
        >>> from torch import randn
        >>> preds = randn(20,)
        >>> target = randn(20,)
        >>> weighted_mean_absolute_percentage_error(preds, target)
        tensor(1.3967)

    )r   r   r	   r   r   r   'weighted_mean_absolute_percentage_error:   s   
r   )r   )	r   r   torchmetrics.utilities.checksr   tupler   floatr   r   r   r   r   r   <module>   s*   


