§
    ’©ºjV   ã                   ó´   — 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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sd
gZ G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensor)ÚLiteral)Ú_kendall_corrcoef_computeÚ_kendall_corrcoef_updateÚ_MetricVariantÚ_TestAlternative)ÚMetric)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzKendallRankCorrCoef.plotc                   óL  ‡ — 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ded         dedeed                  dededdfˆ fd„Zd	ed
eddfd„Zdeeeeef         f         fd„Z	 ddeeeee         f                  dee         defd„Zˆ x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é   Úvariant)Úar   ÚcÚt_testÚalternative)r   ÚlessÚgreaterÚnum_outputsÚkwargsÚreturnc                 óØ  •—  t          ¦   «         j        di |¤Ž t          |t          ¦  «        s t	          dt          |¦  «        › d�¦  «        ‚|r|€t	          d¦  «        ‚t          j        t          |¦  «        ¦  «        | _	        |r!t          j        t          |¦  «        ¦  «        nd | _        || _        |                      dg d¬¦  «         |                      dg 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_strÚstrr   r   r    r#   Ú	add_state)Úselfr   r   r    r#   r$   Ú	__class__s         €úo/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/regression/kendall.pyr,   zKendallRankCorrCoef.__init__~   só   ø€ ð 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý˜&¥$Ñ'Ô'ð 	oÝÐmÕ^bÐciÑ^jÔ^jÐmÐmÐmÑnÔnÐnØð 	d�kÐ)ÝÐbÑcÔcÐcå%Ô.­s°7©|¬|Ñ<Ô<ˆŒØJPÐZÕ+Ô4µS¸Ñ5EÔ5EÑFÔFÐFÐVZˆÔØ&ˆÔà�Š�w °5ˆÑ9Ô9Ð9Ø�Š�x °EˆÑ:Ô:Ð:Ð:Ð:ó    c                 óf   — t          ||| j        | j        | j        ¬¦  «        \  | _        | _        dS )zJUpdate variables required to compute Kendall rank correlation coefficient.)r#   N)r   r   r   r#   )r4   r   r   s      r6   ÚupdatezKendallRankCorrCoef.update“   s9   € å":ØØØŒJØŒKØÔ(ð#
ñ #
ô #
ÑˆŒ
�D”K�K�Kr7   c                 ó    — t          | j        ¦  «        }t          | j        ¦  «        }t          ||| j        | j        ¦  «        \  }}|�||fS |S )zgCompute Kendall rank correlation coefficient, and optionally p-value of corresponding statistical test.)r   r   r   r
   r   r    )r4   r   r   ÚtauÚp_values        r6   ÚcomputezKendallRankCorrCoef.compute�   s[   € å˜TœZÑ(Ô(ˆÝ˜dœkÑ*Ô*ˆÝ0ØØØŒLØÔñ	
ô 
‰ˆˆWð ÐØ˜�<ÐØˆ
r7   ÚvalÚaxc                 ó.   — |                       ||¦  «        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)

        )Ú_plot)r4   r>   r?   s      r6   ÚplotzKendallRankCorrCoef.plot¬   s   € ðP �zŠz˜#˜rÑ"Ô"Ð"r7   )r   Fr   r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úis_differentiableÚhigher_is_betterÚfull_state_updater   ÚfloatÚ__annotations__r   r   r   r	   r.   r   Úintr   r,   r9   r   Útupler=   r   r   r   rB   Ú__classcell__)r5   s   @r6   r   r   $   s¨  ø€ € € € € € ðNð Nð` ÐØÐØÐØ!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!à�Œ<ÐÐÑØ�ŒLÐÐÑð +.ØØITØð;ð ;à˜Ô'ð;ð ð;ð ˜gÐ&DÔEÔFð	;ð
 ð;ð ð;ð 
ð;ð ;ð ;ð ;ð ;ð ;ð*
˜Fð 
¨Fð 
°tð 
ð 
ð 
ð 
ð˜˜v u¨V°V¨^Ô'<Ð<Ô=ð ð ð ð ð  _cð(#ð (#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð(#ØIQÐRZÔI[ð(#à	ð(#ð (#ð (#ð (#ð (#ð (#ð (#ð (#r7   r   N)Úcollections.abcr   Útypingr   r   r   r   Útorchr   Útyping_extensionsr	   Ú*torchmetrics.functional.regression.kendallr
   r   r   r   Útorchmetrics.metricr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r*   r7   r6   ú<module>rY      s<  ðð %Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %ðð ð ð ð ð ð ð ð ð ð ð ð 'Ð &Ð &Ð &Ð &Ð &Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð 4Ø2Ð3Ððp#ð p#ð p#ð p#ð p#˜&ñ p#ô p#ð p#ð p#ð p#r7   