B
    0d[N                 @   s.  d dl Zd dlZd dlm  mZ d dlZdZ	e
ede	Ze
ede	 Zedej Zedej ZdZedZejd Zejd Zejd Zd	d
ddddddgZdd Zdd Zd,ddZd-ddZdd Zd.ddZd/ddZd0d d!Z d"d# Z!d$d% Z"d1d&d'Z#d(d) Z$d2d*d+Z%dS )3    N         i<         gSˆBgAAz?g}<ٰj_g#+K?g88CgJ?gllfgUUUUUU?c             C   s6   d|  }t | d |  td  |t t||    S )Ng      ?r   )nplog_LOG_2PIZpolyval_STIRLING_COEFFS)nZrn r   F/var/www/html/venv/lib/python3.7/site-packages/scipy/stats/_ksstats.py_log_nfactorial_div_n_pow_n\   s    r   c             C   s   t | ddS )z%clips a probability to range 0<=p<=1.g        g      ?)r   Zclip)pr   r   r   
_clip_probf   s    r   Tc             C   s   t || |}t|S )z>Selects either the CDF or SF, and then clips to range 0<=p<=1.)r   wherer   )cdfprobZsfprobcdfr   r   r   r   _select_and_clip_probk   s    r   c             C   sp  |dkrt dd|S | | }|dkr0t dd|S tt|}|| }d| d }t||g}td|d }d||  }	t|}
d}x4|D ],}||
|d < || }|	|d   |9  < qW td| d d| d||   }d| | |	d< x6td|D ](}|
d|| d  ||d d|f< qW |	|dddf< tj	|	dd	|dddf< t
t|d }| }d}d}xz|dkr|d rt||}||7 }t||}|d9 }t||d |d f tkr|t }|t7 }|d }qvW ||d |d f }xDtd| d D ]2}|| |  }t|tk r|t9 }|t8 }qW |dkr`t||}t |d| |S )
zComputes the Kolmogorov CDF:  Pr(D_n <= d) using the MTW approach to
    the Durbin matrix algorithm.

    Durbin (1968); Marsaglia, Tsang, Wang (2003). [1], [3].
    g      ?g        g      ?r   r   r   N)Zaxis)r   intr   ceilzerosarangeemptymaxrangeflipeyeshapematmulabs_EP128_E128_EM128ldexp)r   dr   ndkhmHZintmvwZfacjttiZHpwrnnexpntZHexpntr   r   r   r   _kolmogn_DMTWq   s\    

"(
 
r4   c       	      C   s   | dkr&| | d || d  }}nt | d d\}}|dkr||d krp|| | d || | d  }}q|d | | d || d | d  }}n"|d | d || | d  }}t|d dt||fS )z0Compute the endpoints of the interval for row i.r   r   r   )divmodr   min)	r1   r   llceilfroundfj1j2Zip1div2Zip1mod2r   r   r   _pomeranz_compute_j1j2   s    $,"r<   c              C   s  | | }t t|}d||  }t|d| }|dkr<dnd}|dkrLdnd}d|d  }	t|	}
t|	}t|	}d|
d< d|d< d|d< d}||  d| |  dd|  |    }}}xZtd|	D ]L}|
|d  | | |
|< ||d  | | ||< ||d  | | ||< qW t|	g}t|	g}d|d< d\}}td| |||\}}x0tdd|  d D ]}|}|| }}|| }}|d t|| |||\}}|dks|d|  d kr|
}n|d r|n|}|| d }|dkrlt	||| || |  |d| }|| }|| d }||||  |d|< dt
|  k rdtk rxn n|t9 }|t8 }|| | }qlW || |  }x@td| d D ].}t|tkr|t9 }|t7 }||9 }qW |dkrt||}t|d| |}|S )	z[Computes Pr(D_n <= d) using the Pomeranz recursion algorithm.

    Pomeranz (1974) [2]
    g      ?r   r   g      ?r   )r   r   g        N)r   r   floorr6   r   r   r   r<   fillZconvolver   r%   r#   r$   r"   r&   r   ) r   xr   tr7   fgr8   r9   ZnpwrsZgpowerZ	twogpowerZonem2gpowerr3   Zg_over_nZtwo_g_over_nZone_minus_two_g_over_nr+   ZV0ZV1ZV0sZV1sr:   r;   r1   Zk1ZpwrsZln2convZ
conv_startZconv_lenZansr   r   r   _kolmogn_Pomeranz   sj    


(



("
rD   c       %   	   C   s4  |dkrt dd|dS |dkr,t dd|dS t| | }|d |d |d |d f\}}}}t d | }|tk rt dd|dS t|}	| }
td }d| d|  }d| d	|  t d }td
d|   d }td	d|   d }td| d|   d }td| d|   d }d| d|d   }td}t	t
d| tj }xt|ddD ]}d| d
 }|d |d |d   }}}t|	d| }td|
||  |||  ||  |||  ||  ||  g}||9 }||7 }q^W ||	9 }|t9 }|t|d| d|d  d|d  g }tt d | }	t|dd}|d }t| }tj| }|	| } t||  }!|!tt d|  9 }!|d  |!7  < t|| ||  | |  }"|"tt d|  9 }"|d  |"7  < t| d tt|d }#||# }|s(|d9 }|d  d
7  < t|}$|$S )aP  Computes the Pelz-Good approximation to Prob(Dn <= x) with 0<=x<=1.

    Start with Li-Chien, Korolyuk approximation:
        Prob(Dn <= x) ~ K0(z) + K1(z)/sqrt(n) + K2(z)/n + K3(z)/n**1.5
    where z = x*sqrt(n).
    Transform each K_(z) using Jacobi theta functions into a form suitable
    for small z.
    Pelz-Good (1976). [6]
    g        g      ?)r   r   r   r   r         r         @   i      `   iZ   r   r   H      iP  
   i   g       @)r   r   sqrt_PI_SQUARED_MIN_LOGexp_PI_FOUR_PI_SIXr   r   r   pir   powerarray_SQRT2PIr   _SQRT3sumlen)%r   r?   r   zZzsquaredZzthreeZzfourZzsixZqlogqZk1aZk1bZk2aZk2bZk2cZk3dZk3cZk3bZk3aZK0to3Zmaxkr)   r+   ZmsquaredZmfourZmsixZqpowerZcoeffsksZksquaredZsqrt3zZkspiZqpwersZk2extraZk3extraZpowers_of_nZKsumr   r   r   _kolmogn_PelzGood"  sj    
