a
    d                     @   s   d dl mZ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 d dlmZ d dlmZmZ esd	gZG d
d deZdS )    )AnyListOptionalSequenceTupleUnion)Tensor)Literal)_kendall_corrcoef_compute_kendall_corrcoef_update_MetricVariant_TestAlternative)Metric)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEKendallRankCorrCoef.plotc                       s   e Zd ZU dZdZdZdZdZee	d< dZ
ee	d< ee e	d	< ee e	d
< ded eeed  eedd fddZeeddddZeeeeef f dddZdeeeee f  ee edddZ  ZS )KendallRankCorrCoefa  Compute `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):
        >>> from torch import tensor
        >>> from torchmetrics.regression import KendallRankCorrCoef
        >>> preds = tensor([2.5, 0.0, 2, 8])
        >>> target = tensor([3, -0.5, 2, 1])
        >>> kendall = KendallRankCorrCoef()
        >>> kendall(preds, target)
        tensor(0.3333)

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

    Example (single output regression with t-test):
        >>> from torchmetrics.regression import KendallRankCorrCoef
        >>> preds = tensor([2.5, 0.0, 2, 8])
        >>> target = 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):
        >>> from torchmetrics.regression import KendallRankCorrCoef
        >>> preds = tensor([[2.5, 0.0], [2, 8]])
        >>> target = 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]))

    FNTg        plot_lower_boundg      ?plot_upper_boundpredstargetb	two-sided   )ar   c)r   ZlessZgreater)variantt_testalternativenum_outputskwargsreturnc                    s   t  jf i | t|ts0tdt| d|rD|d u rDtdtt|| _	|rft
t|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)Zdist_reduce_fxr   )super__init__
isinstancebool
ValueErrortyper   Zfrom_strstrr   r   r    r!   Z	add_state)selfr   r   r    r!   r"   	__class__ h/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/regression/kendall.pyr'   |   s    
zKendallRankCorrCoef.__init__)r   r   r#   c                 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   r0   r0   r1   update   s    zKendallRankCorrCoef.update)r#   c                 C   s>   t | j}t | j}t||| j| j\}}|dur:||fS |S )zgCompute Kendall rank correlation coefficient, and optionally p-value of corresponding statistical test.N)r   r   r   r
   r   r    )r-   r   r   tauZp_valuer0   r0   r1   compute   s    

zKendallRankCorrCoef.compute)valaxr#   c                 C   s   |  ||S )a  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting a single value
            >>> from torchmetrics.regression import KendallRankCorrCoef
            >>> metric = KendallRankCorrCoef()
            >>> metric.update(randn(10,), randn(10,))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting multiple values
            >>> from torchmetrics.regression import KendallRankCorrCoef
            >>> metric = KendallRankCorrCoef()
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metric(randn(10,), randn(10,)))
            >>> fig, ax = metric.plot(values)

        )Z_plot)r-   r5   r6   r0   r0   r1   plot   s    (r   )r   Fr   r   )NN)__name__
__module____qualname____doc__Zis_differentiableZhigher_is_betterZfull_state_updater   float__annotations__r   r   r   r	   r)   r   intr   r'   r2   r   r   r4   r   r   r   r7   __classcell__r0   r0   r.   r1   r   #   s6   
O    

 r   N)typingr   r   r   r   r   r   Ztorchr   Ztyping_extensionsr	   Z*torchmetrics.functional.regression.kendallr
   r   r   r   Ztorchmetrics.metricr   Ztorchmetrics.utilities.datar   Ztorchmetrics.utilities.importsr   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   r0   r0   r0   r1   <module>   s    