§
    ’©ºjÛ  ã                   ó  — d dl Z d dlmZ d dlZd dl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	ed
ededed         dee         defd„Zdedefd„Z	 	 dd	ed
eded         dee         def
d„Z	 	 ddeded         dee         defd„ZdS )é    N)ÚOptional)ÚTensor)ÚLiteral)Ú#_multiclass_confusion_matrix_update)Ú_compute_chi_squaredÚ_drop_empty_rows_and_colsÚ_handle_nan_in_dataÚ_nominal_input_validationÚreplaceç        ÚpredsÚtargetÚnum_classesÚnan_strategy)r   ÚdropÚnan_replace_valueÚreturnc                 óÖ   — | j         dk    r|                      d¦  «        n| } |j         dk    r|                     d¦  «        n|}t          | |||¦  «        \  } }t          | ||¦  «        S )a  Compute the bins to update the confusion matrix with for Pearson's Contingency Coefficient calculation.

    Args:
        preds: 1D or 2D tensor of categorical (nominal) data
        target: 1D or 2D tensor of categorical (nominal) data
        num_classes: Integer specifying the number of classes
        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN`s when ``nan_strategy = 'replace```

    Returns:
        Non-reduced confusion matrix

    é   é   )ÚndimÚargmaxr	   r   )r   r   r   r   r   s        úw/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/nominal/pearson.pyÚ(_pearsons_contingency_coefficient_updater      sm   € ð(  %œz¨Qš˜ˆE�LŠL˜‰OŒOˆO°E€EØ!'¤°Ò!1Ð!1ˆV�]Š]˜1ÑÔÐ°v€FÝ'¨¨v°|ÐEVÑWÔW�M€Eˆ6Ý.¨u°f¸kÑJÔJÐJó    Úconfmatc                 óÔ   — t          | ¦  «        } |                      ¦   «         }t          | d¬¦  «        }||z  }t          j        |d|z   z  ¦  «        }|                     dd¦  «        S )zÐCompute Pearson's Contingency Coefficient based on a pre-computed confusion matrix.

    Args:
        confmat: Confusion matrix for observed data

    Returns:
        Pearson's Contingency Coefficient

    F)Úbias_correctionr   r   g      ð?)r   Úsumr   ÚtorchÚsqrtÚclamp)r   Úcm_sumÚchi_squaredÚphi_squaredÚtschuprows_t_values        r   Ú)_pearsons_contingency_coefficient_computer'   8   si   € õ (¨Ñ0Ô0€GØ�[Š[‰]Œ]€FÝ& wÀÐFÑFÔF€KØ Ñ&€Kåœ K°1°{±?Ñ$CÑDÔDÐØ×#Ò# C¨Ñ-Ô-Ð-r   c                 óÐ   — t          ||¦  «         t          t          j        | |g¦  «                             ¦   «         ¦  «        }t          | ||||¦  «        }t          |¦  «        S )aJ  Compute `Pearson's Contingency Coefficient`_ for measuring the association between two categorical data series.

    .. math::
        Pearson = \sqrt{\frac{\chi^2 / n}{1 + \chi^2 / n}}

    where

    .. math::
        \chi^2 = \sum_{i,j} \ frac{\left(n_{ij} - \frac{n_{i.} n_{.j}}{n}\right)^2}{\frac{n_{i.} n_{.j}}{n}}

    where :math:`n_{ij}` denotes the number of times the values :math:`(A_i, B_j)` are observed with :math:`A_i, B_j`
    represent frequencies of values in ``preds`` and ``target``, respectively.

    Pearson's Contingency Coefficient is a symmetric coefficient, i.e.
    :math:`Pearson(preds, target) = Pearson(target, preds)`.

    The output values lies in [0, 1] with 1 meaning the perfect association.

    Args:
        preds: 1D or 2D tensor of categorical (nominal) data:

            - 1D shape: (batch_size,)
            - 2D shape: (batch_size, num_classes)

        target: 1D or 2D tensor of categorical (nominal) data:

            - 1D shape: (batch_size,)
            - 2D shape: (batch_size, num_classes)

        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN``s when ``nan_strategy = 'replace'``

    Returns:
        Pearson's Contingency Coefficient

    Example:
        >>> from torch import randint, round
        >>> from torchmetrics.functional.nominal import pearsons_contingency_coefficient
        >>> preds = randint(0, 4, (100,))
        >>> target = round(preds + torch.randn(100)).clamp(0, 4)
        >>> pearsons_contingency_coefficient(preds, target)
        tensor(0.6948)

    )r
   Úlenr    ÚcatÚuniquer   r'   )r   r   r   r   r   r   s         r   Ú pearsons_contingency_coefficientr,   K   sb   € õd ˜lÐ,=Ñ>Ô>Ð>Ý•e”i ¨ Ñ0Ô0×7Ò7Ñ9Ô9Ñ:Ô:€KÝ6°u¸fÀkÐS_ÐarÑsÔs€GÝ4°WÑ=Ô=Ð=r   Úmatrixc                 óÂ  — t          ||¦  «         | j        d         }t          j        ||| j        ¬¦  «        }t          j        t          |¦  «        d¦  «        D ]‚\  }}| dd…|f         | dd…|f         }}t          t          j	        ||g¦  «         
                    ¦   «         ¦  «        }	t          |||	||¦  «        }
t          |
¦  «        }|x|||f<   |||f<   Œƒ|S )a   Compute `Pearson's Contingency Coefficient`_ statistic between a set of multiple variables.

    This can serve as a convenient tool to compute Pearson's Contingency Coefficient for analyses
    of correlation between categorical variables in your dataset.

    Args:
        matrix: A tensor of categorical (nominal) data, where:

            - rows represent a number of data points
            - columns represent a number of categorical (nominal) features

        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN``s when ``nan_strategy = 'replace'``

    Returns:
        Pearson's Contingency Coefficient statistic for a dataset of categorical variables

    Example:
        >>> from torch import randint
        >>> from torchmetrics.functional.nominal import pearsons_contingency_coefficient_matrix
        >>> matrix = randint(0, 4, (200, 5))
        >>> pearsons_contingency_coefficient_matrix(matrix)
        tensor([[1.0000, 0.2326, 0.1959, 0.2262, 0.2989],
                [0.2326, 1.0000, 0.1386, 0.1895, 0.1329],
                [0.1959, 0.1386, 1.0000, 0.1840, 0.2335],
                [0.2262, 0.1895, 0.1840, 1.0000, 0.2737],
                [0.2989, 0.1329, 0.2335, 0.2737, 1.0000]])

    r   )Údevicer   N)r
   Úshaper    Úonesr/   Ú	itertoolsÚcombinationsÚranger)   r*   r+   r   r'   )r-   r   r   Únum_variablesÚpearsons_cont_coef_matrix_valueÚiÚjÚxÚyr   r   Úvals               r   Ú'pearsons_contingency_coefficient_matrixr<   ƒ   sû   € õD ˜lÐ,=Ñ>Ô>Ð>Ø”L ”O€MÝ&+¤j°ÀÐV\ÔVcÐ&dÑ&dÔ&dÐ#ÝÔ&¥u¨]Ñ';Ô';¸QÑ?Ô?ð \ð \‰ˆˆ1Ø�a�a�a˜�dŒ|˜V A A A q Dœ\ˆ1ˆÝ�%œ) Q¨ FÑ+Ô+×2Ò2Ñ4Ô4Ñ5Ô5ˆÝ:¸1¸aÀÈlÐ\mÑnÔnˆÝ7¸Ñ@Ô@ˆØX[Ð[Ð'¨¨1¨Ñ-Ð0OÐPQÐSTÐPTÑ0UÐ0UØ*Ð*r   )r   r   )r2   Útypingr   r    r   Útyping_extensionsr   Ú7torchmetrics.functional.classification.confusion_matrixr   Ú%torchmetrics.functional.nominal.utilsr   r   r	   r
   ÚintÚfloatr   r'   r,   r<   © r   r   ú<module>rD      sã  ðð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à gÐ gÐ gÐ gÐ gÐ gðð ð ð ð ð ð ð ð ð ð ð ð 09Ø),ðKð KØðKàðKð ðKð Ð+Ô,ð	Kð
   ”ðKð ðKð Kð Kð Kð4.°vð .À&ð .ð .ð .ð .ð, 09Ø),ð	5>ð 5>Øð5>àð5>ð Ð+Ô,ð5>ð   ”ð	5>ð
 ð5>ð 5>ð 5>ð 5>ðt 09Ø),ð++ð ++Øð++àÐ+Ô,ð++ð   ”ð++ð ð	++ð ++ð ++ð ++ð ++ð ++r   