o
    5Bi?                     @   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   ).0nir   r   U/var/www/html/Trade-python/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   r0   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   )r8   r
   r   r*   r5   r   r/   r   cdf*      
zWilcoxonDistribution.cdfc                    r9   )Nc                    s   d  | d | S r   )r*   r:   r/   r   r   r;   2   r<   z)WilcoxonDistribution.sf.<locals>.<lambda>r=   r   )r8   r
   r   r-   r5   r   r/   r   sf/   r@   zWilcoxonDistribution.sfN)__name__
__module____qualname__r   r#   r*   r,   r-   r0   r8   r?   rA   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.>   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approxz`method` must be one of z- or an instance of `stats.PermutationMethod`.rQ   rO   2   rP   z^Exact p-value calculation does not work if there are zeros. Switching to normal approximation.   )
stacklevel)rI   rH   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   
issubdtyper1   integerndim
ValueErrorr   moveaxis	AxisErrorr3   r   r4   floatingstrlower
isinstancer   PermutationMethodr   anywarningswarnsize)xyzero_method
correctionalternativemethodrF   messagedezero_methodscorrectionsalternativesmethodsoutput_zn_zero	has_zerosr   r   r   _wilcoxon_iv5   s|   


$,ru   rI   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   rI   	WRITEABLErG   rE   averageT)return_tiesrJ   rS   g      ?g      ?g       @rH         )flagsr   r   nanisnanr   r3   r   absra   sqrt)rl   rg   i_zerosi_nann_nancountrtr_plusr_minusr_zero_2r6   sers   tie_correctzr   r   r   _wilcoxon_statistic   s6   


r   c                 C   s"   |dkrdS |dkrdS t | S )NrM   r   rL   rG   )r   sign)r   ri   r   r   r   _correction_sign   s
   
r   TrN   rO   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pvaluerQ   g      ?rP   rL   rM   rS   r   c                    s   t |  d S )Nr   )r   )rl   rg   r   r   r;      s    z_wilcoxon_nd.<locals>.<lambda>permutation_typesamplesrG   )ri   rF   rN   r   )ru   rd   r   r   WilcoxonResult
zstatisticr   r   r   r   normr   r?   rA   r   minimumclippermutation_test_asdictr   r~   )re   rf   rg   rh   ri   rj   rF   temprl   rr   NaNresr   r   r   r   r   r   pdistr   r   r   r   _wilcoxon_nd   sN   

 
r   )rI   )NrI   TrN   rO   r   )rb   numpyr   scipyr   	_stats_pyr   r    r   _axis_nan_policyr   
_hypotestsr	   scipy._lib._utilr
   r   r   ru   r   r   r   r   r   r   r   <module>   s    )
Q5	