o
    i$                     @   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   O/home/kim/smarthome/.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   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   )r7   r   r   r)   r4   r   r.   r   cdf)      
zWilcoxonDistribution.cdfc                    r8   )Nc                    s   d  | d | S r   )r)   r9   r.   r   r   r:   1   r;   z)WilcoxonDistribution.sf.<locals>.<lambda>r<   r   )r7   r   r   r,   r4   r   r.   r   sf.   r?   zWilcoxonDistribution.sfN)__name__
__module____qualname__r   r#   r)   r+   r,   r/   r7   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 |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.>   wilcoxzsplitprattz`zero_method` must be one of .TFz`correction` must be one of >   less	two-sidedgreaterz`alternative` must be one of >   autoexact
asymptoticz`method` must be one of z- or an instance of `stats.PermutationMethod`.rP   rN   2   )r   r   Z
issubdtyper0   integerndim
ValueErrorr   ZmoveaxisZ	AxisErrorr2   r   r3   Zfloatingstrlower
isinstancer   PermutationMethodr    )xyzero_method
correctionalternativemethodrE   messagedeZzero_methodsZcorrectionsZalternativesmethodsoutput_zn_zeror   r   r   _wilcoxon_iv4   s`   

re   rG   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   rG   Z	WRITEABLErF   rD   ZaverageT)Zreturn_tiesrH      g      ?g      ?g       @rI         )rP   rN   )flagsr   r   nanisnanr    r2   r   absanysqrt)r`   r^   r[   Zi_zerosZi_nanZn_nancountrtr_plusr_minushas_tiesZr_zero_2r5   serd   Ztie_correctzr   r   r   _wilcoxon_statistict   s<   


rw   c                 C   s"   |dkrdS |dkrdS t | S )NrM   r   rK   rF   )r   sign)rv   r]   r   r   r   _correction_sign   s
   
ry   TrL   rN   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pvaluerP   rN   rO   rF      g      ?)ZxprK   rM   rf   r   c                    s   t |  d S )Nr   )rw   )r`   r^   r[   r   r   r:      s    z_wilcoxon_nd.<locals>.<lambda>Zpermutation_typeZsamples)r]   rE   rL   r   )re   sizer   r   ZWilcoxonResultZ
zstatisticrw   r2   r   rX   ry   r   r   r   r   r>   ceilr@   floorminimumZclipZpermutation_test_asdictr{   rl   )rY   rZ   r[   r\   r]   r^   rE   tempr`   rc   rd   NaNresrr   rs   ru   rv   ro   rt   rx   pdistrz   r   r}   r   _wilcoxon_nd   sb   
	

 
r   )rG   )NrG   TrL   rN   r   )numpyr   Zscipyr   Z	_stats_pyr   r   r    r   Z_axis_nan_policyr   Z
_hypotestsr
   Zscipy._lib._utilr   r   r   re   rw   ry   r   r   r   r   r   <module>   s    )
@?	