
    bi9?                       d Z ddlmZ ddlZddlmZ ddlZddlm	Z
 ddlmZ ddlmZ ddlmZ 	 ddlZdd	lmZ dd
lmZ  edd      Z edd      Z edd      Z edd      Z edd      Z edd      Z edd      Z edd      ZdddddddZg dZ  eejB                        d/d       Z" eejB                        d0d       Z# eejB                        d0d       Z$d1dZ% eejB                        d2d       Z& eejB                        d3d        Z' eejB                        d0d!       Z( eejB                        d4d"       Z) eejB                        d0d#       Z* eejB                        d0d$       Z+ eejB                        d%        Z, eejB                        d5d&       Z- eej\                  e'      Z/ eej`                  e'      Z1d( Z2d) Z3d* Z4d+ Z5d6d,Z6d7d-Z7d7d.Z8y# e$ rZ ed      edZ[ww xY w)8zu
Statistical functions and tests, following scipy.stats.

Some differences

- We don't handle missing values at all

    )annotationsN)
namedtuple)delayed)wrap_elemwise)derived_fromz4`dask.array.stats` requires `scipy` to be installed.)special)distributionsF_onewayResult)	statisticpvalueKurtosistestResultNormaltestResultPower_divergenceResultSkewtestResultTtest_1sampResultTtest_indResultTtest_relResult   g      gUUUUUU?)pearsonzlog-likelihoodzfreeman-tukeyzmod-log-likelihoodneymanzcressie-read)	ttest_indttest_1samp	ttest_rel	chisquarepower_divergenceskewskewtestkurtosiskurtosistest
normaltestf_onewaymomentc                |   t        j                  | |d      }t        j                  ||d      }| j                  |   }|j                  |   }|rt        ||||      \  }}	nt	        ||||      \  }}	t        t        j                  | |      t        j                  ||      |	|      }
 t        t        d      |
 S )Nr   ddof   nout)	davarshape_equal_var_ttest_denom_unequal_var_ttest_denom_ttest_ind_from_statsmeanr   r   )abaxis	equal_varv1v2n1n2dfdenomress              K/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/dask/array/stats.pyr   r   Z   s    	4a	 B	4a	 B	
B	
B*2r2r:	E,RR<	E
4 0"''!T2BE2
NC+7?+S11    c                   |dk7  rt        d      | j                  |   }|dz
  }t        j                  | |      |z
  }t        j                  | |d      }t        j
                  |t        |      z        }t        j                  dd      5  t        j                  ||      }	d d d        t        |	      \  }	}
 t        t        d      |	|
      S # 1 sw Y   0xY w	N	propagate>`nan_policy` other than 'propagate' have not been implemented.r   r&   ignoredivideinvalidr(   r)   )NotImplementedErrorr-   r+   r1   r,   sqrtfloatnperrstaterE   _ttest_finishr   r   )r2   popmeanr4   
nan_policynr:   dvr;   tprobs              r=   r   r   k   s    [ !L
 	
 	
A	
QB
47"A
q$QAGGAaL!E	Hh	7  IIa B"GAt-7$1-a66   s   CCc                   |dk7  rt        d      | j                  |   }t        |dz
        }| |z
  j                  t        j
                        }t        j                  ||d      }t        j                  ||      }t        j                  |t        |      z        }	t	        j                  dd      5  t        j                  ||	      }
