o
    Nrf8?                  
   @  sP  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 zddlZW n eyE Z zededZ[ww 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j!dDddZ"eej!dEdd Z#eej!dEd!d"Z$dFd#d$Z%eej!dGd%d&Z&eej!dHd'd(Z'eej!dEd)d*Z(eej!dId+d,Z)eej!dEd-d.Z*eej!dEd/d0Z+eej!d1d2 Z,eej!dJd3d4Z-eej.ed5Z/eej0ed5Z1d6d7 Z2d8d9 Z3d:d; Z4d<d= Z5dKd>d?Z6dLd@dAZ7dLdBdCZ8dS )Mzu
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)Z	statisticZpvalueKurtosistestResultNormaltestResultPower_divergenceResultSkewtestResultTtest_1sampResultTtest_indResultTtest_relResult   g      gUUUUUU?)pearsonzlog-likelihoodzfreeman-tukeyzmod-log-likelihoodZneymanzcressie-read)	ttest_indttest_1samp	ttest_rel	chisquarepower_divergenceskewskewtestkurtosiskurtosistest
normaltestf_onewaymomentTc                 C  s   t j| |dd}t j||dd}| j| }|j| }|r&t||||\}}	n	t||||\}}	tt | |t |||	|}
ttdd|
 S )Nr   ddof   Znout)	davarshape_equal_var_ttest_denom_unequal_var_ttest_denom_ttest_ind_from_statsmeanr   r   )abaxisZ	equal_varv1v2n1n2dfdenomres r6   Y/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/dask/array/stats.pyr   Y   s   

r   	propagatec                 C  s   |dkrt d| j| }|d }t| || }tj| |dd}t|t| }tjddd t	||}	W d    n1 sBw   Y  t
||	\}	}
ttdd|	|
S 	Nr8   >`nan_policy` other than 'propagate' have not been implemented.r   r!   ignoredivideinvalidr#   r$   )NotImplementedErrorr'   r%   r+   r&   sqrtfloatnperrstater=   _ttest_finishr   r   )r,   Zpopmeanr.   
nan_policynr3   dvr4   tprobr6   r6   r7   r   j   s   
r   c                 C  s   |dkrt d| j| }t|d }| | tj}tj||dd}t||}t	|t| }	tj
ddd t||	}
W d    n1 sJw   Y  t||
\}
}ttdd|
|S r9   )r?   r'   rA   ZastyperB   Zfloat64r%   r&   r+   r@   rC   r=   rD   r   r   )r,   r-   r.   rE   rF   r3   rG   rH   dmr4   rI   rJ   r6   r6   r7   r   }   s   
r   c                 C  s   t | |||d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"   r.   lambda_)r   )f_obsrL   r"   r.   r6   r6   r7   r      s   *r   c           
      C  s*  t |tr$|tvrttt dd }td|d| t| }n|d u r*d}|d ur/n| j|dd}|dkrC| | d | }n0|dkrQd	t| | |  }n"|dkr_d	t|||   }n| | | | d  }|d
| |d   }|j	|d}t
||d}ttjj||d | }	ttdd||	S )Nr   r   zinvalid string for lambda_: z. Valid strings are T)r.   Zkeepdimsr#   r          @      ?r.   r$   )
isinstancestr_power_div_lambda_namesreprlistkeys
ValueErrorr+   _xlogysum_countr   r   chi2sfr   )
rN   rL   r"   r.   rM   namesZtermsstatZnum_obspr6   r6   r7   r      s4   

r   c           	      C  st   |dkrt d| j| }t| d|}t| d|}|dk}t| ||d  d}|s/t d|jdkr8| S |S )	Nr8   r:   r#      r   g      ?g        bias=False is not implemented.)r?   r'   r    r%   wherendimmin)	r,   r.   biasrE   rF   m2Zm3zerovalsr6   r6   r7   r      s   

r   c                 C  sL  |dkrt dt| |}t| j| }|dk r tdt| |t|d |d  d|d    }d	|d d
|  d  |d  |d  |d |d  |d  |d   }dtd|d   }dtdt|  }td|d  }	t	
|dkd|}|t	||	 t	||	 d d   }
ttdd|
dtjt	|
 S )Nr8   r:      zFskewtest is not valid with less than 8 samples; %i samples were given.r   ra         @r#         @   F   rO         	   r   rP   r   r$   )r?   r   rA   r'   rX   intmathr@   logrB   rc   r   r   r   normr]   abs)r,   r.   rE   b2rF   yZbeta2ZW2deltaalphaZr6   r6   r7   r     s:   
