
p7]c           @  s
	  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 d e f d     YZ e d	 k r	e j d
 d d d d d g  d d d  Z e j 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 j d' d( d) d* d+ d, g d- d' d. d/ d0 d1 g d2 d3 d4 d5 d6 d7 g d8 d9 d: d; d< d= g d> d? d@ dA dB dC g dD dE d8 dF dG dH g dI dJ dK dL d; dM g dN dO dP dQ dR dS g dT dU dV dW d3 dX g dY dZ d[ d\ d] d^ g d_ d` dN da dL db g dc dd de df dg dh g di dj dk dl dm dn g do dp dd dq dr ds g dt du dv dZ d\ dw g dx dy dc dz dJ d{ g d| d} di d~ d d g d d d| d de d g d d d d| dv dI g d d d d d d g d d d d d d g d d d d d d g d! d d d" d d g g  d d  d d d  f d Z e e e e  Z e e j d d   d d  e d  e e j d d   d d d  e e j d d   d d d  e e j d d   d d d  e d  e e j d d   d d d  e e j d d   d d d  e e j d d   d d d  e d  e e j d d   d d d  e e j d d   d d d  e e j d d   d d d  e d  e e j d d   d d d  e e j d d   d d d  e e j d d   d d d  e e j e j e j g  e e e   D]- Z e e D] Z e j e e e  ^ qq j  d e j!  e j    e e j e j e j g  e e e   D], Z e j D] Z e j e e e  ^ qq j  d e j!  e    e e j e j e j g  e e e   D], Z e j D] Z e j e e e  ^ qq j  d e j!  e    e e j g  e e e   D]4 Z e j d  d D] Z e j e e e  ^ qxqc j  d e j! d  e d d  d d  f k  j"    e e j g  e e e   D]4 Z e j d  d D] Z e j e e e  ^ qq j  d e j! d  e d d  d d  f k j"    e e j g  e e e   D]= Z e e d d  f D]  Z e j e d e e  ^ qq j  d e j! d  e j d  k j"    e e j g  e e e   D]= Z e e d d  f D]  Z e j e d e e  ^ q$q
 j  d e j! d  e j d k  j"    e e j g  e d e e   D]= Z e e d d  f D]  Z e j e e e d  ^ qq j  d e j! d  e j d  k j"    n  d S(   sM  
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


i(   t   print_function(   t   rangeN(   t   interp1dt   interp2dt   Rbf(   t   cache_readonlyt	   TableDistc           B  sq   e  Z d  Z d   Z e d    Z e d    Z e d    Z d   Z d   Z	 d   Z
 d   Z d	   Z RS(
   s  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  j |  |  _ t  j |  |  _ t  j |  |  _ t |  |  _ t  j t  j |  j d  j	    |  _
 |  j
 d k r |  j d  d   d d g f |  _ n" |  j d  d   d d g f |  _ d  S(   Ni   i    (   t   npt   asarrayt   alphat   sizet
   crit_tablet   lent   n_alphat   signt   difft   meant   signcritt   critv_bounds(   t   selfR	   R
   R   (    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyt   __init__0   s    '%c         C  sE   g  t  |  j  D]+ } t |  j |  j d  d   | f  ^ q } | S(   N(   R   R   R   R
   R   (   R   t   it   polyn(    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyR   <   s    >c         C  s   t  |  j |  j |  j  } | S(   N(   R   R
   R	   R   (   R   t   poly2d(    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyR   B   s    c         C  s[   t  j |  j j t  |  j  \ } } t | j   | j   |  j j	 j   d d } | S(   Nt   functiont   linear(
   R   t   meshgridR
   t   astypet   floatR	   R   t   ravelR   t   T(   R   t   xst   xat   polyrbf(    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyR!   I   s    '0c         C  s)   t  j g  |  j D] } | |  ^ q  S(   s  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

        (   R   t   arrayR   (   R   t   nt   p(    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyt	   _critvalsO   s    c   	      C  s:  |  j  |  } |  j } |  j d k  rQ | d d d  | d d d  } } n  t j |  d k r | | d k  r~ | d S| | d k r | d St | |  |  d S| | d k  } | | d k } t j | |  } t j t j | j	  } | d | | <| d | | <t | |  | |  | | <| Sd S(   s  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.

        i   Nii    (    (
   R%   R	   R   R   R
   R   t
   logical_ort   nant   onest   shape(	   R   t   xR#   t   critvR	   t   cond_lowt	   cond_hight   cond_interiort   probs(    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyt   probe   s$    	*c         C  s   |  j  | |  S(   s   returns 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   R0   R#   (    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyt   crit2   s    c   	      C  s   t  j |  } |  j } |  j |  } | | d k } | | d k  } t  j | |  } | j d k r | r t | |  |  St  j Sn  t  j t  j | j	  } t | |  | |  | | <| S(   s  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

        i    ii   (
   R   R   R	   R%   R&   R
   R   R'   R(   R)   (	   R   R0   R#   R	   R+   t	   cond_ilowt
   cond_ihighR.   t   quantile(    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyt   crit   s    	
c         C  s   t  j |  } |  j } | | d k } | | d k  } t  j | |  } | j d k ry | ro |  j | |  St  j Sn  t  j t  j | j  } |  j | | |  | | <| S(   s$  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

        i    ii   (	   R   R   R	   R&   R
   R!   R'   R(   R)   (   R   R0   R#   R	   R2   R3   R.   R4   (    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyt   crit3   s    	
(   t   __name__t
   __module__t   __doc__R   R   R   R   R!   R%   R0   R1   R5   R6   (    (    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyR      s   			9		't   __main__g?g333333?g?g?g{Gz?gMbP?i   i   i   i   i   i	   i
   i   i   i   i   i   i   i   i   i   i   i   i   i(   id   i  i  i/  iA  iZ  ix  i  i  i!  i?  iW  i  i  i  i  i)  iC  is  i  i   i  i  i0  i_  i  i   i   i	  i   iM  i  i   i   i  i=  im  i   i   i   i  i`  i   i   i   i   i#  iR  i   i   i   i   iE  i   i   i   i   i  i:  i   i   i   i1  i   i   i   i   i   i(  i   i   i   i   i   i  i   i   i   i   i  i   i   i   i  i   i   i   i
  i   i   i   i   i  i   i   i   i   i   i   i   is   ix   i   i   i   i   iJ   iM   iR   iY   ih   iz   i%   i'   i)   i-   i4   i=   i   i   i#   i*   g     @@gsh|??s
   should be:t    s   interp2d bads   two 1dR   g(\?s   should be in:g'1Z?gRQ?gFx?g?i   g?i   (   g'1Z?gsh|??(   g'1Z?gsh|??(   g'1Z?gsh|??(   g'1Z?gsh|??(   g'1Z?gsh|??(   g'1Z?gsh|??(   g'1Z?gsh|??(   g'1Z?gsh|??(   g'1Z?gsh|??(#   R9   t
   __future__R    t   statsmodels.compat.pythonR   t   numpyR   t   scipy.interpolateR   R   R   t   statsmodels.tools.decoratorsR   t   objectR   R7   R"   R	   R   R
   t   crit_lft   lft   printR0   R1   R5   R6   t   maxt   absR   R   t   ct   reshapeR   t   all(    (    (    s:   lib/python2.7/site-packages/statsmodels/stats/tabledist.pyt   <module>   sv   
.*-;



{ww