d d d        t        |
      \  }
} t        t        d      |
|      S # 1 sw Y   0xY wr@   )rG   r-   rI   astyperJ   float64r+   r,   r1   rH   rK   rE   rL   r   r   )r2   r3   r4   rN   rO   r:   rP   rQ   dmr;   rR   rS   s               r=   r   r   ~   s    [ !L
 	
 	
A	q1uB	
Qrzz"A
q$QA	D	BGGAaL!E	Hh	7 !IIb% !B"GAt+7?+At44	! !s   6C<<Dc                "    t        | |||d      S )a3  Calculate a one-way chi-square test.

    Please see the docstring for :py:func:`scipy.stats.chisquare` for
    complete information including notes, references, and examples.

    Some inconsistencies with the Dask version may exist.

    The chi-square test tests the null hypothesis that the categorical
    data has the given frequencies.

    Parameters
    ----------
    f_obs : array_like
        Observed frequencies in each category.
    f_exp : array_like, optional
        Expected frequencies in each category.  By default the categories are
        assumed to be equally likely.
    ddof : int, optional
        "Delta degrees of freedom": adjustment to the degrees of freedom
        for the p-value.  The p-value is computed using a chi-squared
        distribution with ``k - 1 - ddof`` degrees of freedom, where `k`
        is the number of observed frequencies.  The default value of `ddof`
        is 0.
    axis : int or None, optional
        The axis of the broadcast result of `f_obs` and `f_exp` along which to
        apply the test.  If axis is None, all values in `f_obs` are treated
        as a single data set.  Default is 0.

    Returns
    -------
    res: Delayed Power_divergenceResult
        An object containing attributes:

        chisq : float or ndarray
            The chi-squared test statistic.  The value is a float if `axis` is
            None or `f_obs` and `f_exp` are 1-D.
        pvalue : float or ndarray
            The p-value of the test.  The value is a float if `ddof` and the
            return value `chisq` are scalars.

    r   )f_expr'   r4   lambda_)r   )f_obsrY   r'   r4   s       r=   r   r      s    T ETiXXr>   c                v   t        |t              rL|t        vr:t        t	        t        j                                     dd }t        d|d|       t        |   }n|d}|n| j                  |d      }|dk(  r| |z
  dz  |z  }nL|dk(  rd	t        | | |z        z  }n4|dk(  rd	t        ||| z        z  }n| | |z  |z  dz
  z  }|d
|z  |dz   z  z  }|j                  |      }t        ||      } t        t        j                  j                        ||dz
  |z
        }	 t        t        d      ||	      S )Nr   r   zinvalid string for lambda_: z. Valid strings are T)r4   keepdimsr(   r          @      ?r4   r)   )
isinstancestr_power_div_lambda_namesreprlistkeys
ValueErrorr1   _xlogysum_countr   r	   chi2sfr   )
r[   rY   r'   r4   rZ   namestermsstatnum_obsps
             r=   r   r      si   '3115::<=>qDE.wk :%%*G-  *'2	

t
4
 !|1$u,	AfUEEM22	BfUEEM22 %%-G3a78w'A+..99$9DU&G&""%%&tWq[4-?@A27)24;;r>   c                   |dk7  rt        d      | j                  |   }t        | d|      }t        | d|      }|dk(  }t        j                  | ||dz  z  d      }|st        d      |j
                  dk(  r|j                         S |S )	NrA   rB   r(      r   g      ?g        bias=False is not implemented.)rG   r-   r$   r+   wherendimmin)	r2   r4   biasrN   rO   m2m3zerovalss	            r=   r   r      s    [ !L
 	
 	
A	1d	B	1d	B7D88TE2C<-D !"BCCyyA~xxzKr>   c           	     l   |dk7  rt        d      t        | |      }t        | j                  |         }|dk  rt	        dt        |      z        |t        j                  |dz   |dz   z  d|dz
  z  z        z  }d	|dz  d
|z  z   dz
  z  |dz   z  |dz   z  |dz
  |dz   z  |dz   z  |dz   z  z  }dt        j                  d|dz
  z        z   }dt        j                  dt        j                  |      z        z  }t        j                  d|dz
  z        }	t        j                  |dk(  d|      }|t        j                  ||	z  t        j                  ||	z  dz  dz         z         z  }
 t        t        d      |
dt        j                  j                  t        j                   |
            z        S )NrA   rB      zFskewtest is not valid with less than 8 samples; %i samples were given.r   rs         @r(         @   F   r^         	   r   r_   r   r)   )rG   r   rI   r-   rg   intmathrH   logrJ   ru   r   r   r	   normrl   abs)r2   r4   rN   b2rO   ybeta2W2deltaalphaZs              r=   r   r     s   [ !L
 	
 
aBaggdmA1u V$
 	
 	TYYQ1q5)cQUm<==Aa4"q&=2	q5	 q5	 GA!a%(AE2		4 
 
