
    M/Ph%                     L    d Z ddlZddlmZmZmZ ddlmZ  G d d          Z	dS )aJ  
Created on Sat Oct 01 20:20:16 2011

Author: Josef Perktold
License: BSD-3

TODO:
check orientation, size and alpha should be increasing for interp1d,
but what is alpha? can be either sf or cdf probability
change it to use one consistent notation

check: instead of bound checking I could use the fill-value of the
interpolators
    N)interp1dinterp2dRbf)cache_readonlyc                   x    e Zd ZdZ	 	 ddZed             Zed             Zed             Zd Z	d Z
d	 Zd
 ZdS )	TableDista  
    Distribution, critical values and p-values from tables

    currently only 1 extra parameter, e.g. sample size

    Parameters
    ----------
    alpha : array_like, 1d
        probabiliy in the table, could be either sf (right tail) or cdf (left
        tail)
    size : array_like, 1d
        The sample sizes for the table
    crit_table : array_like, 2d
        The sample sizes in the table
        array with critical values for sample size in rows and probability in
        columns
    asymptotic : callable, optional
        Callable function with the form fn(nobs) that returns len(alpha)
        critical values where the critical value in position i corresponds to
        alpha[i]
    min_nobs : int, optional
        Minimum number of observations to use the asymptotic distribution. If
        not provided, uses max(size).
    max_nobs : int, optional
        Maximum number of observations to use the tabular distribution. If not
        provided, uses max(size)

    Notes
    -----
    size and alpha must be sorted and increasing.

    If both min_nobs and max_nobs are provided, then
    the critical values from the tabular distribution and the asymptotic
    distribution are linearly blended using the formula
    :math:`w cv_a + (1-w) cv_t` where the weight is
    :math:`w = (n - a_{min}) / (a_{max} - a_{min})`. This ensures the
    transition between the tabular and the asymptotic critical values is
    continuous. If these are not provided, then the asymptotic critical value
    is used for nobs > max(size).
    Nc                 D   t          j        |          | _        | j        j        dk    rt	          d          t          j        | j                  dk                                    rt	          d          t          j        |          | _        | j        j        dk    rt	          d          t          j        | j                  dk                                    rt	          d          | j        j        dk    r9t          j        |          dk                                    rt	          d          t          j        |          | _        | j        j	        | j        j	        d         | j        j	        d         fk    rt	          d          t          |          | _        t          j        t          j        | j        d                                                    | _        | j        dk    r| j        d d ddgf         | _        n| j        d d ddgf         | _        d | _        t#          |          x}| _        |	  || j        dz             }nI# t&          $ r<}	 t)          |	          d                    |	j        d         	                    d }	~	ww xY wt          |          t          |          k    rt	          d
          || _        ||n|| _        ||n|| _        | j        |k    rt	          d          | j        |k    rt	          d          d S )N   zalpha is not 1dr   zalpha is not sortedzsize is not 1dzsize is not sortedz1crit_table must have shape(len(size), len(alpha))zICalling asymptotic(self.size+1) failed. The error message was:

