o
    ±Th«N  ã                   @   s&  d dl Zd dlZd dlm  mZ d dlm	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g d
¢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 d&dd„Z!dd„ Z"dd„ Z#d&d d!„Z$d"d#„ Z%d&d$d%„Z&dS )'é    N)Ú_derivativeé€   é   é   i<ýÿÿé   é   é   )g˜SË†Bž¿g¤A¤Az?g}<™Ù°j_¿g#ÿ+•K?g88C¿g  J?glÁlÁf¿gUUUUUUµ?c                 C   s6   d|  }t  | ¡d |  td  |t  t||  ¡  S )Nç      ð?r   )ÚnpÚlogÚ_LOG_2PIÚpolyvalÚ_STIRLING_COEFFS)ÚnÚrn© r   úT/home/air/segue/gpt/backup/venv/lib/python3.10/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.ç        r	   )r
   Úclip)Úpr   r   r   Ú
_clip_probg   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   )ÚcdfprobÚsfprobÚcdfr   r   r   r   Ú_select_and_clip_probl   s   r   c                 C   s^  |dkr
t dd|ƒS | | }|dkrt dd|ƒS tt |¡ƒ}|| }d| d }t ||g¡}t d|d ¡}d||  }	t |¡}
d}|D ]}||
|d < || }|	|d   |9  < qGtd| d dƒ| d||   }d| | |	d< td|ƒD ]}|
d|| d … ||d d…|f< q||	|dd…df< tj	|	dd	|ddd…f< t 
t |¡d ¡}| }d}d}|dkrñ|d rÈt ||¡}||7 }t ||¡}|d9 }t ||d |d f ¡tkré|t }|t7 }|d }|dksº||d |d f }td| d ƒD ]}|| |  }t |¡tk r|t9 }|t8 }q|dkr't ||¡}t |d| |ƒS )
z§Computes the Kolmogorov CDF:  Pr(D_n <= d) using the MTW approach to
    the Durbin matrix algorithm.

    Durbin (1968); Marsaglia, Tsang, Wang (2003). [1], [3].
    r	   r   ç      à?r   r   r   éÿÿÿÿN)Úaxis)r   Úintr
   ÚceilÚzerosÚarangeÚemptyÚmaxÚrangeÚflipÚeyeÚshapeÚmatmulÚabsÚ_EP128Ú_E128Ú_EM128Úldexp)r   Údr   ÚndÚkÚhÚmÚHÚintmÚvÚwÚfacÚjÚttÚiÚHpwrÚnnÚexpntÚHexpntr   r   r   r   Ú_kolmogn_DMTWr   s`   
"&ö€
rA   c           	      C   sÖ   | dkr| | d || d }}nLt | d dƒ\}}|dkrN||d kr8|| | d || | d }}n'|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)	r<   r   ÚllÚceilfÚroundfÚj1Új2Úip1div2Úip1mod2r   r   r   Ú_pomeranz_compute_j1j2½   s   $,"rK   c                  C   sê  | | }t t |¡ƒ}d||  }t|d| ƒ}|dkrdnd}|dkr&dnd}d|d  }	t |	¡}
t |	¡}t |	¡}d|
d< d|d< d|d< d}||  d| |  dd|  |  }}}td|	ƒD ]&}|
|d  | | |
|< ||d  | | ||< ||d  | | ||< qdt |	g¡}t |	g¡}d|d< d\}}td| |||ƒ\}}tdd|  d ƒD ]ˆ}|}||}}||}}| d¡ t|| |||ƒ\}}|dksÛ|d|  d krÞ|
}n|d rä|n|}|| d }|dkr:t 	||| || | … |d|… ¡}|| }|| d }|||| … |d|…< dt 
|¡  k r*tk r4n n|t9 }|t8 }|| | }q²|| |  }td| d ƒD ]}t |¡tkrZ|t9 }|t7 }||9 }qH|dkrkt ||¡}t|d| |ƒ}|S )	z[Computes Pr(D_n <= d) using the Pomeranz recursion algorithm.

    Pomeranz (1974) [2]
    r	   r   r   r   r   )r   r   r   N)r    r
   ÚfloorrC   r$   r&   r"   rK   ÚfillÚconvolver%   r.   r,   r-   r+   r/   r   ) r   Úxr   ÚtrD   ÚfÚgrE   rF   ÚnpwrsÚgpowerÚ	twogpowerÚonem2gpowerr?   Úg_over_nÚtwo_g_over_nÚone_minus_two_g_over_nr4   ÚV0ÚV1ÚV0sÚV1srG   rH   r<   Úk1ÚpwrsÚln2ÚconvÚ
conv_startÚconv_lenÚansr   r   r   Ú_kolmogn_PomeranzÏ   sl   


(



("€
re   c           %   	   C   s.  |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 rAt 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 ¡ƒ}t|ddƒD ]G}d| d
 }|d |d |d }}}t |	d| ¡}t d|
||  |||  ||  |||  ||  ||  g¡}||9 }||7 }q®||	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]
    r   r	   ©r   r   r   r   r   é   é   r   é   é   é@   iÄÿÿÿéÔ   é‡   é`   iâÿÿÿéZ   r   r   éH   é   iP  é
   iÜÿÿÿéØ   ç       @)r   r
   ÚsqrtÚ_PI_SQUAREDÚ_MIN_LOGÚexpÚ_PI_FOURÚ_PI_SIXr"   r    r!   Úpir&   ÚpowerÚarrayÚ_SQRT2PIr#   Ú_SQRT3ÚsumÚlen)%r   rO   r   ÚzÚzsquaredÚzthreeÚzfourÚzsixÚqlogÚqÚk1aÚk1bÚk2aÚk2bÚk2cÚk3dÚk3cÚk3bÚk3aÚK0to3Úmaxkr2   r4   ÚmsquaredÚmfourÚmsixÚqpowerÚcoeffsÚksÚksquaredÚsqrt3zÚkspiÚqpwersÚk2extraÚk3extraÚpowers_of_nÚKsumr   r   r   Ú_kolmogn_PelzGood#  sl   