diiUQY(	(B		#,--EIIcR!Vn%E
aAAq5y277AI!+;a+?#@@AAA*7>*1a-2D2D2G2Gq	2R.RSSr>   c                   |dk7  rt        d      | j                  |   }t        | d|      }t        | d|      }|dk(  }t        j                  d      }		 t        j                  |d||dz  z        }
t        j                  di |	 |st        d	      |r|
d
z
  S |
j                  dk(  r|
j                         S |
S # t        j                  di |	 w xY w)NrA   rB   r(      r   rC   )allr^   rt   rs    )	rG   r-   r$   rJ   seterrr+   ru   rv   rw   )r2   r4   fisherrx   rN   rO   ry   m4r{   olderrr|   s              r=   r    r    '  s    [ !L
 	
 	
A	1d	B	1d	B7DYY8$Fxxab#g.
		F!"BCCax99>88: 			Fs   B= =Cc           	     6   |dk7  rt        d      t        | j                  |         }t        | |d      }d|dz
  z  |dz   z  }d|z  |dz
  z  |d	z
  z  |dz   |d
z   z  |d	z   z  |dz   z  z  }||z
  t	        j
                  |      z  }d||z  d|z  z
  dz   z  |dz   |dz   z  z  t	        j
                  d|d	z   z  |dz   z  ||dz
  z  |d	z
  z  z        z  }dd|z  d|z  t	        j
                  dd|dz  z  z         z   z  z   }	ddd|	z  z  z
  }
d|t	        j
                  d|	dz
  z        z  z   }t	        j                  |dk  d|      }t	        j                  |dk  |
t	        j                  dd|	z  z
  |z  d            }|
|z
  t	        j
                  dd|	z  z        z  }t	        j                  |dk(  d|      }|j                  dk(  r|d   } t        t        d      |dt        j                  j                  t	        j                  |            z        S )NrA   rB   F)r   r   r   g      8@r(   rs         ?r   r   r   r   g       @r^   g      @g      "@r   c   gUUUUUU?r   r)   )rG   rI   r-   r    rJ   rH   ru   powerrv   r   r   r	   r   rl   r   )r2   r4   rN   rO   r   Evarb2x	sqrtbeta1Aterm1r;   term2r   s                 r=   r!   r!   E  sL   [ !L
 	
 	aggdmA	!T%	(Bq1uQAqAEa!e$Q1s7(;q1u(EQ(OP 
 
a2775>!A 	q51q5=1	Ea!e	 ''3!a%=AE*qAE{a!e/DE
F	G  	cIoy2771siQRl?S;S3T!TUUAS1WEBGGASM***EHHUQYE*EHHUQYrxxS1W0Ew'OPE	"''!sQw-00A
"a#Avv{bE /7%A.q!m6H6H6K6KBFFSTI6V2VWWr>   c                    |dk7  rt        d      t        | |      \  }}t        | |      \  }}||z  ||z  z   } t        t        d      | t        t
        j                  j                        |d            S )NrA   rB   r(   r)   )rG   r   r!   r   r   r	   rk   rl   )r2   r4   rN   s_kk2s          r=   r"   r"   j  s    [ !L
 	
 AtDAq4 DAq	
QQB,7#!,R1O9K9K9N9N1OPRTU1VWWr>   c                    t        |       }t        j                  |       }t        |      }|j                         }||z  }t	        |      t        |      t        |      z  z
  }d}| D ](  }|t        ||z
        t        t        |            z  z  }* |t        |      t        |      z  z  }||z
  }|dz
  }	||z
  }
|t        |	      z  }|t        |
      z  }||z  }t        |	|
|      } t        t        d      ||      S )Nr   r   r(   r)   )
lenr+   concatenater1   _sum_of_squares_square_of_sumsrI   _fdtrcr   r
   )args
