
    >h'                     D    S r SSKrSSKJrJrJr  SSKJr   " S S5      r	g)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                   n    \ rS rSrSr  SS jr\S 5       r\S 5       r\S 5       r	S r
S	 rS
 rS rSrg)	TableDist   a  
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                    [         R                  " U5      U l        U R                  R                  S:w  a  [	        S5      e[         R
                  " U R                  5      S:*  R                  5       (       a  [	        S5      e[         R                  " U5      U l        U R                  R                  S:w  a  [	        S5      e[         R
                  " U R                  5      S:*  R                  5       (       a  [	        S5      eU R                  R                  S:X  a7  [         R
                  " U5      S:*  R                  5       (       a  [	        S5      e[         R                  " U5      U l        U R                  R                  U R                  R                  S   U R                  R                  S   4:w  a  [	        S5      e[        U5      U l        [         R                  " [         R
                  " U R                  S5      R                  5       5      U l        U R                  S:  a  U R                  S S 2SS/4   U l        OU R                  S S 2SS/4   U l        S U l        [#        U5      =opl        Ub?   U" U R$                  S-   5      n[        U5      [        U5      :w  a  [	        S
5      eX@l        Uc  UOUU l        Uc  UOUU l        U R.                  U:  a  [	        S5      eU R0                  U:  a  [	        S5      eg ! [&         a0  n	[)        U	5      " SR+                  U	R,                  S   S	95      eS n	A	ff = f)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
             nC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/stats/tabledist.py__init__TableDist.__init__?   s   ZZ&
::??a.//ggdjj!Q&++--233JJt$	99>>Q-..ggdii A%**,,12299>>Q!#((** !677**Z0??  TYY__Q%79I9I!9L$MM 7 8 8 5z ; @ @ BC==1 $Aq6	 :D $Aq6	 :D#&t9,=!M 12
 2w#e*$  "* + +(O$,$4($,$4(==8#344==8#344 $  M3i !006sxx{0KM MMs   =L
 

M+L??Mc           	          [        U R                  5       Vs/ s H*  n[        U R                  U R                  S S 2U4   5      PM,     nnU$ s  snf N)ranger   r   r   r   )r&   ipolyns      r)   r0   TableDist.polyno   sO      -/-Q $))T__QT%:;- 	 //s   1Ac                 \    [        U R                  U R                  U R                  5      nU$ r-   )r   r   r   r   )r&   poly2ds     r)   r3   TableDist.poly2du   s#     $))TZZA    c                    [         R                  " U R                  R                  [        5      U R
                  5      u  p[        UR                  5       UR                  5       U R                  R                  R                  5       SS9nU$ )Nlinear)function)
r   meshgridr   astypefloatr   r   ravelr   T)r&   xsxapolyrbfs       r)   r@   TableDist.polyrbf|   s^    TYY--e4djjAbhhj"((*doo.?.?.E.E.G')r5   c                    XR                   :  a+  U R                  b  U R                  U5      nU$ [        S5      eU R                   Vs/ s H
  o3" U5      PM     nnXR                  :  aR  XR                  -
  U R
                  U R                  -
  -  n[        SU5      nU R                  U5      nXE-  SU-
  U-  -   nU$ s  snf )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
z?n is above max(size) and no asymptotic distribtuion is providedg      ?r   )r   r   r   r0   r$   r%   min)r&   nr'   pwa_cvs         r)   	_critvalsTableDist._critvals   s    ( }}*__Q' 	 ! "< = = "&,A1Q4B,== &4==4==+HIQKq)XQ",	 -s   	B?c                     U R                  U5      nU R                  nU R                  S:  a  USSS2   USSS2   pC[        R                  " U5      S:X  a.  XS   :  a  US   $ XS   :  a  US   $ [        X45      " U5      S   $ XS   :  nXS   :  n[        R                  " XV5      ) n[        R                  [        R                  " UR                  5      -  nUS   X'   US   X'   [        X45      " X   5      X'   U$ )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    )
rH   r   r   r   r   r   
logical_ornanonesr   )	r&   xrD   critvr   cond_low	cond_highcond_interiorprobss	            r)   probTableDist.prob   s    & q!

==1 2;dd5 771:?8|Qx2YRy E)!,R00 !HH2YI]]8??MFFRWWQWW--E#AhEO#BiEO#+E#9!:J#KE Lr5   c                    [         R                  " U5      nU R                  nU R                  U5      nXS   :  nXS   :  n[         R                  " XV5      nUR
                  S:X  a(  U(       a  [        X45      " U5      $ [         R                  $ [         R                  [         R                  " UR                  5      -  n[        X45      " X   5      X'   U$ )ao  
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   rK   r   )
r   r   r   rH   rM   r   r   rN   rO   r   )	r&   rV   rD   r   rQ   	cond_ilow
cond_ihighrT   quantiles	            r)   critTableDist.crit   s    $ zz$

q! !H_	2Y&
i< 99>-d33vv 66BGGDJJ//"*5"89L"Mr5   c                    [         R                  " U5      nU R                  nXS   :  nXS   :  n[         R                  " XE5      nUR                  S:X  a(  U(       a  U R                  X!5      $ [         R                  $ [         R                  [         R                  " UR                  5      -  nU R                  X!U   5      Xv'   U$ )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   rK   r   )	r   r   r   rM   r   r@   rN   rO   r   )r&   rV   rD   r   rY   rZ   rT   r[   s           r)   crit3TableDist.crit3   s    & zz$

 !H_	2Y&
i< 99>||A,,vv 66BGGDJJ//"&,,q}2E"Fr5   )
r   r   r   r   r%   r   r$   r   r   r   )NNN)__name__
__module____qualname____firstlineno____doc__r*   r   r0   r3   r@   rH   rV   r\   r_   __static_attributes__rL   r5   r)   r   r      sj    'R <@)-.5`  
    "H,\%N&r5   r   )
re   numpyr   scipy.interpolater   r   r   statsmodels.tools.decoratorsr   r   rL   r5   r)   <module>rj      s$     5 5 7M Mr5   