o
    yi                     @   s<   d dl mZ d dlmZ d dlmZmZ G dd deZdS )    )Tensor)_concordance_corrcoef_compute)PearsonCorrCoef_final_aggregationc                   @   s   e Zd ZdZdefddZdS )ConcordanceCorrCoefa  Computes concordance correlation coefficient that measures the agreement between two variables. It is
    defined as.

    .. math::
        \rho_c = \frac{2 \rho \sigma_x \sigma_y}{\sigma_x^2 + \sigma_y^2 + (\mu_x - \mu_y)^2}

    where :math:`\mu_x, \mu_y` is the means for the two variables, :math:`\sigma_x^2, \sigma_y^2` are the corresponding
    variances and \rho is the pearson correlation coefficient between the two variables.

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

    - ``preds`` (:class:`~torch.Tensor`): either single output float tensor with shape ``(N,)`` or multioutput
      float tensor of shape ``(N,d)``
    - ``target`` (:class:`~torch.Tensor`): either single output float tensor with shape ``(N,)`` or multioutput
      float tensor of shape ``(N,d)``

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

    - ``concordance`` (:class:`~torch.Tensor`): A scalar float tensor with the concordance coefficient(s) for
      non-multioutput input or a float tensor with shape ``(d,)`` for multioutput input

    Args:
        num_outputs: Number of outputs in multioutput setting
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example (single output regression):
        >>> from torchmetrics import ConcordanceCorrCoef
        >>> import torch
        >>> target = torch.tensor([3, -0.5, 2, 7])
        >>> preds = torch.tensor([2.5, 0.0, 2, 8])
        >>> concordance = ConcordanceCorrCoef()
        >>> concordance(preds, target)
        tensor(0.9777)

    Example (multi output regression):
        >>> from torchmetrics import ConcordanceCorrCoef
        >>> import torch
        >>> target = torch.tensor([[3, -0.5], [2, 7]])
        >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
        >>> concordance = ConcordanceCorrCoef(num_outputs=2)
        >>> concordance(preds, target)
        tensor([0.7273, 0.9887])
    returnc                 C   s   | j dkr| j dks| j dkr-| jjdkr-t| j| j| j| j| j| j	\}}}}}}n| j}| j}| j}| j}| j}| j	}t
||||||S )zFComputes final concordance correlation coefficient over metric states.   )num_outputsmean_xnumelndimr   mean_yvar_xvar_ycorr_xyn_totalr   )selfr
   r   r   r   r   r    r   W/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/regression/concordance.pycomputeA   s   .zConcordanceCorrCoef.computeN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    ,r   N)torchr   .torchmetrics.functional.regression.concordancer   torchmetrics.regression.pearsonr   r   r   r   r   r   r   <module>   s   