U
    md                     @   sD   d Z ddlZddlmZ ddlmZmZmZ ddd	Z	dd
dZ
dS )zb
Created on Sun Sep 25 21:23:38 2011

Author: Josef Perktold and Scipy developers
License : BSD-3
    N)stats)
array_like	bool_likeint_likenormT c                 C   s|  t | ddd} t|d}t|d}tj| |d}|j| }|r|dkrttj| |d|}ttj| d|d	|}|| | }	t	j
|	}
qt|r|| }|j|f| }
qtd
n"t|r|j|f| }
ntdtd|d }dg| j }td||< t|}tdg| j }tddd||< t|}tjd||  d | t|
t|
|    |d}| | }|S )a  
    Calculate the Anderson-Darling a2 statistic.

    Parameters
    ----------
    x : array_like
        The data to test.
    dist : {'norm', callable}
        The assumed distribution under the null of test statistic.
    fit : bool
        If True, then the distribution parameters are estimated.
        Currently only for 1d data x, except in case dist='norm'.
    params : tuple
        The optional distribution parameters if fit is False.
    axis : int
        If dist is 'norm' or fit is False, then data can be an n-dimensional
        and axis specifies the axis of a variable.

    Returns
    -------
    {float, ndarray}
        The Anderson-Darling statistic.
    xN)ndimfitaxis)r   r      )Zddofr   z!dist must be 'norm' or a Callablez+if fit is false, then dist must be callable   g      ?)r   r   r   npsortshapeZexpand_dimsZmeanZstdr   r   Zcdfcallabler
   
ValueErrorZaranger	   slicetuplesumloglog1p)r   distr
   paramsr   yZnobsZxbarswziZsl1Zsl2Za2r   r   R/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/stats/_adnorm.pyanderson_statistic   s<    




0
r!   c                 C   s  t | dd|d}| j| }|dd|  d|d    }t|dkr|dkrx|d	k rxdtd
d|  d|d    }n|dk rdtdd|  d|d    }nT|dk rtdd|  d|d   }n,|dkrtdd|  d|d   }nd}ntdd	ddg}dd }dd }dd }	dd }
dd }|||	|
|g}tj||dd }tjt| }t	d!D ]"}||k}|| || ||< qj||fS )"a  
    Anderson-Darling test for normal distribution unknown mean and variance.

    Parameters
    ----------
    x : array_like
        The data array.
    axis : int
        The axis to perform the test along.

    Returns
    -------
    ad2 : float
        Anderson Darling test statistic.
    pval : float
        The pvalue for hypothesis that the data comes from a normal
        distribution with unknown mean and variance.

    See Also
    --------
    statsmodels.stats.diagnostic.anderson_statistic
        The Anderson-Darling a2 statistic.
    statsmodels.stats.diagnostic.kstest_fit
        Kolmogorov-Smirnov test with estimated parameters for Normal or
        Exponential distributions.
    r   T)r   r
   r   r   g      ?g      @r   g        g?Zd;*)\HY@(\k@g(\?A`Т SeE@MbM@g333333? Ac]?V-@Gz?   \m?t@Έ?c                 S   s   t jt |  S )N)r   nan	ones_likead2ar   r   r    <lambda>x       znormal_ad.<locals>.<lambda>c                 S   s"   dt dd|   d| d    S )Nr   r"   r#   r$   r   r   expr1   r   r   r    r3   y   s   c                 S   s"   dt dd|   d| d    S )Nr   r%   r&   r'   r   r5   r1   r   r   r    r3   {   s   c                 S   s   t dd|   d| d   S )Nr(   r)   r*   r   r5   r1   r   r   r    r3   }   r4   c                 S   s   t dd|   d| d   S )Nr,   r-   r.   r   r5   r1   r   r   r    r3   ~   r4   right)Zside   )
r!   r   r   sizer6   arrayZsearchsortedr/   r0   range)r   r   Zad2nr2   ZpvalZboundsZpval0Zpval1Zpval2Zpval3Zpval4Zpvalliidxr   maskr   r   r    	normal_adI   s4    
$$  r?   )r   Tr   r   )r   )__doc__numpyr   Zscipyr   Zstatsmodels.tools.validationr   r   r   r!   r?   r   r   r   r    <module>   s
   
;