$


 *
rb   c             C   s`  t | r| S t| | ks"| dkr(t jS |dkr>tdd|dS |dkrTtdd|dS | | }|dkr|dkrztdd|dS | dkrt t d| d d|   d| d  }n$t t| | t 	d| d   }t|d| |dS || d krdd| |   }td| ||dS |dkrBdt
j| | }td| ||dS || }| dkr|d	kr~t| |d
d}t|d| |dS |dkrt| |d
d}t|d| |dS dt
j| | }td| ||dS |s|dkrdS |dkrdt
j| | }t|S |dkrd}n:| dkr@| |d  dkr@t| |d
d}nt| |d
d}t|d| |dS )zComputes the CDF(or SF) for the two-sided Kolmogorov-Smirnov statistic.

    x must be of type float, n of type integer.

    Simard & L'Ecuyer (2011) [7].
    r   g      ?g        )r   g      ?   r   r   g0q&?Tr   g      w@g@g      2@i g      ?gffffff?)r   isnanr   nanr   prodr   rU   r   r	   scipyspecialZsmirnovr4   rD   r   rb   )r   r?   r   r@   ZprobZ	nxsquaredr   r   r   r   _kolmognu  sX    
,$






ri   c                sJ  t  r S t  ks" dkr(t jS |dks8|dkr<dS  | }|dkr|dkrXdS  dkrt t d d   d| d  }n(t t  d t d| d   }|d  d  S | d krdd|  d     S |dkrdt	j
j|  S |d }t||d   }t|d| } fd	d
}t	jj|||ddS )zvComputes the PDF for the two-sided Kolmogorov-Smirnov statistic.

    x must be of type float, n of type integer.
    r   g      ?g      ?g        rc   r   r   g      @c                s
   t  | S )N)kolmogn)_x)r   r   r   _kk  s    z_kolmogn_p.<locals>._kkrF   )Zdxorder)r   rd   r   re   rf   r   rU   r   r	   rg   statsZksoneZpdfr6   miscZ
derivative)r   r?   r@   Zprddeltarl   r   )r   r   
_kolmogn_p  s.    
((
rq   c                s   t  r S t  ks" dkr(t jS dkr8d  S |dkrDdS t t tj d    }|d  kr|d   d S t 	t |d    }|dd   kr|S t
t   }t|dd   } fdd}tjj|d  |dd	S )
zeComputes the PPF/ISF of kolmogn.

    n of type integer, n>= 1
    p is the CDF, q the SF, p+q=1
    r   g      ?r   r   g       @c                s   t  |  S )N)ri   )r?   )r   r   r   r   <lambda>      z_kolmogni.<locals>.<lambda>g+=)Zxtol)r   rd   r   re   rU   r	   rg   rh   Zloggammaexpm1scuZ	_kolmogcirR   r6   optimizeZbrentq)r   r   r`   rp   r?   x1_fr   )r   r   r   	_kolmogni  s$    