{err_msg})err_msgz,asymptotic does not return len(alpha) valueszmin_nobs > max(size)zmax_nobs > max(size))npasarrayalphandim
ValueErrordiffanysize
crit_tableshapelenn_alphasignmeansigncritcritv_bounds
asymptoticmaxmax_size	Exceptiontypeformatargsmin_nobsmax_nobs)
selfr   r   r   r   r#   r$   r   cvexcs
             [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/tabledist.py__init__zTableDist.__init__?   s   Z&&
:?a.///gdj!!Q&++-- 	42333Jt$$	9>Q-...gdi  A%**,, 	312229>Q!#((** 8 !6777*Z00? TY_Q%79I!9L$MMM 7 8 8 8 5zz ; ; @ @ B BCC=1 $Aq6	 :D $Aq6	 :D#&t99,4=!MZ 122 M M Md3ii !006sx{0K0KM M MM 2ww#e**$$  "* + + +(DO$,$4($,$4(=8##3444=8##3444 $#s   I 
J!7JJc                 H      fdt           j                  D             }|S )Nc                 X    g | ]&}t          j        j        d d |f                   'S N)r   r   r   ).0ir%   s     r(   
<listcomp>z#TableDist.polyn.<locals>.<listcomp>q   sD     / / / $)T_QQQT%:;; / / /    )ranger   )r%   polyns   ` r(   r2   zTableDist.polyno   s:    / / / /--/ / /r0   c                 F    t          | j        | j        | j                  }|S r,   )r   r   r   r   )r%   poly2ds     r(   r4   zTableDist.poly2du   s      $)TZAAr0   c                     t          j        | j                            t                    | j                  \  }}t          |                                |                                | j        j	                                        d          }|S )Nlinear)function)
r   meshgridr   astypefloatr   r   ravelr   T)r%   xsxapolyrbfs       r(   r?   zTableDist.polyrbf|   sl    TY--e44djAABbhhjj"((**do.?.E.E.G.G') ) )r0   c                 L   | j         k    r,| j        |                               }nzt          d          fd| j        D             }| j        k    rM| j        z
  | j        | j        z
  z  }t          d|          }|                               }||z  d|z
  |z  z   }|S )a  
        Rows of the table, linearly interpolated for given sample size

        Parameters
        ----------
        n : float
            sample size, second parameter of the table

        Returns
        -------
        critv : ndarray, 1d
            critical values (ppf) corresponding to a row of the table

        Notes
        -----
        This is used in two step interpolation, or if we want to know the
        critical values for all alphas for any sample size that we can obtain
        through interpolation
        Nz?n is above max(size) and no asymptotic distribtuion is providedc                 &    g | ]} |          S  rB   )r-   pns     r(   r/   z'TableDist._critvals.<locals>.<listcomp>   s!    ,,,A11Q44,,,r0   g      ?r
   )r   r   r   r2   r#   r$   min)r%   rD   r&   wa_cvs    `   r(   	_critvalszTableDist._critvals   s    ( t}*__Q''  "< = = = -,,,,,,B4=  &4=4=+HIQKKq))XQ",	r0   c                 V   |                      |          }| j        }| j        dk     r|ddd         |ddd         }}t          j        |          dk    rG||d         k     r|d         S ||d         k    r|d         S  t          ||          |          d         S ||d         k     }||d         k    }t          j        ||           }t          j        t          j        |j	                  z  }|d         ||<   |d         ||<    t          ||          ||                   ||<   |S )a  
        Find pvalues by interpolation, either cdf(x)

        Returns extreme probabilities, 0.001 and 0.2, for out of range

        Parameters
        ----------
        x : array_like
            observed value, assumed to follow the distribution in the table
        n : float
            sample size, second parameter of the table

        Returns
        -------
        prob : array_like
            This is the probability for each value of x, the p-value in
            underlying distribution is for a statistical test.
        r
   Nr   rB   )
rH   r   r   r   r   r   
logical_ornanonesr   )	r%   xrD   critvr   cond_low	cond_highcond_interiorprobss	            r(   probzTableDist.prob   s/   & q!!
=1 2;ddd5E 71::??58||QxU2YRy )8E5))!,,R00 E!HHU2YI]8Y???MFRWQW---E#AhE(O#BiE(O#98E5#9#9!M:J#K#KE- Lr0   c                    t          j        |          }| j        }|                     |          }||d         k    }||d         k     }t          j        ||          }|j        dk    r'|r t          ||          |          S t           j        S t           j        t          j        |j	                  z  } t          ||          ||                   ||<   |S )a  
        Returns interpolated quantiles, similar to ppf or isf

        use two sequential 1d interpolation, first by n then by prob

        Parameters
        ----------
        prob : array_like
            probabilities corresponding to the definition of table columns
        n : int or float
            sample size, second parameter of the table

        Returns
        -------
        ppf : array_like
            critical values with same shape as prob
        r   rJ   r
   )
r   r   r   rH   rK   r   r   rL   rM   r   )	r%   rT   rD   r   rO   	cond_ilow
cond_ihighrR   quantiles	            r(   critzTableDist.crit   s    $ z$
q!! E!H_	U2Y&
i<< 9>> -xu--d333v 6BGDJ///"8(5%"8"8m9L"M"Mr0   c                 ~   t          j        |          }| j        }||d         k    }||d         k     }t          j        ||          }|j        dk    r$|r|                     ||          S t           j        S t           j        t          j        |j                  z  }|                     |||                   ||<   |S )a,  
        Returns interpolated quantiles, similar to ppf or isf

        uses Rbf to interpolate critical values as function of `prob` and `n`

        Parameters
        ----------
        prob : array_like
            probabilities corresponding to the definition of table columns
        n : int or float
            sample size, second parameter of the table

        Returns
        -------
        ppf : array_like
            critical values with same shape as prob, returns nan for arguments
            that are outside of the table bounds
        r   rJ   r
   )	r   r   r   rK   r   r?   rL   rM   r   )r%   rT   rD   r   rV   rW   rR   rX   s           r(   crit3zTableDist.crit3   s    & z$
 E!H_	U2Y&
i<< 9>> ||At,,,v 6BGDJ///"&,,q$}2E"F"Fr0   )NNN)__name__
__module____qualname____doc__r)   r   r2   r4   r?   rH   rT   rY   r[   rB   r0   r(   r   r      s        ' 'R <@)-.5 .5 .5 .5`   ^
   ^   ^" " "H, , ,\% % %N& & & & &r0   r   )
r_   numpyr   scipy.interpolater   r   r   statsmodels.tools.decoratorsr   r   rB   r0   r(   <module>rc      s         5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7M M M M M M M M M Mr0   