o
    ?Hh$                     @   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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_SimpleNormal)
_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	   ).0nir   r   U/home/air/sanwanet/gpt-api/venv/lib/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   pmfsr   r   r   _cdf1   s   
zWilcoxonDistribution._cdf1c                 C      t j| jtgd||S N)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   r1   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   r0   r   r   <lambda>,       z*WilcoxonDistribution.cdf.<locals>.<lambda>f2r   )r9   r   r   r+   r6   r   r0   r   cdf)      
zWilcoxonDistribution.cdfc                    r:   )Nc                    s   d  | d | S r   )r+   r;   r0   r   r   r<   1   r=   z)WilcoxonDistribution.sf.<locals>.<lambda>r>   r   )r9   r   r   r.   r6   r   r0   r   sf.   rA   zWilcoxonDistribution.sfN)__name__
__module____qualname__r   r$   r+   r-   r.   r1   r9   r@   rB   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 |dk}|dkr|j
d dkrd}||||||||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.>   prattwilcoxzsplitz`zero_method` must be one of .TFz`correction` must be one of >   lessgreater	two-sidedz`alternative` must be one of >   autoexact
asymptoticz`method` must be one of z- or an instance of `stats.PermutationMethod`.rR   rP   2   )r   r   
issubdtyper2   integerndim
ValueErrorr   moveaxis	AxisErrorr4   r   r5   floatingstrlower
isinstancer   PermutationMethodr    )xyzero_method
correctionalternativemethodrG   messagedezero_methodscorrectionsalternativesmethodsoutput_zn_zeror   r   r   _wilcoxon_iv4   s`   

rn   rJ   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	 }|d	krgt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 }|dv r|	| | }ntj}|	|
||||fS )Nr   rJ   	WRITEABLErH   rF   averageT)return_tiesrK      g      ?g      ?g       @rI         )rR   rP   )flagsr   r   nanisnanr    r4   r   absanysqrt)rf   rd   ra   i_zerosi_nann_nancountrtr_plusr_minushas_tiesr_zero_2r7   serm   tie_correctzr   r   r   _wilcoxon_statistict   s<   


r   c                 C   s"   |dkrdS |dkrdS t | S )NrN   r   rM   rH   )r   sign)r   rc   r   r   r   _correction_sign   s
   
r   TrO   rP   c              	      s  t | ||| |}|\}}} }}	}
|jdkr-t|}tj||d} dkr+||_|S t| \}}}}}} dkrT|sF|
dksFd n|jd dkrRt	  nd  dkrq|rgt
||}||d | 8 }t|t |td	}nX dkrt|}|d
kr|t|}nC|dkr|t|}n6dt|t||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pvaluerR   rP   rQ   rH      g      ?)xprM   rN   rr   r   c                    s   t |  d S )Nr   )r   )rf   rd   ra   r   r   r<      s    z_wilcoxon_nd.<locals>.<lambda>permutation_typesamples)rc   rG   rO   r   )rn   sizer   r   WilcoxonResult
zstatisticr   r4   r   r^   r   r   r   r   r   r@   ceilrB   floorminimumclippermutation_test_asdictr   rx   )r_   r`   ra   rb   rc   rd   rG   temprf   rl   rm   NaNresr   r   r   r   r~   r   r   pdistr   r   r   r   _wilcoxon_nd   sb   
	

 
r   )rJ   )NrJ   TrO   rP   r   )numpyr   scipyr   	_stats_pyr   r   r    r   _axis_nan_policyr   
_hypotestsr
   scipy._lib._utilr   r   r   rn   r   r   r   r   r   r   r   <module>   s    )
@?	