num_groupsalldatabignoffsetsstotssbnr2   sswndfbndfwnmsbmswfrS   s                  r=   r#   r#   w  s    TJnnT"Gw<D \\^FvGG$(@5;(NOED <F
+eCFm;;<
 	OG$uT{22D4<D>D*D
t
C
t
Cc	A$a D*7>*1d33r>   c                R    |dk7  rt        d      t        j                  | ||      S )NrA   rB   r`   )rG   r+   r$   )r2   r$   r4   rN   s       r=   r$   r$     s/    [ !L
 	
 99QT**r>   )sourcec                    ||z   dz
  }|dz
  | z  |dz
  |z  z   |z  }t        j                  |d|z  d|z  z   z        }||fS )Nr^   r   r   )r+   rH   )r6   r8   r7   r9   r:   svarr;   s          r=   r.   r.     sX    	b3B!VrMR!VrM)R/DGGDC"HsRx/01Eu9r>   c                :   | |z  }||z  }t        j                  dd      5  ||z   dz  |dz  |dz
  z  |dz  |dz
  z  z   z  }d d d        t        j                  t        j                        d|      }t        j
                  ||z         }||fS # 1 sw Y   OxY w)NrC   rD   r(   r   )rJ   rK   r+   ru   isnanrH   )r6   r8   r7   r9   vn1vn2r:   r;   s           r=   r/   r/     s    
r'C
r'C	Hh	7 HCiAa26!2S!VrAv5F!FGH
 
"((2,2	&BGGC#IEu9H Hs   !BBc                    | |z
  }t        j                  dd      5  t        j                  ||      }d d d        t	        |      \  }}||fS # 1 sw Y   xY w)NrC   rD   )rJ   rK   r+   rE   rL   )mean1mean2r;   r:   rP   rR   rS   s          r=   r0   r0     sX    A	Hh	7  IIa B"GAtt9	   s   AAc                     t        t        j                  j                        t	        j
                  |      |       dz  }|j                  dk(  r|d   }||fS )z+Common code between all 3 t-test functions.r(   r   r   )r   r	   rR   rl   r+   absoluterv   )r:   rR   rS   s      r=   rL   rL     sP    
 	$""#BKKNB7!; 	 	vv{bEd7Nr>   c                <    || j                   S | j                  |   S N)sizer-   )r   r4   s     r=   rj   rj     s    |vvwwt}r>   c                4    t        j                  | | z  |      S )a  
    Squares each element of the input array, and returns the sum(s) of that.
    Parameters
    ----------
    a : array_like
        Input array.
    axis : int or None, optional
        Axis along which to calculate. Default is 0. If None, compute over
        the whole array `a`.
    Returns
    -------
    sum_of_squares : ndarray
        The sum along the given axis for (a**2).
    See also
    --------
    _square_of_sums : The square(s) of the sum(s) (the opposite of
    `_sum_of_squares`).
    r+   ri   )r2   r4   s     r=   r   r     s    & 66!a%r>   c                8    t        j                  | |      }||z  S )a  
    Sums elements of the input array, and returns the square(s) of that sum.
    Parameters
    ----------
    a : array_like
        Input array.
    axis : int or None, optional
        Axis along which to calculate. Default is 0. If None, compute over
        the whole array `a`.
    Returns
    -------
    square_of_sums : float or ndarray
        The square of the sum over `axis`.
    See also
    --------
    _sum_of_squares : The sum of squares (the opposite of `square_of_sums`).
    r   )r2   r4   r   s      r=   r   r     s    $ 	q$Aq5Lr>   )r   T)r   rA   )Nr   r   )Nr   r   N)r   TrA   )r   TTrA   )r   r   rA   r   )r   )9__doc__
__future__r   r   collectionsr   numpyrJ   
dask.arrayarrayr+   daskr   dask.array.ufuncr   
dask.utilsr   scipy.statsscipyImportErrorer   r	   r
   r   r   r   r   r   r   r   rc   __all__statsr   r   r   r   r   r   r   r    r!   r"   r#   r$   xlogyrh   fdtrcr   r.   r/   r0   rL   rj   r   r   r   r>   r=   <module>r      s   #,  "    * #U  % ,.EF 46MN 02IJ #$<>UV ,.EF24KL .0GH.0GH  ( ekk2 2  ekk7 7$ ekk5 5**YZ ekk)< )<X ekk 2 ekkT T< ekk : ekk!X !XH ekk	X 	X ekk4 4D ekk+ + 
w}}W	5	w}}W	5

,O  U
L
MSTTUs   G, ,G?1	G::G?