o
    Eb                     @   s<   d dl Zdd Zdd Zdd Zdd	 Zd
d Zdd ZdS )    Nc                 C   s8   | d|d|f   | |d d|d df    S )z>Sum of upper-left and lower right blocks of contingency table.N   sumAij r	   @/usr/lib/python3/dist-packages/scipy/stats/_hypotests_pythran.py_Aij      8r   c                 C   s8   | |d dd|f   | d||d df    S )z>Sum of lower-left and upper-right blocks of contingency table.r   Nr   r   r	   r	   r
   _Dij   r   r   c                 C   J   | j \}}d}t|D ]}t|D ]}|| ||f t| || 7 }qq|S )z5Twice the number of concordant pairs, excluding ties.r   )shaperanger   r   mncountr   r   r	   r	   r
   _P      
r   c                 C   r   )z5Twice the number of discordant pairs, excluding ties.r   )r   r   r   r   r	   r	   r
   _Q   r   r   c              	   C   sZ   | j \}}d}t|D ]}t|D ]}|| ||f t| ||t| || d  7 }qq|S )z>A term that appears in the ASE of Kendall's tau and Somers' D.r      )r   r   r   r   r   r	   r	   r
   _a_ij_Aij_Dij2+   s   
.r   c                 C   s  | |k r	|| } }| | }|| }dt tt|| |d }}|| }t d| d |d }	tj}
tj|	|
d}d|||< td| d D ]x}||}}ttt|| | | d d}t ||}t tt|| | | |d }||kr dS |dkrdnd}t|| D ]}|| }||| |  | ||  ||  }|||< q|| }||krd||| || ||  < qH||| d  S )a  
    Count the proportion of paths that do not stay strictly inside two
    diagonal lines.

    Parameters
    ----------
    m : integer
        m > 0
    n : integer
        n > 0
    g : integer
        g is greatest common divisor of m and n
    h : integer
        0 <= h <= lcm(m,n)

    Returns
    -------
    p : float
        The proportion of paths that do not stay inside the two lines.

    The classical algorithm counts the integer lattice paths from (0, 0)
    to (m, n) which satisfy |x/m - y/n| < h / lcm(m, n).
    The paths make steps of size +1 in either positive x or positive y
    directions.
    We are, however, interested in 1 - proportion to computes p-values,
    so we change the recursion to compute 1 - p directly while staying
    within the "inside method" a described by Hodges.

    We generally follow Hodges' treatment of Drion/Gnedenko/Korolyuk.
    Hodges, J.L. Jr.,
    "The Significance Probability of the Smirnov Two-Sample Test,"
    Arkiv fiur Matematik, 3, No. 43 (1958), 469-86.

    For the recursion for 1-p see
    Viehmann, T.: "Numerically more stable computation of the p-values
    for the two-sample Kolmogorov-Smirnov test," arXiv: 2102.08037

    r   r   r   )dtypeg        g      ?)	minintnpZceilZfloat64Zonesr   maxZfloor)r   r   ghZmgZngZminjZmaxjZcurlenZlenAr   r   r   ZlastminjZlastlenvalZjjr   r	   r	   r
   !_compute_outer_prob_inside_method7   s6   (
"

$
$$
r"   )Znumpyr   r   r   r   r   r   r"   r	   r	   r	   r
   <module>   s    