o
    yiD                     @   sx   d dl mZmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZ G dd deZd	S )
    )AnyListOptionalTupleUnion)Tensor)Literal)_kendall_corrcoef_compute_kendall_corrcoef_update_MetricVariant_TestAlternative)Metric)dim_zero_catc                       s   e Zd ZU dZdZdZdZee e	d< ee e	d< 					dd
e
d dedee
d  dedef
 fddZdededdfddZdeeeeef f fddZ  ZS )KendallRankCorrCoefa   Computes `Kendall Rank Correlation Coefficient`_:

    .. math::
        tau_a = \frac{C - D}{C + D}

    where :math:`C` represents concordant pairs, :math:`D` stands for discordant pairs.

    .. math::
        tau_b = \frac{C - D}{\sqrt{(C + D + T_{preds}) * (C + D + T_{target})}}

    where :math:`C` represents concordant pairs, :math:`D` stands for discordant pairs and :math:`T` represents
    a total number of ties.

    .. math::
        tau_c = 2 * \frac{C - D}{n^2 * \frac{m - 1}{m}}

    where :math:`C` represents concordant pairs, :math:`D` stands for discordant pairs, :math:`n` is a total number
    of observations and :math:`m` is a ``min`` of unique values in ``preds`` and ``target`` sequence.

    Definitions according to Definition according to `The Treatment of Ties in Ranking Problems`_.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~torch.Tensor`): Sequence of data in float tensor of either shape ``(N,)`` or ``(N,d)``
    - ``target`` (:class:`~torch.Tensor`): Sequence of data in float tensor of either shape ``(N,)`` or ``(N,d)``

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``kendall`` (:class:`~torch.Tensor`): A tensor with the correlation tau statistic,
      and if it is not None, the p-value of corresponding statistical test.

    Args:
        variant: Indication of which variant of Kendall's tau to be used
        t_test: Indication whether to run t-test
        alternative: Alternative hypothesis for t-test. Possible values:
            - 'two-sided': the rank correlation is nonzero
            - 'less': the rank correlation is negative (less than zero)
            - 'greater':  the rank correlation is positive (greater than zero)
        num_outputs: Number of outputs in multioutput setting
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        ValueError: If ``t_test`` is not of a type bool
        ValueError: If ``t_test=True`` and ``alternative=None``

    Example (single output regression):
        >>> import torch
        >>> from torchmetrics.regression import KendallRankCorrCoef
        >>> preds = torch.tensor([2.5, 0.0, 2, 8])
        >>> target = torch.tensor([3, -0.5, 2, 1])
        >>> kendall = KendallRankCorrCoef()
        >>> kendall(preds, target)
        tensor(0.3333)

    Example (multi output regression):
        >>> import torch
        >>> from torchmetrics.regression import KendallRankCorrCoef
        >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
        >>> target = torch.tensor([[3, -0.5], [2, 1]])
        >>> kendall = KendallRankCorrCoef(num_outputs=2)
        >>> kendall(preds, target)
        tensor([1., 1.])

    Example (single output regression with t-test):
        >>> import torch
        >>> from torchmetrics.regression import KendallRankCorrCoef
        >>> preds = torch.tensor([2.5, 0.0, 2, 8])
        >>> target = torch.tensor([3, -0.5, 2, 1])
        >>> kendall = KendallRankCorrCoef(t_test=True, alternative='two-sided')
        >>> kendall(preds, target)
        (tensor(0.3333), tensor(0.4969))

    Example (multi output regression with t-test):
        >>> import torch
        >>> from torchmetrics.regression import KendallRankCorrCoef
        >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
        >>> target = torch.tensor([[3, -0.5], [2, 1]])
        >>> kendall = KendallRankCorrCoef(t_test=True, alternative='two-sided', num_outputs=2)
        >>> kendall(preds, target)
        (tensor([1., 1.]), tensor([nan, nan]))
    FNTpredstargetb	two-sided   variant)ar   ct_testalternative)r   lessgreaternum_outputskwargsc                    s   t  jdi | t|tstdt| d|r"|d u r"tdt|| _|r1|r1t	|nd | _
|| _| jdg dd | jdg dd d S )	Nz>Argument `t_test` is expected to be of a type `bool`, but got .zCArgument `alternative` is required if `t_test=True` but got `None`.r   cat)dist_reduce_fxr    )super__init__
isinstancebool
ValueErrortyper   from_strr   r   r   r   	add_state)selfr   r   r   r   r   	__class__r!   S/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/regression/kendall.pyr#   w   s   
zKendallRankCorrCoef.__init__returnc                 C   s$   t ||| j| j| jd\| _| _dS )zJUpdate variables required to compute Kendall rank correlation coefficient.)r   N)r
   r   r   r   )r*   r   r   r!   r!   r-   update   s   zKendallRankCorrCoef.updatec                 C   s>   t | j}t | j}t||| j| j\}}|dur||fS |S )zoCompute Kendall rank correlation coefficient, and optionally p-value of corresponding statistical
        test.N)r   r   r   r	   r   r   )r*   r   r   taup_valuer!   r!   r-   compute   s   

zKendallRankCorrCoef.compute)r   Fr   r   )__name__
__module____qualname____doc__is_differentiablehigher_is_betterfull_state_updater   r   __annotations__r   r%   r   intr   r#   r/   r   r   r2   __classcell__r!   r!   r+   r-   r      s0   
 R
&
r   N)typingr   r   r   r   r   torchr   typing_extensionsr   *torchmetrics.functional.regression.kendallr	   r
   r   r   torchmetrics.metricr   torchmetrics.utilities.datar   r   r!   r!   r!   r-   <module>   s   