$ry   c       	      C   s   t j| ||dgdt jt jt jgd}xX|D ]P\}}}}t |rJ||d< q*t||krdtd| tt|||d|d< q*W |jd }|S )a  Computes the CDF for the two-sided Kolmogorov-Smirnov distribution.

    The two-sided Kolmogorov-Smirnov distribution has as its CDF Pr(D_n <= x),
    for a sample of size n drawn from a distribution with CDF F(t), where
    D_n &= sup_t |F_n(t) - F(t)|, and
    F_n(t) is the Empirical Cumulative Distribution Function of the sample.

    Parameters
    ----------
    n : integer, array_like
        the number of samples
    x : float, array_like
        The K-S statistic, float between 0 and 1
    cdf : bool, optional
        whether to compute the CDF(default=true) or the SF.

    Returns
    -------
    cdf : ndarray
        CDF (or SF it cdf is False) at the specified locations.

    The return value has shape the result of numpy broadcasting n and x.
    N)Z	op_dtypes.zn is not integral: )r   r   )	r   nditerZfloat64Zbool_rd   r   
ValueErrorri   operands)	r   r?   r   it_nrk   _cdfr_   resultr   r   r   rj     s    

rj   c             C   sr   t | |dg}xR|D ]J\}}}t |r4||d< qt||krNtd| tt|||d< qW |jd }|S )a  Computes the PDF for the two-sided Kolmogorov-Smirnov distribution.

    Parameters
    ----------
    n : integer, array_like
        the number of samples
    x : float, array_like
        The K-S statistic, float between 0 and 1

    Returns
    -------
    pdf : ndarray
        The PDF at the specified locations

    The return value has shape the result of numpy broadcasting n and x.
    N.zn is not integral: r   )r   rz   rd   r   r{   rq   r|   )r   r?   r}   r~   rk   r_   r   r   r   r   kolmognp  s    

r   c             C   s   t | ||dg}xv|D ]n\}}}}t |r8||d< qt||krRtd| |rb|d| fn
d| |f\}}	tt|||	|d< qW |jd }
|
S )a  Computes the PPF(or ISF) for the two-sided Kolmogorov-Smirnov distribution.

    Parameters
    ----------
    n : integer, array_like
        the number of samples
    q : float, array_like
        Probabilities, float between 0 and 1
    cdf : bool, optional
        whether to compute the PPF(default=true) or the ISF.

    Returns
    -------
    ppf : ndarray
        PPF (or ISF if cdf is False) at the specified locations

    The return value has shape the result of numpy broadcasting n and x.
    N.zn is not integral: r   r   )r   rz   rd   r   r{   ry   r|   )r   r`   r   r}   r~   Z_qr   r_   Z_pcdfZ_psfr   r   r   r   kolmogni7  s    
 
r   )T)T)T)T)T)T)T)&numpyr   Zscipy.specialrg   Zscipy.special._ufuncsrh   Z_ufuncsru   Z
scipy.miscr$   r&   Z
longdoubler#   r%   rR   rX   r[   r	   r
   rT   r\   rS   rV   rW   r   r   r   r   r4   r<   rD   rb   ri   rq   ry   rj   r   r   r   r   r   r   <module>D   s<   






K
T
S
<*
%