&($r   c              
   C  s   |dkrt d| j| }t| d|}t| d|}|dk}tjdd}	zt|d||d  }
W tjdi |	 n	tjdi |	 w |sHt d	|rN|
d
 S |
jdkrW|
 S |
S )Nr8   r:   r#      r   r;   )allrO   rb   ra   r6   )	r?   r'   r    rB   Zseterrr%   rc   rd   re   )r,   r.   fisherrf   rE   rF   rg   Zm4rh   Zolderrri   r6   r6   r7   r   &  s&   
$
r   c              	   C  s  |dkrt dt| j| }t| |dd}d|d  |d  }d| |d  |d	  |d |d
  |d	  |d   }|| t| }d|| d|  d  |d |d   td|d	  |d  ||d  |d	    }dd| d| tdd|d      }	ddd|	   }
d|td|	d    }t|dk d|}t|dk |
tdd|	  | d}|
| tdd|	   }t|dkd|}|jdkr|d }t	t
dd|dtjt| S )Nr8   r:   F)r~   rl   r   g      8@r#   ra         ?ro   rk   rp   rq   g       @rO   g      @g      "@r   c   gUUUUUU?r6   r$   )r?   rA   r'   r   rB   r@   rc   powerrd   r   r
   r   ru   r]   rv   )r,   r.   rE   rF   rw   EZvarb2xZ	sqrtbeta1AZterm1r4   Zterm2r{   r6   r6   r7   r   D  s8   6,*&
$r   c                 C  s\   |dkrt dt| |\}}t| |\}}|| ||  }ttdd|ttjj|dS )Nr8   r:   r#   r$   )r?   r   r   r   r   r   r\   r]   )r,   r.   rE   s_kZk2r6   r6   r7   r   i  s    r   c                  G  s   t | }t| }t |}| }||8 }t|t|t|  }d}| D ]}|t|| tt | 7 }q%|t|t| 8 }|| }|d }	|| }
|t|	 }|t|
 }|| }t|	|
|}tt	dd||S )Nr   r   r#   r$   )
lenr%   Zconcatenater+   _sum_of_squares_square_of_sumsrA   _fdtrcr   r	   )argsZ
num_groupsZalldataZbignoffsetZsstotZssbnr,   ZsswnZdfbnZdfwnZmsbZmswfrJ   r6   r6   r7   r   v  s$   
r   c                 C  s    |dkrt dtj| ||dS )Nr8   r:   rQ   )r?   r%   r    )r,   r    r.   rE   r6   r6   r7   r      s
   r    )sourcec                 C  sJ   || d }|d |  |d |  | }t |d| d|   }||fS )NrO   r   r   )r%   r@   )r/   r1   r0   r2   r3   Zsvarr4   r6   r6   r7   r(     s   r(   c                 C  s   | | }|| }t jddd || d |d |d  |d |d    }W d    n1 s0w   Y  tt|d|}t|| }||fS )Nr;   r<   r#   r   )rB   rC   r%   rc   isnanr@   )r/   r1   r0   r2   Zvn1Zvn2r3   r4   r6   r6   r7   r)     s   .r)   c                 C  sX   | | }t jddd t||}W d    n1 sw   Y  t||\}}||fS )Nr;   r<   )rB   rC   r%   r=   rD   )Zmean1Zmean2r4   r3   rG   rI   rJ   r6   r6   r7   r*     s   r*   c                 C  s6   t tjjt|| d }|jdkr|d }||fS )z+Common code between all 3 t-test functions.r#   r   r6   )r   r   rI   r]   r%   absoluterd   )r3   rI   rJ   r6   r6   r7   rD     s
   
rD   c                 C  s   |d u r| j S | j| S N)sizer'   )r   r.   r6   r6   r7   r[     s   
r[   c                 C  s   t | |  |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%   rZ   )r,   r.   r6   r6   r7   r     s   r   c                 C  s   t | |}|| 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   )r,   r.   r   r6   r6   r7   r     s   r   )r   T)r   r8   )Nr   r   )Nr   r   N)r   Tr8   )r   TTr8   )r   r   r8   r   )r   )9__doc__
__future__r   rs   collectionsr   numpyrB   Z
dask.arrayarrayr%   Zdaskr   Zdask.array.ufuncr   Z
dask.utilsr   Zscipy.statsZscipyImportErrorer   r   r	   r
   r   r   r   r   r   r   rT   __all__statsr   r   r   r   r   r   r   r   r   r   r   r    ZxlogyrY   Zfdtrcr   r(   r)   r*   rD   r[   r   r   r6   r6   r6   r7   <module>   s    








	
-,$
"	

