o
    h                     @   s   d dl Zd dlmZ d dlmZmZmZ d dlm	Z	 dgZ
dd Zdd	 Zd
d Zdd Ze	eddedddd dddddZdS )    N)stats)_SimpleNormalSignificanceResult_get_pvalue)_axis_nan_policy_factorychatterjeexic           
      C   s   | j d }tj| dd}t||\}}tj||dd}tj|ddd}tj| ddd}tjttj	|dddd}|rKdd| |d d   }ndtj|| | dd }	d|| |	  }|||fS )Naxismax)methodr
            )
shapenpargsortbroadcast_arraystake_along_axisr   rankdatasumabsdiff)
xyy_continuousnjrlnum	statisticden r#   L/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/stats/_correlation.py_xi_statistic   s   

r%   c                 C   s0  t | jd }|rt dt | S t d|d }t j| dd}t j|dd}d|d  t jd| d|  d |d  dd }d|d  t j||| |  d dd }d|d  t jd| d|  d | dd }	d|d  t j|||  dd }
|d|  |	d  |
d  }t |t | S )	Nr   g?r   r	      r      r   )r   float64r   sqrtarangesortcumsumr   )r   r   r   r   iuvanbncndntau2r#   r#   r$   _xi_std*   s   2*."r5   c                 C   s@   | dvrt dt|tjs| }d}|dkrt || |fS )N>   FTz`y_continuous` must be boolean.z@`method` must be 'asymptotic' or a `PermutationMethod` instance.
asymptotic)
ValueError
isinstancer   PermutationMethodlower)r   r   messager#   r#   r$   _chatterjeexi_ivD   s   r<   c                 C   s   | j | jfS )N)r!   pvalue)resr#   r#   r$   _unpackT   s   r?   Tr   r   )paired	n_samplesresult_to_tuple	n_outputs	too_smallFr6   )r
   r   r   c                   s   t |\}d}|dkr)t |\}}}t||}	t }
t||	 |
|d}n&t|tjrOtjd|f fdd|dd|	 dd	i}|j
|j}}t||S )a  Compute the xi correlation and perform a test of independence

    The xi correlation coefficient is a measure of association between two
    variables; the value tends to be close to zero when the variables are
    independent and close to 1 when there is a strong association. Unlike
    other correlation coefficients, the xi correlation is effective even
    when the association is not monotonic.

    Parameters
    ----------
    x, y : array-like
        The samples: corresponding observations of the independent and
        dependent variable. The (N-d) arrays must be broadcastable.
    axis : int, default: 0
        Axis along which to perform the test.
    method : 'asymptotic' or `PermutationMethod` instance, optional
        Selects the method used to calculate the *p*-value.
        Default is 'asymptotic'. The following options are available.

        * ``'asymptotic'``: compares the standardized test statistic
          against the normal distribution.
        * `PermutationMethod` instance. In this case, the p-value
          is computed using `permutation_test` with the provided
          configuration options and other appropriate settings.

    y_continuous : bool, default: False
        Whether `y` is assumed to be drawn from a continuous distribution.
        If `y` is drawn from a continuous distribution, results are valid
        whether this is assumed or not, but enabling this assumption will
        result in faster computation and typically produce similar results.

    Returns
    -------
    res : SignificanceResult
        An object containing attributes:

        statistic : float
            The xi correlation statistic.
        pvalue : float
            The associated *p*-value: the probability of a statistic at least as
            high as the observed value under the null hypothesis of independence.

    See Also
    --------
    scipy.stats.pearsonr, scipy.stats.spearmanr, scipy.stats.kendalltau

    Notes
    -----
    There is currently no special handling of ties in `x`; they are broken arbitrarily
    by the implementation.

    [1]_ notes that the statistic is not symmetric in `x` and `y` *by design*:
    "...we may want to understand if :math:`Y` is a function :math:`X`, and not just
    if one of the variables is a function of the other." See [1]_ Remark 1.

    References
    ----------
    .. [1] Chatterjee, Sourav. "A new coefficient of correlation." Journal of
           the American Statistical Association 116.536 (2021): 2009-2022.
           :doi:`10.1080/01621459.2020.1758115`.

    Examples
    --------
    Generate perfectly correlated data, and observe that the xi correlation is
    nearly 1.0.

    >>> import numpy as np
    >>> from scipy import stats
    >>> rng = np.random.default_rng(348932549825235)
    >>> x = rng.uniform(0, 10, size=100)
    >>> y = np.sin(x)
    >>> res = stats.chatterjeexi(x, y)
    >>> res.statistic
    np.float64(0.9012901290129013)

    The probability of observing such a high value of the statistic under the
    null hypothesis of independence is very low.

    >>> res.pvalue
    np.float64(2.2206974648177804e-46)

    As noise is introduced, the correlation coefficient decreases.

    >>> noise = rng.normal(scale=[[0.1], [0.5], [1]], size=(3, 100))
    >>> res = stats.chatterjeexi(x, y + noise, axis=-1)
    >>> res.statistic
    array([0.79507951, 0.41824182, 0.16651665])

    Because the distribution of `y` is continuous, it is valid to pass
    ``y_continuous=True``. The statistic is identical, and the p-value
    (not shown) is only slightly different.

    >>> stats.chatterjeexi(x, y + noise, y_continuous=True, axis=-1).statistic
    array([0.79507951, 0.41824182, 0.16651665])

    greaterr6   )alternativec                    s   t  | d S )Nr   )r%   )r   r
   r   r   r#   r$   <lambda>   s    zchatterjeexi.<locals>.<lambda>pairings)datar!   rF   permutation_typer
   r   Nr#   )r<   r%   r5   r   r   r8   r   r9   permutation_test_asdictr!   r=   r   )r   r   r
   r   r   rF   xir   r   stdnormr=   r>   r#   rG   r$   r   X   s$   e
)numpyr   scipyr   scipy.stats._stats_pyr   r   r   scipy.stats._axis_nan_policyr   __all__r%   r5   r<   r?   r   r#   r#   r#   r$   <module>   s    