$


ý
*
r¢   c                 C   sP  t  | ¡r| S t| ƒ| ks| dkrt jS |dkrtdd|dS |dkr*tdd|dS | | }|dkrr|dkr=tdd|dS | dkrWt  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rŸdt
j | |¡ }td| ||dS || }| dkrá|d	kr»t| |d
d}t|d| |dS |dkrÏt| |d
d}t|d| |dS dt
j | |¡ }td| ||dS |sú|dkréd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 )z Computes 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   r	   r   rf   r   éŒ   r   r   gã¤0ïq&è?Tr   g      w@gš™™™™™@g      2@i † g      ø?gffffffö?)r
   Úisnanr    Únanr   Úprodr#   rx   r   r   ÚscipyÚspecialÚsmirnovrA   re   r   r¢   )r   rO   r   rP   ÚprobÚ	nxsquaredr   r   r   r   Ú_kolmognv  sX   
,$
r¬   c                    sD  t  ˆ ¡rˆ S tˆ ƒˆ ksˆ dkrt jS |dks|dkrdS ˆ | }|dkr`|dkr,dS ˆ dkrDt  t  dˆ ¡dˆ   d| d  ¡}nt  tˆ ƒˆ d t  d| d ¡  ¡}|d ˆ d  S |ˆ d krrdd| ˆ d   ˆ  S |dkr€dt	j
j |ˆ ¡ S |d }t||dˆ   ƒ}t|d| ƒ}‡ fd	d
„}t|||ddS )zvComputes the PDF for the two-sided Kolmogorov-Smirnov statistic.

    x must be of type float, n of type integer.
    r   r	   r   r   r£   r   r   g      ð@c                    s
   t ˆ | ƒS ©N)Úkolmogn)Ú_x©r   r   r   Ú_kkÖ  s   
z_kolmogn_p.<locals>._kkrh   )ÚdxÚorder)r
   r¤   r    r¥   r¦   r#   rx   r   r   r§   ÚstatsÚksoneÚpdfrC   r   )r   rO   rP   ÚprdÚdeltar±   r   r°   r   Ú
_kolmogn_p²  s.   
((r¹   c                    sþ   t  ˆ ¡rˆ S tˆ ƒˆ ksˆ dkrt jS ˆdkrdˆ  S |dkr"dS t  t  ˆ¡tj ˆ d ¡ ˆ  ¡}|dˆ  krB|dˆ   d S t  	t  |d ¡ˆ  ¡ }|ddˆ   krY|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   r	   r   r   rt   c                    s   t ˆ | ƒˆ S r­   )r¬   )rO   ©r   r   r   r   Ú_fó  s   z_kolmogni.<locals>._fg›+¡†›„=)Úxtol)r
   r¤   r    r¥   rx   r   r§   r¨   ÚloggammaÚexpm1ÚscuÚ	_kolmogciru   rC   ÚoptimizeÚbrentq)r   r   rˆ   r¸   rO   Úx1r»   r   rº   r   Ú	_kolmogniÜ  s$   
$rÄ   c           	      C   sŒ   t j| ||dgdgdt jt jt jgd}|D ](\}}}}t  |¡r&||d< qt|ƒ|kr3td|› ƒ‚tt|ƒ||d|d< q|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
    :math:`D_n &= sup_t |F_n(t) - F(t)|`, and
    :math:`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Úzerosize_ok)ÚflagsÚ	op_dtypes.ún is not integral: rf   r   )	r
   ÚnditerÚfloat64Úbool_r¤   r    Ú
ValueErrorr¬   Úoperands)	r   rO   r   ÚitÚ_nr¯   Ú_cdfr‚   Úresultr   r   r   r®   ù  s   ÿ

r®   c                 C   sn   t  | |dg¡}|D ]%\}}}t  |¡r||d< q
t|ƒ|kr&td|› ƒ‚tt|ƒ|ƒ|d< q
|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.rÈ   r   )r
   rÉ   r¤   r    rÌ   r¹   rÍ   )r   rO   rÎ   rÏ   r¯   r‚   rÑ   r   r   r   Úkolmognp  s   

rÒ   c                 C   s”   t  | ||dg¡}|D ]7\}}}}t  |¡r||d< qt|ƒ|kr(td|› ƒ‚|r0|d| fnd| |f\}}	tt|ƒ||	ƒ|d< q|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.rÈ   r   r   )r
   rÉ   r¤   r    rÌ   rÄ   rÍ   )r   rˆ   r   rÎ   rÏ   Ú_qrÐ   r‚   Ú_pcdfÚ_psfrÑ   r   r   r   Úkolmogni;  s   
 
rÖ   )T)'Únumpyr
   Úscipy.specialr§   Úscipy.special._ufuncsr¨   Ú_ufuncsr¿   Úscipy._lib._finite_differencesr   r-   r/   Ú
longdoubler,   r.   ru   r{   r~   r   r   rw   r   rv   ry   rz   r   r   r   r   rA   rK   re   r¢   r¬   r¹   rÄ   r®   rÒ   rÖ   r   r   r   r   Ú<module>   s8   C






K

T
S<*
%