o
    ̋iN                     @   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<         )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 )N      ?r   )nplog_LOG_2PIpolyval_STIRLING_COEFFS)nrn r   X/home/air/goalskill_sales/back/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 rt||}||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 )
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].
    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      `   iZ   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rdd| |   }td| ||dS |dkrdt
j| | }td| ||dS || }| dkr|d	krt| |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rd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   r	   r   rf   r      r   r   g0q&?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rd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<*
%