o
    EOf?                     @   s   d dl Z d dlZd dlmZ ddl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 G d	d
 d
Zdd ZdddZdd Z		 dddZdS )    N)stats   )_get_pvalue	_rankdata)
_morestats)_broadcast_arrays_get_wilcoxon_distr)
_lazywhere_get_nanc                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )WilcoxonDistributionc                 C   s4   t |jtdd}|| _dd t |D | _d S )NFcopyc                 S   s   i | ]}|t |qS  r   ).0Znir   r   5lib/python3.10/site-packages/scipy/stats/_wilcoxon.py
<dictcomp>   s    z1WilcoxonDistribution.__init__.<locals>.<dictcomp>)npasarrayastypeintnunique_dists)selfr   r   r   r   __init__   s   zWilcoxonDistribution.__init__c                 C   s   | j | }|d |d   S Nr   r   sumr   kr   Zpmfsr   r   r   _cdf1   s   
zWilcoxonDistribution._cdf1c                 C      t j| jtgd||S N)Zotypes)r   	vectorizer!   floatr   r    r   r   r   r   _cdf      zWilcoxonDistribution._cdfc                 C   s   | j | }||d   S )Nr   r   r   r   r   _sf1   s   
zWilcoxonDistribution._sf1c                 C   r"   r#   )r   r$   r)   r%   r&   r   r   r   _sf   r(   zWilcoxonDistribution._sfc                 C   s   | j | j d  d S )Nr      )r   r   r   r   r   mean!   s   zWilcoxonDistribution.meanc                 C   s8   t |jtdd}|  }t j|jt jd}|||fS )NFr   )dtype)r   r   r   r   r-   emptyshapefloat64r   r    mnoutr   r   r   _prep$   s   
zWilcoxonDistribution._prepc                    8     |\}}}t||k| jf j fdddd S )Nc                    s   d  | d | S r   )r*   r    r   r,   r   r   <lambda>-       z*WilcoxonDistribution.cdf.<locals>.<lambda>f2r   )r5   r
   r   r'   r2   r   r,   r   cdf*      
zWilcoxonDistribution.cdfc                    r6   )Nc                    s   d  | d | S r   )r'   r7   r,   r   r   r8   2   r9   z)WilcoxonDistribution.sf.<locals>.<lambda>r:   r   )r5   r
   r   r*   r2   r   r,   r   sf/   r=   zWilcoxonDistribution.sfN)__name__
__module____qualname__r   r!   r'   r)   r*   r-   r5   r<   r>   r   r   r   r   r      s    r   c              
   C   s  t |d }d}t |jt jr|jdkrt|d}z#|d u r)t | } | }nt| |f|d\} }| | }t ||d}W n t j	yQ }	 zt||	d }	~	ww d}|d urf| j
| |j
| krft|d}t |jt jrv|t j}t |jt jst|t| }h d	}
d
|
 d}||
vrt|ddh}d| d}||vrt|t| }h d}d| d}||vrt|t|tjsh d}d| d}||vrt||dkrdnd}t j|dkdd}t |dk}|dkr	|j
d dkr|sd}nd}t |dk}|dkr#|dkr#d}tjddd |dkrC|dv rC||jkrC|jdkrC|jdkrCtdd|j
d   k rRdk r`n n|dkr`tjddd |||||||fS ) Nr   z`axis` must be an integer.r   z<`axis` must be compatible with the shape(s) of `x` (and `y`)axisz3`x` and `y` must have the same length along `axis`.z<`x` (and `y`, if provided) must be an array of real numbers.>   wilcoxprattzsplitz`zero_method` must be one of .TFz`correction` must be one of >   	two-sidedlessgreaterz`alternative` must be one of >   autoapproxexactz`method` must be one of z- or an instance of `stats.PermutationMethod`.rM   rL   2   rN   z^Exact p-value calculation does not work if there are zeros. Switching to normal approximation.   )
stacklevel)rE   rF   r   zOzero_method 'wilcox' and 'pratt' do not work if x - y is zero for all elements.
   z/Sample size too small for normal approximation.)r   r   Z
issubdtyper.   Zintegerndim
ValueErrorr   ZmoveaxisZ	AxisErrorr0   r   r1   Zfloatingstrlower
isinstancer   ZPermutationMethodr   anywarningswarnsize)xyzero_method
correctionalternativemethodrC   messagedeZzero_methodsZcorrectionsZalternativesmethodsoutput_zn_zeroZ	has_zerosr   r   r   _wilcoxon_iv5   s|   


$,rh   rE   c                 C   s  | dk}|dkr| j d s|  } tj| |< t| }tj|dd}| jd | }tt| ddd\}}tj| dk| dd}tj| dk | dd}	|d	kratj|| ddd
 }
||
7 }|	|
7 }	||d  d }||d  d| d  }|dkr|jdd}|||d  d 8 }|||d  d| d  8 }d||j	dddf< |d | jdd}||d
 8 }t
|d }|| | }||	|||fS )Nr   rE   Z	WRITEABLErD   rB   ZaverageT)Zreturn_tiesrG   rP   g      ?g      ?g       @rF         )flagsr   r   nanZisnanr   r0   r   absrX   Zsqrt)rc   r^   Zi_zerosZi_nanZn_nancountrtr_plusr_minusZr_zero_2r3   serg   Ztie_correctzr   r   r   _wilcoxon_statistic   s6   


ru   c                 C   s"   |dkrdS |dkrdS t | S )NrK   r   rJ   rD   )r   sign)rt   r`   r   r   r   _correction_sign   s
   
rw   TrI   rL   c                    s  t | | ||||}|\} }}}}}	|jdkr,t|}
tj|
|
d}|dkr*|
|_|S t| \}}}}}|dkrQ|rIt||}||d | 8 }t|t	j
|}nK|dkrt|}|dkrc||}n9|dkrm||}n/dt|||| }t|dd	}nt	j|f fd
dfddi| |ddj}|dkrt||n|}|dkr|dkrt| n|}tj||d d}|	r|d |_|S )Nr   )	statisticpvaluerM   g      ?rN   rJ   rK   rP   r   c                    s   t |  d S )Nr   )ru   )rc   r^   r   r   r8      s    z_wilcoxon_nd.<locals>.<lambda>Zpermutation_typeZsamplesrD   )r`   rC   rI   r   )rh   r[   r   r   ZWilcoxonResultZ
zstatisticru   rw   r   r   Znormr   r<   r>   r   ZminimumZclipZpermutation_test_asdictry   rm   )r\   r]   r^   r_   r`   ra   rC   Ztemprc   rf   ZNaNresrq   rr   rs   rt   rn   rv   pZdistrx   r   rz   r   _wilcoxon_nd   sN   

 
r~   )rE   )NrE   TrI   rL   r   )rY   Znumpyr   Zscipyr   Z	_stats_pyr   r    r   Z_axis_nan_policyr   Z
_hypotestsr	   Zscipy._lib._utilr
   r   r   rh   ru   rw   r~   r   r   r   r   <module>   s    )
Q5	