o
    yi
                  	   @   s   d dl mZ 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 )    )TupleN)Tensor)_check_same_shapepredstargetreturnc                 C   s.   t | | | |   }|  }||fS )zUpdates and returns variables required to compute Weighted Absolute Percentage Error.

    Checks 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   \/home/ubuntu/.local/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 )zComputes 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  Computes 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:
        >>> import torch
        >>> _ = torch.manual_seed(42)
        >>> preds = torch.randn(20,)
        >>> target = torch.randn(20,)
        >>> weighted_mean_absolute_percentage_error(preds, target)
        tensor(1.3967)
    )r   r   )r   r   r
   r   weighted_aper   r   r   'weighted_mean_absolute_percentage_error:   s   r   )r   )typingr   r   r   torchmetrics.utilities.checksr   intr   floatr   r   r   r   r   r   <module>   s,   


