U
    Kvf@                     @   sL   d Z ddl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 P tjddd d||  d | t|
t|
|    }tj||d}W 5 Q R X | | }|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ignorez#divide by zero encountered in log1p)message   g      ?)r   r   r   npsortshapeZexpand_dimsZmeanZstdr   r   Zcdfcallabler
   
ValueErrorZaranger	   slicetuplewarningscatch_warningsfilterwarningsloglog1psum)r   distr
   paramsr   yZnobsZxbarswziZsl1Zsl2Z	ad_valuesZa2r   r   =/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/stats/_adnorm.pyanderson_statistic   sD    





 .
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>~       znormal_ad.<locals>.<lambda>c                 S   s"   dt dd|   d| d    S )Nr   r'   r(   r)   r   r   expr6   r   r   r%   r8      s   c                 S   s"   dt dd|   d| d    S )Nr   r*   r+   r,   r   r:   r6   r   r   r%   r8      s   c                 S   s   t dd|   d| d   S )Nr-   r.   r/   r   r:   r6   r   r   r%   r8      r9   c                 S   s   t dd|   d| d   S )Nr1   r2   r3   r   r:   r6   r   r   r%   r8      r9   right)Zside   )
r&   r   r   sizer;   arrayZsearchsortedr4   r5   range)r   r   Zad2nr7   ZpvalZboundsZpval0Zpval1Zpval2Zpval3Zpval4Zpvalliidxr$   maskr   r   r%   	normal_adO   s4    
$$  rD   )r   Tr   r   )r   )__doc__r   Znumpyr   Zscipyr   Zstatsmodels.tools.validationr   r   r   r&   rD   r   r   r   r%   <module>   s   
?