B
    Z.                 @   sd  d Z ddlmZ ddlmZ ddlZddlmZm	Z	m
Z
 ddlmZ G dd deZed	kr`ed
dddddgddd Zedddddddddddddddd d!d"d#d$d%d&d'geZed(d)d*d+d,d-gd.d(d/d0d1d2gd3d4d5d6d7d8gd9d:d;d<d=d>gd?d@dAdBdCdDgdEdFd9dGdHdIgdJdKdLdMd<dNgdOdPdQdRdSdTgdUdVdWdXd4dYgdZd[d\d]d^d_gd`dadOdbdMdcgdddedfdgdhdigdjdkdldmdndogdpdqdedrdsdtgdudvdwd[d]dxgdydzddd{dKd|gd}d~djdddgddd}ddfdgdddd}dwdJgddddddgddddddgddddddgd"ddd#ddggdddddf d ZeeeeZeedd!dd ed eedd!ddd eedd!ddd eedd!ddd ed eedd!ddd eedd!ddd eedd!ddd ed eedd!ddd eedd!ddd eedd!ddd ed eedd!ddd eedd!ddd eedd!ddd eeeedd eeeD dejej  eeeedd eeeD deje  eeeedd eeeD deje  eedd eeeD dejd eddddf k    eedd eeeD dejd eddddf k   eedd eeeD dejd ejdd k   eedd eeeD dejd ejdd k    eedd edeeD dejd ejdd k   dS )aM  
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


    )print_function)rangeN)interp1dinterp2dRbf)cache_readonlyc               @   sd   e Zd ZdZdd Zedd Zedd Zedd	 Zd
d Z	dd Z
dd Zdd Zd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
        second paramater in the table
    crit_table : array_like, 2d
        array with critical values for sample size in rows and probability in
        columns

    Notes
    -----
    size and alpha should be increasing


    c             C   s   t || _t || _t || _t|| _t t | jd	 | _
| j
dkrl| jd d ddgf | _n| jd d ddgf | _d S )N   r   )npasarrayalphasize
crit_tablelenn_alphaZsignZdiffZmeansigncritZcritv_bounds)selfr   r   r    r   :lib/python3.7/site-packages/statsmodels/stats/tabledist.py__init__0   s    

zTableDist.__init__c                s    fddt  jD }|S )Nc                s&   g | ]}t  j jd d |f qS )N)r   r   r   ).0i)r   r   r   
<listcomp>>   s   z#TableDist.polyn.<locals>.<listcomp>)r   r   )r   polynr   )r   r   r   <   s    
zTableDist.polync             C   s   t | j| j| j}|S )N)r   r   r   r   )r   poly2dr   r   r   r   B   s    zTableDist.poly2dc             C   s>   t | jt| j\}}t| | | jj	 dd}|S )NZlinear)Zfunction)
r
   Zmeshgridr   Zastypefloatr   r   Zravelr   T)r   ZxsZxapolyrbfr   r   r   r   I   s     zTableDist.polyrbfc                s   t  fdd| jD 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

        c                s   g | ]}| qS r   r   )r   p)nr   r   r   c   s    z'TableDist._critvals.<locals>.<listcomp>)r
   arrayr   )r   r   r   )r   r   	_critvalsO   s    zTableDist._critvalsc       	      C   s   |  |}| j}| jdk r8|ddd |ddd  }}t|dkr||d k rZ|d S ||d krn|d S t|||d S ||d k }||d k}t|| }tjt|j	 }|d ||< |d ||< t|||| ||< |S dS )a  find pvalues by interpolation, eiter cdf(x) or sf(x)

        returns extrem 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 : arraylike
            This is the probability for each value of x, the p-value in
            underlying distribution is for a statistical test.

        r	   Nr   r   )
r!   r   r   r
   r   r   
logical_ornanonesshape)	r   xr   critvr   Zcond_lowZ	cond_highcond_interiorZprobsr   r   r   probe   s$    

zTableDist.probc             C   s   |  ||S )zreturns interpolated quantiles, similar to ppf or isf

        this can be either cdf or sf depending on the table, twosided?

        this doesn't work, no more knots warning

        )r   )r   r*   r   r   r   r   crit2   s    zTableDist.crit2c       	      C   s   t |}| j}| |}||d k}||d k }t ||}|jdkr`|rZt|||S t jS t jt |j	 }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   r"   r	   )
r
   r   r   r!   r#   r   r   r$   r%   r&   )	r   r*   r   r   r(   	cond_ilow
cond_ihighr)   quantiler   r   r   crit   s    


zTableDist.critc             C   s~   t |}| j}||d k}||d k }t ||}|jdkrT|rN| ||S t jS t jt |j }| ||| ||< |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   r"   r	   )	r
   r   r   r#   r   r   r$   r%   r&   )r   r*   r   r   r,   r-   r)   r.   r   r   r   crit3   s    

zTableDist.crit3N)__name__
__module____qualname____doc__r   r   r   r   r   r!   r*   r+   r/   r0   r   r   r   r   r      s   9'r   __main__g?g333333?g?g?g{Gz?gMbP?r"                  	   
                                       (   d   i  i  i/  iA  iZ  ix  i  i  i!  i?  iW  i  i  i  i  i)  iC  is  i     i  i  i0  i_  i        i	  i   iM  i        i  i=  im           i  i`              i#  iR              iE              i  i:           i1                 i(                 i              i           i           i
              i                       s   x               J   M   R   Y   h   z   %   '   )   -   4   =         #   *   g     @@gsh|??z
should be: zinterp2d badztwo 1dr   g(\?zshould be in:)g'1Z?gsh|??gRQ?gFx?c             C   s*   g | ]"}t | D ]}t|t| qqS r   )crit_lflfr*   r   )r   r   cr   r   r   r   5  s    r   c             C   s(   g | ] }t jD ]}t |t| qqS r   )r   r   r/   r   )r   r   r   r   r   r   r   7  s    c             C   s(   g | ] }t jD ]}t |t| qqS r   )r   r   r0   r   )r   r   r   r   r   r   r   9  s    c             C   s4   g | ],}t jd d d D ]}t |t| qqS )Nr"   g?)r   r   r0   r   )r   r   r   r   r   r   r   ;  s    r	   c             C   s4   g | ],}t jd d d D ]}t |t| qqS )Nr"   g?)r   r   r0   r   )r   r   r   r   r   r   r   <  s    c             C   s6   g | ].}t |d df D ]}t|d t| qqS )Nr"   g?)r   r   r*   r   )r   r   r   r   r   r   r   =  s    c             C   s6   g | ].}t |d df D ]}t|d t| qqS )r	   Ng?)r   r   r*   r   )r   r   r   r   r   r   r   >  s    c             C   s6   g | ].}t |d df D ]}t|t| d qqS )Nr"   g?)r   r   r*   r   )r   r   r   r   r   r   r   @  s       )!r4   Z
__future__r   Zstatsmodels.compat.pythonr   Znumpyr
   Zscipy.interpolater   r   r   Zstatsmodels.tools.decoratorsr   objectr   r1   r    r   r   r   r   r   printr*   r+   r/   r0   maxabsr   Zreshaper   allr   r   r   r   <module>   sx    b

 *<::FF@@